KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 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 java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.swt.SWT;
18 import org.eclipse.swt.events.ControlAdapter;
19 import org.eclipse.swt.events.ControlEvent;
20 import org.eclipse.swt.events.DisposeEvent;
21 import org.eclipse.swt.events.DisposeListener;
22 import org.eclipse.swt.events.KeyAdapter;
23 import org.eclipse.swt.events.KeyEvent;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.TraverseEvent;
27 import org.eclipse.swt.events.TraverseListener;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Display;
34 import org.eclipse.swt.widgets.Event;
35 import org.eclipse.swt.widgets.Listener;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.swt.widgets.Table;
38 import org.eclipse.swt.widgets.TableItem;
39 import org.eclipse.swt.widgets.Text;
40 import org.eclipse.ui.internal.WorkbenchPlugin;
41
42 /**
43  * The FilteredTextTree is a filtered tree that uses an editable text.
44  */

45 public class FilteredTextTree extends FilteredTree {
46     // A list contains all strings in search history
47
private List JavaDoc searchHistory;
48
49     private static final String JavaDoc SEARCHHISTORY = "search"; //$NON-NLS-1$
50

51     // A table which contains only strings begin with typed strings
52
private Table currentSeachTable;
53
54     /**
55      * Create a new instance of the receiver.
56      *
57      * @param parent
58      * @param treeStyle
59      */

60     public FilteredTextTree(Composite parent, int treeStyle) {
61         super(parent, treeStyle);
62     }
63
64     /**
65      * Create a new instance of the receiver with a supplied filter.
66      *
67      * @param parent
68      * @param treeStyle
69      * @param filter
70      */

71     public FilteredTextTree(Composite parent, int treeStyle,
72             PatternItemFilter filter) {
73         super(parent, treeStyle, filter);
74     }
75
76     /*
77      * (non-Javadoc)
78      *
79      * @see org.eclipse.ui.internal.dialogs.FilteredTree#createFilterControl(org.eclipse.swt.widgets.Composite)
80      */

81     protected void createFilterControl(final Composite parent) {
82         filterText = new Text(parent, SWT.DROP_DOWN | SWT.BORDER);
83         filterText.setFont(parent.getFont());
84         searchHistory = getPreferenceSearchHistory();
85
86         final Shell shell = new Shell(parent.getShell(), SWT.NO_TRIM);
87         shell
88                 .setBackground(parent.getDisplay().getSystemColor(
89                         SWT.COLOR_WHITE));
90         GridLayout shellGL = new GridLayout();
91         shellGL.marginHeight = 0;
92         shellGL.marginWidth = 0;
93         shell.setLayout(shellGL);
94         shell.setLayoutData(new GridData(
95                 (GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL)));
96
97         currentSeachTable = new Table(shell, SWT.SINGLE | SWT.BORDER);
98         currentSeachTable.setLayoutData(new GridData(
99                 (GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL)));
100
101         filterText.addTraverseListener(new TraverseListener() {
102             public void keyTraversed(TraverseEvent e) {
103                 if (e.detail == SWT.TRAVERSE_RETURN) {
104                     e.doit = false;
105                     shell.setVisible(false);
106                     if (getViewer().getTree().getItemCount() == 0) {
107                         Display.getCurrent().beep();
108                         setFilterText(""); //$NON-NLS-1$
109
} else {
110                         getViewer().getTree().setFocus();
111                     }
112                 }
113             }
114         });
115
116         filterText.addKeyListener(new KeyAdapter() {
117             /*
118              * (non-Javadoc)
119              *
120              * @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent)
121              */

122             public void keyReleased(KeyEvent e) {
123
124                 if (e.keyCode == SWT.ARROW_DOWN) {
125                     if (currentSeachTable.isVisible()) {
126                         // Make selection at popup table
127
if (currentSeachTable.getSelectionCount() < 1)
128                             currentSeachTable.setSelection(0);
129                         currentSeachTable.setFocus();
130                     } else
131                         // Make selection be on the left tree
132
treeViewer.getTree().setFocus();
133                 } else {
134                     if (e.character == SWT.CR)
135                         return;
136
137                     textChanged();
138                     List JavaDoc result = new ArrayList JavaDoc();
139                     result = reduceSearch(searchHistory, filterText.getText());
140                     upDateTable(currentSeachTable, result);
141
142                     if (currentSeachTable.getItemCount() > 0) {
143                         Rectangle textBounds = filterText.getBounds();
144                         Point point = getDisplay().map(parent, null,
145                                 textBounds.x, textBounds.y);
146                         int space = currentSeachTable.getItemHeight();
147                         shell.setBounds(point.x, point.y + textBounds.height,
148                                 textBounds.width, currentSeachTable
149                                         .getItemHeight()
150                                         * currentSeachTable.getItemCount()
151                                         + space);
152
153                         if (shell.isDisposed())
154                             shell.open();
155
156                         if (!shell.getVisible()) {
157                             shell.setVisible(true);
158                             filterText.setFocus();
159                         }
160
161                     } else
162                         shell.setVisible(false);
163                 }
164
165             }
166         });
167
168         parent.getDisplay().addFilter(SWT.MouseDown, new Listener() {
169             /*
170              * (non-Javadoc)
171              *
172              * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Events)
173              */

174             public void handleEvent(Event event) {
175                 if (!shell.isDisposed())
176                     shell.setVisible(false);
177             }
178         });
179
180         getShell().addControlListener(new ControlAdapter() {
181             /*
182              * (non-Javadoc)
183              *
184              * @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent)
185              */

186             public void controlMoved(ControlEvent e) {
187                 shell.setVisible(false);
188             }
189         });
190
191         currentSeachTable.addSelectionListener(new SelectionAdapter() {
192             /*
193              * (non-Javadoc)
194              *
195              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
196              */

197             public void widgetSelected(SelectionEvent e) {
198
199                 setFilterText(currentSeachTable.getSelection()[0].getText());
200                 textChanged();
201             }
202
203             public void widgetDefaultSelected(SelectionEvent e) {
204                 shell.setVisible(false);
205             }
206         });
207
208         filterText.addDisposeListener(new DisposeListener() {
209             /*
210              * (non-Javadoc)
211              *
212              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
213              */

214             public void widgetDisposed(DisposeEvent e) {
215                 saveDialogSettings();
216             }
217         });
218
219         filterText.getAccessible().addAccessibleListener(
220                 getAccessibleListener());
221
222     }
223
224     /**
225      * Find all items which start with typed words list the list contains all
226      * strings of the search history
227      *
228      * @param list
229      * the list to search
230      * @param wordsEntered
231      * String
232      * @return a list in which all strings start from the typed letter(s)
233      */

234     public List JavaDoc reduceSearch(List JavaDoc list, String JavaDoc wordsEntered) {
235         List JavaDoc result = new ArrayList JavaDoc();
236         if (list == null)
237             return result;
238         for (int i = 0; i < list.size(); i++) {
239             if (filterText.getText() == "") //$NON-NLS-1$
240
return result;
241             else if (((String JavaDoc) list.get(i)).startsWith(wordsEntered))
242                 result.add(list.get(i));
243         }
244
245         return result;
246     }
247
248     /**
249      * Copy all elements from a list to a table
250      *
251      * @param table
252      * @param list
253      */

254     public void upDateTable(Table table, List JavaDoc list) {
255         table.removeAll();
256         if (list.size() > 0) {
257             TableItem newItem;
258             for (int i = 0; i < list.size(); i++) {
259                 newItem = new TableItem(table, SWT.NULL, i);
260                 newItem.setText((String JavaDoc) list.get(i));
261
262             }
263         }
264
265     }
266
267     /**
268      * Return a dialog setting section for this dialog
269      *
270      * @return IDialogSettings
271      */

272     private IDialogSettings getDialogSettings() {
273         IDialogSettings settings = WorkbenchPlugin.getDefault()
274                 .getDialogSettings();
275         IDialogSettings thisSettings = settings
276                 .getSection(getClass().getName());
277         if (thisSettings == null)
278             thisSettings = settings.addNewSection(getClass().getName());
279
280         return thisSettings;
281     }
282
283     /**
284      * Get the preferences search history for this eclipse's start, Note that
285      * this history will not be cleared until this eclipse closes
286      *
287      * @return a list
288      */

289     public List JavaDoc getPreferenceSearchHistory() {
290
291         List JavaDoc searchList = new ArrayList JavaDoc();
292         IDialogSettings settings = getDialogSettings();
293         String JavaDoc[] search = settings.getArray(SEARCHHISTORY); //$NON-NLS-1$
294

295         if (search != null) {
296             for (int i = 0; i < search.length; i++)
297                 searchList.add(search[i]);
298         }
299         return searchList;
300
301     }
302
303     /**
304      * Saves the search history.
305      */

306     private void saveDialogSettings() {
307         IDialogSettings settings = getDialogSettings();
308
309         // If the settings contains the same key, the previous value will be
310
// replaced by new one
311
String JavaDoc[] result = new String JavaDoc[searchHistory.size()];
312         listToArray(searchHistory, result);
313         settings.put(SEARCHHISTORY, result);
314
315     }
316
317     private void listToArray(List JavaDoc list, String JavaDoc[] string) {
318         int size = list.size();
319         for (int i = 0; i < size; i++)
320             string[i] = (String JavaDoc) list.get(i);
321     }
322
323     /*
324      * (non-Javadoc)
325      *
326      * @see org.eclipse.ui.internal.dialogs.FilteredTree#filterFocusLost()
327      */

328     protected void filterFocusLost() {
329         String JavaDoc newText = filterText.getText();
330         Object JavaDoc[] textValues = searchHistory.toArray();
331
332         if ((newText.equals("")) || (newText.equals(initialText)))//$NON-NLS-1$
333
return;
334
335         for (int i = 0; i < textValues.length; i++) {
336             if (((String JavaDoc) textValues[i]).equals(newText))
337                 return;
338         }
339         searchHistory.add(newText);
340     }
341
342 }
343
Popular Tags