1 11 12 package org.eclipse.debug.internal.ui.viewers; 13 14 import org.eclipse.jface.viewers.CellEditor; 15 import org.eclipse.jface.viewers.ICellEditorListener; 16 import org.eclipse.jface.viewers.ICellModifier; 17 import org.eclipse.jface.viewers.IStructuredSelection; 18 import org.eclipse.jface.viewers.StructuredSelection; 19 import org.eclipse.jface.viewers.StructuredViewer; 20 import org.eclipse.jface.viewers.TreePath; 21 import org.eclipse.jface.viewers.TreeSelection; 22 import org.eclipse.swt.events.FocusAdapter; 23 import org.eclipse.swt.events.FocusEvent; 24 import org.eclipse.swt.events.FocusListener; 25 import org.eclipse.swt.events.MouseAdapter; 26 import org.eclipse.swt.events.MouseEvent; 27 import org.eclipse.swt.events.MouseListener; 28 import org.eclipse.swt.graphics.Rectangle; 29 import org.eclipse.swt.widgets.Control; 30 import org.eclipse.swt.widgets.Display; 31 import org.eclipse.swt.widgets.Item; 32 33 38 abstract class TreeEditorImpl { 39 40 private CellEditor cellEditor; 41 42 private CellEditor[] cellEditors; 43 44 private ICellModifier cellModifier; 45 46 private String [] columnProperties; 47 48 private Item treeItem; 49 50 private int columnNumber; 51 52 private ICellEditorListener cellEditorListener; 53 54 private FocusListener focusListener; 55 56 private MouseListener mouseListener; 57 58 private int doubleClickExpirationTime; 59 60 private StructuredViewer viewer; 61 62 TreeEditorImpl(StructuredViewer viewer) { 63 this.viewer = viewer; 64 initCellEditorListener(); 65 } 66 67 72 public StructuredViewer getViewer() { 73 return viewer; 74 } 75 76 private void activateCellEditor() { 77 if (cellEditors != null) { 78 if (cellEditors[columnNumber] != null && cellModifier != null) { 79 Object element = treeItem.getData(); 80 String property = columnProperties[columnNumber]; 81 if (cellModifier.canModify(element, property)) { 82 cellEditor = cellEditors[columnNumber]; 83 cellEditor.addListener(cellEditorListener); 85 Object value = cellModifier.getValue(element, property); 86 cellEditor.setValue(value); 87 final Control control = cellEditor.getControl(); 91 cellEditor.activate(); 92 if (control == null) { 93 return; 94 } 95 setLayoutData(cellEditor.getLayoutData()); 96 setEditor(control, treeItem, columnNumber); 97 cellEditor.setFocus(); 98 if (focusListener == null) { 99 focusListener = new FocusAdapter() { 100 public void focusLost(FocusEvent e) { 101 applyEditorValue(); 102 } 103 }; 104 } 105 control.addFocusListener(focusListener); 106 mouseListener = new MouseAdapter() { 107 public void mouseDown(MouseEvent e) { 108 if (e.time <= doubleClickExpirationTime) { 111 control.removeMouseListener(mouseListener); 112 cancelEditing(); 113 handleDoubleClickEvent(); 114 } else if (mouseListener != null) { 115 control.removeMouseListener(mouseListener); 116 } 117 } 118 }; 119 control.addMouseListener(mouseListener); 120 } 121 } 122 } 123 } 124 125 128 private void activateCellEditor(MouseEvent event) { 129 if (treeItem == null || treeItem.isDisposed()) { 130 return; 132 } 133 int columnToEdit; 134 int columns = getColumnCount(); 135 if (columns == 0) { 136 columnToEdit = 0; 139 } else { 140 columnToEdit = -1; 141 for (int i = 0; i < columns; i++) { 142 Rectangle bounds = getBounds(treeItem, i); 143 if (bounds.contains(event.x, event.y)) { 144 columnToEdit = i; 145 break; 146 } 147 } 148 if (columnToEdit == -1) { 149 return; 150 } 151 } 152 153 columnNumber = columnToEdit; 154 activateCellEditor(); 155 } 156 157 160 public void applyEditorValue() { 161 CellEditor c = this.cellEditor; 162 if (c != null) { 163 this.cellEditor = null; 167 Item t = this.treeItem; 168 if (t != null && !t.isDisposed()) { 170 saveEditorValue(c, t); 171 } 172 setEditor(null, null, 0); 173 c.removeListener(cellEditorListener); 174 Control control = c.getControl(); 175 if (control != null) { 176 if (mouseListener != null) { 177 control.removeMouseListener(mouseListener); 178 } 179 if (focusListener != null) { 180 control.removeFocusListener(focusListener); 181 } 182 } 183 c.deactivate(); 184 } 185 } 186 187 191 public void cancelEditing() { 192 if (cellEditor != null) { 193 setEditor(null, null, 0); 194 cellEditor.removeListener(cellEditorListener); 195 CellEditor oldEditor = cellEditor; 196 cellEditor = null; 197 oldEditor.deactivate(); 198 } 199 } 200 201 206 public void editElement(Object element, int column) { 207 if (cellEditor != null) { 208 applyEditorValue(); 209 } 210 211 IStructuredSelection structuredSelection; 212 if(element instanceof TreePath) { 213 structuredSelection = new TreeSelection((TreePath)element, viewer.getComparer()); 214 } else { 215 structuredSelection = new StructuredSelection(element); 216 } 217 setSelection(structuredSelection, true); 218 Item[] selection = getSelection(); 219 if (selection.length != 1) { 220 return; 221 } 222 223 treeItem = selection[0]; 224 225 showSelection(); 227 columnNumber = column; 228 activateCellEditor(); 229 230 } 231 232 abstract Rectangle getBounds(Item item, int columnIndex); 233 234 238 public CellEditor[] getCellEditors() { 239 return cellEditors; 240 } 241 242 246 public ICellModifier getCellModifier() { 247 return cellModifier; 248 } 249 250 abstract int getColumnCount(); 251 252 256 public Object [] getColumnProperties() { 257 return columnProperties; 258 } 259 260 abstract Item[] getSelection(); 261 262 266 public void handleMouseDown(MouseEvent event) { 267 if (event.button != 1) { 268 return; 269 } 270 271 if (cellEditor != null) { 272 applyEditorValue(); 273 } 274 275 doubleClickExpirationTime = event.time 281 + Display.getCurrent().getDoubleClickTime(); 282 283 Item[] items = getSelection(); 284 if (items.length != 1) { 286 treeItem = null; 287 return; 288 } 289 treeItem = items[0]; 290 activateCellEditor(event); 291 } 292 293 private void initCellEditorListener() { 294 cellEditorListener = new ICellEditorListener() { 295 public void editorValueChanged(boolean oldValidState, 296 boolean newValidState) { 297 } 299 300 public void cancelEditor() { 301 TreeEditorImpl.this.cancelEditing(); 302 } 303 304 public void applyEditorValue() { 305 TreeEditorImpl.this.applyEditorValue(); 306 } 307 }; 308 } 309 310 315 public boolean isCellEditorActive() { 316 return cellEditor != null; 317 } 318 319 323 private void saveEditorValue(CellEditor editor, Item item) { 324 if (cellModifier != null) { 325 String property = null; 326 if (columnProperties != null 327 && columnNumber < columnProperties.length) { 328 property = columnProperties[columnNumber]; 329 } 330 cellModifier.modify(item.getData(), property, editor.getValue()); 331 } 332 } 333 334 338 public void setCellEditors(CellEditor[] editors) { 339 this.cellEditors = editors; 340 } 341 342 346 public void setCellModifier(ICellModifier modifier) { 347 this.cellModifier = modifier; 348 } 349 350 354 public void setColumnProperties(String [] columnProperties) { 355 this.columnProperties = columnProperties; 356 } 357 358 abstract void setEditor(Control w, Item item, int fColumnNumber); 359 360 abstract void setLayoutData(CellEditor.LayoutData layoutData); 361 362 abstract void setSelection(IStructuredSelection selection, boolean b); 363 364 abstract void showSelection(); 365 366 abstract void handleDoubleClickEvent(); 367 } 368 | Popular Tags |