最近和Assist比较有源,这次是ContentAssist
以前研究过eclipse的ContentAssist,发现他是MVC的模式,而已和platform密切的联系,一怒之下,自己写了个
虽然也是遵照MVC的模式不过已经有了很大的灵活性。可以脱离eclipse的平台使用了。
GeneralContentAssistant:
这家伙是controller,用来管理Assistant的一些动作,和数据的表现
1
package
com.hactl.eaf.ui.rich.composite.widget.contentassistant;
2
3
import
java.util.HashMap;
4
import
java.util.Map;
5
import
java.util.Vector;
6
7
import
org.eclipse.swt.SWT;
8
import
org.eclipse.swt.events.FocusEvent;
9
import
org.eclipse.swt.events.FocusListener;
10
import
org.eclipse.swt.events.KeyEvent;
11
import
org.eclipse.swt.events.KeyListener;
12
import
org.eclipse.swt.events.MouseEvent;
13
import
org.eclipse.swt.events.MouseListener;
14
import
org.eclipse.swt.widgets.Control;
15
import
org.eclipse.swt.widgets.Display;
16
17
import
com.hactl.eaf.ui.rich.composite.widget.HText;
18
19
public
class
GeneralContentAssistant
{
20
//
KeyListener keylistener;
21
private
static
int
autoRespondKey
=
SWT.F2;
22
private
static
int
closeKey
=
SWT.ESC;
23
private
Vector
<
Proposal
>
proposals;
24
private
Vector
<
Proposal
>
updateProposals;
25
//
private Control control;
26
private
Control[] controls;
27
private
Control control2;
28
private
boolean
isfilter
=
true
;
29
30
31
@SuppressWarnings(
{
"
unchecked
"
,
"
unchecked
"
}
)
32
public
GeneralContentAssistant(Control control, Vector
<
Proposal
>
proposals)
{
33
this
(control,
null
,proposals);
34
}
35
36
public
GeneralContentAssistant(Control control,Control control2, Vector
<
Proposal
>
proposals)
{
37
this
.controls
=
new
Control[
1
];
38
this
.controls[
0
]
=
control;
39
this
.control2
=
control2;
40
this
.proposals
=
proposals;
41
this
.updateProposals
=
(Vector
<
Proposal
>
)proposals.clone();
42
ContentPopup popup
=
new
ContentPopup(control,(Vector
<
Proposal
>
)proposals.clone());
43
44
this
.controls[
0
].addKeyListener(
new
AssistantKeyListener(popup));
45
this
.controls[
0
].addFocusListener(
new
ControlFocusListener(popup));
46
47
if
(control2
!=
null
)
48
{
49
this
.control2.addMouseListener(
new
OnClickMouseListener(control,popup));
50
}
51
}
52
53
public
GeneralContentAssistant(Control[] controls,Control control2, Vector
<
Proposal
>
proposals)
{
54
this
.controls
=
controls;
55
this
.control2
=
control2;
56
this
.proposals
=
proposals;
57
this
.updateProposals
=
(Vector
<
Proposal
>
)proposals.clone();
58
59
Map popUpMap
=
new
HashMap();
60
61
for
(
int
i
=
0
;i
<
this
.controls.length;i
++
)
62
{
63
ContentPopup popup
=
new
ContentPopup(
this
.controls[i],(Vector
<
Proposal
>
)proposals.clone());
64
this
.controls[i].addKeyListener(
new
AssistantKeyListener(popup));
65
this
.controls[i].addFocusListener(
new
ControlFocusListener(popup));
66
popUpMap.put(
this
.controls[i],popup);
67
}
68
69
if
(control2
!=
null
)
70
{
71
this
.control2.addMouseListener(
new
OnClickMouseListener(
this
.controls,popUpMap));
72
}
73
}
74
75
@SuppressWarnings(
"
unchecked
"
)
76
private
Vector
<
Proposal
>
fillProposals(ContentPopup popup)
{
77
78
final
String innerText
=
popup.getText();
79
final
int
length
=
innerText.length();
80
if
(
!
isfilter)
{
81
return
updateProposals
=
(Vector
<
Proposal
>
)proposals.clone();
82
83
}
else
{
84
updateProposals.clear();
85
//
compare input string of lenght with each items's data
86
for
(
int
i
=
0
; i
<
proposals.size(); i
++
)
{
87
final
String temp
=
((Proposal)proposals.get(i)).getDisplayString();
88
if
(
!
""
.equals(temp))
89
try
{
90
if
(innerText.equalsIgnoreCase(temp.substring(
0
,length)))
91
updateProposals.add(proposals.get(i));
92
}
catch
(StringIndexOutOfBoundsException ex)
{
93
94
}
95
}
96
return
(Vector
<
Proposal
>
)updateProposals.clone();
97
}
98
99
}
100
101
class
OnClickMouseListener
implements
MouseListener
{
102
103
private
ContentPopup popup;
104
private
Map popUpMap;
105
private
Control[] controls;
106
private
Control control;
107
108
public
OnClickMouseListener(Control control,ContentPopup popup)
109
{
110
this
.popup
=
popup;
111
this
.control
=
control;
112
}
113
114
public
OnClickMouseListener(Control[] controls, Map popUpMap)
{
115
this
.popUpMap
=
popUpMap;
116
this
.controls
=
controls;
117
}
118
119
public
void
keyPressed(KeyEvent e)
{
120
121
}
122
123
public
void
mouseDoubleClick(MouseEvent e)
{
124
//
TODO Auto-generated method stub
125
126
}
127
128
public
void
mouseDown(MouseEvent e)
{
129
if
(control
!=
null
) control.setFocus();
130
if
(popUpMap
!=
null
)
131
{
132
boolean
bingo
=
false
;
133
for
(
int
i
=
0
;i
<
controls.length;i
++
)
134
{
135
if
(controls[i].isFocusControl())
136
{
137
popup
=
(ContentPopup) popUpMap.get(controls[i]);
138
bingo
=
true
;
139
break
;
140
}
141
}
142
if
(
!
bingo)
143
{
144
for
(
int
i
=
0
;i
<
controls.length;i
++
)
145
{
146
HText text
=
(HText)controls[i];
147
if
(text.getText().length()
==
0
)
148
{
149
popup
=
(ContentPopup) popUpMap.get(controls[i]);
150
bingo
=
true
;
151
break
;
152
}
153
}
154
}
155
}
156
if
(popup
==
null
) popup
=
(ContentPopup) popUpMap.get(controls[controls.length
-
1
]);
157
158
if
(
!
popup.isShow)
{
159
final
Vector
<
Proposal
>
temp
=
fillProposals(popup);
160
if
(temp.size()
==
0
)
return
;
161
popup.setProposals(fillProposals(popup));
162
popup.show();
163
}
else
164
{
165
updateProposals.clear();
166
popup.hide();
167
}
168
169
}
170
171
public
void
mouseUp(MouseEvent e)
{
172
//
TODO Auto-generated method stub
173
174
}
175
176
177
178
}
179
180
181
182
class
AssistantKeyListener
implements
KeyListener
{
183
184
private
ContentPopup popup;
185
186
public
AssistantKeyListener(ContentPopup popup)
187
{
188
this
.popup
=
popup;
189
}
190
191
public
void
keyPressed(KeyEvent e)
{
192
193
}
194
195
public
void
keyReleased(KeyEvent e)
{
196
if
(e.keyCode
==
autoRespondKey )
{
197
if
(
!
popup.isShow)
{
198
//
System.out.print(popup.isShow);
199
final
Vector
<
Proposal
>
temp
=
fillProposals(popup);
200
if
(temp.size()
==
0
)
return
;
201
popup.setProposals(fillProposals(popup));
202
popup.show();
203
}
204
}
else
if
(e.keyCode
==
closeKey )
{
205
//
System.out.println(popup.isShow);
206
if
(popup.isShow)
{
207
updateProposals.clear();
208
popup.hide();
209
}
210
}
else
if
(e.keyCode
==
SWT.CR)
{
211
if
(popup.isShow)
{
212
popup.setText();
213
}
214
}
else
{
215
if
(popup.isShow)
{
216
popup.hide();
217
final
Vector
<
Proposal
>
temp
=
fillProposals(popup);
218
if
(temp.size()
==
0
)
return
;
219
popup.setProposals(fillProposals(popup));
220
popup.show();
221
System.out.println(
"
autoRespondKey
"
+
e.keyCode );
222
}
223
}
224
if
(popup.isShow)
{
225
popup.selectItem(e);
226
}
227
}
228
}
229
230
231
class
ControlFocusListener
implements
FocusListener
{
232
233
private
ContentPopup popup;
234
235
public
ControlFocusListener(ContentPopup popup)
236
{
237
this
.popup
=
popup;
238
}
239
240
public
void
keyPressed(KeyEvent e)
{
241
242
}
243
/** */
/**
244
* Invoked when a component gains the keyboard focus.
245
*/
246
public
void
focusGained(FocusEvent e)
{
247
248
}
249
250
/** */
/**
251
* Invoked when a component loses the keyboard focus.
252
*/
253
public
void
focusLost(FocusEvent e)
{
254
//
final FocusEvent event = e;
255
if
(popup.isShow)
{
256
Display d
=
controls[
0
].getDisplay();
257
if
(d
!=
null
)
{
258
d.asyncExec(
new
Runnable()
{
259
public
void
run()
{
260
if
(
!
popup.isfocus)
{
261
//
System.out.println("hide shell");
262
popup.hide();
263
}
264
}
265
}
);
266
}
267
}
268
}
269
270
}
271
272
273
public
boolean
isfilter()
{
274
return
isfilter;
275
}
276
277
278
public
void
setIsfilter(
boolean
isfilter)
{
279
/** */
/**
280
* Andy
281
*/
282
//
this.isfilter = isfilter;
283
}
284
}
285
ContentPopup:
就是viev啦content长什么样在写在这里。
1
package
com.hactl.eaf.ui.rich.composite.widget.contentassistant;
2
3
import
java.lang.reflect.Method;
4
import
java.util.Vector;
5
6
import
org.eclipse.swt.SWT;
7
import
org.eclipse.swt.events.ControlEvent;
8
import
org.eclipse.swt.events.ControlListener;
9
import
org.eclipse.swt.events.DisposeEvent;
10
import
org.eclipse.swt.events.DisposeListener;
11
import
org.eclipse.swt.events.FocusEvent;
12
import
org.eclipse.swt.events.FocusListener;
13
import
org.eclipse.swt.events.KeyEvent;
14
import
org.eclipse.swt.events.KeyListener;
15
import
org.eclipse.swt.events.MouseEvent;
16
import
org.eclipse.swt.events.MouseListener;
17
import
org.eclipse.swt.events.SelectionEvent;
18
import
org.eclipse.swt.events.SelectionListener;
19
import
org.eclipse.swt.graphics.Color;
20
import
org.eclipse.swt.graphics.Point;
21
import
org.eclipse.swt.layout.GridData;
22
import
org.eclipse.swt.layout.GridLayout;
23
import
org.eclipse.swt.widgets.Combo;
24
import
org.eclipse.swt.widgets.Control;
25
import
org.eclipse.swt.widgets.Shell;
26
import
org.eclipse.swt.widgets.Table;
27
import
org.eclipse.swt.widgets.TableItem;
28
import
org.eclipse.swt.widgets.Text;
29
30
import
com.hactl.eaf.ui.rich.composite.widget.HCombo;
31
import
com.hactl.eaf.ui.rich.composite.widget.HText;
32
33
public
class
ContentPopup
{
34
35
class
TableKeyListener
implements
KeyListener
{
36
37
public
void
keyPressed(KeyEvent e)
{
38
}
39
40
public
void
keyReleased(KeyEvent e)
{
41
if
(e.keyCode
==
SWT.CR)
{
42
43
setText();
44
}
45
}
46
47
}
48
49
class
TableMouseListener
implements
MouseListener
{
50
51
public
void
mouseDoubleClick(MouseEvent e)
{
52
53
setText();
54
}
55
56
public
void
mouseDown(MouseEvent e)
{
57
//
TODO Auto-generated method stub
58
59
}
60
61
public
void
mouseUp(MouseEvent e)
{
62
//
TODO Auto-generated method stub
63
64
}
65
66
}
67
68
class
TableFocusListener
implements
FocusListener
{
69
/** */
/**
70
* Invoked when a component gains the keyboard focus.
71
*/
72
public
void
focusGained(FocusEvent e)
{
73
isfocus
=
true
;
74
table.setFocus();
75
}
76
77
/** */
/**
78
* Invoked when a component loses the keyboard focus.
79
*/
80
public
void
focusLost(FocusEvent e)
{
81
isfocus
=
false
;
82
}
83
84
}
85
86
//
---------------------------------
87
//
fields
88
89
protected
Shell shell;
90
protected
Table table;
91
private
Control control;
92
private
Vector
<
Proposal
>
proposals;
93
protected
Proposal selectedProposal;
94
private
Method setText;
95
private
Method getText;
96
public
boolean
isfocus
=
false
;
97
public
boolean
isShow
=
false
;
98
private
Text text;
99
private
Combo combo;
100
101
102
//
----------------------------------
103
//
methods
104
105
106
public
ContentPopup(Control control, Vector
<
Proposal
>
proposals)
{
107
org.eclipse.jface.util.Assert.isNotNull(control);
108
this
.control
=
control;
109
//
this.proposals = proposals;
110
setProposals(proposals);
111
}
112
113
public
void
setText()
{
114
selectedProposal
=
(Proposal)table.getItem(table.getSelectionIndex()).getData();
115
final
String replace
=
selectedProposal.getReplacementString();
116
if
(control
instanceof
Text)
{
117
text
=
(Text)control;
118
text.append(replace);
119
}
120
if
(control
instanceof
Combo)
{
121
combo
=
(Combo)control;
122
combo.setText(replace);
123
124
}
125
if
(control
instanceof
HText)
{
126
//
text = ((HText)control).getTextControl();
127
text
=
((HText)control);
128
text.setText(replace);
129
}
130
if
(control
instanceof
HCombo)
{
131
//
combo = ((HCombo)control).getCombo();
132
((HCombo)control).setText(replace);
133
}
134
this
.hide();
135
}
136
137
public
String getText()
{
138
checkControl();
139
String str
=
new
String();
140
if
(control
instanceof
Text)
141
str
=
text.getText();
142
if
(control
instanceof
Combo)
143
str
=
combo.getText();
144
return
str;
145
146
}
147
148
protected
void
createProposalSelector()
{
149
shell
=
new
Shell(control.getShell(), SWT.ON_TOP
|
SWT.RESIZE );
150
table
=
new
Table(shell, SWT.H_SCROLL
|
SWT.V_SCROLL);
151
152
153
GridLayout layout
=
new
GridLayout();
154
layout.marginWidth
=
0
;
155
layout.marginHeight
=
0
;
156
shell.setLayout(layout);
157
158
Shell parent
=
control.getShell();
159
160
shell.setLocation(getStackedLocation(parent,control));
161
162
163
GridData data
=
new
GridData(GridData.FILL_BOTH);
164
data.heightHint
=
table.getItemHeight()
*
10
;
165
data.widthHint
=
control.getBounds().width;
166
table.setLayoutData(data);
167
shell.pack();
168
169
shell.addControlListener(
new
ControlListener()
{
170
171
public
void
controlMoved(ControlEvent e)
{}
172
173
public
void
controlResized(ControlEvent e)
{
174
table.setSize(shell.getSize());
175
}
176
}
);
177
178
shell.addDisposeListener(
new
DisposeListener()
{
179
public
void
widgetDisposed(DisposeEvent e)
{
180
isShow
=
false
;
181
}
182
}
);
183
184
Color c
=
control.getDisplay().getSystemColor(SWT.COLOR_INFO_BACKGROUND);
185
table.setBackground(c);
186
table.setHeaderVisible(
false
);
187
188
table.addKeyListener(
new
TableKeyListener());
189
table.addFocusListener(
new
TableFocusListener());
190
table.addMouseListener(
new
TableMouseListener());
191
192
table.getVerticalBar().addSelectionListener(
new
SelectionListener()
{
193
public
void
widgetSelected(SelectionEvent e)
{
194
isfocus
=
true
;
195
table.setFocus();
196
}
197
198
public
void
widgetDefaultSelected(SelectionEvent e)
{
199
}
200
}
);
201
202
refreshProposals();
203
}
204
205
206
public
void
setProposals(Vector
<
Proposal
>
proposals)
{
207
this
.proposals
=
proposals;
208
createProposalSelector();
209
}
210
211
212
private
void
refreshProposals()
{
213
table.clearAll();
214
final
int
size
=
proposals.size();
215
table.setRedraw(
false
);
216
table.setItemCount(size);
217
TableItem[] items
=
table.getItems();
218
for
(
int
i
=
0
; i
<
items.length; i
++
)
{
219
TableItem item
=
items[i];
220
Proposal proposal
=
proposals.get(i);
221
item.setText(proposal.getDisplayString());
222
//
item.setImage(proposal.getImage());
223
item.setData(proposal);
224
}
225
table.setRedraw(
true
);
226
table.select(
0
);
227
}
228
229
public
Control getControl()
{
230
return
control;
231
}
232
233
public
void
setControl(Control control)
{
234
this
.control
=
control;
235
}
236
237
private
void
checkControl()
{
238
if
(control
instanceof
Text)
{
239
text
=
(Text)control;
240
}
241
if
(control
instanceof
Combo)
{
242
combo
=
(Combo)control;
243
}
244
}
245
246
247
public
void
show()
{
248
if
(isShow
==
false
)
{
249
createProposalSelector();
250
}
251
isShow
=
true
;
252
shell.setVisible(
true
);
253
//
table.setFocus();
254
}
255
256
public
void
hide()
{
257
isShow
=
false
;
258
if
(shell
!=
null
&&
shell.isDisposed()
==
false
)
{
259
table
=
null
;
260
shell.setVisible(
false
);
261
shell.dispose();
262
shell
=
null
;
263
}
264
}
265
266
protected
Point getStackedLocation(Shell parent, Control control)
{
267
//
Point shellToParent = parent.toDisplay(control.getLocation());
268
Point shellToParent
=
control.getParent().toDisplay(control.getLocation());
269
//
shellToParent.y += parentToControl.y;
270
//
shellToParent.x += parentToControl.x;
271
shellToParent.y
+=
control.getBounds().height;
272
return
shellToParent;
273
}
274
275
276
277
public
void
selectItem(KeyEvent e)
{
278
if
(e.keyCode
==
SWT.ARROW_UP
||
279
e.keyCode
==
SWT.ARROW_DOWN )
{
280
table.setFocus();
281
}
282
}
283
284
protected
void
notSupportContorl()
{
285
286
}
287
}
288
Proposal:
model啦
1
package
com.hactl.eaf.ui.rich.composite.widget.contentassistant;
2
3
import
org.eclipse.swt.graphics.Image;
4
5
public
class
Proposal
{
6
7
private
String replacementString;
8
private
Image image;
9
private
String displayString;
10
public
Proposal()
{
11
12
}
13
14
public
Proposal(String replacementString, Image image, String displayString)
{
15
org.eclipse.jface.util.Assert.isNotNull(replacementString);
16
this
.replacementString
=
replacementString;
17
this
.image
=
image;
18
this
.displayString
=
displayString;
19
}
20
21
22
public
String getDisplayString()
{
23
return
displayString;
24
}
25
26
27
public
Image getImage()
{
28
return
image;
29
}
30
31
32
public
String getReplacementString()
{
33
return
replacementString;
34
}
35
36
37
38
}
39
利用这3个类可以完成一个colunm的assist应用,封装的很乱,自己都越看越不爽。