|
Added
Link Here
|
| 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; |
| 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.FocusAdapter; |
| 23 |
import org.eclipse.swt.events.FocusEvent; |
| 24 |
import org.eclipse.swt.events.KeyAdapter; |
| 25 |
import org.eclipse.swt.events.KeyEvent; |
| 26 |
import org.eclipse.swt.events.SelectionAdapter; |
| 27 |
import org.eclipse.swt.events.SelectionEvent; |
| 28 |
import org.eclipse.swt.events.TraverseEvent; |
| 29 |
import org.eclipse.swt.events.TraverseListener; |
| 30 |
import org.eclipse.swt.graphics.Point; |
| 31 |
import org.eclipse.swt.graphics.Rectangle; |
| 32 |
import org.eclipse.swt.layout.GridData; |
| 33 |
import org.eclipse.swt.layout.GridLayout; |
| 34 |
import org.eclipse.swt.widgets.Composite; |
| 35 |
import org.eclipse.swt.widgets.Display; |
| 36 |
import org.eclipse.swt.widgets.Event; |
| 37 |
import org.eclipse.swt.widgets.Listener; |
| 38 |
import org.eclipse.swt.widgets.Shell; |
| 39 |
import org.eclipse.swt.widgets.Table; |
| 40 |
import org.eclipse.swt.widgets.TableItem; |
| 41 |
import org.eclipse.swt.widgets.Text; |
| 42 |
import org.eclipse.ui.internal.WorkbenchPlugin; |
| 43 |
|
| 44 |
/** |
| 45 |
* The FilteredTextTree is a filtered tree that uses an |
| 46 |
* editable text. |
| 47 |
*/ |
| 48 |
public class FilteredTextTree extends FilteredTree { |
| 49 |
//A list contains all strings in search history |
| 50 |
private List searchHistory; |
| 51 |
|
| 52 |
private static final String SEARCHHISTORY = "search"; //$NON-NLS-1$ |
| 53 |
|
| 54 |
//A table which contains only strings begin with typed strings |
| 55 |
private Table currentSeachTable; |
| 56 |
/** |
| 57 |
* Create a new instance of the receiver. |
| 58 |
* @param parent |
| 59 |
* @param treeStyle |
| 60 |
*/ |
| 61 |
public FilteredTextTree(Composite parent, int treeStyle) { |
| 62 |
super(parent, treeStyle); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Create a new instance of the receiver with a supplied filter. |
| 67 |
* @param parent |
| 68 |
* @param treeStyle |
| 69 |
* @param filter |
| 70 |
*/ |
| 71 |
public FilteredTextTree(Composite parent, int treeStyle, PatternItemFilter filter) { |
| 72 |
super(parent, treeStyle, filter); |
| 73 |
} |
| 74 |
|
| 75 |
/* (non-Javadoc) |
| 76 |
* @see org.eclipse.ui.internal.dialogs.FilteredTree#createFilterControl(org.eclipse.swt.widgets.Composite) |
| 77 |
*/ |
| 78 |
protected void createFilterControl(final Composite parent) { |
| 79 |
filterText = new Text(parent, SWT.DROP_DOWN | SWT.BORDER); |
| 80 |
filterText.setFont(parent.getFont()); |
| 81 |
searchHistory = getPreferenceSearchHistory(); |
| 82 |
|
| 83 |
final Shell shell = new Shell (parent.getShell(), SWT.NO_TRIM ); |
| 84 |
shell.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); |
| 85 |
GridLayout shellGL = new GridLayout(); |
| 86 |
shellGL.marginHeight = 0; |
| 87 |
shellGL.marginWidth = 0; |
| 88 |
shell.setLayout(shellGL); |
| 89 |
shell.setLayoutData (new GridData((GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL))); |
| 90 |
|
| 91 |
currentSeachTable = new Table(shell, SWT.SINGLE |SWT.BORDER); |
| 92 |
currentSeachTable.setLayoutData(new GridData((GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL))); |
| 93 |
|
| 94 |
filterText.addTraverseListener( new TraverseListener () { |
| 95 |
public void keyTraversed(TraverseEvent e) { |
| 96 |
if (e.detail == SWT.TRAVERSE_RETURN) { |
| 97 |
e.doit = false; |
| 98 |
shell.setVisible(false); |
| 99 |
if (getViewer().getTree().getItemCount() == 0) { |
| 100 |
Display.getCurrent().beep(); |
| 101 |
setFilterText(""); //$NON-NLS-1$ |
| 102 |
} else { |
| 103 |
getViewer().getTree().setFocus(); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
}); |
| 108 |
|
| 109 |
filterText.addKeyListener(new KeyAdapter() { |
| 110 |
/* |
| 111 |
* (non-Javadoc) |
| 112 |
* |
| 113 |
* @see org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.swt.events.KeyEvent) |
| 114 |
*/ |
| 115 |
public void keyReleased(KeyEvent e) { |
| 116 |
|
| 117 |
if(e.keyCode == SWT.ARROW_DOWN){ |
| 118 |
if(currentSeachTable.isVisible()){ |
| 119 |
//Make selection at popup table |
| 120 |
if(currentSeachTable.getSelectionCount()< 1) |
| 121 |
currentSeachTable.setSelection(0); |
| 122 |
currentSeachTable.setFocus(); |
| 123 |
} |
| 124 |
else |
| 125 |
//Make selection be on the left tree |
| 126 |
treeViewer.getTree().setFocus(); |
| 127 |
} |
| 128 |
else{ |
| 129 |
if(e.character == '\r') |
| 130 |
return; |
| 131 |
|
| 132 |
textChanged(); |
| 133 |
List result = new ArrayList(); |
| 134 |
result = reduceSearch(searchHistory,filterText.getText()); |
| 135 |
upDateTable(currentSeachTable,result); |
| 136 |
|
| 137 |
if(currentSeachTable.getItemCount()> 0){ |
| 138 |
Rectangle textBounds = filterText.getBounds(); |
| 139 |
Point point = getDisplay().map(parent, null, textBounds.x, textBounds.y); |
| 140 |
int space = currentSeachTable.getItemHeight(); |
| 141 |
shell.setBounds(point.x, point.y + textBounds.height,textBounds.width, |
| 142 |
currentSeachTable.getItemHeight()*currentSeachTable.getItemCount()+ space); |
| 143 |
|
| 144 |
if(shell.isDisposed()) |
| 145 |
shell.open (); |
| 146 |
|
| 147 |
if(!shell.getVisible()){ |
| 148 |
shell.setVisible(true); |
| 149 |
filterText.setFocus(); |
| 150 |
} |
| 151 |
|
| 152 |
}else |
| 153 |
shell.setVisible(false); |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
}); |
| 158 |
|
| 159 |
|
| 160 |
filterText.addFocusListener(new FocusAdapter(){ |
| 161 |
/* (non-Javadoc) |
| 162 |
* @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent) |
| 163 |
*/ |
| 164 |
public void focusLost(FocusEvent e) { |
| 165 |
|
| 166 |
String newText = filterText.getText(); |
| 167 |
Object[] textValues = searchHistory.toArray(); |
| 168 |
|
| 169 |
if((newText.equals(""))||(newText .equals(initialText)))//$NON-NLS-1$ |
| 170 |
return; |
| 171 |
|
| 172 |
for (int i = 0; i < textValues.length; i++) { |
| 173 |
if(((String)textValues[i]).equals(newText)) |
| 174 |
return; |
| 175 |
} |
| 176 |
searchHistory.add(newText); |
| 177 |
} |
| 178 |
}); |
| 179 |
|
| 180 |
|
| 181 |
parent.getDisplay().addFilter(SWT.MouseDown , new Listener(){ |
| 182 |
/* (non-Javadoc) |
| 183 |
* @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Events) |
| 184 |
*/ |
| 185 |
public void handleEvent(Event event) { |
| 186 |
if(!shell.isDisposed()) |
| 187 |
shell.setVisible(false); |
| 188 |
}}); |
| 189 |
|
| 190 |
getShell().addControlListener(new ControlAdapter(){ |
| 191 |
/* (non-Javadoc) |
| 192 |
* @see org.eclipse.swt.events.ControlListener#controlMoved(org.eclipse.swt.events.ControlEvent) |
| 193 |
*/ |
| 194 |
public void controlMoved(ControlEvent e) { |
| 195 |
shell.setVisible(false); |
| 196 |
} |
| 197 |
}); |
| 198 |
|
| 199 |
currentSeachTable.addSelectionListener(new SelectionAdapter(){ |
| 200 |
/* (non-Javadoc) |
| 201 |
* @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent) |
| 202 |
*/ |
| 203 |
public void widgetSelected(SelectionEvent e) { |
| 204 |
|
| 205 |
setFilterText(currentSeachTable.getSelection()[0].getText()); |
| 206 |
textChanged(); |
| 207 |
} |
| 208 |
|
| 209 |
public void widgetDefaultSelected(SelectionEvent e) { |
| 210 |
shell.setVisible(false); |
| 211 |
} |
| 212 |
}); |
| 213 |
|
| 214 |
filterText.addDisposeListener(new DisposeListener() { |
| 215 |
/* |
| 216 |
(non-Javadoc) |
| 217 |
* @see org.eclipse.swt.events.DisposeListener#widgetDisposed(org.eclipse.swt.events.DisposeEvent) |
| 218 |
*/ |
| 219 |
public void widgetDisposed(DisposeEvent e) { |
| 220 |
saveDialogSettings(); |
| 221 |
} |
| 222 |
}); |
| 223 |
|
| 224 |
filterText.getAccessible().addAccessibleListener(getAccessibleListener()); |
| 225 |
|
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Find all items which start with typyed words |
| 230 |
* list the list contains all strings of the search history |
| 231 |
* @param wordsEntered |
| 232 |
* @return a list in which all strings start from the typed letter(s) |
| 233 |
*/ |
| 234 |
public List reduceSearch(List list,String wordsEntered){ |
| 235 |
List result = new ArrayList(); |
| 236 |
if(list == null) return result; |
| 237 |
for(int i = 0; i<list.size();i++){ |
| 238 |
if(filterText.getText()== "") //$NON-NLS-1$ |
| 239 |
return result; |
| 240 |
else if(((String)list.get(i)).startsWith(wordsEntered)) |
| 241 |
result.add(list.get(i)); |
| 242 |
} |
| 243 |
|
| 244 |
return result; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
/**Copy all elements from a list to a table |
| 249 |
* @param table |
| 250 |
* @param list |
| 251 |
*/ |
| 252 |
public void upDateTable(Table table, List list){ |
| 253 |
table.removeAll(); |
| 254 |
if(list.size()>0){ |
| 255 |
TableItem newItem ; |
| 256 |
for(int i=0;i<list.size();i++){ |
| 257 |
newItem = new TableItem(table, SWT.NULL, i); |
| 258 |
newItem.setText((String)list.get(i)); |
| 259 |
|
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Return a dialog setting section for this dialog |
| 267 |
* @return IDialogSettings |
| 268 |
*/ |
| 269 |
private IDialogSettings getDialogSettings() { |
| 270 |
IDialogSettings settings = WorkbenchPlugin.getDefault() |
| 271 |
.getDialogSettings(); |
| 272 |
IDialogSettings thisSettings = settings |
| 273 |
.getSection(getClass().getName()); |
| 274 |
if (thisSettings == null) |
| 275 |
thisSettings = settings.addNewSection(getClass().getName()); |
| 276 |
|
| 277 |
return thisSettings; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
/** |
| 282 |
* Get the preferences search history for this eclipse's start, |
| 283 |
* Note that this history will not be cleared until this eclipse closes |
| 284 |
* @return a list |
| 285 |
*/ |
| 286 |
public List getPreferenceSearchHistory(){ |
| 287 |
|
| 288 |
List searchList = new ArrayList(); |
| 289 |
IDialogSettings settings = getDialogSettings(); |
| 290 |
String[] search = settings.getArray(SEARCHHISTORY); //$NON-NLS-1$ |
| 291 |
|
| 292 |
if(search != null){ |
| 293 |
for(int i = 0; i < search.length;i++) |
| 294 |
searchList.add(search[i]); |
| 295 |
} |
| 296 |
return searchList; |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Saves the search history. |
| 302 |
*/ |
| 303 |
private void saveDialogSettings() { |
| 304 |
IDialogSettings settings =getDialogSettings(); |
| 305 |
|
| 306 |
//If the settings contains the same key, the previous value will be replaced by new one |
| 307 |
String []result = new String[searchHistory.size()]; |
| 308 |
listToArray(searchHistory,result); |
| 309 |
settings.put(SEARCHHISTORY, result); |
| 310 |
|
| 311 |
} |
| 312 |
|
| 313 |
private void listToArray(List list,String[] string){ |
| 314 |
int size = list.size(); |
| 315 |
for(int i=0;i<size;i++) |
| 316 |
string[i]=(String)list.get(i); |
| 317 |
} |
| 318 |
|
| 319 |
} |