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 28 abstract class TableViewerImpl { 29 30 private CellEditor cellEditor; 31 private CellEditor[] cellEditors; 32 private ICellModifier cellModifier; 33 private String [] columnProperties; 34 private Item tableItem; 35 private int columnNumber; 36 private ICellEditorListener cellEditorListener; 37 private FocusListener focusListener; 38 private MouseListener mouseListener; 39 private int doubleClickExpirationTime; 40 private StructuredViewer viewer; 41 42 43 TableViewerImpl(StructuredViewer viewer) { 44 this.viewer = viewer; 45 initCellEditorListener(); 46 } 47 48 53 public StructuredViewer getViewer() { 54 return viewer; 55 } 56 57 private void activateCellEditor() { 58 if (cellEditors != null) { 59 if(cellEditors[columnNumber] != null && cellModifier != null) { 60 Object element = tableItem.getData(); 61 String property = columnProperties[columnNumber]; 62 if (cellModifier.canModify(element, property)) { 63 cellEditor = cellEditors[columnNumber]; 64 cellEditor.addListener(cellEditorListener); 66 Object value = cellModifier.getValue(element, property); 67 cellEditor.setValue(value); 68 final Control control = cellEditor.getControl(); 72 cellEditor.activate(); 73 if (control == null) 74 return; 75 setLayoutData(cellEditor.getLayoutData()); 76 setEditor(control, tableItem, columnNumber); 77 cellEditor.setFocus(); 78 if(focusListener == null) { 79 focusListener = new FocusAdapter() { 80 public void focusLost(FocusEvent e) { 81 applyEditorValue(); 82 } 83 }; 84 } 85 control.addFocusListener(focusListener); 86 mouseListener = new MouseAdapter() { 87 public void mouseDown(MouseEvent e) { 88 if (e.time <= doubleClickExpirationTime ) { 91 control.removeMouseListener(mouseListener); 92 cancelEditing(); 93 handleDoubleClickEvent(); 94 } else if (mouseListener != null) { 95 control.removeMouseListener(mouseListener); 96 } 97 } 98 }; 99 control.addMouseListener(mouseListener); 100 } 101 } 102 } 103 } 104 107 private void activateCellEditor(MouseEvent event) { 108 if (tableItem == null || tableItem.isDisposed()) { 109 return; 111 } 112 int columnToEdit; 113 int columns = getColumnCount(); 114 if (columns == 0) { 115 columnToEdit = 0; 118 } 119 else { 120 columnToEdit = -1; 121 for (int i = 0; i < columns; i++) { 122 Rectangle bounds = getBounds(tableItem, i); 123 if (bounds.contains(event.x, event.y)) { 124 columnToEdit = i; 125 break; 126 } 127 } 128 if (columnToEdit == -1) { 129 return; 130 } 131 } 132 133 columnNumber = columnToEdit; 134 activateCellEditor(); 135 } 136 139 public void applyEditorValue() { 140 CellEditor c = this.cellEditor; 141 if (c != null) { 142 this.cellEditor = null; 146 Item t = this.tableItem; 147 if (t != null && !t.isDisposed()) { 149 saveEditorValue(c, t); 150 } 151 setEditor(null, null, 0); 152 c.removeListener(cellEditorListener); 153 Control control = c.getControl(); 154 if (control != null) { 155 if (mouseListener != null) { 156 control.removeMouseListener(mouseListener); 157 } 158 if (focusListener != null) { 159 control.removeFocusListener(focusListener); 160 } 161 } 162 c.deactivate(); 163 } 164 } 165 169 public void cancelEditing() { 170 if (cellEditor != null) { 171 setEditor(null, null, 0); 172 cellEditor.removeListener(cellEditorListener); 173 CellEditor oldEditor = cellEditor; 174 cellEditor = null; 175 oldEditor.deactivate(); 176 } 177 } 178 181 public void editElement(Object element, int column) { 182 if (cellEditor != null) 183 applyEditorValue(); 184 185 setSelection(new StructuredSelection(element), true); 186 Item[] selection = getSelection(); 187 if (selection.length != 1) 188 return; 189 190 tableItem = selection[0]; 191 192 showSelection(); 194 columnNumber = column; 195 activateCellEditor(); 196 197 } 198 abstract Rectangle getBounds(Item item, int columnNumber); 199 public CellEditor[] getCellEditors() { 200 return cellEditors; 201 } 202 public ICellModifier getCellModifier() { 203 return cellModifier; 204 } 205 abstract int getColumnCount(); 206 public Object [] getColumnProperties() { 207 return columnProperties; 208 } 209 abstract Item[] getSelection(); 210 213 public void handleMouseDown(MouseEvent event) { 214 if (event.button != 1) 215 return; 216 217 if (cellEditor != null) 218 applyEditorValue(); 219 220 doubleClickExpirationTime = event.time + Display.getCurrent().getDoubleClickTime(); 226 227 Item[] items = getSelection(); 228 if (items.length != 1) { 230 tableItem = null; 231 return; 232 } 233 tableItem = items[0]; 234 activateCellEditor(event); 235 } 236 private void initCellEditorListener() { 237 cellEditorListener = new ICellEditorListener() { 238 public void editorValueChanged(boolean oldValidState, boolean newValidState) { 239 } 241 242 public void cancelEditor() { 243 TableViewerImpl.this.cancelEditing(); 244 } 245 246 public void applyEditorValue() { 247 TableViewerImpl.this.applyEditorValue(); 248 } 249 }; 250 } 251 255 public boolean isCellEditorActive() { 256 return cellEditor != null; 257 } 258 262 private void saveEditorValue(CellEditor cellEditor, Item tableItem) { 263 if (cellModifier != null) { 264 if (!cellEditor.isValueValid()) { 265 } 267 String property = null; 268 if (columnProperties != null && columnNumber < columnProperties.length) 269 property = columnProperties[columnNumber]; 270 cellModifier.modify(tableItem, property, cellEditor.getValue()); 271 } 272 } 273 public void setCellEditors(CellEditor[] editors) { 274 this.cellEditors = editors; 275 } 276 public void setCellModifier(ICellModifier modifier) { 277 this.cellModifier = modifier; 278 } 279 public void setColumnProperties(String [] columnProperties) { 280 this.columnProperties = columnProperties; 281 } 282 abstract void setEditor(Control w, Item item, int fColumnNumber); 283 abstract void setLayoutData(CellEditor.LayoutData layoutData); 284 abstract void setSelection(StructuredSelection selection, boolean b); 285 abstract void showSelection(); 286 abstract void handleDoubleClickEvent(); 287 } 288 | Popular Tags |