1 11 package org.eclipse.swt.custom; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import org.eclipse.swt.widgets.*; 17 18 25 public class TableTreeItem extends Item { 26 TableItem tableItem; 27 TableTree parent; 28 TableTreeItem parentItem; 29 TableTreeItem [] items = TableTree.EMPTY_ITEMS; 30 String [] texts = TableTree.EMPTY_TEXTS; 31 Image[] images = TableTree.EMPTY_IMAGES; 32 Color background; 33 Color foreground; 34 Font font; 35 boolean expanded; 36 boolean checked; 37 boolean grayed; 38 39 67 public TableTreeItem(TableTree parent, int style) { 68 this (parent, style, parent.getItemCount()); 69 } 70 71 100 public TableTreeItem(TableTree parent, int style, int index) { 101 this (parent, null, style, index); 102 } 103 104 132 public TableTreeItem(TableTreeItem parent, int style) { 133 this (parent, style, parent.getItemCount()); 134 } 135 136 165 public TableTreeItem(TableTreeItem parent, int style, int index) { 166 this (parent.getParent(), parent, style, index); 167 } 168 169 TableTreeItem(TableTree parent, TableTreeItem parentItem, int style, int index) { 170 super(parent, style); 171 this.parent = parent; 172 this.parentItem = parentItem; 173 if (parentItem == null) { 174 175 176 int tableIndex = parent.addItem(this, index); 177 tableItem = new TableItem(parent.getTable(), style, tableIndex); 178 tableItem.setData(TableTree.ITEMID, this); 179 addCheck(); 180 187 if (parent.sizeImage == null) { 188 int itemHeight = parent.getItemHeight(); 189 parent.sizeImage = new Image(null, itemHeight, itemHeight); 190 GC gc = new GC (parent.sizeImage); 191 gc.setBackground(parent.getBackground()); 192 gc.fillRectangle(0, 0, itemHeight, itemHeight); 193 gc.dispose(); 194 tableItem.setImage(0, parent.sizeImage); 195 } 196 } else { 197 parentItem.addItem(this, index); 198 } 199 } 200 void addCheck() { 201 Table table = parent.getTable(); 202 if ((table.getStyle() & SWT.CHECK) == 0) return; 203 tableItem.setChecked(checked); 204 tableItem.setGrayed(grayed); 205 } 206 void addItem(TableTreeItem item, int index) { 207 if (item == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); 208 if (index < 0 || index > items.length) SWT.error(SWT.ERROR_INVALID_ARGUMENT); 209 210 211 if (items.length == 0 && index == 0) { 212 if (tableItem != null) { 213 Image image = expanded ? parent.getMinusImage() : parent.getPlusImage(); 214 tableItem.setImage(0, image); 215 } 216 } 217 218 219 TableTreeItem[] newItems = new TableTreeItem[items.length + 1]; 220 System.arraycopy(items, 0, newItems, 0, index); 221 newItems[index] = item; 222 System.arraycopy(items, index, newItems, index + 1, items.length - index); 223 items = newItems; 224 if (expanded) item.setVisible(true); 225 } 226 227 240 public Color getBackground () { 241 checkWidget (); 242 return (background == null) ? parent.getBackground() : background; 243 } 244 245 256 public Rectangle getBounds (int index) { 257 checkWidget(); 258 if (tableItem != null) { 259 return tableItem.getBounds(index); 260 } else { 261 return new Rectangle(0, 0, 0, 0); 262 } 263 } 264 276 public boolean getChecked () { 277 checkWidget(); 278 if (tableItem == null) return checked; 279 return tableItem.getChecked(); 280 } 281 282 296 public boolean getGrayed () { 297 checkWidget(); 298 if (tableItem == null) return grayed; 299 return tableItem.getGrayed(); 300 } 301 302 309 public boolean getExpanded () { 310 return expanded; 312 } 313 314 326 public Font getFont () { 327 checkWidget (); 328 return (font == null) ? parent.getFont() : font; 329 } 330 343 public Color getForeground () { 344 checkWidget (); 345 return (foreground == null) ? parent.getForeground() : foreground; 346 } 347 360 public Image getImage () { 361 checkWidget(); 362 return getImage(0); 363 } 364 365 376 public Image getImage (int index) { 377 if (0 < index && index < images.length) return images[index]; 379 return null; 380 } 381 382 int getIndent() { 383 if (parentItem == null) return 0; 384 return parentItem.getIndent() + 1; 385 } 386 387 404 public TableTreeItem getItem (int index) { 405 checkWidget(); 406 int count = items.length; 407 if (!(0 <= index && index < count)) SWT.error (SWT.ERROR_INVALID_RANGE); 408 return items [index]; 409 } 410 411 417 public int getItemCount () { 418 return items.length; 420 } 421 422 433 public TableTreeItem[] getItems () { 434 TableTreeItem[] newItems = new TableTreeItem[items.length]; 436 System.arraycopy(items, 0, newItems, 0, items.length); 437 return newItems; 438 } 439 440 TableTreeItem getItem(TableItem tableItem) { 441 if (tableItem == null) return null; 442 if (this.tableItem == tableItem) return this; 443 for (int i = 0; i < items.length; i++) { 444 TableTreeItem item = items[i].getItem(tableItem); 445 if (item != null) return item; 446 } 447 return null; 448 } 449 450 455 public TableTree getParent () { 456 return parent; 458 } 459 460 467 public TableTreeItem getParentItem () { 468 return parentItem; 470 } 471 public String getText () { 472 checkWidget(); 473 return getText(0); 474 } 475 476 488 public String getText(int index) { 489 if (0 <= index && index < texts.length) return texts[index]; 491 return null; 492 } 493 494 boolean getVisible () { 495 return tableItem != null; 496 } 497 498 510 public int indexOf (TableTreeItem item) { 511 for (int i = 0; i < items.length; i++) { 513 if (items[i] == item) return i; 514 } 515 return -1; 516 } 517 518 void expandAll(boolean notify) { 519 if (items.length == 0) return; 520 if (!expanded) { 521 setExpanded(true); 522 if (notify) { 523 Event event = new Event(); 524 event.item = this; 525 parent.notifyListeners(SWT.Expand, event); 526 } 527 } 528 for (int i = 0; i < items.length; i++) { 529 items[i].expandAll(notify); 530 } 531 } 532 int expandedIndexOf (TableTreeItem item) { 533 int index = 0; 534 for (int i = 0; i < items.length; i++) { 535 if (items[i] == item) return index; 536 if (items[i].expanded) index += items[i].visibleChildrenCount (); 537 index++; 538 } 539 return -1; 540 } 541 542 int visibleChildrenCount () { 543 int count = 0; 544 for (int i = 0; i < items.length; i++) { 545 if (items[i].getVisible ()) { 546 count += 1 + items[i].visibleChildrenCount (); 547 } 548 } 549 return count; 550 } 551 552 public void dispose () { 553 if (isDisposed()) return; 554 for (int i = items.length - 1; i >= 0; i--) { 555 items[i].dispose(); 556 } 557 super.dispose(); 558 if (!parent.inDispose) { 559 if (parentItem != null) { 560 parentItem.removeItem(this); 561 } else { 562 parent.removeItem(this); 563 } 564 if (tableItem != null) tableItem.dispose(); 565 } 566 items = null; 567 parentItem = null; 568 parent = null; 569 images = null; 570 texts = null; 571 tableItem = null; 572 foreground = null; 573 background = null; 574 font = null; 575 } 576 577 void removeItem(TableTreeItem item) { 578 int index = 0; 579 while (index < items.length && items[index] != item) index++; 580 if (index == items.length) return; 581 TableTreeItem[] newItems = new TableTreeItem[items.length - 1]; 582 System.arraycopy(items, 0, newItems, 0, index); 583 System.arraycopy(items, index + 1, newItems, index, items.length - index - 1); 584 items = newItems; 585 if (items.length == 0) { 586 if (tableItem != null) tableItem.setImage(0, null); 587 } 588 } 589 590 608 public void setBackground (Color color) { 609 checkWidget (); 610 if (color != null && color.isDisposed ()) { 611 SWT.error (SWT.ERROR_INVALID_ARGUMENT); 612 } 613 if (tableItem != null) { 614 tableItem.setBackground(color); 615 } 616 background = color; 617 } 618 619 630 public void setChecked (boolean checked) { 631 checkWidget(); 632 Table table = parent.getTable(); 633 if ((table.getStyle() & SWT.CHECK) == 0) return; 634 if (tableItem != null) { 635 tableItem.setChecked(checked); 636 } 637 this.checked = checked; 638 } 639 640 653 public void setGrayed (boolean grayed) { 654 checkWidget(); 655 Table table = parent.getTable(); 656 if ((table.getStyle() & SWT.CHECK) == 0) return; 657 if (tableItem != null) { 658 tableItem.setGrayed(grayed); 659 } 660 this.grayed = grayed; 661 } 662 663 673 public void setExpanded (boolean expanded) { 674 checkWidget(); 675 if (items.length == 0) return; 676 if (this.expanded == expanded) return; 677 this.expanded = expanded; 678 if (tableItem == null) return; 679 parent.setRedraw(false); 680 for (int i = 0; i < items.length; i++) { 681 items[i].setVisible(expanded); 682 } 683 Image image = expanded ? parent.getMinusImage() : parent.getPlusImage(); 684 tableItem.setImage(0, image); 685 parent.setRedraw(true); 686 } 687 688 705 public void setFont (Font font){ 706 checkWidget (); 707 if (font != null && font.isDisposed ()) { 708 SWT.error (SWT.ERROR_INVALID_ARGUMENT); 709 } 710 if (tableItem != null) { 711 tableItem.setFont(font); 712 } 713 this.font = font; 714 } 715 735 public void setForeground (Color color) { 736 checkWidget (); 737 if (color != null && color.isDisposed ()) { 738 SWT.error (SWT.ERROR_INVALID_ARGUMENT); 739 } 740 if (tableItem != null) { 741 tableItem.setForeground(color); 742 } 743 foreground = color; 744 } 745 746 760 public void setImage (int index, Image image) { 761 checkWidget(); 762 int columnCount = Math.max(parent.getTable().getColumnCount(), 1); 763 if (index <= 0 || index >= columnCount) return; 764 if (images.length < columnCount) { 765 Image[] newImages = new Image[columnCount]; 766 System.arraycopy(images, 0, newImages, 0, images.length); 767 images = newImages; 768 } 769 images[index] = image; 770 if (tableItem != null) tableItem.setImage(index, image); 771 } 772 773 787 public void setImage (Image image) { 788 setImage(0, image); 789 } 790 791 810 public void setText(int index, String text) { 811 checkWidget(); 812 if (text == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); 813 int columnCount = Math.max(parent.getTable().getColumnCount(), 1); 814 if (index < 0 || index >= columnCount) return; 815 if (texts.length < columnCount) { 816 String [] newTexts = new String [columnCount]; 817 System.arraycopy(texts, 0, newTexts, 0, texts.length); 818 texts = newTexts; 819 } 820 texts[index] = text; 821 if (tableItem != null) tableItem.setText(index, text); 822 } 823 public void setText (String string) { 824 setText(0, string); 825 } 826 827 void setVisible (boolean show) { 828 if (parentItem == null) return; if (getVisible() == show) return; 830 831 if (show) { 832 if (!parentItem.getVisible()) return; Table table = parent.getTable(); 835 int parentIndex = table.indexOf(parentItem.tableItem); 836 int index = parentItem.expandedIndexOf(this) + parentIndex + 1; 837 if (index < 0) return; 838 tableItem = new TableItem(table, getStyle(), index); 839 tableItem.setData(TableTree.ITEMID, this); 840 tableItem.setImageIndent(getIndent()); 841 if (background != null) tableItem.setBackground(background); 842 if (foreground != null) tableItem.setForeground(foreground); 843 if (font != null) tableItem.setFont(font); 844 addCheck(); 845 846 int columnCount = Math.max(table.getColumnCount(), 1); 849 for (int i = 0; i < columnCount; i++) { 850 if (i < texts.length && texts[i] != null) setText(i, texts[i]); 851 if (i < images.length && images[i] != null) setImage(i, images[i]); 852 } 853 854 if (items.length != 0) { 856 if (expanded) { 857 tableItem.setImage(0, parent.getMinusImage()); 858 for (int i = 0, length = items.length; i < length; i++) { 859 items[i].setVisible(true); 860 } 861 } else { 862 tableItem.setImage(0, parent.getPlusImage()); 863 } 864 } 865 866 } else { 867 868 for (int i = 0, length = items.length; i < length; i++) { 869 items[i].setVisible(false); 870 } 871 tableItem.dispose(); 873 tableItem = null; 874 } 875 } 876 } 877 | Popular Tags |