1 /*******************************************************************************
2 * Copyright (c) 2000, 2015 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
22 * This class is the abstract superclass of all non-windowed
23 * user interface objects that occur within specific controls.
24 * For example, a tree will contain tree items.
26 * <dt><b>Styles:</b></dt>
28 * <dt><b>Events:</b></dt>
32 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
35 public abstract class Item extends Widget {
39 * Maximum number of characters Windows can reliably display in one line.
40 * Mac and Linux can display more but we are limited by windows here.
42 static final int TEXT_LIMIT = 8192;
44 static final String ELLIPSIS = "...";
49 * Constructs a new instance of this class given its parent
50 * and a style value describing its behavior and appearance.
51 * The item is added to the end of the items maintained by its parent.
53 * The style value is either one of the style constants defined in
54 * class <code>SWT</code> which is applicable to instances of this
55 * class, or must be built by <em>bitwise OR</em>'ing together
56 * (that is, using the <code>int</code> "|" operator) two or more
57 * of those <code>SWT</code> style constants. The class description
58 * lists the style constants that are applicable to the class.
59 * Style bits are also inherited from superclasses.
62 * @param parent a widget which will be the parent of the new instance (cannot be null)
63 * @param style the style of item to construct
65 * @exception IllegalArgumentException <ul>
66 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
68 * @exception SWTException <ul>
69 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
73 * @see Widget#getStyle
75 public Item (Widget parent, int style) {
76 super (parent, style);
81 * Constructs a new instance of this class given its parent
82 * and a style value describing its behavior and appearance,
83 * and the index at which to place it in the items maintained
86 * The style value is either one of the style constants defined in
87 * class <code>SWT</code> which is applicable to instances of this
88 * class, or must be built by <em>bitwise OR</em>'ing together
89 * (that is, using the <code>int</code> "|" operator) two or more
90 * of those <code>SWT</code> style constants. The class description
91 * lists the style constants that are applicable to the class.
92 * Style bits are also inherited from superclasses.
95 * @param parent a widget which will be the parent of the new instance (cannot be null)
96 * @param style the style of item to construct
97 * @param index the zero-relative index at which to store the receiver in its parent
99 * @exception IllegalArgumentException <ul>
100 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
101 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
103 * @exception SWTException <ul>
104 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
108 * @see Widget#getStyle
110 public Item (Widget parent, int style, int index) {
111 this (parent, style);
115 protected void checkSubclass () {
116 /* Do Nothing - Subclassing is allowed */
120 * Returns the receiver's image if it has one, or null
123 * @return the receiver's image
125 * @exception SWTException <ul>
126 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
127 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
130 public Image getImage () {
136 String getNameText () {
141 * Returns the receiver's text, which will be an empty
142 * string if it has never been set.
144 * @return the receiver's text
146 * @exception SWTException <ul>
147 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
148 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
151 public String getText () {
157 void releaseWidget () {
158 super.releaseWidget ();
164 * Sets the receiver's image to the argument, which may be
165 * null indicating that no image should be displayed.
167 * @param image the image to display on the receiver (may be null)
169 * @exception IllegalArgumentException <ul>
170 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
172 * @exception SWTException <ul>
173 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
174 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
177 public void setImage (Image image) {
179 if (image != null && image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
184 * Sets the receiver's text.
186 * Note: If control characters like '\n', '\t' etc. are used
187 * in the string, then the behavior is platform dependent.
189 * @param string the new text
191 * @exception IllegalArgumentException <ul>
192 * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
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>
199 public void setText (String string) {
201 if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
203 if ((state & HAS_AUTO_DIRECTION) != 0) {
204 updateTextDirection (AUTO_TEXT_DIRECTION);
208 boolean updateTextDirection(int textDirection) {
210 * textDirection argument passed here is either (1) AUTO_TEXT_DIRECTION, or
211 * (2) 0 (i.e. match orientation) or FLIP_TEXT_DIRECTION (mismatch orientation).
213 if (textDirection == AUTO_TEXT_DIRECTION) {
214 state |= HAS_AUTO_DIRECTION;
215 textDirection = (style ^ BidiUtil.resolveTextDirection (text)) == 0 ? 0 : SWT.FLIP_TEXT_DIRECTION;
217 state &= ~HAS_AUTO_DIRECTION;
219 if (((style & SWT.FLIP_TEXT_DIRECTION) ^ textDirection) != 0) {
220 style ^= SWT.FLIP_TEXT_DIRECTION;
223 return textDirection == AUTO_TEXT_DIRECTION;