1 11 package org.eclipse.ui.internal.dialogs; 12 13 import org.eclipse.core.runtime.IProgressMonitor; 14 import org.eclipse.core.runtime.IStatus; 15 import org.eclipse.core.runtime.Status; 16 import org.eclipse.core.runtime.jobs.Job; 17 import org.eclipse.jface.action.Action; 18 import org.eclipse.jface.action.IAction; 19 import org.eclipse.jface.action.ToolBarManager; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 import org.eclipse.jface.resource.JFaceResources; 22 import org.eclipse.jface.viewers.TreeViewer; 23 import org.eclipse.osgi.util.NLS; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.accessibility.AccessibleAdapter; 26 import org.eclipse.swt.accessibility.AccessibleEvent; 27 import org.eclipse.swt.events.DisposeEvent; 28 import org.eclipse.swt.events.DisposeListener; 29 import org.eclipse.swt.events.FocusEvent; 30 import org.eclipse.swt.events.FocusListener; 31 import org.eclipse.swt.events.KeyAdapter; 32 import org.eclipse.swt.events.KeyEvent; 33 import org.eclipse.swt.graphics.Color; 34 import org.eclipse.swt.layout.GridData; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Text; 38 import org.eclipse.swt.widgets.ToolBar; 39 import org.eclipse.ui.PlatformUI; 40 import org.eclipse.ui.internal.WorkbenchMessages; 41 import org.eclipse.ui.plugin.AbstractUIPlugin; 42 import org.eclipse.ui.progress.WorkbenchJob; 43 44 51 public class FilteredTree extends Composite { 52 53 protected Text filterText; 54 55 private ToolBarManager filterToolBar; 56 57 protected TreeViewer treeViewer; 58 59 private Composite filterParent; 60 61 private PatternFilter patternFilter; 62 63 private PreferenceNodeFilter preferenceFilter; 64 65 private FocusListener listener; 66 67 private static final String CLEAR_ICON = "org.eclipse.ui.internal.dialogs.CLEAR_ICON"; 69 private static final String DCLEAR_ICON = "org.eclipse.ui.internal.dialogs.DCLEAR_ICON"; 71 protected String initialText = ""; 73 private String cachedTitle; 74 75 private Job refreshJob; 77 78 static { 79 ImageDescriptor descriptor = AbstractUIPlugin 80 .imageDescriptorFromPlugin(PlatformUI.PLUGIN_ID, 81 "$nl$/icons/full/etool16/clear_co.gif"); if (descriptor != null) { 83 JFaceResources.getImageRegistry().put(CLEAR_ICON, descriptor); 84 } 85 descriptor = AbstractUIPlugin.imageDescriptorFromPlugin( 86 PlatformUI.PLUGIN_ID, "$nl$/icons/full/dtool16/clear_co.gif"); if (descriptor != null) { 88 JFaceResources.getImageRegistry().put(DCLEAR_ICON, descriptor); 89 } 90 } 91 92 101 public FilteredTree(Composite parent, int treeStyle) { 102 this(parent, treeStyle, new PatternFilter()); 103 } 104 105 115 public FilteredTree(Composite parent, int treeStyle, PatternFilter filter) { 116 super(parent, SWT.NONE); 117 patternFilter = filter; 118 GridLayout layout = new GridLayout(); 119 layout.marginHeight = 0; 120 layout.marginWidth = 0; 121 setLayout(layout); 122 123 filterParent = new Composite(this, SWT.NONE); 124 GridLayout filterLayout = new GridLayout(); 125 filterLayout.numColumns = 2; 126 filterLayout.marginHeight = 0; 127 filterLayout.marginWidth = 0; 128 filterParent.setLayout(filterLayout); 129 filterParent.setFont(parent.getFont()); 130 filterParent.setLayoutData(new GridData(GridData.FILL_HORIZONTAL 131 | GridData.GRAB_HORIZONTAL)); 132 133 createFilterControl(filterParent); 134 135 GridData data = new GridData(GridData.FILL_HORIZONTAL 136 | GridData.GRAB_HORIZONTAL); 137 getFilterControl().setLayoutData(data); 138 139 ToolBar toolBar = new ToolBar(filterParent, SWT.FLAT | SWT.HORIZONTAL); 140 filterToolBar = new ToolBarManager(toolBar); 141 142 createClearText(filterToolBar); 143 144 filterToolBar.update(false); 145 filterToolBar.getControl().setVisible(false); 147 148 treeViewer = new TreeViewer(this, treeStyle); 149 data = new GridData(GridData.FILL_BOTH); 150 treeViewer.getControl().setLayoutData(data); 151 treeViewer.getControl().addDisposeListener(new DisposeListener(){ 152 155 public void widgetDisposed(DisposeEvent e) { 156 refreshJob.cancel(); 157 158 } 159 }); 160 treeViewer.addFilter(patternFilter); 161 162 createRefreshJob(); 163 } 164 165 169 private void createRefreshJob() { 170 refreshJob = new WorkbenchJob("Refresh Filter"){ 174 public IStatus runInUIThread(IProgressMonitor monitor) { 175 if(treeViewer.getControl().isDisposed()) 176 return Status.CANCEL_STATUS; 177 178 String filterText = getFilterControlText(); 179 boolean initial = initialText != null && filterText.equals(initialText); 180 if (initial) { 181 patternFilter.setPattern(null); 182 } else { 183 patternFilter.setPattern(getFilterControlText()); 184 } 185 treeViewer.getControl().setRedraw(false); 186 treeViewer.refresh(true); 187 treeViewer.getControl().setRedraw(true); 188 189 if (filterText.length() > 0 && !initial) { 190 treeViewer.expandAll(); 191 filterToolBar.getControl().setVisible(true); 194 } else { 195 filterToolBar.getControl().setVisible(preferenceFilter != null); 198 } 199 return Status.OK_STATUS; 200 } 201 202 }; 203 refreshJob.setSystem(true); 204 } 205 206 209 protected void createFilterControl(Composite parent) { 210 filterText = new Text(parent, SWT.SINGLE | SWT.BORDER); 211 filterText.getAccessible().addAccessibleListener(getAccessibleListener()); 212 getFilterControl().addKeyListener(getKeyListener()); 213 214 } 215 216 220 protected KeyAdapter getKeyListener() { 221 return new KeyAdapter() { 222 223 228 public void keyReleased(KeyEvent e) { 229 if(e.keyCode == SWT.ARROW_DOWN) 231 treeViewer.getTree().setFocus(); 232 else 233 textChanged(); 234 } 235 }; 236 } 237 238 protected AccessibleAdapter getAccessibleListener() { 239 return new AccessibleAdapter(){ 240 243 public void getName(AccessibleEvent e) { 244 String filterTextString = getFilterText(); 245 if(filterTextString.length() == 0){ 246 e.result = initialText; 247 } 248 else 249 e.result = filterTextString; 250 } 251 252 }; 253 } 254 255 259 protected String getFilterText() { 260 return filterText.getText(); 261 } 262 265 protected void textChanged() { 266 refreshJob.schedule(200); 267 } 268 269 273 protected String getFilterControlText() { 274 return filterText.getText(); 275 } 276 277 282 public void setBackground(Color background) { 283 super.setBackground(background); 284 filterParent.setBackground(background); 285 getFilterControl().setBackground(background); 286 filterToolBar.getControl().setBackground(background); 287 } 288 289 294 private void createClearText(ToolBarManager filterToolBar) { 295 296 IAction clearTextAction = new Action("", IAction.AS_PUSH_BUTTON) { 302 public void run() { 303 clearText(); 304 } 305 }; 306 307 clearTextAction.setToolTipText(WorkbenchMessages.FilteredTree_ClearToolTip); 308 clearTextAction.setImageDescriptor(JFaceResources.getImageRegistry() 309 .getDescriptor(CLEAR_ICON)); 310 clearTextAction.setDisabledImageDescriptor(JFaceResources 311 .getImageRegistry().getDescriptor(DCLEAR_ICON)); 312 313 filterToolBar.add(clearTextAction); 314 } 315 316 319 protected void clearText() { 320 setFilterText(""); 322 if(preferenceFilter != null){ 323 getViewer().removeFilter(preferenceFilter); 324 preferenceFilter = null; 325 getShell().setText(cachedTitle); 326 } 327 328 textChanged(); 329 } 330 331 335 protected void setFilterText(String string) { 336 filterText.setText(string); 337 selectAll(); 338 } 339 340 345 public TreeViewer getViewer() { 346 return treeViewer; 347 } 348 349 354 public Text getFilterControl() { 355 return filterText; 356 } 357 358 363 public void setInitialText(String text) { 364 initialText = text; 365 setFilterText(initialText); 366 367 textChanged(); 368 listener = new FocusListener() { 369 public void focusGained(FocusEvent event) { 370 filterFocusGained(); 371 } 372 373 378 public void focusLost(FocusEvent e) { 379 filterFocusLost(); 380 } 381 }; 382 getFilterControl().addFocusListener(listener); 383 } 384 385 386 387 protected void selectAll() { 388 filterText.selectAll(); 389 } 390 391 395 protected String getInitialText() { 396 return initialText; 397 } 398 399 403 public void addFilter(PreferenceNodeFilter filter) { 404 preferenceFilter = filter; 405 getViewer().addFilter(filter); 406 setInitialText(WorkbenchMessages.FilteredTree_FilteredMessage); 407 408 if(getFilterControl() != null){ 409 setFilterText(WorkbenchMessages.FilteredTree_FilteredMessage); 410 textChanged(); 411 } 412 413 cachedTitle = getShell().getText(); 414 getShell().setText( 415 NLS.bind( 416 WorkbenchMessages.FilteredTree_FilteredDialogTitle, 417 cachedTitle)); 418 419 } 420 421 425 protected void filterFocusGained() { 426 selectAll(); 427 getFilterControl().removeFocusListener(listener); 428 } 429 430 434 protected void filterFocusLost() { 435 436 } 437 438 } 439 | Popular Tags |