Source code: org/eclipse/swt/widgets/Item.java
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16
17 /**
18 * This class is the abstract superclass of all non-windowed
19 * user interface objects that occur within specific controls.
20 * For example, a tree will contain tree items.
21 * <dl>
22 * <dt><b>Styles:</b></dt>
23 * <dd>(none)</dd>
24 * <dt><b>Events:</b></dt>
25 * <dd>(none)</dd>
26 * </dl>
27 */
28
29 public abstract class Item extends Widget {
30 String text;
31 Image image;
32
33 /**
34 * Constructs a new instance of this class given its parent
35 * and a style value describing its behavior and appearance.
36 * The item is added to the end of the items maintained by its parent.
37 * <p>
38 * The style value is either one of the style constants defined in
39 * class <code>SWT</code> which is applicable to instances of this
40 * class, or must be built by <em>bitwise OR</em>'ing together
41 * (that is, using the <code>int</code> "|" operator) two or more
42 * of those <code>SWT</code> style constants. The class description
43 * lists the style constants that are applicable to the class.
44 * Style bits are also inherited from superclasses.
45 * </p>
46 *
47 * @param parent a widget which will be the parent of the new instance (cannot be null)
48 * @param style the style of item to construct
49 *
50 * @exception IllegalArgumentException <ul>
51 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
52 * </ul>
53 * @exception SWTException <ul>
54 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
55 * </ul>
56 *
57 * @see SWT
58 * @see #getStyle
59 */
60 public Item (Widget parent, int style) {
61 super (parent, style);
62 text = "";
63 }
64
65 /**
66 * Constructs a new instance of this class given its parent
67 * and a style value describing its behavior and appearance,
68 * and the index at which to place it in the items maintained
69 * by its parent.
70 * <p>
71 * The style value is either one of the style constants defined in
72 * class <code>SWT</code> which is applicable to instances of this
73 * class, or must be built by <em>bitwise OR</em>'ing together
74 * (that is, using the <code>int</code> "|" operator) two or more
75 * of those <code>SWT</code> style constants. The class description
76 * lists the style constants that are applicable to the class.
77 * Style bits are also inherited from superclasses.
78 * </p>
79 *
80 * @param parent a widget which will be the parent of the new instance (cannot be null)
81 * @param style the style of item to construct
82 * @param index the index at which to store the receiver in its parent
83 *
84 * @exception IllegalArgumentException <ul>
85 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
86 * </ul>
87 * @exception SWTException <ul>
88 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
89 * </ul>
90 *
91 * @see SWT
92 * @see #getStyle
93 */
94 public Item (Widget parent, int style, int index) {
95 this (parent, style);
96 }
97
98 protected void checkSubclass () {
99 /* Do Nothing - Subclassing is allowed */
100 }
101
102 /**
103 * Returns the receiver's image if it has one, or null
104 * if it does not.
105 *
106 * @return the receiver's image
107 *
108 * @exception SWTException <ul>
109 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
110 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
111 * </ul>
112 */
113 public Image getImage () {
114 checkWidget ();
115 return image;
116 }
117
118 String getNameText () {
119 return getText ();
120 }
121
122 /**
123 * Returns the receiver's text, which will be an empty
124 * string if it has never been set.
125 *
126 * @return the receiver's text
127 *
128 * @exception SWTException <ul>
129 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
130 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
131 * </ul>
132 */
133 public String getText () {
134 checkWidget();
135 return text;
136 }
137
138 void releaseWidget () {
139 super.releaseWidget ();
140 text = null;
141 image = null;
142 }
143 /**
144 * Sets the receiver's image to the argument, which may be
145 * null indicating that no image should be displayed.
146 *
147 * @param image the image to display on the receiver (may be null)
148 *
149 * @exception IllegalArgumentException <ul>
150 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
151 * </ul>
152 * @exception SWTException <ul>
153 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
154 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
155 * </ul>
156 */
157 public void setImage (Image image) {
158 checkWidget ();
159 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
160 this.image = image;
161 }
162
163 /**
164 * Sets the receiver's text.
165 *
166 * @param string the new text
167 *
168 * @exception IllegalArgumentException <ul>
169 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
170 * </ul>
171 * @exception SWTException <ul>
172 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
173 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
174 * </ul>
175 */
176 public void setText (String string) {
177 checkWidget ();
178 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
179 text = string;
180 }
181
182 }