1 11 package org.eclipse.debug.internal.ui.viewers; 12 13 import org.eclipse.jface.viewers.CellEditor; 14 import org.eclipse.jface.viewers.ICellEditorListener; 15 import org.eclipse.jface.viewers.ICellModifier; 16 import org.eclipse.jface.viewers.StructuredSelection; 17 import org.eclipse.jface.viewers.StructuredViewer; 18 import org.eclipse.swt.events.FocusAdapter; 19 import org.eclipse.swt.events.FocusEvent; 20 import org.eclipse.swt.events.FocusListener; 21 import org.eclipse.swt.events.MouseAdapter; 22 import org.eclipse.swt.events.MouseEvent; 23 import org.eclipse.swt.events.MouseListener; 24 import org.eclipse.swt.graphics.Rectangle; 25 import org.eclipse.swt.widgets.Control; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.swt.widgets.Item; 28 29 36 public abstract class TableEditorImpl { 37 private CellEditor fCellEditor; 38 private CellEditor[] fCellEditors; 39 private ICellModifier fCellModifier; 40 private String [] fColumnProperties; 41 private Item fTableItem; 42 private int fColumnNumber; 43 private ICellEditorListener fCellEditorListener; 44 private FocusListener fFocusListener; 45 private MouseListener fMouseListener; 46 private int fDoubleClickExpirationTime; 47 private StructuredViewer fViewer; 48 49 TableEditorImpl(StructuredViewer viewer) { 50 fViewer = viewer; 51 initCellEditorListener(); 52 } 53 54 59 public StructuredViewer getViewer() { 60 return fViewer; 61 } 62 63 private void activateCellEditor() { 64 if (fCellEditors != null) { 65 if (fCellEditors[fColumnNumber] != null && fCellModifier != null) { 66 Object element = fTableItem.getData(); 67 String property = fColumnProperties[fColumnNumber]; 68 if (fCellModifier.canModify(element, property)) { 69 fCellEditor = fCellEditors[fColumnNumber]; 70 fCellEditor.addListener(fCellEditorListener); 72 Object value = fCellModifier.getValue(element, property); 73 fCellEditor.setValue(value); 74 final Control control = fCellEditor.getControl(); 80 fCellEditor.activate(); 81 if (control == null) 82 return; 83 setLayoutData(fCellEditor.getLayoutData()); 84 setEditor(control, fTableItem, fColumnNumber); 85 fCellEditor.setFocus(); 86 if (fFocusListener == null) { 87 fFocusListener = new FocusAdapter() { 88 public void focusLost(FocusEvent e) { 89 applyEditorValue(); 90 } 91 }; 92 } 93 control.addFocusListener(fFocusListener); 94 fMouseListener = new MouseAdapter() { 95 public void mouseDown(MouseEvent e) { 96 if (e.time <= fDoubleClickExpirationTime) { 99 control.removeMouseListener(fMouseListener); 100 cancelEditing(); 101 handleDoubleClickEvent(); 102 } else if (fMouseListener != null) { 103 control.removeMouseListener(fMouseListener); 104 } 105 } 106 }; 107 control.addMouseListener(fMouseListener); 108 } 109 } 110 } 111 } 112 113 116 private void activateCellEditor(MouseEvent event) { 117 if (fTableItem == null || fTableItem.isDisposed()) { 118 return; 120 } 121 int columnToEdit; 122 int columns = getColumnCount(); 123 if (columns == 0) { 124 columnToEdit = 0; 127 } else { 128 columnToEdit = -1; 129 for (int i = 0; i < columns; i++) { 130 Rectangle bounds = getBounds(fTableItem, i); 131 if (bounds.contains(event.x, event.y)) { 132 columnToEdit = i; 133 break; 134 } 135 } 136 if (columnToEdit == -1) { 137 return; 138 } 139 } 140 141 fColumnNumber = columnToEdit; 142 activateCellEditor(); 143 } 144 145 148 public void applyEditorValue() { 149 CellEditor c = fCellEditor; 150 if (c != null) { 151 fCellEditor = null; 156 Item t = fTableItem; 157 if (t != null && !t.isDisposed()) { 159 saveEditorValue(c, t); 160 } 161 setEditor(null, null, 0); 162 c.removeListener(fCellEditorListener); 163 Control control = c.getControl(); 164 if (control != null) { 165 if (fMouseListener != null) { 166 control.removeMouseListener(fMouseListener); 167 } 168 if (fFocusListener != null) { 169 control.removeFocusListener(fFocusListener); 170 } 171 } 172 c.deactivate(); 173 } 174 } 175 176 180 public void cancelEditing() { 181 if (fCellEditor != null) { 182 setEditor(null, null, 0); 183 fCellEditor.removeListener(fCellEditorListener); 184 CellEditor oldEditor = fCellEditor; 185 fCellEditor = null; 186 oldEditor.deactivate(); 187 } 188 } 189 190 196 public void editElement(Object element, int column) { 197 if (fCellEditor != null) 198 applyEditorValue(); 199 200 setSelection(new StructuredSelection(element), true); 201 Item[] selection = getSelection(); 202 if (selection.length != 1) 203 return; 204 205 fTableItem = selection[0]; 206 207 showSelection(); 209 fColumnNumber = column; 210 activateCellEditor(); 211 212 } 213 214 abstract Rectangle getBounds(Item item, int columnNumber); 215 216 221 public CellEditor[] getCellEditors() { 222 return fCellEditors; 223 } 224 225 230 public ICellModifier getCellModifier() { 231 return fCellModifier; 232 } 233 234 abstract int getColumnCount(); 235 236 241 public Object [] getColumnProperties() { 242 return fColumnProperties; 243 } 244 245 abstract Item[] getSelection(); 246 247 253 public void handleMouseDown(MouseEvent event) { 254 if (event.button != 1) 255 return; 256 257 if (fCellEditor != null) 258 applyEditorValue(); 259 260 fDoubleClickExpirationTime = event.time + Display.getCurrent().getDoubleClickTime(); 266 267 Item[] items = getSelection(); 268 if (items.length != 1) { 270 fTableItem = null; 271 return; 272 } 273 fTableItem = items[0]; 274 activateCellEditor(event); 275 } 276 277 private void initCellEditorListener() { 278 fCellEditorListener = new ICellEditorListener() { 279 public void editorValueChanged(boolean oldValidState, boolean newValidState) { 280 } 282 283 public void cancelEditor() { 284 TableEditorImpl.this.cancelEditing(); 285 } 286 287 public void applyEditorValue() { 288 TableEditorImpl.this.applyEditorValue(); 289 } 290 }; 291 } 292 293 299 public boolean isCellEditorActive() { 300 return fCellEditor != null; 301 } 302 303 307 private void saveEditorValue(CellEditor cellEditor, Item tableItem) { 308 if (fCellModifier != null) { 309 if (!cellEditor.isValueValid()) { 310 } 312 String property = null; 313 if (fColumnProperties != null && fColumnNumber < fColumnProperties.length) 314 property = fColumnProperties[fColumnNumber]; 315 fCellModifier.modify(tableItem, property, cellEditor.getValue()); 316 } 317 } 318 319 324 public void setCellEditors(CellEditor[] editors) { 325 fCellEditors = editors; 326 } 327 328 333 public void setCellModifier(ICellModifier modifier) { 334 fCellModifier = modifier; 335 } 336 337 342 public void setColumnProperties(String [] columnProperties) { 343 fColumnProperties = columnProperties; 344 } 345 346 abstract void setEditor(Control w, Item item, int fColumnNumber); 347 348 abstract void setLayoutData(CellEditor.LayoutData layoutData); 349 350 abstract void setSelection(StructuredSelection selection, boolean b); 351 352 abstract void showSelection(); 353 354 abstract void handleDoubleClickEvent(); 355 } 356 | Popular Tags |