Eclipse Platform
2.0

org.eclipse.swt.widgets
Class Tree

java.lang.Object
  |
  +--org.eclipse.swt.widgets.Widget
        |
        +--org.eclipse.swt.widgets.Control
              |
              +--org.eclipse.swt.widgets.Scrollable
                    |
                    +--org.eclipse.swt.widgets.Composite
                          |
                          +--org.eclipse.swt.widgets.Tree
All Implemented Interfaces:
Drawable

public class Tree
extends Composite

Instances of this class provide a selectable user interface object that displays a hierarchy of items and issue notificiation when an item in the hierarchy is selected.

The item children that may be added to instances of this class must be of type TreeItem.

Note that although this class is a subclass of Composite, it does not make sense to add Control children to it, or set a layout on it.

Styles:
SINGLE, MULTI, CHECK
Events:
Selection, DefaultSelection, Collapse, Expand

Note: Only one of the styles SINGLE and MULTI may be specified.

IMPORTANT: This class is not intended to be subclassed.


Field Summary
 
Fields inherited from class org.eclipse.swt.widgets.Control
handle
 
Constructor Summary
Tree(Composite parent, int style)
          Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
 
Method Summary
 void addSelectionListener(SelectionListener listener)
          Adds the listener to the collection of listeners who will be notified when the receiver's selection changes, by sending it one of the messages defined in the SelectionListener interface.
 void addTreeListener(TreeListener listener)
          Adds the listener to the collection of listeners who will be notified when an item in the receiver is expanded or collapsed by sending it one of the messages defined in the TreeListener interface.
 Point computeSize(int wHint, int hHint, boolean changed)
          Returns the preferred size of the receiver.
 void deselectAll()
          Deselects all selected items in the receiver.
 TreeItem getItem(Point point)
          Returns the item at the given point in the receiver or null if no such item exists.
 int getItemCount()
          Returns the number of items contained in the receiver that are direct item children of the receiver.
 int getItemHeight()
          Returns the height of the area which would be used to display one of the items in the tree.
 TreeItem[] getItems()
          Returns the number of items contained in the receiver that are direct item children of the receiver.
 TreeItem getParentItem()
          Returns the receiver's parent item, which must be a TreeItem or null when the receiver is a root.
 TreeItem[] getSelection()
          Returns an array of TreeItems that are currently selected in the receiver.
 int getSelectionCount()
          Returns the number of selected items contained in the receiver.
 void removeAll()
          Removes all of the items from the receiver.
 void removeSelectionListener(SelectionListener listener)
          Removes the listener from the collection of listeners who will be notified when the receiver's selection changes.
 void removeTreeListener(TreeListener listener)
          Removes the listener from the collection of listeners who will be notified when items in the receiver are expanded or collapsed..
 void selectAll()
          Selects all the items in the receiver.
 void setInsertMark(TreeItem item, boolean before)
          Display a mark indicating the point at which an item will be inserted.
 void setRedraw(boolean redraw)
          If the argument is false, causes subsequent drawing operations in the receiver to be ignored.
 void setSelection(TreeItem[] items)
          Sets the receiver's selection to be the given array of items.
 void showItem(TreeItem item)
          Shows the item.
 void showSelection()
          Shows the selection.
 
Methods inherited from class org.eclipse.swt.widgets.Composite
checkSubclass, getChildren, getLayout, getTabList, layout, layout, setFocus, setLayout, setTabList
 
Methods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getVerticalBar
 
Methods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addFocusListener, addHelpListener, addKeyListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addPaintListener, addTraverseListener, computeSize, forceFocus, getAccessible, getBackground, getBorderWidth, getBounds, getDisplay, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getParent, getShell, getSize, getToolTipText, getVisible, internal_dispose_GC, internal_new_GC, isDisposed, isEnabled, isFocusControl, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, redraw, redraw, removeControlListener, removeFocusListener, removeHelpListener, removeKeyListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removePaintListener, removeTraverseListener, setBackground, setBounds, setBounds, setCapture, setCursor, setEnabled, setFont, setForeground, setLayoutData, setLocation, setLocation, setMenu, setParent, setSize, setSize, setToolTipText, setVisible, toControl, toDisplay, traverse, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, checkWidget, dispose, getData, getData, getStyle, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Tree

public Tree(Composite parent,
            int style)
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

Parameters:
parent - a composite control which will be the parent of the new instance (cannot be null)
style - the style of control to construct
Throws:
IllegalArgumentException -
SWTException -
See Also:
SWT.SINGLE, SWT.MULTI, SWT.CHECK, Widget.checkSubclass(), Widget.getStyle()
Method Detail

addSelectionListener

public void addSelectionListener(SelectionListener listener)
Adds the listener to the collection of listeners who will be notified when the receiver's selection changes, by sending it one of the messages defined in the SelectionListener interface.

When widgetSelected is called, the item field of the event object is valid. If the reciever has SWT.CHECK style set and the check selection changes, the event object detail field contains the value SWT.CHECK. widgetDefaultSelected is typically called when an item is double-clicked. The item field of the event object is valid for default selection, but the detail field is not used.

Parameters:
listener - the listener which should be notified
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
SelectionListener, removeSelectionListener(org.eclipse.swt.events.SelectionListener), SelectionEvent

addTreeListener

public void addTreeListener(TreeListener listener)
Adds the listener to the collection of listeners who will be notified when an item in the receiver is expanded or collapsed by sending it one of the messages defined in the TreeListener interface.

Parameters:
listener - the listener which should be notified
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
TreeListener, removeTreeListener(org.eclipse.swt.events.TreeListener)

computeSize

public Point computeSize(int wHint,
                         int hHint,
                         boolean changed)
Description copied from class: Control
Returns the preferred size of the receiver.

The preferred size of a control is the size that it would best be displayed at. The width hint and height hint arguments allow the caller to ask a control questions such as "Given a particular width, how high does the control need to be to show all of the contents?" To indicate that the caller does not wish to constrain a particular dimension, the constant SWT.DEFAULT is passed for the hint.

If the changed flag is true, it indicates that the receiver's contents have changed, therefore any caches that a layout manager containing the control may have been keeping need to be flushed. When the control is resized, the changed flag will be false, so layout manager caches can be retained.

Overrides:
computeSize in class Composite
Parameters:
wHint - the width hint (can be SWT.DEFAULT)
hHint - the height hint (can be SWT.DEFAULT)
changed - true if the control's contents have changed, and false otherwise
Returns:
the preferred size of the control.
See Also:
Layout, Control.getBorderWidth(), Control.getBounds(), Control.getSize(), Control.pack(), "computeTrim, getClientArea for controls that implement them"

deselectAll

public void deselectAll()
Deselects all selected items in the receiver.

Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getItem

public TreeItem getItem(Point point)
Returns the item at the given point in the receiver or null if no such item exists. The point is in the coordinate system of the receiver.

Parameters:
point - the point used to locate the item
Returns:
the item at the given point
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getItemCount

public int getItemCount()
Returns the number of items contained in the receiver that are direct item children of the receiver. The number that is returned is the number of roots in the tree.

Returns:
the number of items
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getItemHeight

public int getItemHeight()
Returns the height of the area which would be used to display one of the items in the tree.

Returns:
the height of one item
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getItems

public TreeItem[] getItems()
Returns the number of items contained in the receiver that are direct item children of the receiver. These are the roots of the tree.

Note: This is not the actual structure used by the receiver to maintain its list of items, so modifying the array will not affect the receiver.

Returns:
the number of items
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getParentItem

public TreeItem getParentItem()
Returns the receiver's parent item, which must be a TreeItem or null when the receiver is a root.

Returns:
the receiver's parent item
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getSelection

public TreeItem[] getSelection()
Returns an array of TreeItems that are currently selected in the receiver. An empty array indicates that no items are selected.

Note: This is not the actual structure used by the receiver to maintain its selection, so modifying the array will not affect the receiver.

Returns:
an array representing the selection
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

getSelectionCount

public int getSelectionCount()
Returns the number of selected items contained in the receiver.

Returns:
the number of selected items
Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

removeAll

public void removeAll()
Removes all of the items from the receiver.

Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

removeSelectionListener

public void removeSelectionListener(SelectionListener listener)
Removes the listener from the collection of listeners who will be notified when the receiver's selection changes.

Parameters:
listener - the listener which should no longer be notified
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
SelectionListener, addSelectionListener(org.eclipse.swt.events.SelectionListener)

removeTreeListener

public void removeTreeListener(TreeListener listener)
Removes the listener from the collection of listeners who will be notified when items in the receiver are expanded or collapsed..

Parameters:
listener - the listener which should no longer be notified
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
TreeListener, addTreeListener(org.eclipse.swt.events.TreeListener)

setInsertMark

public void setInsertMark(TreeItem item,
                          boolean before)
Display a mark indicating the point at which an item will be inserted. The drop insert item has a visual hint to show where a dragged item will be inserted when dropped on the tree.

Parameters:
item - the insert item. Null will clear the insertion mark.
Throws:
IllegalArgumentException -
  • ERROR_INVALID_ARGUMENT - if the item has been disposed
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

selectAll

public void selectAll()
Selects all the items in the receiver.

Throws:
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver

setRedraw

public void setRedraw(boolean redraw)
Description copied from class: Control
If the argument is false, causes subsequent drawing operations in the receiver to be ignored. No drawing of any kind can occur in the receiver until the flag is set to true. Graphics operations that occurred while the flag was false are lost. When the flag is set to true, the entire widget is marked as needing to be redrawn.

Note: This operation is a hint and may not be supported on some platforms or for some widgets.

Overrides:
setRedraw in class Control
Parameters:
redraw - the new redraw state
See Also:
Control.redraw(), Control.update()

setSelection

public void setSelection(TreeItem[] items)
Sets the receiver's selection to be the given array of items. The current selected is first cleared, then the new items are selected.

Parameters:
items - the array of items
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the array of items is null
  • ERROR_INVALID_ARGUMENT - if one of the item has been disposed
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
deselectAll()

showItem

public void showItem(TreeItem item)
Shows the item. If the item is already showing in the receiver, this method simply returns. Otherwise, the items are scrolled and expanded until the item is visible.

Parameters:
item - the item to be shown
Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the item is null
  • ERROR_INVALID_ARGUMENT - if the item has been disposed
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
showSelection()

showSelection

public void showSelection()
Shows the selection. If the selection is already showing in the receiver, this method simply returns. Otherwise, the items are scrolled until the selection is visible.

Throws:
IllegalArgumentException -
  • ERROR_NULL_ARGUMENT - if the listener is null
SWTException -
  • ERROR_WIDGET_DISPOSED - if the receiver has been disposed
  • ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
See Also:
showItem(TreeItem)

Eclipse Platform
2.0

Copyright (c) IBM Corp. and others 2000, 2002. All Rights Reserved.