1 11 12 package org.eclipse.jface.viewers; 13 14 import org.eclipse.swt.events.FocusAdapter; 15 import org.eclipse.swt.events.FocusEvent; 16 import org.eclipse.swt.events.FocusListener; 17 import org.eclipse.swt.events.MouseAdapter; 18 import org.eclipse.swt.events.MouseEvent; 19 import org.eclipse.swt.events.MouseListener; 20 import org.eclipse.swt.graphics.Rectangle; 21 import org.eclipse.swt.widgets.Control; 22 import org.eclipse.swt.widgets.Display; 23 import org.eclipse.swt.widgets.Item; 24 25 30 abstract class TableEditorImpl { 31 32 private CellEditor cellEditor; 33 34 private CellEditor[] cellEditors; 35 36 private ICellModifier cellModifier; 37 38 private String [] columnProperties; 39 40 private Item tableItem; 41 42 private int columnNumber; 43 44 private ICellEditorListener cellEditorListener; 45 46 private FocusListener focusListener; 47 48 private MouseListener mouseListener; 49 50 private int doubleClickExpirationTime; 51 52 private StructuredViewer viewer; 53 54 TableEditorImpl(StructuredViewer viewer) { 55 this.viewer = viewer; 56 initCellEditorListener(); 57 } 58 59 64 public StructuredViewer getViewer() { 65 return viewer; 66 } 67 68 private void activateCellEditor() { 69 if (cellEditors != null) { 70 if (cellEditors[columnNumber] != null && cellModifier != null) { 71 Object element = tableItem.getData(); 72 String property = columnProperties[columnNumber]; 73 if (cellModifier.canModify(element, property)) { 74 cellEditor = cellEditors[columnNumber]; 75 cellEditor.addListener(cellEditorListener); 77 Object value = cellModifier.getValue(element, property); 78 cellEditor.setValue(value); 79 final Control control = cellEditor.getControl(); 83 cellEditor.activate(); 84 if (control == null) { 85 return; 86 } 87 setLayoutData(cellEditor.getLayoutData()); 88 setEditor(control, tableItem, columnNumber); 89 cellEditor.setFocus(); 90 if (focusListener == null) { 91 focusListener = new FocusAdapter() { 92 public void focusLost(FocusEvent e) { 93 applyEditorValue(); 94 } 95 }; 96 } 97 control.addFocusListener(focusListener); 98 mouseListener = new MouseAdapter() { 99 public void mouseDown(MouseEvent e) { 100 if (e.time <= doubleClickExpirationTime) { 103 control.removeMouseListener(mouseListener); 104 cancelEditing(); 105 handleDoubleClickEvent(); 106 } else if (mouseListener != null) { 107 control.removeMouseListener(mouseListener); 108 } 109 } 110 }; 111 control.addMouseListener(mouseListener); 112 } 113 } 114 } 115 } 116 117 120 private void activateCellEditor(MouseEvent event) { 121 if (tableItem == null || tableItem.isDisposed()) { 122 return; 124 } 125 int columnToEdit; 126 int columns = getColumnCount(); 127 if (columns == 0) { 128 columnToEdit = 0; 131 } else { 132 columnToEdit = -1; 133 for (int i = 0; i < columns; i++) { 134 Rectangle bounds = getBounds(tableItem, i); 135 if (bounds.contains(event.x, event.y)) { 136 columnToEdit = i; 137 break; 138 } 139 } 140 if (columnToEdit == -1) { 141 return; 142 } 143 } 144 145 columnNumber = columnToEdit; 146 activateCellEditor(); 147 } 148 149 152 public void applyEditorValue() { 153 CellEditor c = this.cellEditor; 154 if (c != null) { 155 this.cellEditor = null; 159 Item t = this.tableItem; 160 if (t != null && !t.isDisposed()) { 162 saveEditorValue(c, t); 163 } 164 setEditor(null, null, 0); 165 c.removeListener(cellEditorListener); 166 Control control = c.getControl(); 167 if (control != null) { 168 if (mouseListener != null) { 169 control.removeMouseListener(mouseListener); 170 } 171 if (focusListener != null) { 172 control.removeFocusListener(focusListener); 173 } 174 } 175 c.deactivate(); 176 } 177 } 178 179 183 public void cancelEditing() { 184 if (cellEditor != null) { 185 setEditor(null, null, 0); 186 cellEditor.removeListener(cellEditorListener); 187 CellEditor oldEditor = cellEditor; 188 cellEditor = null; 189 oldEditor.deactivate(); 190 } 191 } 192 193 198 public void editElement(Object element, int column) { 199 if (cellEditor != null) { 200 applyEditorValue(); 201 } 202 203 setSelection(new StructuredSelection(element), true); 204 Item[] selection = getSelection(); 205 if (selection.length != 1) { 206 return; 207 } 208 209 tableItem = selection[0]; 210 211 showSelection(); 213 columnNumber = column; 214 activateCellEditor(); 215 216 } 217 218 abstract Rectangle getBounds(Item item, int columnNumber); 219 220 224 public CellEditor[] getCellEditors() { 225 return cellEditors; 226 } 227 228 232 public ICellModifier getCellModifier() { 233 return cellModifier; 234 } 235 236 abstract int getColumnCount(); 237 238 242 public Object [] getColumnProperties() { 243 return columnProperties; 244 } 245 246 abstract Item[] getSelection(); 247 248 252 public void handleMouseDown(MouseEvent event) { 253 if (event.button != 1) { 254 return; 255 } 256 257 if (cellEditor != null) { 258 applyEditorValue(); 259 } 260 261 doubleClickExpirationTime = event.time 267 + Display.getCurrent().getDoubleClickTime(); 268 269 Item[] items = getSelection(); 270 if (items.length != 1) { 272 tableItem = null; 273 return; 274 } 275 tableItem = items[0]; 276 activateCellEditor(event); 277 } 278 279 private void initCellEditorListener() { 280 cellEditorListener = new ICellEditorListener() { 281 public void editorValueChanged(boolean oldValidState, 282 boolean newValidState) { 283 } 285 286 public void cancelEditor() { 287 TableEditorImpl.this.cancelEditing(); 288 } 289 290 public void applyEditorValue() { 291 TableEditorImpl.this.applyEditorValue(); 292 } 293 }; 294 } 295 296 302 public boolean isCellEditorActive() { 303 return cellEditor != null; 304 } 305 306 310 private void saveEditorValue(CellEditor cellEditor, Item tableItem) { 311 if (cellModifier != null) { 312 if (!cellEditor.isValueValid()) { 313 } 315 String property = null; 316 if (columnProperties != null 317 && columnNumber < columnProperties.length) { 318 property = columnProperties[columnNumber]; 319 } 320 cellModifier.modify(tableItem, property, cellEditor.getValue()); 321 } 322 } 323 324 328 public void setCellEditors(CellEditor[] editors) { 329 this.cellEditors = editors; 330 } 331 332 336 public void setCellModifier(ICellModifier modifier) { 337 this.cellModifier = modifier; 338 } 339 340 344 public void setColumnProperties(String [] columnProperties) { 345 this.columnProperties = columnProperties; 346 } 347 348 abstract void setEditor(Control w, Item item, int fColumnNumber); 349 350 abstract void setLayoutData(CellEditor.LayoutData layoutData); 351 352 abstract void setSelection(StructuredSelection selection, boolean b); 353 354 abstract void showSelection(); 355 356 abstract void handleDoubleClickEvent(); 357 } 358 | Popular Tags |