1 11 package org.eclipse.ui.internal.dialogs; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 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 45 public class FilteredTextTree extends FilteredTree { 46 private List searchHistory; 48 49 private static final String SEARCHHISTORY = "search"; 51 private Table currentSeachTable; 53 54 60 public FilteredTextTree(Composite parent, int treeStyle) { 61 super(parent, treeStyle); 62 } 63 64 71 public FilteredTextTree(Composite parent, int treeStyle, 72 PatternItemFilter filter) { 73 super(parent, treeStyle, filter); 74 } 75 76 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(""); } else { 110 getViewer().getTree().setFocus(); 111 } 112 } 113 } 114 }); 115 116 filterText.addKeyListener(new KeyAdapter() { 117 122 public void keyReleased(KeyEvent e) { 123 124 if (e.keyCode == SWT.ARROW_DOWN) { 125 if (currentSeachTable.isVisible()) { 126 if (currentSeachTable.getSelectionCount() < 1) 128 currentSeachTable.setSelection(0); 129 currentSeachTable.setFocus(); 130 } else 131 treeViewer.getTree().setFocus(); 133 } else { 134 if (e.character == SWT.CR) 135 return; 136 137 textChanged(); 138 List result = new ArrayList (); 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 174 public void handleEvent(Event event) { 175 if (!shell.isDisposed()) 176 shell.setVisible(false); 177 } 178 }); 179 180 getShell().addControlListener(new ControlAdapter() { 181 186 public void controlMoved(ControlEvent e) { 187 shell.setVisible(false); 188 } 189 }); 190 191 currentSeachTable.addSelectionListener(new SelectionAdapter() { 192 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 214 public void widgetDisposed(DisposeEvent e) { 215 saveDialogSettings(); 216 } 217 }); 218 219 filterText.getAccessible().addAccessibleListener( 220 getAccessibleListener()); 221 222 } 223 224 234 public List reduceSearch(List list, String wordsEntered) { 235 List result = new ArrayList (); 236 if (list == null) 237 return result; 238 for (int i = 0; i < list.size(); i++) { 239 if (filterText.getText() == "") return result; 241 else if (((String ) list.get(i)).startsWith(wordsEntered)) 242 result.add(list.get(i)); 243 } 244 245 return result; 246 } 247 248 254 public void upDateTable(Table table, List 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 ) list.get(i)); 261 262 } 263 } 264 265 } 266 267 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 289 public List getPreferenceSearchHistory() { 290 291 List searchList = new ArrayList (); 292 IDialogSettings settings = getDialogSettings(); 293 String [] search = settings.getArray(SEARCHHISTORY); 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 306 private void saveDialogSettings() { 307 IDialogSettings settings = getDialogSettings(); 308 309 String [] result = new String [searchHistory.size()]; 312 listToArray(searchHistory, result); 313 settings.put(SEARCHHISTORY, result); 314 315 } 316 317 private void listToArray(List list, String [] string) { 318 int size = list.size(); 319 for (int i = 0; i < size; i++) 320 string[i] = (String ) list.get(i); 321 } 322 323 328 protected void filterFocusLost() { 329 String newText = filterText.getText(); 330 Object [] textValues = searchHistory.toArray(); 331 332 if ((newText.equals("")) || (newText.equals(initialText))) return; 334 335 for (int i = 0; i < textValues.length; i++) { 336 if (((String ) textValues[i]).equals(newText)) 337 return; 338 } 339 searchHistory.add(newText); 340 } 341 342 } 343 | Popular Tags |