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