1 17 package org.eclipse.emf.common.ui.celleditor; 18 19 20 import org.eclipse.swt.SWT; 21 import org.eclipse.swt.custom.TableTree; 22 import org.eclipse.swt.custom.TableTreeEditor; 23 import org.eclipse.swt.custom.TableTreeItem; 24 import org.eclipse.swt.events.KeyEvent; 25 import org.eclipse.swt.events.KeyListener; 26 import org.eclipse.swt.events.MouseEvent; 27 import org.eclipse.swt.events.MouseListener; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.events.SelectionListener; 30 import org.eclipse.swt.graphics.Point; 31 import org.eclipse.swt.graphics.Rectangle; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Event; 34 import org.eclipse.swt.widgets.Table; 35 import org.eclipse.swt.widgets.TableItem; 36 37 import org.eclipse.emf.common.ui.viewer.ExtendedTableTreeViewer; 38 39 40 45 public abstract class ExtendedTableTreeEditor 46 extends TableTreeEditor 47 implements KeyListener, MouseListener, SelectionListener 48 { 49 protected Table table; 50 protected TableItem selectedTableItem; 51 protected TableTree tableTree; 52 protected TableTreeItem editTableTreeItem; 53 protected int editTableTreeItemColumn; 54 boolean isActivating; 55 boolean isDeactivating; 56 boolean isActive; 57 58 public ExtendedTableTreeEditor(TableTree tableTree) 59 { 60 super(tableTree); 61 62 this.tableTree = tableTree; 63 this.table = tableTree.getTable(); 64 65 table.addKeyListener(this); 66 table.addMouseListener(this); 67 table.addSelectionListener(this); 68 } 69 70 public void mouseDoubleClick(MouseEvent event) 71 { 72 editTableTreeItem = null; 74 } 75 76 public void mouseDown(MouseEvent event) 77 { 78 editTableTreeItem = null; 79 editTableTreeItemColumn = -1; 80 81 if (event.button == 1) 83 { 84 boolean wasActive = getEditor() != null && !getEditor().isDisposed(); 85 86 Point point = new Point(event.x, event.y); 87 TableItem [] tableItems = tableTree.getTable().getItems(); 88 int columnCount = tableTree.getTable().getColumnCount(); 89 LOOP: for (int i = tableTree.getTable().getTopIndex(); i < tableItems.length; ++i) 90 { 91 for (int j = 0; j < columnCount; ++j) 92 { 93 Rectangle bounds = tableItems[i].getBounds(j); 94 if (bounds.y > event.y) 95 { 96 break LOOP; 97 } 98 else if (bounds.contains(event.x, event.y)) 99 { 100 if (j != 0 || !tableItems[i].getImageBounds(0).contains(event.x, event.y)) 101 { 102 TableItem tableItem = tableItems[i]; 103 if (tableItem == selectedTableItem || wasActive) 104 { 105 if (tableItem != selectedTableItem) 106 { 107 table.setSelection(i); 108 Event selectionEvent = new Event(); 109 selectionEvent.widget = table; 110 selectionEvent.item = tableItem; 111 table.notifyListeners(SWT.Selection, selectionEvent); 112 selectedTableItem = tableItem; 113 } 114 editTableTreeItem = (TableTreeItem)tableItems[i].getData(ExtendedTableTreeViewer.ITEM_ID); 115 editTableTreeItemColumn = j; 116 } 117 else 118 { 119 TableItem mouseBasedTableItem = table.getItem(point); 120 if (mouseBasedTableItem == null) 121 { 122 table.setSelection(i); 123 Event selectionEvent = new Event(); 124 selectionEvent.widget = table; 125 selectionEvent.item = tableItem; 126 table.notifyListeners(SWT.Selection, selectionEvent); 127 selectedTableItem = tableItem; 128 } 129 } 130 } 131 132 break LOOP; 133 } 134 } 135 } 136 137 if (editTableTreeItem == null && wasActive) 138 { 139 dismiss(); 140 } 141 } 142 } 143 144 public void dismiss() 145 { 146 setEditor(null, null, -1); 147 } 148 149 public void mouseUp(MouseEvent event) 150 { 151 if (event.button == 1) 153 { 154 TableItem [] tableItems = tableTree.getTable().getItems(); 155 int columnCount = tableTree.getTable().getColumnCount(); 156 LOOP: for (int i = tableTree.getTable().getTopIndex(); i < tableItems.length; ++i) 157 { 158 for (int j = 0; j < columnCount; ++j) 159 { 160 Rectangle bounds = tableItems[i].getBounds(j); 161 if (bounds.y > event.y) 162 { 163 break LOOP; 164 } 165 else if (bounds.contains(event.x, event.y)) 166 { 167 if (j != 0 || !tableItems[i].getImageBounds(0).contains(event.x, event.y)) 168 { 169 TableItem tableItem = tableItems[i]; 170 TableTreeItem tableTreeItem = (TableTreeItem)tableItems[i].getData(ExtendedTableTreeViewer.ITEM_ID); 171 if (tableTreeItem == editTableTreeItem) 172 { 173 selectedTableItem = null; 174 table.showSelection(); 175 editItem(tableItem, editTableTreeItem, editTableTreeItemColumn); 176 } 177 } 178 179 break LOOP; 180 } 181 } 182 } 183 } 184 } 185 186 public void widgetDefaultSelected(SelectionEvent event) 187 { 188 widgetSelected(event); 189 } 190 191 public void widgetSelected(SelectionEvent event) 192 { 193 TableItem [] selection = table.getSelection(); 194 selectedTableItem = selection.length == 1 ? selection[0] : null; 195 } 196 197 public void keyPressed(KeyEvent event) 198 { 199 } 200 201 public void keyReleased(KeyEvent event) 202 { 203 TableItem [] selection = table.getSelection(); 204 selectedTableItem = selection.length == 1 ? selection[0] : null; 205 if (event.character == ' ' && selectedTableItem != null) 206 { 207 editItem(selectedTableItem, (TableTreeItem)selectedTableItem.getData(ExtendedTableTreeViewer.ITEM_ID), 0); 208 selectedTableItem = null; 209 } 210 } 211 212 protected abstract void editItem(TableItem tableItem, TableTreeItem tableTreeItem, int column); 213 214 public void setEditor(Control canvas, TableTreeItem tableTreeItem, int column) 215 { 216 super.setEditor(canvas, tableTreeItem, column); 217 } 218 } 219 | Popular Tags |