KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.jface.dialogs.IDialogSettings;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.events.DisposeEvent;
16 import org.eclipse.swt.events.DisposeListener;
17 import org.eclipse.swt.events.FocusAdapter;
18 import org.eclipse.swt.events.FocusEvent;
19 import org.eclipse.swt.events.SelectionAdapter;
20 import org.eclipse.swt.events.SelectionEvent;
21 import org.eclipse.swt.events.TraverseEvent;
22 import org.eclipse.swt.events.TraverseListener;
23 import org.eclipse.swt.graphics.Point;
24 import org.eclipse.swt.widgets.Combo;
25 import org.eclipse.swt.widgets.Composite;
26 import org.eclipse.swt.widgets.Control;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.ui.internal.WorkbenchPlugin;
29
30 /**
31  * The FilteredComboTree is a filtered tree that uses an
32  * editable combo rather than just a text.
33  */

34 public class FilteredComboTree extends FilteredTree {
35
36     private Combo filterCombo;
37     
38     private static final String JavaDoc SEARCHHISTORY = "search"; //$NON-NLS-1$
39

40     //Set a number limitation of items to be saved in combo
41
private static final int maxNumItems = 20;
42     /**
43      * Create a new instance of the receiver.
44      * @param parent
45      * @param treeStyle
46      */

47     public FilteredComboTree(Composite parent, int treeStyle) {
48         super(parent, treeStyle);
49     }
50
51     /**
52      * Create a new instance of the receiver with a supplied filter.
53      * @param parent
54      * @param treeStyle
55      * @param filter
56      */

57     public FilteredComboTree(Composite parent, int treeStyle, PatternItemFilter filter) {
58         super(parent, treeStyle, filter);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.ui.internal.dialogs.FilteredTree#createFilterControl(org.eclipse.swt.widgets.Composite)
63      */

64     protected void createFilterControl(Composite parent) {
65         filterCombo = new Combo(parent, SWT.DROP_DOWN | SWT.BORDER);
66         filterCombo.setFont(parent.getFont());
67         getPreferenceSearchHistory();
68         filterCombo.addTraverseListener( new TraverseListener () {
69             public void keyTraversed(TraverseEvent e) {
70                 if (e.detail == SWT.TRAVERSE_RETURN) {
71                     e.doit = false;
72                     if (getViewer().getTree().getItemCount() == 0) {
73                         Display.getCurrent().beep();
74                         setFilterText(""); //$NON-NLS-1$
75
} else {
76                         getViewer().getTree().setFocus();
77                     }
78                 }
79             }
80         });
81         filterCombo.addFocusListener(new FocusAdapter(){
82             /* (non-Javadoc)
83              * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
84              */

85             public void focusLost(FocusEvent e) {
86                 String JavaDoc [] textValues = filterCombo.getItems();
87                 String JavaDoc newText = filterCombo.getText();
88                 
89                 if((newText.equals(""))||(newText .equals(initialText)))//$NON-NLS-1$
90
return;
91             
92                 for (int i = 0; i < textValues.length; i++) {
93                     if(textValues[i].equals(newText))
94                         return;
95                 }
96                     
97                 if(textValues.length >= maxNumItems)
98                     //Discard the oldest search to get space for new search
99
filterCombo.remove(maxNumItems-1);
100                 
101                 filterCombo.add(newText,0);
102             }
103         });
104         filterCombo.addSelectionListener(new SelectionAdapter(){
105             /* (non-Javadoc)
106              * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
107              */

108             public void widgetSelected(SelectionEvent e) {
109                 textChanged();
110             }
111         });
112         
113         filterCombo.addDisposeListener(new DisposeListener() {
114         
115             /* (non-Javadoc)
116              * @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent)
117              */

118             public void widgetDisposed(DisposeEvent e) {
119                 saveDialogSettings();
120             }
121         });
122         
123         filterCombo.getAccessible().addAccessibleListener(getAccessibleListener());
124         
125     }
126
127     /* (non-Javadoc)
128      * @see org.eclipse.ui.internal.dialogs.FilteredTree#getFilterControl()
129      */

130     public Control getFilterControl() {
131         return filterCombo;
132     }
133
134     /* (non-Javadoc)
135      * @see org.eclipse.ui.internal.dialogs.FilteredTree#getFilterControlText()
136      */

137     protected String JavaDoc getFilterControlText() {
138         return filterCombo.getText();
139     }
140
141     /* (non-Javadoc)
142      * @see org.eclipse.ui.internal.dialogs.FilteredTree#setFilterText(java.lang.String)
143      */

144     protected void setFilterText(String JavaDoc string) {
145         filterCombo.setText(string);
146         selectAll();
147     }
148     
149     protected void selectAll() {
150         filterCombo.setSelection(new Point(0,filterCombo.getText().length()));
151     }
152     
153     /**
154      * Get the combo box used by the receiver.
155      * @return Combo
156      */

157     public Combo getFilterCombo() {
158         return filterCombo;
159     }
160     
161     /* (non-Javadoc)
162      * @see org.eclipse.ui.internal.dialogs.FilteredTree#getFilterText()
163      */

164     protected String JavaDoc getFilterText() {
165         return filterCombo.getText();
166     }
167         
168     /**
169      * Return a dialog setting section for this dialog
170      */

171     private IDialogSettings getDialogSettings() {
172         IDialogSettings settings = WorkbenchPlugin.getDefault()
173                 .getDialogSettings();
174         IDialogSettings thisSettings = settings
175                 .getSection(getClass().getName());
176         if (thisSettings == null)
177             thisSettings = settings.addNewSection(getClass().getName());
178         return thisSettings;
179     }
180    
181     
182     /**
183      * Get the preferences search history for this eclipse's start,
184      * Note that this history will not be cleared until this eclipse closes
185      *
186      */

187     public void getPreferenceSearchHistory(){
188         IDialogSettings settings = getDialogSettings();
189         String JavaDoc[] search = settings.getArray(SEARCHHISTORY); //$NON-NLS-1$
190

191         if(search == null)
192             return;
193         
194         for(int i = 0; i < search.length;i++){
195             filterCombo.add(search[i]);
196         }
197                 
198     }
199     
200      /**
201      * Saves the search history.
202      */

203     private void saveDialogSettings() {
204         IDialogSettings settings =getDialogSettings();
205         
206         //If the settings contains the same key, the previous value will be replaced by new one
207
settings.put(SEARCHHISTORY,filterCombo.getItems());
208                
209     }
210 }
211
Popular Tags