KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > custom > TableTreeItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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 /**
19  * A TableTreeItem is a selectable user interface object
20  * that represents an item in a hierarchy of items in a
21  * TableTree.
22  *
23  * @deprecated As of 3.1 use Tree, TreeItem and TreeColumn
24  */

25 public class TableTreeItem extends Item {
26     TableItem tableItem;
27     TableTree parent;
28     TableTreeItem parentItem;
29     TableTreeItem [] items = TableTree.EMPTY_ITEMS;
30     String JavaDoc[] 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 /**
40  * Constructs a new instance of this class given its parent
41  * (which must be a <code>TableTree</code>)
42  * and a style value describing its behavior and appearance.
43  * The item is added to the end of the items maintained by its parent.
44  * <p>
45  * The style value is either one of the style constants defined in
46  * class <code>SWT</code> which is applicable to instances of this
47  * class, or must be built by <em>bitwise OR</em>'ing together
48  * (that is, using the <code>int</code> "|" operator) two or more
49  * of those <code>SWT</code> style constants. The class description
50  * lists the style constants that are applicable to the class.
51  * Style bits are also inherited from superclasses.
52  * </p>
53  *
54  * @param parent a composite control which will be the parent of the new instance (cannot be null)
55  * @param style the style of control to construct
56  *
57  * @exception IllegalArgumentException <ul>
58  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
59  * </ul>
60  * @exception SWTException <ul>
61  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
62  * </ul>
63  *
64  * @see SWT
65  * @see Widget#getStyle()
66  */

67 public TableTreeItem(TableTree parent, int style) {
68     this (parent, style, parent.getItemCount());
69 }
70
71 /**
72  * Constructs a new instance of this class given its parent
73  * (which must be a <code>TableTree</code>,
74  * a style value describing its behavior and appearance, and the index
75  * at which to place it in the items maintained by its parent.
76  * <p>
77  * The style value is either one of the style constants defined in
78  * class <code>SWT</code> which is applicable to instances of this
79  * class, or must be built by <em>bitwise OR</em>'ing together
80  * (that is, using the <code>int</code> "|" operator) two or more
81  * of those <code>SWT</code> style constants. The class description
82  * lists the style constants that are applicable to the class.
83  * Style bits are also inherited from superclasses.
84  * </p>
85  *
86  * @param parent a composite control which will be the parent of the new instance (cannot be null)
87  * @param style the style of control to construct
88  * @param index the index to store the receiver in its parent
89  *
90  * @exception IllegalArgumentException <ul>
91  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
92  * </ul>
93  * @exception SWTException <ul>
94  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
95  * </ul>
96  *
97  * @see SWT
98  * @see Widget#getStyle()
99  */

100 public TableTreeItem(TableTree parent, int style, int index) {
101     this (parent, null, style, index);
102 }
103
104 /**
105  * Constructs a new instance of this class given its parent
106  * (which must be a <code>TableTreeItem</code>)
107  * and a style value describing its behavior and appearance.
108  * The item is added to the end of the items maintained by its parent.
109  * <p>
110  * The style value is either one of the style constants defined in
111  * class <code>SWT</code> which is applicable to instances of this
112  * class, or must be built by <em>bitwise OR</em>'ing together
113  * (that is, using the <code>int</code> "|" operator) two or more
114  * of those <code>SWT</code> style constants. The class description
115  * lists the style constants that are applicable to the class.
116  * Style bits are also inherited from superclasses.
117  * </p>
118  *
119  * @param parent a composite control which will be the parent of the new instance (cannot be null)
120  * @param style the style of control to construct
121  *
122  * @exception IllegalArgumentException <ul>
123  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
124  * </ul>
125  * @exception SWTException <ul>
126  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
127  * </ul>
128  *
129  * @see SWT
130  * @see Widget#getStyle()
131  */

132 public TableTreeItem(TableTreeItem parent, int style) {
133     this (parent, style, parent.getItemCount());
134 }
135
136 /**
137  * Constructs a new instance of this class given its parent
138  * (which must be a <code>TableTreeItem</code>),
139  * a style value describing its behavior and appearance, and the index
140  * at which to place it in the items maintained by its parent.
141  * <p>
142  * The style value is either one of the style constants defined in
143  * class <code>SWT</code> which is applicable to instances of this
144  * class, or must be built by <em>bitwise OR</em>'ing together
145  * (that is, using the <code>int</code> "|" operator) two or more
146  * of those <code>SWT</code> style constants. The class description
147  * lists the style constants that are applicable to the class.
148  * Style bits are also inherited from superclasses.
149  * </p>
150  *
151  * @param parent a composite control which will be the parent of the new instance (cannot be null)
152  * @param style the style of control to construct
153  * @param index the index to store the receiver in its parent
154  *
155  * @exception IllegalArgumentException <ul>
156  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
157  * </ul>
158  * @exception SWTException <ul>
159  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
160  * </ul>
161  *
162  * @see SWT
163  * @see Widget#getStyle()
164  */

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         /* Root items are visible immediately */
176         int tableIndex = parent.addItem(this, index);
177         tableItem = new TableItem(parent.getTable(), style, tableIndex);
178         tableItem.setData(TableTree.ITEMID, this);
179         addCheck();
180         /*
181         * Feature in the Table. The table uses the first image that
182         * is inserted into the table to size the table rows. If the
183         * user is allowed to insert the first image, this will cause
184         * the +/- images to be scaled. The fix is to insert a dummy
185         * image to force the size.
186         */

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     /* Now that item has a sub-node it must indicate that it can be expanded */
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     /* Put the item in the items list */
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 /**
228  * Returns the receiver's background color.
229  *
230  * @return the background color
231  *
232  * @exception SWTException <ul>
233  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
234  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
235  * </ul>
236  *
237  * @since 2.0
238  *
239  */

240 public Color getBackground () {
241     checkWidget ();
242     return (background == null) ? parent.getBackground() : background;
243 }
244
245 /**
246  * Returns a rectangle describing the receiver's size and location
247  * relative to its parent.
248  *
249  * @return the receiver's bounding rectangle
250  *
251  * @exception SWTException <ul>
252  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
253  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
254  * </ul>
255  */

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 /**
265  * Returns <code>true</code> if the receiver is checked,
266  * and false otherwise. When the parent does not have
267  * the <code>CHECK style, return false.
268  *
269  * @return the checked state of the checkbox
270  *
271  * @exception SWTException <ul>
272  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
273  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
274  * </ul>
275  */

276 public boolean getChecked () {
277     checkWidget();
278     if (tableItem == null) return checked;
279     return tableItem.getChecked();
280 }
281
282 /**
283  * Returns <code>true</code> if the receiver is grayed,
284  * and false otherwise. When the parent does not have
285  * the <code>CHECK</code> style, return false.
286  *
287  * @return the grayed state of the checkbox
288  *
289  * @exception SWTException <ul>
290  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
291  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
292  * </ul>
293  *
294  * @since 2.1
295  */

296 public boolean getGrayed () {
297     checkWidget();
298     if (tableItem == null) return grayed;
299     return tableItem.getGrayed();
300 }
301
302 /**
303  * Returns <code>true</code> if the receiver is expanded,
304  * and false otherwise.
305  * <p>
306  *
307  * @return the expanded state
308  */

309 public boolean getExpanded () {
310     //checkWidget();
311
return expanded;
312 }
313
314 /**
315  * Returns the font that the receiver will use to paint textual information for this item.
316  *
317  * @return the receiver's font
318  *
319  * @exception SWTException <ul>
320  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
321  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
322  * </ul>
323  *
324  * @since 3.0
325  */

326 public Font getFont () {
327     checkWidget ();
328     return (font == null) ? parent.getFont() : font;
329 }
330 /**
331  * Returns the foreground color that the receiver will use to draw.
332  *
333  * @return the receiver's foreground color
334  *
335  * @exception SWTException <ul>
336  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
337  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
338  * </ul>
339  *
340  * @since 2.0
341  *
342  */

343 public Color getForeground () {
344     checkWidget ();
345     return (foreground == null) ? parent.getForeground() : foreground;
346 }
347 /**
348  * Gets the first image.
349  * <p>
350  * The image in column 0 is reserved for the [+] and [-]
351  * images of the tree, therefore getImage(0) will return null.
352  *
353  * @return the image at index 0
354  *
355  * @exception SWTException <ul>
356  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
357  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
358  * </ul>
359  */

360 public Image getImage () {
361     checkWidget();
362     return getImage(0);
363 }
364
365 /**
366  * Gets the image at the specified index.
367  * <p>
368  * Indexing is zero based. The image can be null.
369  * The image in column 0 is reserved for the [+] and [-]
370  * images of the tree, therefore getImage(0) will return null.
371  * Return null if the index is out of range.
372  *
373  * @param index the index of the image
374  * @return the image at the specified index or null
375  */

376 public Image getImage (int index) {
377     //checkWidget();
378
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 /**
388  * Returns the item at the given, zero-relative index in the
389  * receiver. Throws an exception if the index is out of range.
390  *
391  * @param index the index of the item to return
392  * @return the item at the given index
393  *
394  * @exception IllegalArgumentException <ul>
395  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
396  * </ul>
397  * @exception SWTException <ul>
398  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
399  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
400  * </ul>
401  *
402  * @since 3.1
403  */

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 /**
412  * Returns the number of items contained in the receiver
413  * that are direct item children of the receiver.
414  *
415  * @return the number of items
416  */

417 public int getItemCount () {
418     //checkWidget();
419
return items.length;
420 }
421
422 /**
423  * Returns an array of <code>TableTreeItem</code>s which are the
424  * direct item children of the receiver.
425  * <p>
426  * Note: This is not the actual structure used by the receiver
427  * to maintain its list of items, so modifying the array will
428  * not affect the receiver.
429  * </p>
430  *
431  * @return the receiver's items
432  */

433 public TableTreeItem[] getItems () {
434     //checkWidget();
435
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 /**
451  * Returns the receiver's parent, which must be a <code>TableTree</code>.
452  *
453  * @return the receiver's parent
454  */

455 public TableTree getParent () {
456     //checkWidget();
457
return parent;
458 }
459
460 /**
461  * Returns the receiver's parent item, which must be a
462  * <code>TableTreeItem</code> or null when the receiver is a
463  * root.
464  *
465  * @return the receiver's parent item
466  */

467 public TableTreeItem getParentItem () {
468     //checkWidget();
469
return parentItem;
470 }
471 public String JavaDoc getText () {
472     checkWidget();
473     return getText(0);
474 }
475
476 /**
477  * Gets the item text at the specified index.
478  * <p>
479  * Indexing is zero based.
480  *
481  * This operation will fail when the index is out
482  * of range or an item could not be queried from
483  * the OS.
484  *
485  * @param index the index of the item
486  * @return the item text at the specified index, which can be null
487  */

488 public String JavaDoc getText(int index) {
489     //checkWidget();
490
if (0 <= index && index < texts.length) return texts[index];
491     return null;
492 }
493
494 boolean getVisible () {
495     return tableItem != null;
496 }
497
498 /**
499  * Gets the index of the specified item.
500  *
501  * <p>The widget is searched starting at 0 until an
502  * item is found that is equal to the search item.
503  * If no item is found, -1 is returned. Indexing
504  * is zero based. This index is relative to the parent only.
505  *
506  * @param item the search item
507  * @return the index of the item or -1 if the item is not found
508  *
509  */

510 public int indexOf (TableTreeItem item) {
511     //checkWidget();
512
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 /**
591  * Sets the receiver's background color to the color specified
592  * by the argument, or to the default system color for the item
593  * if the argument is null.
594  *
595  * @param color the new color (or null)
596  *
597  * @exception IllegalArgumentException <ul>
598  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
599  * </ul>
600  * @exception SWTException <ul>
601  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
602  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
603  * </ul>
604  *
605  * @since 2.0
606  *
607  */

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 /**
620  * Sets the checked state of the checkbox for this item. This state change
621  * only applies if the Table was created with the SWT.CHECK style.
622  *
623  * @param checked the new checked state of the checkbox
624  *
625  * @exception SWTException <ul>
626  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
627  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
628  * </ul>
629  */

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 /**
641  * Sets the grayed state of the checkbox for this item. This state change
642  * only applies if the Table was created with the SWT.CHECK style.
643  *
644  * @param grayed the new grayed state of the checkbox;
645  *
646  * @exception SWTException <ul>
647  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
648  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
649  * </ul>
650  *
651  * @since 2.1
652  */

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 /**
664  * Sets the expanded state.
665  * <p>
666  * @param expanded the new expanded state.
667  *
668  * @exception SWTException <ul>
669  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
670  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
671  * </ul>
672  */

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 /**
689  * Sets the font that the receiver will use to paint textual information
690  * for this item to the font specified by the argument, or to the default font
691  * for that kind of control if the argument is null.
692  *
693  * @param font the new font (or null)
694  *
695  * @exception IllegalArgumentException <ul>
696  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
697  * </ul>
698  * @exception SWTException <ul>
699  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
700  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
701  * </ul>
702  *
703  * @since 3.0
704  */

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 /**
716  * Sets the receiver's foreground color to the color specified
717  * by the argument, or to the default system color for the item
718  * if the argument is null.
719  *
720  * @param color the new color (or null)
721  *
722  * @since 2.0
723  *
724  * @exception IllegalArgumentException <ul>
725  * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
726  * </ul>
727  * @exception SWTException <ul>
728  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
729  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
730  * </ul>
731  *
732  * @since 2.0
733  *
734  */

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 /**
747  * Sets the image at an index.
748  * <p>
749  * The image can be null.
750  * The image in column 0 is reserved for the [+] and [-]
751  * images of the tree, therefore do nothing if index is 0.
752  *
753  * @param image the new image or null
754  *
755  * @exception SWTException <ul>
756  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
757  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
758  * </ul>
759  */

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 /**
774  * Sets the first image.
775  * <p>
776  * The image can be null.
777  * The image in column 0 is reserved for the [+] and [-]
778  * images of the tree, therefore do nothing.
779  *
780  * @param image the new image or null
781  *
782  * @exception SWTException <ul>
783  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
784  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
785  * </ul>
786  */

787 public void setImage (Image image) {
788     setImage(0, image);
789 }
790
791 /**
792  * Sets the widget text.
793  * <p>
794  *
795  * The widget text for an item is the label of the
796  * item or the label of the text specified by a column
797  * number.
798  *
799  * @param index the column number
800  * @param text the new text
801  *
802  * @exception IllegalArgumentException <ul>
803  * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
804  * </ul>
805  * @exception SWTException <ul>
806  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
807  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
808  * </ul>
809  */

810 public void setText(int index, String JavaDoc 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 JavaDoc[] newTexts = new String JavaDoc[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 JavaDoc string) {
824     setText(0, string);
825 }
826
827 void setVisible (boolean show) {
828     if (parentItem == null) return; // this is a root and can not be toggled between visible and hidden
829
if (getVisible() == show) return;
830
831     if (show) {
832         if (!parentItem.getVisible()) return; // parentItem must already be visible
833
// create underlying table item and set data in table item to stored data
834
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         // restore fields to item
847
// ignore any images in the first column
848
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         // display the children and the appropriate [+]/[-] symbol as required
855
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         // remove row from table
872
tableItem.dispose();
873         tableItem = null;
874     }
875 }
876 }
877
Popular Tags