1 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 34 public class FilteredComboTree extends FilteredTree { 35 36 private Combo filterCombo; 37 38 private static final String SEARCHHISTORY = "search"; 40 private static final int maxNumItems = 20; 42 47 public FilteredComboTree(Composite parent, int treeStyle) { 48 super(parent, treeStyle); 49 } 50 51 57 public FilteredComboTree(Composite parent, int treeStyle, PatternItemFilter filter) { 58 super(parent, treeStyle, filter); 59 } 60 61 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(""); } else { 76 getViewer().getTree().setFocus(); 77 } 78 } 79 } 80 }); 81 filterCombo.addFocusListener(new FocusAdapter(){ 82 85 public void focusLost(FocusEvent e) { 86 String [] textValues = filterCombo.getItems(); 87 String newText = filterCombo.getText(); 88 89 if((newText.equals(""))||(newText .equals(initialText))) 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 filterCombo.remove(maxNumItems-1); 100 101 filterCombo.add(newText,0); 102 } 103 }); 104 filterCombo.addSelectionListener(new SelectionAdapter(){ 105 108 public void widgetSelected(SelectionEvent e) { 109 textChanged(); 110 } 111 }); 112 113 filterCombo.addDisposeListener(new DisposeListener() { 114 115 118 public void widgetDisposed(DisposeEvent e) { 119 saveDialogSettings(); 120 } 121 }); 122 123 filterCombo.getAccessible().addAccessibleListener(getAccessibleListener()); 124 125 } 126 127 130 public Control getFilterControl() { 131 return filterCombo; 132 } 133 134 137 protected String getFilterControlText() { 138 return filterCombo.getText(); 139 } 140 141 144 protected void setFilterText(String 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 157 public Combo getFilterCombo() { 158 return filterCombo; 159 } 160 161 164 protected String getFilterText() { 165 return filterCombo.getText(); 166 } 167 168 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 187 public void getPreferenceSearchHistory(){ 188 IDialogSettings settings = getDialogSettings(); 189 String [] search = settings.getArray(SEARCHHISTORY); 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 203 private void saveDialogSettings() { 204 IDialogSettings settings =getDialogSettings(); 205 206 settings.put(SEARCHHISTORY,filterCombo.getItems()); 208 209 } 210 } 211 | Popular Tags |