1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.events.*; 16 import org.eclipse.swt.graphics.*; 17 import org.eclipse.swt.widgets.*; 18 19 42 public class TableTree extends Composite { 43 Table table; 44 TableTreeItem[] items = EMPTY_ITEMS; 45 Image plusImage, minusImage, sizeImage; 46 47 59 boolean inDispose = false; 60 61 static final TableTreeItem[] EMPTY_ITEMS = new TableTreeItem [0]; 62 static final String [] EMPTY_TEXTS = new String [0]; 63 static final Image[] EMPTY_IMAGES = new Image [0]; 64 static final String ITEMID = "TableTreeItemID"; 66 95 public TableTree(Composite parent, int style) { 96 super(parent, checkStyle (style)); 97 table = new Table(this, style); 98 Listener tableListener = new Listener() { 99 public void handleEvent(Event e) { 100 switch (e.type) { 101 case SWT.MouseDown: onMouseDown(e); break; 102 case SWT.Selection: onSelection(e); break; 103 case SWT.DefaultSelection: onSelection(e); break; 104 case SWT.KeyDown: onKeyDown(e); break; 105 } 106 } 107 }; 108 int[] tableEvents = new int[]{SWT.MouseDown, 109 SWT.Selection, 110 SWT.DefaultSelection, 111 SWT.KeyDown}; 112 for (int i = 0; i < tableEvents.length; i++) { 113 table.addListener(tableEvents[i], tableListener); 114 } 115 116 Listener listener = new Listener() { 117 public void handleEvent(Event e) { 118 switch (e.type) { 119 case SWT.Dispose: onDispose(e); break; 120 case SWT.Resize: onResize(e); break; 121 case SWT.FocusIn: onFocusIn(e); break; 122 } 123 } 124 }; 125 int[] events = new int[]{SWT.Dispose, 126 SWT.Resize, 127 SWT.FocusIn}; 128 for (int i = 0; i < events.length; i++) { 129 addListener(events[i], listener); 130 } 131 } 132 133 int addItem(TableTreeItem item, int index) { 134 if (index < 0 || index > items.length) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 135 TableTreeItem[] newItems = new TableTreeItem[items.length + 1]; 136 System.arraycopy(items, 0, newItems, 0, index); 137 newItems[index] = item; 138 System.arraycopy(items, index, newItems, index + 1, items.length - index); 139 items = newItems; 140 141 142 if (index == items.length - 1 ) 143 return table.getItemCount(); 144 else 145 return table.indexOf(items[index+1].tableItem); 146 } 147 148 175 public void addSelectionListener(SelectionListener listener) { 176 checkWidget(); 177 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 178 TypedListener typedListener = new TypedListener (listener); 179 addListener (SWT.Selection,typedListener); 180 addListener (SWT.DefaultSelection,typedListener); 181 } 182 183 202 public void addTreeListener(TreeListener listener) { 203 checkWidget(); 204 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 205 TypedListener typedListener = new TypedListener (listener); 206 addListener (SWT.Expand, typedListener); 207 addListener (SWT.Collapse, typedListener); 208 } 209 private static int checkStyle (int style) { 210 int mask = SWT.LEFT_TO_RIGHT | SWT.RIGHT_TO_LEFT; 211 style = style & mask; 212 return style; 213 } 214 public Point computeSize (int wHint, int hHint, boolean changed) { 215 checkWidget(); 216 return table.computeSize (wHint, hHint, changed); 217 } 218 public Rectangle computeTrim (int x, int y, int width, int height) { 219 checkWidget(); 220 return table.computeTrim(x, y, width, height); 221 } 222 223 234 public void deselectAll () { 235 checkWidget(); 236 table.deselectAll(); 237 } 238 239 240 void expandItem (TableTreeItem item) { 241 if (item == null) return; 242 expandItem(item.parentItem); 243 if (!item.getVisible()) item.setVisible(true); 244 if ( !item.expanded && item.items.length > 0) { 245 item.setExpanded(true); 246 Event event = new Event(); 247 event.item = item; 248 notifyListeners(SWT.Expand, event); 249 } 250 } 251 public Color getBackground () { 252 return table.getBackground(); 259 } 260 public Rectangle getClientArea () { 261 return table.getClientArea(); 262 } 263 public Color getForeground () { 264 return table.getForeground(); 265 } 266 public Font getFont () { 267 return table.getFont(); 268 } 269 274 public int getItemCount () { 275 return items.length; 277 } 278 279 292 public int getItemHeight () { 293 checkWidget(); 294 return table.getItemHeight(); 295 } 296 297 302 public TableTreeItem [] getItems () { 303 TableTreeItem[] newItems = new TableTreeItem[items.length]; 305 System.arraycopy(items, 0, newItems, 0, items.length); 306 return newItems; 307 } 308 309 322 public TableTreeItem [] getSelection () { 323 checkWidget(); 324 TableItem[] selection = table.getSelection(); 325 TableTreeItem [] result = new TableTreeItem[selection.length]; 326 for (int i = 0; i < selection.length; i++){ 327 result[i] = (TableTreeItem) selection[i].getData(ITEMID); 328 } 329 return result; 330 } 331 332 345 public int getSelectionCount () { 346 checkWidget(); 347 return table.getSelectionCount(); 348 } 349 350 public int getStyle () { 351 checkWidget(); 352 return table.getStyle(); 353 } 354 355 360 public Table getTable () { 361 return table; 363 } 364 365 void createImages () { 366 367 int itemHeight = sizeImage.getBounds().height; 368 int indent = Math.min(6, (itemHeight - 9) / 2); 372 indent = Math.max(0, indent); 373 int size = Math.max (10, itemHeight - 2 * indent); 374 size = ((size + 1) / 2) * 2; int midpoint = indent + size / 2; 376 377 Color foreground = getForeground(); 378 Color plusMinus = getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW); 379 Color background = getBackground(); 380 381 382 PaletteData palette = new PaletteData(new RGB[]{foreground.getRGB(), background.getRGB(), plusMinus.getRGB()}); 383 ImageData imageData = new ImageData(itemHeight, itemHeight, 4, palette); 384 imageData.transparentPixel = 1; 385 plusImage = new Image(getDisplay(), imageData); 386 GC gc = new GC(plusImage); 387 gc.setBackground(background); 388 gc.fillRectangle(0, 0, itemHeight, itemHeight); 389 gc.setForeground(plusMinus); 390 gc.drawRectangle(indent, indent, size, size); 391 gc.setForeground(foreground); 392 gc.drawLine(midpoint, indent + 2, midpoint, indent + size - 2); 393 gc.drawLine(indent + 2, midpoint, indent + size - 2, midpoint); 394 gc.dispose(); 395 396 397 palette = new PaletteData(new RGB[]{foreground.getRGB(), background.getRGB(), plusMinus.getRGB()}); 398 imageData = new ImageData(itemHeight, itemHeight, 4, palette); 399 imageData.transparentPixel = 1; 400 minusImage = new Image(getDisplay(), imageData); 401 gc = new GC(minusImage); 402 gc.setBackground(background); 403 gc.fillRectangle(0, 0, itemHeight, itemHeight); 404 gc.setForeground(plusMinus); 405 gc.drawRectangle(indent, indent, size, size); 406 gc.setForeground(foreground); 407 gc.drawLine(indent + 2, midpoint, indent + size - 2, midpoint); 408 gc.dispose(); 409 } 410 411 Image getPlusImage() { 412 if (plusImage == null) createImages(); 413 return plusImage; 414 } 415 416 Image getMinusImage() { 417 if (minusImage == null) createImages(); 418 return minusImage; 419 } 420 421 432 public int indexOf (TableTreeItem item) { 433 for (int i = 0; i < items.length; i++) { 435 if (item == items[i]) return i; 436 } 437 return -1; 438 } 439 440 void onDispose(Event e) { 441 447 inDispose = true; 448 for (int i = 0; i < items.length; i++) { 449 items[i].dispose(); 450 } 451 inDispose = false; 452 if (plusImage != null) plusImage.dispose(); 453 if (minusImage != null) minusImage.dispose(); 454 if (sizeImage != null) sizeImage.dispose(); 455 plusImage = minusImage = sizeImage = null; 456 } 457 458 void onResize(Event e) { 459 Point size = getSize(); 460 table.setBounds(0, 0, size.x, size.y); 461 } 462 463 void onSelection(Event e) { 464 Event event = new Event(); 465 TableItem tableItem = (TableItem)e.item; 466 TableTreeItem item = getItem(tableItem); 467 event.item = item; 468 469 if (e.type == SWT.Selection && e.detail == SWT.CHECK && item != null) { 470 event.detail = SWT.CHECK; 471 item.checked = tableItem.getChecked(); 472 } 473 notifyListeners(e.type, event); 474 } 475 492 public TableTreeItem getItem (int index) { 493 checkWidget(); 494 int count = items.length; 495 if (!(0 <= index && index < count)) SWT.error (SWT.ERROR_INVALID_RANGE); 496 return items [index]; 497 } 498 499 515 public TableTreeItem getItem(Point point) { 516 checkWidget(); 517 TableItem item = table.getItem(point); 518 if (item == null) return null; 519 return getItem(item); 520 521 } 522 TableTreeItem getItem(TableItem tableItem) { 523 if (tableItem == null) return null; 524 for (int i = 0; i < items.length; i++) { 525 TableTreeItem item = items[i].getItem(tableItem); 526 if (item != null) return item; 527 } 528 return null; 529 } 530 void onFocusIn (Event e) { 531 table.setFocus(); 532 } 533 534 void onKeyDown (Event e) { 535 TableTreeItem[] selection = getSelection(); 536 if (selection.length == 0) return; 537 TableTreeItem item = selection[0]; 538 int type = 0; 539 if (e.keyCode == SWT.ARROW_RIGHT || e.keyCode == SWT.ARROW_LEFT) { 540 int trailKey = (getStyle() & SWT.MIRRORED) != 0 ? SWT.ARROW_LEFT : SWT.ARROW_RIGHT; 541 if (e.keyCode == trailKey) { 542 if (item.getItemCount() == 0) return; 543 if (item.getExpanded()) { 544 TableTreeItem newSelection = item.getItems()[0]; 545 table.setSelection(new TableItem[]{newSelection.tableItem}); 546 showItem(newSelection); 547 type = SWT.Selection; 548 } else { 549 item.setExpanded(true); 550 type = SWT.Expand; 551 } 552 } else { 553 if (item.getExpanded()) { 554 item.setExpanded(false); 555 type = SWT.Collapse; 556 } else { 557 TableTreeItem parent = item.getParentItem(); 558 if (parent != null) { 559 int index = parent.indexOf(item); 560 if (index != 0) return; 561 table.setSelection(new TableItem[]{parent.tableItem}); 562 type = SWT.Selection; 563 } 564 } 565 } 566 } 567 if (e.character == '*') { 568 item.expandAll(true); 569 } 570 if (e.character == '-') { 571 if (item.getExpanded()) { 572 item.setExpanded(false); 573 type = SWT.Collapse; 574 } 575 } 576 if (e.character == '+') { 577 if (item.getItemCount() > 0 && !item.getExpanded()) { 578 item.setExpanded(true); 579 type = SWT.Expand; 580 } 581 } 582 if (type == 0) return; 583 Event event = new Event(); 584 event.item = item; 585 notifyListeners(type, event); 586 } 587 void onMouseDown(Event event) { 588 589 TableItem[] items = table.getItems(); 590 for (int i = 0; i < items.length; i++) { 591 Rectangle rect = items[i].getImageBounds(0); 592 if (rect.contains(event.x, event.y)) { 593 TableTreeItem item = (TableTreeItem) items[i].getData(ITEMID); 594 event = new Event(); 595 event.item = item; 596 item.setExpanded(!item.getExpanded()); 597 if (item.getExpanded()) { 598 notifyListeners(SWT.Expand, event); 599 } else { 600 notifyListeners(SWT.Collapse, event); 601 } 602 return; 603 } 604 } 605 } 606 607 618 public void removeAll () { 619 checkWidget(); 620 setRedraw(false); 621 for (int i = items.length - 1; i >= 0; i--) { 622 items[i].dispose(); 623 } 624 items = EMPTY_ITEMS; 625 setRedraw(true); 626 } 627 628 void removeItem(TableTreeItem item) { 629 int index = 0; 630 while (index < items.length && items[index] != item) index++; 631 if (index == items.length) return; 632 TableTreeItem[] newItems = new TableTreeItem[items.length - 1]; 633 System.arraycopy(items, 0, newItems, 0, index); 634 System.arraycopy(items, index + 1, newItems, index, items.length - index - 1); 635 items = newItems; 636 } 637 638 655 public void removeSelectionListener (SelectionListener listener) { 656 checkWidget(); 657 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 658 removeListener(SWT.Selection, listener); 659 removeListener(SWT.DefaultSelection, listener); 660 } 661 662 679 public void removeTreeListener (TreeListener listener) { 680 checkWidget(); 681 if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 682 removeListener(SWT.Expand, listener); 683 removeListener(SWT.Collapse, listener); 684 } 685 686 696 public void selectAll () { 697 checkWidget(); 698 table.selectAll(); 699 } 700 public void setBackground (Color color) { 701 super.setBackground(color); 702 table.setBackground(color); 703 if (sizeImage != null) { 704 GC gc = new GC (sizeImage); 705 gc.setBackground(getBackground()); 706 Rectangle size = sizeImage.getBounds(); 707 gc.fillRectangle(size); 708 gc.dispose(); 709 } 710 } 711 public void setEnabled (boolean enabled) { 712 super.setEnabled(enabled); 713 table.setEnabled(enabled); 714 } 715 public void setFont (Font font) { 716 super.setFont(font); 717 table.setFont(font); 718 } 719 public void setForeground (Color color) { 720 super.setForeground(color); 721 table.setForeground(color); 722 } 723 public void setMenu (Menu menu) { 724 super.setMenu(menu); 725 table.setMenu(menu); 726 } 727 728 749 public void setSelection (TableTreeItem[] items) { 750 checkWidget (); 751 if (items == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 752 int length = items.length; 753 if (length == 0 || ((table.getStyle() & SWT.SINGLE) != 0 && length > 1)) { 754 deselectAll(); 755 return; 756 } 757 TableItem[] tableItems = new TableItem[length]; 758 for (int i = 0; i < length; i++) { 759 if (items[i] == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 760 if (!items[i].getVisible()) expandItem (items[i]); 761 tableItems[i] = items[i].tableItem; 762 } 763 table.setSelection(tableItems); 764 } 765 public void setToolTipText (String string) { 766 super.setToolTipText(string); 767 table.setToolTipText(string); 768 } 769 770 788 public void showItem (TableTreeItem item) { 789 checkWidget(); 790 if (item == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 791 if (!item.getVisible()) expandItem (item); 792 TableItem tableItem = item.tableItem; 793 table.showItem(tableItem); 794 } 795 796 810 public void showSelection () { 811 checkWidget(); 812 table.showSelection(); 813 } 814 } 815 | Popular Tags |