KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > dialogs > FilteredTree


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
45  * A simple control that provides a text widget and a tree viewer. The contents
46  * of the text widget are used to drive a PatternFilter that is on the viewer.
47  *
48  * @see org.eclipse.ui.internal.dialogs.PatternFilter
49  * @since 3.0
50  */

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 JavaDoc CLEAR_ICON = "org.eclipse.ui.internal.dialogs.CLEAR_ICON"; //$NON-NLS-1$
68

69     private static final String JavaDoc DCLEAR_ICON = "org.eclipse.ui.internal.dialogs.DCLEAR_ICON"; //$NON-NLS-1$
70

71     protected String JavaDoc initialText = ""; //$NON-NLS-1$
72

73     private String JavaDoc cachedTitle;
74     
75     //The job for refreshing the tree
76
private Job refreshJob;
77    
78     static {
79         ImageDescriptor descriptor = AbstractUIPlugin
80                 .imageDescriptorFromPlugin(PlatformUI.PLUGIN_ID,
81                         "$nl$/icons/full/etool16/clear_co.gif"); //$NON-NLS-1$
82
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"); //$NON-NLS-1$
87
if (descriptor != null) {
88             JFaceResources.getImageRegistry().put(DCLEAR_ICON, descriptor);
89         }
90     }
91
92     /**
93      * Create a new instance of the receiver. It will be created with a default
94      * pattern filter.
95      *
96      * @param parent
97      * the parent composite
98      * @param treeStyle
99      * the SWT style bits to be passed to the tree viewer
100      */

101     public FilteredTree(Composite parent, int treeStyle) {
102         this(parent, treeStyle, new PatternFilter());
103     }
104
105     /**
106      * Create a new instance of the receiver.
107      *
108      * @param parent
109      * parent <code>Composite</code>
110      * @param treeStyle
111      * the style bits for the <code>Tree</code>
112      * @param filter
113      * the filter to be used
114      */

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         // initially there is no text to clear
146
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             /* (non-Javadoc)
153              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
154              */

155             public void widgetDisposed(DisposeEvent e) {
156                 refreshJob.cancel();
157                 
158             }
159         });
160         treeViewer.addFilter(patternFilter);
161         
162         createRefreshJob();
163     }
164
165     /**
166      * Create the refresh job for the receiver.
167      *
168      */

169     private void createRefreshJob() {
170         refreshJob = new WorkbenchJob("Refresh Filter"){//$NON-NLS-1$
171
/* (non-Javadoc)
172              * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
173              */

174             public IStatus runInUIThread(IProgressMonitor monitor) {
175                 if(treeViewer.getControl().isDisposed())
176                     return Status.CANCEL_STATUS;
177                 
178                 String JavaDoc 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                     // enabled toolbar is a hint that there is text to clear
192
// and the list is currently being filtered
193
filterToolBar.getControl().setVisible(true);
194                 } else {
195                     // disabled toolbar is a hint that there is no text to clear
196
// and the list is currently not filtered
197
filterToolBar.getControl().setVisible(preferenceFilter != null);
198                 }
199                 return Status.OK_STATUS;
200             }
201             
202         };
203         refreshJob.setSystem(true);
204     }
205
206     /**
207      * Create the filter control.
208      */

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     /**
217      * Get the key listener used for the receiver.
218      * @return KeyListener
219      */

220     protected KeyAdapter getKeyListener() {
221         return new KeyAdapter() {
222                 
223                 /*
224                  * (non-Javadoc)
225                  *
226                  * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
227                  */

228                 public void keyReleased(KeyEvent e) {
229                     // on a CR we want to transfer focus to the list
230
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             /* (non-Javadoc)
241              * @see org.eclipse.swt.accessibility.AccessibleListener#getName(org.eclipse.swt.accessibility.AccessibleEvent)
242              */

243             public void getName(AccessibleEvent e) {
244                 String JavaDoc filterTextString = getFilterText();
245                 if(filterTextString.length() == 0){
246                     e.result = initialText;
247                 }
248                 else
249                     e.result = filterTextString;
250             }
251             
252         };
253     }
254
255     /**
256      * Get the text from the filter widget.
257      * @return String
258      */

259     protected String JavaDoc getFilterText() {
260         return filterText.getText();
261     }
262     /**
263      * update the receiver after the text has changed
264      */

265     protected void textChanged() {
266         refreshJob.schedule(200);
267     }
268
269     /**
270      * Get the text from the filter control.
271      * @return Text
272      */

273     protected String JavaDoc getFilterControlText() {
274         return filterText.getText();
275     }
276
277     /**
278      * Set the background for the widgets that support the filter text area
279      *
280      * @param background
281      */

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     /**
290      * Create the button that clears the text.
291      *
292      * @param filterToolBar
293      */

294     private void createClearText(ToolBarManager filterToolBar) {
295
296         IAction clearTextAction = new Action("", IAction.AS_PUSH_BUTTON) {//$NON-NLS-1$
297
/*
298              * (non-Javadoc)
299              *
300              * @see org.eclipse.jface.action.Action#run()
301              */

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     /**
317      * clear the text in the filter text widget
318      */

319     protected void clearText() {
320         setFilterText(""); //$NON-NLS-1$
321

322         if(preferenceFilter != null){
323             getViewer().removeFilter(preferenceFilter);
324             preferenceFilter = null;
325             getShell().setText(cachedTitle);
326         }
327         
328         textChanged();
329     }
330
331     /**
332      * Set the text in the filter area.
333      * @param string
334      */

335     protected void setFilterText(String JavaDoc string) {
336         filterText.setText(string);
337         selectAll();
338     }
339
340     /**
341      * Get the tree viewer associated with this control.
342      *
343      * @return the tree viewer
344      */

345     public TreeViewer getViewer() {
346         return treeViewer;
347     }
348
349     /**
350      * Get the filter text field associated with this control.
351      *
352      * @return the text field
353      */

354     public Text getFilterControl() {
355         return filterText;
356     }
357
358     /**
359      * Set the text that will be shown until the first focus.
360      *
361      * @param text
362      */

363     public void setInitialText(String JavaDoc 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             /*
374              * (non-Javadoc)
375              *
376              * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
377              */

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     /**
392      * Get the initial text for the receiver.
393      * @return String
394      */

395     protected String JavaDoc getInitialText() {
396         return initialText;
397     }
398
399     /**
400      * Add the filter to the viewer.
401      * @param filter
402      */

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     /**
422      * Focus has been gained on the filter control.
423      *
424      */

425     protected void filterFocusGained() {
426         selectAll();
427         getFilterControl().removeFocusListener(listener);
428     }
429
430     /**
431      * Focus has been lost on the filter control.
432      *
433      */

434     protected void filterFocusLost() {
435         
436     }
437     
438 }
439
Popular Tags