KickJava   Java API By Example, From Geeks To Geeks.

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


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.events.*;
16 import org.eclipse.swt.graphics.*;
17 import org.eclipse.swt.widgets.*;
18
19 /**
20  * A TableTree is a selectable user interface object
21  * that displays a hierarchy of items, and issues
22  * notification when an item is selected.
23  * A TableTree may be single or multi select.
24  * <p>
25  * The item children that may be added to instances of this class
26  * must be of type <code>TableTreeItem</code>.
27  * </p><p>
28  * Note that although this class is a subclass of <code>Composite</code>,
29  * it does not make sense to add <code>Control</code> children to it,
30  * or set a layout on it.
31  * </p><p>
32  * <dl>
33  * <dt><b>Styles:</b> <dd> SINGLE, MULTI, CHECK, FULL_SELECTION
34  * <dt><b>Events:</b> <dd> Selection, DefaultSelection, Collapse, Expand
35  * </dl>
36  * <p>
37  * Note: Only one of the styles SINGLE, and MULTI may be specified.
38  * </p>
39  *
40  * @deprecated As of 3.1 use Tree, TreeItem and TreeColumn
41  */

42 public class TableTree extends Composite {
43     Table table;
44     TableTreeItem[] items = EMPTY_ITEMS;
45     Image plusImage, minusImage, sizeImage;
46
47     /*
48     * TableTreeItems are not treated as children but rather as items.
49     * When the TableTree is disposed, all children are disposed because
50     * TableTree inherits this behaviour from Composite. The items
51     * must be disposed separately. Because TableTree is not part of
52     * the org.eclipse.swt.widgets package, the method releaseWidget can
53     * not be overridden (this is how items are disposed of in Table and Tree).
54     * Instead, the items are disposed of in response to the dispose event on the
55     * TableTree. The "inDispose" flag is used to distinguish between disposing
56     * one TableTreeItem (e.g. when removing an entry from the TableTree) and
57     * disposing the entire TableTree.
58     */

59     boolean inDispose = false;
60     
61     static final TableTreeItem[] EMPTY_ITEMS = new TableTreeItem [0];
62     static final String JavaDoc[] EMPTY_TEXTS = new String JavaDoc [0];
63     static final Image[] EMPTY_IMAGES = new Image [0];
64     static final String JavaDoc ITEMID = "TableTreeItemID"; //$NON-NLS-1$
65

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

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     /* Return the index in the table where this table should be inserted */
142     if (index == items.length - 1 )
143         return table.getItemCount();
144     else
145         return table.indexOf(items[index+1].tableItem);
146 }
147
148 /**
149  * Adds the listener to the collection of listeners who will
150  * be notified when the user changes the receiver's selection, by sending
151  * it one of the messages defined in the <code>SelectionListener</code>
152  * interface.
153  * <p>
154  * When <code>widgetSelected</code> is called, the item field of the event object is valid.
155  * If the receiver has <code>SWT.CHECK</code> style set and the check selection changes,
156  * the event object detail field contains the value <code>SWT.CHECK</code>.
157  * <code>widgetDefaultSelected</code> is typically called when an item is double-clicked.
158  * The item field of the event object is valid for default selection, but the detail field is not used.
159  * </p>
160  *
161  * @param listener the listener which should be notified when the user changes the receiver's selection
162  *
163  * @exception IllegalArgumentException <ul>
164  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
165  * </ul>
166  * @exception SWTException <ul>
167  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
168  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
169  * </ul>
170  *
171  * @see SelectionListener
172  * @see #removeSelectionListener
173  * @see SelectionEvent
174  */

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 /**
184  * Adds the listener to the collection of listeners who will
185  * be notified when an item in the receiver is expanded or collapsed
186  * by sending it one of the messages defined in the <code>TreeListener</code>
187  * interface.
188  *
189  * @param listener the listener which should be notified
190  *
191  * @exception IllegalArgumentException <ul>
192  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
193  * </ul>
194  * @exception SWTException <ul>
195  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
196  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
197  * </ul>
198  *
199  * @see TreeListener
200  * @see #removeTreeListener
201  */

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 /**
224  * Deselects all items.
225  * <p>
226  * If an item is selected, it is deselected.
227  * If an item is not selected, it remains unselected.
228  *
229  * @exception SWTException <ul>
230  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
231  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed
232  * </ul>
233  */

234 public void deselectAll () {
235     checkWidget();
236     table.deselectAll();
237 }
238
239 /* Expand upward from the specified leaf item. */
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     // This method must be overridden otherwise, in a TableTree in which the first
253
// item has no sub items, a grey (Widget background colour) square will appear in
254
// the first column of the first item.
255
// It is not possible in the constructor to set the background of the TableTree
256
// to be the same as the background of the Table because this interferes with
257
// the TableTree adapting to changes in the System color settings.
258
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 /**
270  * Gets the number of items.
271  * <p>
272  * @return the number of items in the widget
273  */

274 public int getItemCount () {
275     //checkWidget();
276
return items.length;
277 }
278
279 /**
280  * Gets the height of one item.
281  * <p>
282  * This operation will fail if the height of
283  * one item could not be queried from the OS.
284  *
285  * @return the height of one item in the widget
286  *
287  * @exception SWTException <ul>
288  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
289  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed
290  * </ul>
291  */

292 public int getItemHeight () {
293     checkWidget();
294     return table.getItemHeight();
295 }
296
297 /**
298  * Gets the items.
299  * <p>
300  * @return the items in the widget
301  */

302 public TableTreeItem [] getItems () {
303     //checkWidget();
304
TableTreeItem[] newItems = new TableTreeItem[items.length];
305     System.arraycopy(items, 0, newItems, 0, items.length);
306     return newItems;
307 }
308
309 /**
310  * Gets the selected items.
311  * <p>
312  * This operation will fail if the selected
313  * items cannot be queried from the OS.
314  *
315  * @return the selected items in the widget
316  *
317  * @exception SWTException <ul>
318  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
319  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
320  * </ul>
321  */

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 /**
333  * Gets the number of selected items.
334  * <p>
335  * This operation will fail if the number of selected
336  * items cannot be queried from the OS.
337  *
338  * @return the number of selected items in the widget
339  *
340  * @exception SWTException <ul>
341  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread</li>
342  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed</li>
343  * </ul>
344  */

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 /**
356  * Returns the underlying Table control.
357  *
358  * @return the underlying Table control
359  */

360 public Table getTable () {
361     //checkWidget();
362
return table;
363 }
364
365 void createImages () {
366     
367     int itemHeight = sizeImage.getBounds().height;
368     // Calculate border around image.
369
// At least 9 pixels are needed to draw the image
370
// Leave at least a 6 pixel border.
371
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; // size must be an even number
375
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     /* Plus image */
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     /* Minus image */
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 /**
422  * Gets the index of an item.
423  *
424  * <p>The widget is searched starting at 0 until an
425  * item is found that is equal to the search item.
426  * If no item is found, -1 is returned. Indexing
427  * is zero based. This index is relative to the parent only.
428  *
429  * @param item the search item
430  * @return the index of the item or -1
431  */

432 public int indexOf (TableTreeItem item) {
433     //checkWidget();
434
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     /*
442      * Usually when an item is disposed, destroyItem will change the size of the items array
443      * and dispose of the underlying table items.
444      * Since the whole table tree is being disposed, this is not necessary. For speed
445      * the inDispose flag is used to skip over this part of the item dispose.
446      */

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 /**
476  * Returns the item at the given, zero-relative index in the
477  * receiver. Throws an exception if the index is out of range.
478  *
479  * @param index the index of the item to return
480  * @return the item at the given index
481  *
482  * @exception IllegalArgumentException <ul>
483  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the list minus 1 (inclusive)</li>
484  * </ul>
485  * @exception SWTException <ul>
486  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
487  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
488  * </ul>
489  *
490  * @since 3.1
491  */

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 /**
500  * Returns the item at the given point in the receiver
501  * or null if no such item exists. The point is in the
502  * coordinate system of the receiver.
503  *
504  * @param point the point used to locate the item
505  * @return the item at the given point
506  *
507  * @exception IllegalArgumentException <ul>
508  * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
509  * </ul>
510  * @exception SWTException <ul>
511  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
512  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
513  * </ul>
514  */

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     /* If user clicked on the [+] or [-], expand or collapse the tree. */
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 /**
608  * Removes all items.
609  * <p>
610  * This operation will fail when an item
611  * could not be removed in the OS.
612  *
613  * @exception SWTException <ul>
614  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
615  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed
616  * </ul>
617  */

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 /**
639  * Removes the listener from the collection of listeners who will
640  * be notified when the user changes the receiver's selection.
641  *
642  * @param listener the listener which should no longer be notified
643  *
644  * @exception IllegalArgumentException <ul>
645  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
646  * </ul>
647  * @exception SWTException <ul>
648  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
649  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
650  * </ul>
651  *
652  * @see SelectionListener
653  * @see #addSelectionListener
654  */

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 /**
663  * Removes the listener from the collection of listeners who will
664  * be notified when items in the receiver are expanded or collapsed.
665  *
666  * @param listener the listener which should no longer be notified
667  *
668  * @exception IllegalArgumentException <ul>
669  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
670  * </ul>
671  * @exception SWTException <ul>
672  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
673  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
674  * </ul>
675  *
676  * @see TreeListener
677  * @see #addTreeListener
678  */

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 /**
687  * Selects all of the items in the receiver.
688  * <p>
689  * If the receiver is single-select, do nothing.
690  *
691  * @exception SWTException <ul>
692  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
693  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed
694  * </ul>
695  */

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 /**
729  * Sets the receiver's selection to be the given array of items.
730  * The current selection is cleared before the new items are selected.
731  * <p>
732  * Items that are not in the receiver are ignored.
733  * If the receiver is single-select and multiple items are specified,
734  * then all items are ignored.
735  *
736  * @param items the array of items
737  *
738  * @exception IllegalArgumentException <ul>
739  * <li>ERROR_NULL_ARGUMENT - if the array of items is null</li>
740  * <li>ERROR_INVALID_ARGUMENT - if one of the item has been disposed</li>
741  * </ul>
742  * @exception SWTException <ul>
743  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
744  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
745  * </ul>
746  *
747  * @see TableTree#deselectAll()
748  */

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 JavaDoc string) {
766     super.setToolTipText(string);
767     table.setToolTipText(string);
768 }
769
770 /**
771  * Shows the item. If the item is already showing in the receiver,
772  * this method simply returns. Otherwise, the items are scrolled
773  * and expanded until the item is visible.
774  *
775  * @param item the item to be shown
776  *
777  * @exception IllegalArgumentException <ul>
778  * <li>ERROR_NULL_ARGUMENT - if the item is null</li>
779  * <li>ERROR_INVALID_ARGUMENT - if the item has been disposed</li>
780  * </ul>
781  * @exception SWTException <ul>
782  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
783  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
784  * </ul>
785  *
786  * @see TableTree#showSelection()
787  */

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 /**
797  * Shows the selection.
798  * <p>
799  * If there is no selection or the selection
800  * is already visible, this method does nothing.
801  * If the selection is scrolled out of view,
802  * the top index of the widget is changed such
803  * that selection becomes visible.
804  *
805  * @exception SWTException <ul>
806  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
807  * <li>ERROR_WIDGET_DISPOSED when the widget has been disposed
808  * </ul>
809  */

810 public void showSelection () {
811     checkWidget();
812     table.showSelection();
813 }
814 }
815
Popular Tags