| /******************************************************************************* |
| * Copyright (c) 2000, 2003 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Common Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/cpl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.swt.widgets; |
| |
| |
| import org.eclipse.swt.internal.carbon.OS; |
| import org.eclipse.swt.internal.carbon.Rect; |
| |
| import org.eclipse.swt.*; |
| import org.eclipse.swt.graphics.*; |
| |
| /** |
| * Instances of this class represent a selectable user interface object |
| * that represents a hierarchy of tree items in a tree widget. |
| * |
| * <dl> |
| * <dt><b>Styles:</b></dt> |
| * <dd>(none)</dd> |
| * <dt><b>Events:</b></dt> |
| * <dd>(none)</dd> |
| * </dl> |
| * <p> |
| * IMPORTANT: This class is <em>not</em> intended to be subclassed. |
| * </p> |
| */ |
| public class TreeItem extends Item { |
| Tree parent; |
| TreeItem parentItem; |
| int id, index = -1, width = -1; |
| boolean checked, grayed; |
| Color foreground, background; |
| |
| /** |
| * Constructs a new instance of this class given its parent |
| * (which must be a <code>Tree</code> or a <code>TreeItem</code>) |
| * and a style value describing its behavior and appearance. |
| * The item is added to the end of the items maintained by its parent. |
| * <p> |
| * The style value is either one of the style constants defined in |
| * class <code>SWT</code> which is applicable to instances of this |
| * class, or must be built by <em>bitwise OR</em>'ing together |
| * (that is, using the <code>int</code> "|" operator) two or more |
| * of those <code>SWT</code> style constants. The class description |
| * lists the style constants that are applicable to the class. |
| * Style bits are also inherited from superclasses. |
| * </p> |
| * |
| * @param parent a composite control which will be the parent of the new instance (cannot be null) |
| * @param style the style of control to construct |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| * |
| * @see SWT |
| * @see Widget#checkSubclass |
| * @see Widget#getStyle |
| */ |
| public TreeItem (Tree parent, int style) { |
| super (parent, style); |
| this.parent = parent; |
| parent.createItem (this, null, -1); |
| } |
| |
| /** |
| * Constructs a new instance of this class given its parent |
| * (which must be a <code>Tree</code> or a <code>TreeItem</code>), |
| * a style value describing its behavior and appearance, and the index |
| * at which to place it in the items maintained by its parent. |
| * <p> |
| * The style value is either one of the style constants defined in |
| * class <code>SWT</code> which is applicable to instances of this |
| * class, or must be built by <em>bitwise OR</em>'ing together |
| * (that is, using the <code>int</code> "|" operator) two or more |
| * of those <code>SWT</code> style constants. The class description |
| * lists the style constants that are applicable to the class. |
| * Style bits are also inherited from superclasses. |
| * </p> |
| * |
| * @param parent a composite control which will be the parent of the new instance (cannot be null) |
| * @param style the style of control to construct |
| * @param index the index to store the receiver in its parent |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| * |
| * @see SWT |
| * @see Widget#checkSubclass |
| * @see Widget#getStyle |
| */ |
| public TreeItem (Tree parent, int style, int index) { |
| super (parent, style); |
| if (index < 0) error (SWT.ERROR_INVALID_RANGE); |
| this.parent = parent; |
| parent.createItem (this, null, index); |
| } |
| |
| /** |
| * Constructs a new instance of this class given its parent |
| * (which must be a <code>Tree</code> or a <code>TreeItem</code>) |
| * and a style value describing its behavior and appearance. |
| * The item is added to the end of the items maintained by its parent. |
| * <p> |
| * The style value is either one of the style constants defined in |
| * class <code>SWT</code> which is applicable to instances of this |
| * class, or must be built by <em>bitwise OR</em>'ing together |
| * (that is, using the <code>int</code> "|" operator) two or more |
| * of those <code>SWT</code> style constants. The class description |
| * lists the style constants that are applicable to the class. |
| * Style bits are also inherited from superclasses. |
| * </p> |
| * |
| * @param parentItem a composite control which will be the parent of the new instance (cannot be null) |
| * @param style the style of control to construct |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| * |
| * @see SWT |
| * @see Widget#checkSubclass |
| * @see Widget#getStyle |
| */ |
| public TreeItem (TreeItem parentItem, int style) { |
| super (checkNull (parentItem).parent, style); |
| parent = parentItem.parent; |
| this.parentItem = parentItem; |
| parent.createItem (this, parentItem, -1); |
| } |
| |
| /** |
| * Constructs a new instance of this class given its parent |
| * (which must be a <code>Tree</code> or a <code>TreeItem</code>), |
| * a style value describing its behavior and appearance, and the index |
| * at which to place it in the items maintained by its parent. |
| * <p> |
| * The style value is either one of the style constants defined in |
| * class <code>SWT</code> which is applicable to instances of this |
| * class, or must be built by <em>bitwise OR</em>'ing together |
| * (that is, using the <code>int</code> "|" operator) two or more |
| * of those <code>SWT</code> style constants. The class description |
| * lists the style constants that are applicable to the class. |
| * Style bits are also inherited from superclasses. |
| * </p> |
| * |
| * @param parentItem a composite control which will be the parent of the new instance (cannot be null) |
| * @param style the style of control to construct |
| * @param index the index to store the receiver in its parent |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> |
| * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> |
| * </ul> |
| * |
| * @see SWT |
| * @see Widget#checkSubclass |
| * @see Widget#getStyle |
| */ |
| public TreeItem (TreeItem parentItem, int style, int index) { |
| super (checkNull (parentItem).parent, style); |
| if (index < 0) error (SWT.ERROR_INVALID_RANGE); |
| parent = parentItem.parent; |
| this.parentItem = parentItem; |
| parent.createItem (this, parentItem, index); |
| } |
| |
| static TreeItem checkNull (TreeItem item) { |
| if (item == null) SWT.error (SWT.ERROR_NULL_ARGUMENT); |
| return item; |
| } |
| |
| int calculateWidth (GC gc) { |
| if (width != -1) return width; |
| Image image = getImage (); |
| String text = getText (); |
| if (image != null) width = image.getBounds ().width + 2; |
| if (text != null && text.length () > 0) width += gc.stringExtent (text).x; |
| return width; |
| } |
| |
| protected void checkSubclass () { |
| if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); |
| } |
| |
| /** |
| * Returns the receiver's background color. |
| * |
| * @return the background color |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| * |
| * @since 2.0 |
| * |
| */ |
| public Color getBackground () { |
| checkWidget (); |
| return background != null ? background : parent.getBackground (); |
| } |
| |
| /** |
| * Returns a rectangle describing the receiver's size and location |
| * relative to its parent. |
| * |
| * @return the receiver's bounding rectangle |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public Rectangle getBounds () { |
| checkWidget (); |
| Rect rect = new Rect(); |
| if (OS.GetDataBrowserItemPartBounds (parent.handle, id, Tree.COLUMN_ID, OS.kDataBrowserPropertyContentPart, rect) != OS.noErr) { |
| return new Rectangle (0, 0, 0, 0); |
| } |
| int x = rect.left, y = rect.top; |
| int width = 0; |
| if (image != null) { |
| Rectangle bounds = image.getBounds (); |
| width += bounds.width + 2; |
| } |
| GC gc = new GC (parent); |
| Point extent = gc.stringExtent (text); |
| gc.dispose (); |
| width += extent.x; |
| int height = rect.bottom - rect.top; |
| OS.GetControlBounds (parent.handle, rect); |
| x -= rect.left; |
| y -= rect.top; |
| return new Rectangle (x, y, width, height); |
| } |
| |
| /** |
| * Returns <code>true</code> if the receiver is checked, |
| * and false otherwise. When the parent does not have |
| * the <code>CHECK style, return false. |
| * <p> |
| * |
| * @return the checked state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public boolean getChecked () { |
| checkWidget (); |
| if ((parent.style & SWT.CHECK) == 0) return false; |
| return checked; |
| } |
| |
| /** |
| * Returns <code>true</code> if the receiver is expanded, |
| * and false otherwise. |
| * <p> |
| * |
| * @return the expanded state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public boolean getExpanded () { |
| checkWidget (); |
| int [] state = new int [1]; |
| OS.GetDataBrowserItemState (parent.handle, id, state); |
| return (state [0] & OS.kDataBrowserContainerIsOpen) != 0; |
| } |
| |
| /** |
| * Returns the foreground color that the receiver will use to draw. |
| * |
| * @return the receiver's foreground color |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| * |
| * @since 2.0 |
| * |
| */ |
| public Color getForeground () { |
| checkWidget (); |
| return foreground != null ? foreground : parent.getForeground (); |
| } |
| |
| /** |
| * Returns <code>true</code> if the receiver is grayed, |
| * and false otherwise. When the parent does not have |
| * the <code>CHECK style, return false. |
| * <p> |
| * |
| * @return the grayed state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public boolean getGrayed () { |
| checkWidget (); |
| if ((parent.style & SWT.CHECK) == 0) return false; |
| return grayed; |
| } |
| |
| /** |
| * Returns the number of items contained in the receiver |
| * that are direct item children of the receiver. |
| * |
| * @return the number of items |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public int getItemCount () { |
| checkWidget (); |
| return parent.getItemCount (this); |
| } |
| |
| /** |
| * Returns an array of <code>TreeItem</code>s which are the |
| * direct item children of the receiver. |
| * <p> |
| * 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. |
| * </p> |
| * |
| * @return the receiver's items |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public TreeItem [] getItems () { |
| checkWidget (); |
| return parent.getItems (this); |
| } |
| |
| /** |
| * Returns the receiver's parent, which must be a <code>Tree</code>. |
| * |
| * @return the receiver's parent |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public Tree getParent () { |
| checkWidget (); |
| return parent; |
| } |
| |
| /** |
| * Returns the receiver's parent item, which must be a |
| * <code>TreeItem</code> or null when the receiver is a |
| * root. |
| * |
| * @return the receiver's parent item |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public TreeItem getParentItem () { |
| checkWidget (); |
| return parentItem; |
| } |
| |
| void redraw () { |
| int parentID = parentItem == null ? OS.kDataBrowserNoItem : parentItem.id; |
| OS.UpdateDataBrowserItems (parent.handle, parentID, 1, new int[] {id}, OS.kDataBrowserItemNoProperty, OS.kDataBrowserNoItem); |
| } |
| |
| void releaseChild () { |
| super.releaseChild (); |
| parent.destroyItem (this); |
| } |
| |
| void releaseWidget () { |
| super.releaseWidget (); |
| background = foreground = null; |
| parentItem = null; |
| parent = null; |
| id = 0; |
| index = -1; |
| } |
| |
| /** |
| * Sets the receiver's background color to the color specified |
| * by the argument, or to the default system color for the item |
| * if the argument is null. |
| * |
| * @param color the new color (or null) |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| * |
| * @since 2.0 |
| * |
| */ |
| public void setBackground (Color color) { |
| checkWidget (); |
| if (color != null && color.isDisposed ()) { |
| SWT.error (SWT.ERROR_INVALID_ARGUMENT); |
| } |
| background = color; |
| redraw (); |
| } |
| |
| /** |
| * Sets the checked state of the receiver. |
| * <p> |
| * |
| * @param checked the new checked state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public void setChecked (boolean checked) { |
| checkWidget (); |
| if ((parent.style & SWT.CHECK) == 0) return; |
| this.checked = checked; |
| redraw (); |
| } |
| |
| /** |
| * Sets the expanded state of the receiver. |
| * <p> |
| * |
| * @param expanded the new expanded state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public void setExpanded (boolean expanded) { |
| checkWidget (); |
| parent.ignoreExpand = true; |
| if (expanded) { |
| OS.OpenDataBrowserContainer (parent.handle, id); |
| } else { |
| OS.CloseDataBrowserContainer (parent.handle, id); |
| } |
| parent.ignoreExpand = false; |
| } |
| |
| /** |
| * Sets the receiver's foreground color to the color specified |
| * by the argument, or to the default system color for the item |
| * if the argument is null. |
| * |
| * @param color the new color (or null) |
| * |
| * @since 2.0 |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li> |
| * </ul> |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| * |
| * @since 2.0 |
| * |
| */ |
| public void setForeground (Color color) { |
| checkWidget (); |
| if (color != null && color.isDisposed ()) { |
| SWT.error (SWT.ERROR_INVALID_ARGUMENT); |
| } |
| foreground = color; |
| redraw (); |
| } |
| |
| /** |
| * Sets the grayed state of the receiver. |
| * <p> |
| * |
| * @param checked the new grayed state |
| * |
| * @exception SWTException <ul> |
| * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> |
| * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> |
| * </ul> |
| */ |
| public void setGrayed (boolean grayed) { |
| checkWidget (); |
| if ((parent.style & SWT.CHECK) == 0) return; |
| this.grayed = grayed; |
| redraw (); |
| } |
| |
| public void setImage (Image image) { |
| checkWidget (); |
| super.setImage (image); |
| width = -1; |
| parent.setScrollWidth (this); |
| redraw (); |
| } |
| |
| public void setText (String string) { |
| checkWidget (); |
| if (string == null) error (SWT.ERROR_NULL_ARGUMENT); |
| super.setText (string); |
| width = -1; |
| parent.setScrollWidth (this); |
| redraw (); |
| } |
| |
| } |