KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > TabItem


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class represent a selectable user interface object
20  * corresponding to a tab for a page in a tab folder.
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  * <p>
28  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
29  * </p>
30  */

31
32 public class TabItem extends Item {
33     TabFolder parent;
34     Control control;
35     String JavaDoc toolTipText;
36
37 /**
38  * Constructs a new instance of this class given its parent
39  * (which must be a <code>TabFolder</code>) and a style value
40  * describing its behavior and appearance. The item is added
41  * to the end of the items maintained by its parent.
42  * <p>
43  * The style value is either one of the style constants defined in
44  * class <code>SWT</code> which is applicable to instances of this
45  * class, or must be built by <em>bitwise OR</em>'ing together
46  * (that is, using the <code>int</code> "|" operator) two or more
47  * of those <code>SWT</code> style constants. The class description
48  * lists the style constants that are applicable to the class.
49  * Style bits are also inherited from superclasses.
50  * </p>
51  *
52  * @param parent a composite control which will be the parent of the new instance (cannot be null)
53  * @param style the style of control to construct
54  *
55  * @exception IllegalArgumentException <ul>
56  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
57  * </ul>
58  * @exception SWTException <ul>
59  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
60  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
61  * </ul>
62  *
63  * @see SWT
64  * @see Widget#checkSubclass
65  * @see Widget#getStyle
66  */

67 public TabItem (TabFolder parent, int style) {
68     super (parent, style);
69     this.parent = parent;
70     parent.createItem (this, parent.getItemCount ());
71 }
72
73 /**
74  * Constructs a new instance of this class given its parent
75  * (which must be a <code>TabFolder</code>), a style value
76  * describing its behavior and appearance, and the index
77  * at which to place it in the items maintained by its parent.
78  * <p>
79  * The style value is either one of the style constants defined in
80  * class <code>SWT</code> which is applicable to instances of this
81  * class, or must be built by <em>bitwise OR</em>'ing together
82  * (that is, using the <code>int</code> "|" operator) two or more
83  * of those <code>SWT</code> style constants. The class description
84  * lists the style constants that are applicable to the class.
85  * Style bits are also inherited from superclasses.
86  * </p>
87  *
88  * @param parent a composite control which will be the parent of the new instance (cannot be null)
89  * @param style the style of control to construct
90  * @param index the zero-relative index to store the receiver in its parent
91  *
92  * @exception IllegalArgumentException <ul>
93  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
94  * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
95  * </ul>
96  * @exception SWTException <ul>
97  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
98  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
99  * </ul>
100  *
101  * @see SWT
102  * @see Widget#checkSubclass
103  * @see Widget#getStyle
104  */

105 public TabItem (TabFolder parent, int style, int index) {
106     super (parent, style);
107     this.parent = parent;
108     parent.createItem (this, index);
109 }
110
111 void _setText (int index, String JavaDoc string) {
112     /*
113     * Bug in Windows. In version 6.00 of COMCTL32.DLL, tab
114     * items with an image and a label that includes '&' cause
115     * the tab to draw incorrectly (even when doubled '&&').
116     * The image overlaps the label. The fix is to remove
117     * all '&' characters from the string.
118     */

119     if (OS.COMCTL32_MAJOR >= 6 && image != null) {
120         if (string.indexOf ('&') != -1) {
121             int length = string.length ();
122             char[] text = new char [length];
123             string.getChars ( 0, length, text, 0);
124             int i = 0, j = 0;
125             for (i=0; i<length; i++) {
126                 if (text[i] != '&') text [j++] = text [i];
127             }
128             if (j < i) string = new String JavaDoc (text, 0, j);
129         }
130     }
131     int hwnd = parent.handle;
132     int hHeap = OS.GetProcessHeap ();
133     TCHAR buffer = new TCHAR (parent.getCodePage (), string, true);
134     int byteCount = buffer.length () * TCHAR.sizeof;
135     int pszText = OS.HeapAlloc (hHeap, OS.HEAP_ZERO_MEMORY, byteCount);
136     OS.MoveMemory (pszText, buffer, byteCount);
137     TCITEM tcItem = new TCITEM ();
138     tcItem.mask = OS.TCIF_TEXT;
139     tcItem.pszText = pszText;
140     OS.SendMessage (hwnd, OS.TCM_SETITEM, index, tcItem);
141     OS.HeapFree (hHeap, 0, pszText);
142 }
143
144 protected void checkSubclass () {
145     if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
146 }
147
148 void destroyWidget () {
149     parent.destroyItem (this);
150     releaseHandle ();
151 }
152
153 /**
154  * Returns the control that is used to fill the client area of
155  * the tab folder when the user selects the tab item. If no
156  * control has been set, return <code>null</code>.
157  * <p>
158  * @return the control
159  *
160  * @exception SWTException <ul>
161  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
162  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
163  * </ul>
164  */

165 public Control getControl () {
166     checkWidget();
167     return control;
168 }
169
170 /**
171  * Returns the receiver's parent, which must be a <code>TabFolder</code>.
172  *
173  * @return the receiver's parent
174  *
175  * @exception SWTException <ul>
176  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
177  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
178  * </ul>
179  */

180 public TabFolder getParent () {
181     checkWidget();
182     return parent;
183 }
184
185 /**
186  * Returns the receiver's tool tip text, or null if it has
187  * not been set.
188  *
189  * @return the receiver's tool tip text
190  *
191  * @exception SWTException <ul>
192  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
193  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
194  * </ul>
195  */

196 public String JavaDoc getToolTipText () {
197     checkWidget();
198     return toolTipText;
199 }
200
201 void releaseHandle () {
202     super.releaseHandle ();
203     parent = null;
204 }
205
206 void releaseParent () {
207     super.releaseParent ();
208     int index = parent.indexOf (this);
209     if (index == parent.getSelectionIndex ()) {
210         if (control != null) control.setVisible (false);
211     }
212 }
213
214 void releaseWidget () {
215     super.releaseWidget ();
216     control = null;
217 }
218
219 /**
220  * Sets the control that is used to fill the client area of
221  * the tab folder when the user selects the tab item.
222  * <p>
223  * @param control the new control (or null)
224  *
225  * @exception IllegalArgumentException <ul>
226  * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
227  * <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
228  * </ul>
229  * @exception SWTException <ul>
230  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
231  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
232  * </ul>
233  */

234 public void setControl (Control control) {
235     checkWidget();
236     if (control != null) {
237         if (control.isDisposed()) error (SWT.ERROR_INVALID_ARGUMENT);
238         if (control.parent != parent) error (SWT.ERROR_INVALID_PARENT);
239     }
240     if (this.control != null && this.control.isDisposed ()) {
241         this.control = null;
242     }
243     Control oldControl = this.control, newControl = control;
244     this.control = control;
245     int index = parent.indexOf (this);
246     if (index != parent.getSelectionIndex ()) {
247         if (newControl != null) newControl.setVisible (false);
248         return;
249     }
250     if (newControl != null) {
251         newControl.setBounds (parent.getClientArea ());
252         newControl.setVisible (true);
253     }
254     if (oldControl != null) oldControl.setVisible (false);
255 }
256
257 public void setImage (Image image) {
258     checkWidget();
259     int index = parent.indexOf (this);
260     if (index == -1) return;
261     super.setImage (image);
262     /*
263     * Bug in Windows. In version 6.00 of COMCTL32.DLL, tab
264     * items with an image and a label that includes '&' cause
265     * the tab to draw incorrectly (even when doubled '&&').
266     * The image overlaps the label. The fix is to remove
267     * all '&' characters from the string and set the text
268     * whenever the image or text is changed.
269     */

270     if (OS.COMCTL32_MAJOR >= 6) {
271         if (text.indexOf ('&') != -1) _setText (index, text);
272     }
273     int hwnd = parent.handle;
274     TCITEM tcItem = new TCITEM ();
275     tcItem.mask = OS.TCIF_IMAGE;
276     tcItem.iImage = parent.imageIndex (image);
277     OS.SendMessage (hwnd, OS.TCM_SETITEM, index, tcItem);
278 }
279 /**
280  * Sets the receiver's text. The string may include
281  * the mnemonic character.
282  * </p>
283  * <p>
284  * Mnemonics are indicated by an '&amp;' that causes the next
285  * character to be the mnemonic. When the user presses a
286  * key sequence that matches the mnemonic, a selection
287  * event occurs. On most platforms, the mnemonic appears
288  * underlined but may be emphasised in a platform specific
289  * manner. The mnemonic indicator character '&amp;' can be
290  * escaped by doubling it in the string, causing a single
291  * '&amp;' to be displayed.
292  * </p>
293  *
294  * @param string the new text
295  *
296  * @exception IllegalArgumentException <ul>
297  * <li>ERROR_NULL_ARGUMENT - if the text is null</li>
298  * </ul>
299  * @exception SWTException <ul>
300  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
301  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
302  * </ul>
303  *
304  */

305 public void setText (String JavaDoc string) {
306     checkWidget();
307     if (string == null) error (SWT.ERROR_NULL_ARGUMENT);
308     if (string.equals (text)) return;
309     int index = parent.indexOf (this);
310     if (index == -1) return;
311     super.setText (string);
312     _setText (index, string);
313 }
314
315 /**
316  * Sets the receiver's tool tip text to the argument, which
317  * may be null indicating that no tool tip text should be shown.
318  *
319  * @param string the new tool tip text (or null)
320  *
321  * @exception SWTException <ul>
322  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
323  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
324  * </ul>
325  */

326 public void setToolTipText (String JavaDoc string) {
327     checkWidget();
328     toolTipText = string;
329 }
330
331 }
332
Popular Tags