|
Lines 19-25
Link Here
|
| 19 |
import org.eclipse.jface.action.ToolBarManager; |
19 |
import org.eclipse.jface.action.ToolBarManager; |
| 20 |
import org.eclipse.jface.resource.ImageDescriptor; |
20 |
import org.eclipse.jface.resource.ImageDescriptor; |
| 21 |
import org.eclipse.jface.resource.JFaceResources; |
21 |
import org.eclipse.jface.resource.JFaceResources; |
|
|
22 |
import org.eclipse.jface.viewers.ISelection; |
| 22 |
import org.eclipse.jface.viewers.TreeViewer; |
23 |
import org.eclipse.jface.viewers.TreeViewer; |
|
|
24 |
import org.eclipse.jface.viewers.ViewerFilter; |
| 23 |
import org.eclipse.osgi.util.NLS; |
25 |
import org.eclipse.osgi.util.NLS; |
| 24 |
import org.eclipse.swt.SWT; |
26 |
import org.eclipse.swt.SWT; |
| 25 |
import org.eclipse.swt.accessibility.AccessibleAdapter; |
27 |
import org.eclipse.swt.accessibility.AccessibleAdapter; |
|
Lines 30-43
Link Here
|
| 30 |
import org.eclipse.swt.events.FocusListener; |
32 |
import org.eclipse.swt.events.FocusListener; |
| 31 |
import org.eclipse.swt.events.KeyAdapter; |
33 |
import org.eclipse.swt.events.KeyAdapter; |
| 32 |
import org.eclipse.swt.events.KeyEvent; |
34 |
import org.eclipse.swt.events.KeyEvent; |
|
|
35 |
import org.eclipse.swt.events.TraverseEvent; |
| 36 |
import org.eclipse.swt.events.TraverseListener; |
| 33 |
import org.eclipse.swt.graphics.Color; |
37 |
import org.eclipse.swt.graphics.Color; |
| 34 |
import org.eclipse.swt.layout.GridData; |
38 |
import org.eclipse.swt.layout.GridData; |
| 35 |
import org.eclipse.swt.layout.GridLayout; |
39 |
import org.eclipse.swt.layout.GridLayout; |
| 36 |
import org.eclipse.swt.widgets.Combo; |
|
|
| 37 |
import org.eclipse.swt.widgets.Composite; |
40 |
import org.eclipse.swt.widgets.Composite; |
| 38 |
import org.eclipse.swt.widgets.Control; |
41 |
import org.eclipse.swt.widgets.Display; |
| 39 |
import org.eclipse.swt.widgets.Text; |
42 |
import org.eclipse.swt.widgets.Text; |
| 40 |
import org.eclipse.swt.widgets.ToolBar; |
43 |
import org.eclipse.swt.widgets.ToolBar; |
|
|
44 |
import org.eclipse.swt.widgets.TreeItem; |
| 41 |
import org.eclipse.ui.PlatformUI; |
45 |
import org.eclipse.ui.PlatformUI; |
| 42 |
import org.eclipse.ui.internal.WorkbenchMessages; |
46 |
import org.eclipse.ui.internal.WorkbenchMessages; |
| 43 |
import org.eclipse.ui.plugin.AbstractUIPlugin; |
47 |
import org.eclipse.ui.plugin.AbstractUIPlugin; |
|
Lines 62-68
Link Here
|
| 62 |
|
66 |
|
| 63 |
private PatternFilter patternFilter; |
67 |
private PatternFilter patternFilter; |
| 64 |
|
68 |
|
| 65 |
private PreferenceNodeFilter preferenceFilter; |
69 |
private ViewerFilter viewerFilter; |
| 66 |
|
70 |
|
| 67 |
private FocusListener listener; |
71 |
private FocusListener listener; |
| 68 |
|
72 |
|
|
Lines 133-156
Link Here
|
| 133 |
| GridData.GRAB_HORIZONTAL)); |
137 |
| GridData.GRAB_HORIZONTAL)); |
| 134 |
|
138 |
|
| 135 |
createFilterControl(filterParent); |
139 |
createFilterControl(filterParent); |
| 136 |
getFilterControl().addKeyListener(new KeyAdapter() { |
140 |
getFilterControl().addKeyListener(new KeyAdapter() { |
| 137 |
|
141 |
/* |
| 138 |
/* |
142 |
* (non-Javadoc) |
| 139 |
* (non-Javadoc) |
143 |
* |
| 140 |
* |
144 |
* @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent) |
| 141 |
* @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent) |
145 |
*/ |
| 142 |
*/ |
146 |
public void keyReleased(KeyEvent e) { |
| 143 |
public void keyReleased(KeyEvent e) { |
147 |
// on a CR we want to transfer focus to the list |
| 144 |
// on a CR we want to transfer focus to the list |
148 |
if(e.keyCode == SWT.ARROW_DOWN){ |
| 145 |
if(e.keyCode == SWT.ARROW_DOWN){ |
149 |
treeViewer.getTree().setFocus(); |
| 146 |
|
150 |
} else if (e.character == SWT.CR) |
| 147 |
if (!(getFilterControl() instanceof Combo)) { |
151 |
return; |
| 148 |
treeViewer.getTree().setFocus(); |
152 |
|
| 149 |
} |
153 |
textChanged(); |
| 150 |
} else |
154 |
} |
| 151 |
textChanged(); |
155 |
}); |
| 152 |
} |
156 |
|
| 153 |
}); |
157 |
// enter key set focus to tree |
|
|
158 |
getFilterControl().addTraverseListener( new TraverseListener () { |
| 159 |
public void keyTraversed(TraverseEvent e) { |
| 160 |
if (e.detail == SWT.TRAVERSE_RETURN) { |
| 161 |
e.doit = false; |
| 162 |
if (getViewer().getTree().getItemCount() == 0) { |
| 163 |
Display.getCurrent().beep(); |
| 164 |
setFilterText(""); //$NON-NLS-1$ |
| 165 |
} else { |
| 166 |
boolean hasFocus = getViewer().getTree().setFocus(); |
| 167 |
if (hasFocus){ |
| 168 |
TreeItem item = getFirstHighlightedItem(getViewer().getTree().getItems()); |
| 169 |
if (item != null){ |
| 170 |
getViewer().getTree().setSelection(new TreeItem[] {item}); |
| 171 |
ISelection sel = getViewer().getSelection(); |
| 172 |
getViewer().setSelection(sel, true); |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
}); |
| 154 |
|
179 |
|
| 155 |
GridData data = new GridData(GridData.FILL_HORIZONTAL |
180 |
GridData data = new GridData(GridData.FILL_HORIZONTAL |
| 156 |
| GridData.GRAB_HORIZONTAL); |
181 |
| GridData.GRAB_HORIZONTAL); |
|
Lines 182-187
Link Here
|
| 182 |
createRefreshJob(); |
207 |
createRefreshJob(); |
| 183 |
} |
208 |
} |
| 184 |
|
209 |
|
|
|
210 |
private TreeItem getFirstHighlightedItem(TreeItem[] items){ |
| 211 |
for (int i = 0; i < items.length; i++){ |
| 212 |
if (patternFilter.isElementMatch(treeViewer, items[i].getData()) && patternFilter.isElementSelectable(items[i].getData())){ |
| 213 |
return items[i]; |
| 214 |
} |
| 215 |
return getFirstHighlightedItem(items[i].getItems()); |
| 216 |
} |
| 217 |
return null; |
| 218 |
} |
| 185 |
/** |
219 |
/** |
| 186 |
* Create the refresh job for the receiver. |
220 |
* Create the refresh job for the receiver. |
| 187 |
* |
221 |
* |
|
Lines 195-206
Link Here
|
| 195 |
if(treeViewer.getControl().isDisposed()) |
229 |
if(treeViewer.getControl().isDisposed()) |
| 196 |
return Status.CANCEL_STATUS; |
230 |
return Status.CANCEL_STATUS; |
| 197 |
|
231 |
|
| 198 |
String filterText = getFilterControlText(); |
232 |
String filterText = getFilterControl().getText(); |
| 199 |
boolean initial = initialText != null && filterText.equals(initialText); |
233 |
boolean initial = initialText != null && filterText.equals(initialText); |
| 200 |
if (initial) { |
234 |
if (initial) { |
| 201 |
patternFilter.setPattern(null); |
235 |
patternFilter.setPattern(null); |
| 202 |
} else { |
236 |
} else { |
| 203 |
patternFilter.setPattern(getFilterControlText()); |
237 |
patternFilter.setPattern(filterText); |
| 204 |
} |
238 |
} |
| 205 |
treeViewer.getControl().setRedraw(false); |
239 |
treeViewer.getControl().setRedraw(false); |
| 206 |
treeViewer.refresh(true); |
240 |
treeViewer.refresh(true); |
|
Lines 208-220
Link Here
|
| 208 |
|
242 |
|
| 209 |
if (filterText.length() > 0 && !initial) { |
243 |
if (filterText.length() > 0 && !initial) { |
| 210 |
treeViewer.expandAll(); |
244 |
treeViewer.expandAll(); |
|
|
245 |
TreeItem[] items = getViewer().getTree().getItems(); |
| 246 |
if (items.length > 0) |
| 247 |
treeViewer.getTree().showItem(items[0]); // to prevent scrolling |
| 211 |
// enabled toolbar is a hint that there is text to clear |
248 |
// enabled toolbar is a hint that there is text to clear |
| 212 |
// and the list is currently being filtered |
249 |
// and the list is currently being filtered |
| 213 |
filterToolBar.getControl().setVisible(true); |
250 |
filterToolBar.getControl().setVisible(true); |
| 214 |
} else { |
251 |
} else { |
| 215 |
// disabled toolbar is a hint that there is no text to clear |
252 |
// disabled toolbar is a hint that there is no text to clear |
| 216 |
// and the list is currently not filtered |
253 |
// and the list is currently not filtered |
| 217 |
filterToolBar.getControl().setVisible(preferenceFilter != null); |
254 |
filterToolBar.getControl().setVisible(viewerFilter != null); |
| 218 |
} |
255 |
} |
| 219 |
return Status.OK_STATUS; |
256 |
return Status.OK_STATUS; |
| 220 |
} |
257 |
} |
|
Lines 228-260
Link Here
|
| 228 |
*/ |
265 |
*/ |
| 229 |
protected void createFilterControl(Composite parent) { |
266 |
protected void createFilterControl(Composite parent) { |
| 230 |
filterText = new Text(parent, SWT.SINGLE | SWT.BORDER); |
267 |
filterText = new Text(parent, SWT.SINGLE | SWT.BORDER); |
| 231 |
filterText.getAccessible().addAccessibleListener(getAccessibleListener()); |
268 |
filterText.getAccessible().addAccessibleListener( |
|
|
269 |
new AccessibleAdapter(){ |
| 270 |
/* (non-Javadoc) |
| 271 |
* @see org.eclipse.swt.accessibility.AccessibleListener#getName(org.eclipse.swt.accessibility.AccessibleEvent) |
| 272 |
*/ |
| 273 |
public void getName(AccessibleEvent e) { |
| 274 |
String filterTextString = getFilterControl().getText(); |
| 275 |
if(filterTextString.length() == 0){ |
| 276 |
e.result = initialText; |
| 277 |
} |
| 278 |
else |
| 279 |
e.result = filterTextString; |
| 280 |
} |
| 281 |
}); |
| 232 |
} |
282 |
} |
| 233 |
|
283 |
|
| 234 |
protected AccessibleAdapter getAccessibleListener() { |
|
|
| 235 |
return new AccessibleAdapter(){ |
| 236 |
/* (non-Javadoc) |
| 237 |
* @see org.eclipse.swt.accessibility.AccessibleListener#getName(org.eclipse.swt.accessibility.AccessibleEvent) |
| 238 |
*/ |
| 239 |
public void getName(AccessibleEvent e) { |
| 240 |
String filterTextString = getFilterText(); |
| 241 |
if(filterTextString.length() == 0){ |
| 242 |
e.result = initialText; |
| 243 |
} |
| 244 |
else |
| 245 |
e.result = filterTextString; |
| 246 |
} |
| 247 |
|
| 248 |
}; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Get the text from the filter widget. |
| 253 |
* @return String |
| 254 |
*/ |
| 255 |
protected String getFilterText() { |
| 256 |
return filterText.getText(); |
| 257 |
} |
| 258 |
/** |
284 |
/** |
| 259 |
* update the receiver after the text has changed |
285 |
* update the receiver after the text has changed |
| 260 |
*/ |
286 |
*/ |
|
Lines 262-275
Link Here
|
| 262 |
refreshJob.schedule(200); |
288 |
refreshJob.schedule(200); |
| 263 |
} |
289 |
} |
| 264 |
|
290 |
|
| 265 |
/** |
|
|
| 266 |
* Get the text from the filter control. |
| 267 |
* @return Text |
| 268 |
*/ |
| 269 |
protected String getFilterControlText() { |
| 270 |
return filterText.getText(); |
| 271 |
} |
| 272 |
|
| 273 |
/** |
291 |
/** |
| 274 |
* Set the background for the widgets that support the filter text area |
292 |
* Set the background for the widgets that support the filter text area |
| 275 |
* |
293 |
* |
|
Lines 278-284
Link Here
|
| 278 |
public void setBackground(Color background) { |
296 |
public void setBackground(Color background) { |
| 279 |
super.setBackground(background); |
297 |
super.setBackground(background); |
| 280 |
filterParent.setBackground(background); |
298 |
filterParent.setBackground(background); |
| 281 |
getFilterControl().setBackground(background); |
|
|
| 282 |
filterToolBar.getControl().setBackground(background); |
299 |
filterToolBar.getControl().setBackground(background); |
| 283 |
} |
300 |
} |
| 284 |
|
301 |
|
|
Lines 315-323
Link Here
|
| 315 |
protected void clearText() { |
332 |
protected void clearText() { |
| 316 |
setFilterText(""); //$NON-NLS-1$ |
333 |
setFilterText(""); //$NON-NLS-1$ |
| 317 |
|
334 |
|
| 318 |
if(preferenceFilter != null){ |
335 |
if(viewerFilter != null){ |
| 319 |
getViewer().removeFilter(preferenceFilter); |
336 |
getViewer().removeFilter(viewerFilter); |
| 320 |
preferenceFilter = null; |
337 |
viewerFilter = null; |
| 321 |
getShell().setText(cachedTitle); |
338 |
getShell().setText(cachedTitle); |
| 322 |
} |
339 |
} |
| 323 |
|
340 |
|
|
Lines 347-353
Link Here
|
| 347 |
* |
364 |
* |
| 348 |
* @return the text field |
365 |
* @return the text field |
| 349 |
*/ |
366 |
*/ |
| 350 |
public Control getFilterControl() { |
367 |
public Text getFilterControl() { |
| 351 |
return filterText; |
368 |
return filterText; |
| 352 |
} |
369 |
} |
| 353 |
|
370 |
|
|
Lines 394-401
Link Here
|
| 394 |
* Add the filter to the viewer. |
411 |
* Add the filter to the viewer. |
| 395 |
* @param filter |
412 |
* @param filter |
| 396 |
*/ |
413 |
*/ |
| 397 |
public void addFilter(PreferenceNodeFilter filter) { |
414 |
public void addFilter(ViewerFilter filter) { |
| 398 |
preferenceFilter = filter; |
415 |
viewerFilter = filter; |
| 399 |
getViewer().addFilter(filter); |
416 |
getViewer().addFilter(filter); |
| 400 |
setInitialText(WorkbenchMessages.FilteredTree_FilteredMessage); |
417 |
setInitialText(WorkbenchMessages.FilteredTree_FilteredMessage); |
| 401 |
|
418 |
|