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