1 12 13 package org.eclipse.jface.viewers; 14 15 import java.util.List ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.custom.TableTree; 19 import org.eclipse.swt.custom.TableTreeEditor; 20 import org.eclipse.swt.custom.TableTreeItem; 21 import org.eclipse.swt.events.FocusAdapter; 22 import org.eclipse.swt.events.FocusEvent; 23 import org.eclipse.swt.events.FocusListener; 24 import org.eclipse.swt.events.MouseAdapter; 25 import org.eclipse.swt.events.MouseEvent; 26 import org.eclipse.swt.events.MouseListener; 27 import org.eclipse.swt.events.TreeListener; 28 import org.eclipse.swt.graphics.Image; 29 import org.eclipse.swt.graphics.Point; 30 import org.eclipse.swt.graphics.Rectangle; 31 import org.eclipse.swt.widgets.Composite; 32 import org.eclipse.swt.widgets.Control; 33 import org.eclipse.swt.widgets.Display; 34 import org.eclipse.swt.widgets.Item; 35 import org.eclipse.swt.widgets.Widget; 36 37 58 public class TableTreeViewer extends AbstractTreeViewer { 59 62 private TableTreeEditorImpl tableEditorImpl; 63 64 67 private TableTree tableTree; 68 69 72 private TableTreeEditor tableTreeEditor; 73 74 80 class TableTreeEditorImpl { 81 82 private CellEditor cellEditor; 83 84 private CellEditor[] cellEditors; 85 86 private ICellModifier cellModifier; 87 88 private String [] columnProperties; 89 90 private Item tableItem; 91 92 private int columnNumber; 93 94 private ICellEditorListener cellEditorListener; 95 96 private FocusListener focusListener; 97 98 private MouseListener mouseListener; 99 100 private int doubleClickExpirationTime; 101 102 private ColumnViewer viewer; 103 104 private TableTreeEditorImpl(ColumnViewer viewer) { 105 this.viewer = viewer; 106 initCellEditorListener(); 107 } 108 109 114 public ColumnViewer getViewer() { 115 return viewer; 116 } 117 118 private void activateCellEditor() { 119 if( cellEditors != null ) { 120 if( cellEditors[columnNumber] != null && cellModifier != null ) { 121 Object element = tableItem.getData(); 122 String property = columnProperties[columnNumber]; 123 124 if( cellModifier.canModify(element, property) ) { 125 cellEditor = cellEditors[columnNumber]; 126 127 cellEditor.addListener(cellEditorListener); 128 129 Object value = cellModifier.getValue(element, property); 130 cellEditor.setValue(value); 131 final Control control = cellEditor.getControl(); 137 cellEditor.activate(); 138 if (control == null) { 139 return; 140 } 141 setLayoutData(cellEditor.getLayoutData()); 142 setEditor(control, tableItem, columnNumber); 143 cellEditor.setFocus(); 144 if (focusListener == null) { 145 focusListener = new FocusAdapter() { 146 public void focusLost(FocusEvent e) { 147 applyEditorValue(); 148 } 149 }; 150 } 151 control.addFocusListener(focusListener); 152 mouseListener = new MouseAdapter() { 153 public void mouseDown(MouseEvent e) { 154 if (e.time <= doubleClickExpirationTime) { 157 control.removeMouseListener(mouseListener); 158 cancelEditing(); 159 handleDoubleClickEvent(); 160 } else if (mouseListener != null) { 161 control.removeMouseListener(mouseListener); 162 } 163 } 164 }; 165 control.addMouseListener(mouseListener); 166 } 167 } 168 } 169 } 170 171 174 private void activateCellEditor(MouseEvent event) { 175 if (tableItem == null || tableItem.isDisposed()) { 176 return; 178 } 179 int columnToEdit; 180 int columns = getColumnCount(); 181 if (columns == 0) { 182 columnToEdit = 0; 185 } else { 186 columnToEdit = -1; 187 for (int i = 0; i < columns; i++) { 188 Rectangle bounds = getBounds(tableItem, i); 189 if (bounds.contains(event.x, event.y)) { 190 columnToEdit = i; 191 break; 192 } 193 } 194 if (columnToEdit == -1) { 195 return; 196 } 197 } 198 199 columnNumber = columnToEdit; 200 activateCellEditor(); 201 } 202 203 206 public void applyEditorValue() { 207 CellEditor c = this.cellEditor; 208 if (c != null) { 209 this.cellEditor = null; 214 Item t = this.tableItem; 215 if (t != null && !t.isDisposed()) { 217 saveEditorValue(c, t); 218 } 219 setEditor(null, null, 0); 220 c.removeListener(cellEditorListener); 221 Control control = c.getControl(); 222 if (control != null) { 223 if (mouseListener != null) { 224 control.removeMouseListener(mouseListener); 225 } 226 if (focusListener != null) { 227 control.removeFocusListener(focusListener); 228 } 229 } 230 c.deactivate(); 231 } 232 } 233 234 238 public void cancelEditing() { 239 if (cellEditor != null) { 240 setEditor(null, null, 0); 241 cellEditor.removeListener(cellEditorListener); 242 CellEditor oldEditor = cellEditor; 243 cellEditor = null; 244 oldEditor.deactivate(); 245 } 246 } 247 248 254 public void editElement(Object element, int column) { 255 if (cellEditor != null) { 256 applyEditorValue(); 257 } 258 259 setSelection(new StructuredSelection(element), true); 260 Item[] selection = getSelection(); 261 if (selection.length != 1) { 262 return; 263 } 264 265 tableItem = selection[0]; 266 267 showSelection(); 269 columnNumber = column; 270 activateCellEditor(); 271 272 } 273 274 279 public CellEditor[] getCellEditors() { 280 return cellEditors; 281 } 282 283 288 public ICellModifier getCellModifier() { 289 return cellModifier; 290 } 291 292 297 public Object [] getColumnProperties() { 298 return columnProperties; 299 } 300 301 307 public void handleMouseDown(MouseEvent event) { 308 if (event.button != 1) { 309 return; 310 } 311 312 if (cellEditor != null) { 313 applyEditorValue(); 314 } 315 316 doubleClickExpirationTime = event.time 322 + Display.getCurrent().getDoubleClickTime(); 323 324 Item[] items = getSelection(); 325 if (items.length != 1) { 327 tableItem = null; 328 return; 329 } 330 tableItem = items[0]; 331 332 activateCellEditor(event); 333 } 334 335 private void initCellEditorListener() { 336 cellEditorListener = new ICellEditorListener() { 337 public void editorValueChanged(boolean oldValidState, 338 boolean newValidState) { 339 } 341 342 public void cancelEditor() { 343 TableTreeEditorImpl.this.cancelEditing(); 344 } 345 346 public void applyEditorValue() { 347 TableTreeEditorImpl.this.applyEditorValue(); 348 } 349 }; 350 } 351 352 358 public boolean isCellEditorActive() { 359 return cellEditor != null; 360 } 361 362 366 private void saveEditorValue(CellEditor cellEditor, Item tableItem) { 367 if( cellModifier != null ) { 368 if( ! cellEditor.isValueValid() ) { 369 } 371 } 372 String property = null; 373 374 if( columnProperties != null && columnNumber < columnProperties.length ) { 375 property = columnProperties[columnNumber]; 376 } 377 cellModifier.modify(tableItem, property, cellEditor.getValue()); 378 } 379 380 385 public void setCellEditors(CellEditor[] editors) { 386 this.cellEditors = editors; 387 } 388 389 394 public void setCellModifier(ICellModifier modifier) { 395 this.cellModifier = modifier; 396 } 397 398 403 public void setColumnProperties(String [] columnProperties) { 404 this.columnProperties = columnProperties; 405 } 406 407 Rectangle getBounds(Item item, int columnNumber) { 408 return ((TableTreeItem) item).getBounds(columnNumber); 409 } 410 411 int getColumnCount() { 412 return getTableTree().getTable().getColumnCount(); 414 } 415 416 Item[] getSelection() { 417 return getTableTree().getSelection(); 418 } 419 420 void setEditor(Control w, Item item, int columnNumber) { 421 tableTreeEditor.setEditor(w, (TableTreeItem) item, columnNumber); 422 } 423 424 void setSelection(StructuredSelection selection, boolean b) { 425 TableTreeViewer.this.setSelection(selection, b); 426 } 427 428 void showSelection() { 429 getTableTree().showSelection(); 430 } 431 432 void setLayoutData(CellEditor.LayoutData layoutData) { 433 tableTreeEditor.horizontalAlignment = layoutData.horizontalAlignment; 434 tableTreeEditor.grabHorizontal = layoutData.grabHorizontal; 435 tableTreeEditor.minimumWidth = layoutData.minimumWidth; 436 } 437 438 void handleDoubleClickEvent() { 439 Viewer viewer = getViewer(); 440 fireDoubleClick(new DoubleClickEvent(viewer, viewer.getSelection())); 441 fireOpen(new OpenEvent(viewer, viewer.getSelection())); 442 } 443 } 444 445 453 public TableTreeViewer(TableTree tree) { 454 super(); 455 tableTree = tree; 456 hookControl(tree); 457 tableTreeEditor = new TableTreeEditor(tableTree); 458 tableEditorImpl = new TableTreeEditorImpl(this); 459 } 460 461 471 public TableTreeViewer(Composite parent) { 472 this(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER); 473 } 474 475 486 public TableTreeViewer(Composite parent, int style) { 487 this(new TableTree(parent, style)); 488 } 489 490 493 protected void addTreeListener(Control c, TreeListener listener) { 494 ((TableTree) c).addTreeListener(listener); 495 } 496 497 501 public void cancelEditing() { 502 tableEditorImpl.cancelEditing(); 503 } 504 505 508 protected void doUpdateItem(Item item, Object element) { 509 IBaseLabelProvider prov = getLabelProvider(); 512 ITableLabelProvider tprov = null; 513 514 if (prov instanceof ITableLabelProvider) { 515 tprov = (ITableLabelProvider) prov; 516 } 517 518 int columnCount = tableTree.getTable().getColumnCount(); 519 TableTreeItem ti = (TableTreeItem) item; 520 for (int column = 0; column < columnCount || column == 0; column++) { 523 String text = ""; Image image = null; 525 if (tprov != null) { 526 text = tprov.getColumnText(element, column); 527 image = tprov.getColumnImage(element, column); 528 } else { 529 if (column == 0) { 530 ViewerLabel updateLabel = new ViewerLabel(item.getText(), 531 item.getImage()); 532 buildLabel(updateLabel, element); 533 534 if (item.isDisposed()) { 537 unmapElement(element, item); 538 return; 539 } 540 541 text = updateLabel.getText(); 542 image = updateLabel.getImage(); 543 } 544 } 545 546 if (text == null) { 548 text = ""; } 550 551 ti.setText(column, text); 552 if (ti.getImage(column) != image) { 554 ti.setImage(column, image); 555 } 556 557 getColorAndFontCollector().setFontsAndColors(element); 558 getColorAndFontCollector().applyFontsAndColors(ti); 559 } 560 561 } 562 563 571 public void editElement(Object element, int column) { 572 tableEditorImpl.editElement(element, column); 573 } 574 575 580 public CellEditor[] getCellEditors() { 581 return tableEditorImpl.getCellEditors(); 582 } 583 584 589 public ICellModifier getCellModifier() { 590 return tableEditorImpl.getCellModifier(); 591 } 592 593 596 protected Item[] getChildren(Widget o) { 597 if (o instanceof TableTreeItem) { 598 return ((TableTreeItem) o).getItems(); 599 } 600 if (o instanceof TableTree) { 601 return ((TableTree) o).getItems(); 602 } 603 return null; 604 } 605 606 612 protected Item getChild(Widget widget, int index) { 613 if (widget instanceof TableTreeItem) { 614 return ((TableTreeItem) widget).getItem(index); 615 } 616 if (widget instanceof TableTree) { 617 return ((TableTree) widget).getItem(index); 618 } 619 return null; 620 } 621 622 629 public Object [] getColumnProperties() { 630 return tableEditorImpl.getColumnProperties(); 631 } 632 633 636 public Control getControl() { 637 return tableTree; 638 } 639 640 652 public Object getElementAt(int index) { 653 TableTreeItem i = tableTree.getItems()[index]; 656 if (i != null) { 657 return i.getData(); 658 } 659 return null; 660 } 661 662 665 protected boolean getExpanded(Item item) { 666 return ((TableTreeItem) item).getExpanded(); 667 } 668 669 674 protected Item getItemAt(Point p) { 675 return getTableTree().getTable().getItem(p); 676 } 677 678 681 protected int getItemCount(Control widget) { 682 return ((TableTree) widget).getItemCount(); 683 } 684 685 688 protected int getItemCount(Item item) { 689 return ((TableTreeItem) item).getItemCount(); 690 } 691 692 695 protected org.eclipse.swt.widgets.Item[] getItems( 696 org.eclipse.swt.widgets.Item item) { 697 return ((TableTreeItem) item).getItems(); 698 } 699 700 710 public IBaseLabelProvider getLabelProvider() { 711 return super.getLabelProvider(); 712 } 713 714 717 protected Item getParentItem(Item item) { 718 return ((TableTreeItem) item).getParentItem(); 719 } 720 721 724 protected Item[] getSelection(Control widget) { 725 return ((TableTree) widget).getSelection(); 726 } 727 728 733 public TableTree getTableTree() { 734 return tableTree; 735 } 736 737 740 protected void hookControl(Control control) { 741 super.hookControl(control); 742 tableTree.getTable().addMouseListener(new MouseAdapter() { 743 public void mouseDown(MouseEvent e) { 744 748 org.eclipse.swt.widgets.TableItem[] items = tableTree 753 .getTable().getItems(); 754 for (int i = 0; i < items.length; i++) { 755 Rectangle rect = items[i].getImageBounds(0); 756 if (rect.contains(e.x, e.y)) { 757 return; 758 } 759 } 760 761 tableEditorImpl.handleMouseDown(e); 762 } 763 }); 764 } 765 766 772 public boolean isCellEditorActive() { 773 return tableEditorImpl.isCellEditorActive(); 774 } 775 776 779 protected Item newItem(Widget parent, int flags, int ix) { 780 TableTreeItem item; 781 if (ix >= 0) { 782 if (parent instanceof TableTreeItem) { 783 item = new TableTreeItem((TableTreeItem) parent, flags, ix); 784 } else { 785 item = new TableTreeItem((TableTree) parent, flags, ix); 786 } 787 } else { 788 if (parent instanceof TableTreeItem) { 789 item = new TableTreeItem((TableTreeItem) parent, flags); 790 } else { 791 item = new TableTreeItem((TableTree) parent, flags); 792 } 793 } 794 return item; 795 } 796 797 800 protected void removeAll(Control widget) { 801 ((TableTree) widget).removeAll(); 802 } 803 804 810 public void setCellEditors(CellEditor[] editors) { 811 tableEditorImpl.setCellEditors(editors); 812 } 813 814 820 public void setCellModifier(ICellModifier modifier) { 821 tableEditorImpl.setCellModifier(modifier); 822 } 823 824 832 public void setColumnProperties(String [] columnProperties) { 833 tableEditorImpl.setColumnProperties(columnProperties); 834 } 835 836 839 protected void setExpanded(Item node, boolean expand) { 840 ((TableTreeItem) node).setExpanded(expand); 841 } 842 843 846 protected void setSelection(List items) { 847 TableTreeItem[] newItems = new TableTreeItem[items.size()]; 848 items.toArray(newItems); 849 getTableTree().setSelection(newItems); 850 } 851 852 855 protected void showItem(Item item) { 856 getTableTree().showItem((TableTreeItem) item); 857 } 858 } 859 | Popular Tags |