1 /*******************************************************************************
2 * Copyright (c) 2000, 2012 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.custom;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.widgets.*;
22 * Instances of this class represent a selectable user interface object
23 * that represent a page in a notebook widget.
26 * <dt><b>Styles:</b></dt>
28 * <dt><b>Events:</b></dt>
32 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
35 * @see <a href="http://www.eclipse.org/swt/snippets/#ctabfolder">CTabFolder, CTabItem snippets</a>
36 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
37 * @noextend This class is not intended to be subclassed by clients.
39 public class CTabItem extends Item {
41 int x,y,width,height = 0;
42 Control control; // the tab page
46 int shortenedTextWidth;
52 Rectangle closeRect = new Rectangle(0, 0, 0, 0);
53 int closeImageState = SWT.BACKGROUND;
55 boolean showClose = false;
56 boolean showing = false;
59 * Constructs a new instance of this class given its parent
60 * (which must be a <code>CTabFolder</code>) and a style value
61 * describing its behavior and appearance. The item is added
62 * to the end of the items maintained by its parent.
64 * The style value is either one of the style constants defined in
65 * class <code>SWT</code> which is applicable to instances of this
66 * class, or must be built by <em>bitwise OR</em>'ing together
67 * (that is, using the <code>int</code> "|" operator) two or more
68 * of those <code>SWT</code> style constants. The class description
69 * lists the style constants that are applicable to the class.
70 * Style bits are also inherited from superclasses.
73 * @param parent a CTabFolder which will be the parent of the new instance (cannot be null)
74 * @param style the style of control to construct
76 * @exception IllegalArgumentException <ul>
77 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
79 * @exception SWTException <ul>
80 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
84 * @see Widget#getStyle()
86 public CTabItem (CTabFolder parent, int style) {
87 this(parent, style, parent.getItemCount());
90 * Constructs a new instance of this class given its parent
91 * (which must be a <code>CTabFolder</code>), a style value
92 * describing its behavior and appearance, and the index
93 * at which to place it in the items maintained by its parent.
95 * The style value is either one of the style constants defined in
96 * class <code>SWT</code> which is applicable to instances of this
97 * class, or must be built by <em>bitwise OR</em>'ing together
98 * (that is, using the <code>int</code> "|" operator) two or more
99 * of those <code>SWT</code> style constants. The class description
100 * lists the style constants that are applicable to the class.
101 * Style bits are also inherited from superclasses.
104 * @param parent a CTabFolder which will be the parent of the new instance (cannot be null)
105 * @param style the style of control to construct
106 * @param index the zero-relative index to store the receiver in its parent
108 * @exception IllegalArgumentException <ul>
109 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
110 * <li>ERROR_INVALID_RANGE - if the index is not between 0 and the number of elements in the parent (inclusive)</li>
112 * @exception SWTException <ul>
113 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
117 * @see Widget#getStyle()
119 public CTabItem (CTabFolder parent, int style, int index) {
120 super (parent, style);
121 showClose = (style & SWT.CLOSE) != 0;
122 parent.createItem (this, index);
127 public void dispose() {
128 if (isDisposed ()) return;
129 //if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS);
130 parent.destroyItem(this);
135 shortenedText = null;
140 * Returns a rectangle describing the receiver's size and location
141 * relative to its parent.
143 * @return the receiver's bounding column rectangle
145 * @exception SWTException <ul>
146 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
147 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
150 public Rectangle getBounds () {
152 * This call is intentionally commented out, to allow this getter method to be
153 * called from a thread which is different from one that created the widget.
157 return new Rectangle(x, y, width, height);
160 * Gets the control that is displayed in the content area of the tab item.
162 * @return the control
164 * @exception SWTException <ul>
165 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
166 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
169 public Control getControl () {
174 * Get the image displayed in the tab if the tab is disabled.
176 * @return the disabled image or null
178 * @exception SWTException <ul>
179 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
180 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
183 * @deprecated the disabled image is not used
186 public Image getDisabledImage(){
188 return disabledImage;
191 * Returns the font that the receiver will use to paint textual information.
193 * @return the receiver's font
195 * @exception SWTException <ul>
196 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
197 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
202 public Font getFont() {
204 if (font != null) return font;
205 return parent.getFont();
208 * Returns the receiver's parent, which must be a <code>CTabFolder</code>.
210 * @return the receiver's parent
212 * @exception SWTException <ul>
213 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
214 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
217 public CTabFolder getParent () {
219 * This call is intentionally commented out, to allow this getter method to be
220 * called from a thread which is different from one that created the widget.
226 * Returns <code>true</code> to indicate that the receiver's close button should be shown.
227 * Otherwise return <code>false</code>. The initial value is defined by the style (SWT.CLOSE)
228 * that was used to create the receiver.
230 * @return <code>true</code> if the close button should be shown
232 * @exception SWTException <ul>
233 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
234 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
239 public boolean getShowClose() {
244 * Returns the receiver's tool tip text, or null if it has
247 * @return the receiver's tool tip text
249 * @exception SWTException <ul>
250 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
251 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
254 public String getToolTipText () {
256 if (toolTipText == null && shortenedText != null) {
257 String text = getText();
258 if (!shortenedText.equals(text)) return text;
263 * Returns <code>true</code> if the item will be rendered in the visible area of the CTabFolder. Returns false otherwise.
265 * @return <code>true</code> if the item will be rendered in the visible area of the CTabFolder. Returns false otherwise.
267 * @exception SWTException <ul>
268 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
269 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
274 public boolean isShowing () {
280 * Sets the control that is used to fill the client area of
281 * the tab folder when the user selects the tab item.
283 * @param control the new control (or null)
285 * @exception IllegalArgumentException <ul>
286 * <li>ERROR_INVALID_ARGUMENT - if the control has been disposed</li>
287 * <li>ERROR_INVALID_PARENT - if the control is not in the same widget tree</li>
289 * @exception SWTException <ul>
290 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
291 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
294 public void setControl (Control control) {
296 if (control != null) {
297 if (control.isDisposed()) SWT.error (SWT.ERROR_INVALID_ARGUMENT);
298 if (control.getParent() != parent) SWT.error (SWT.ERROR_INVALID_PARENT);
300 if (this.control != null && !this.control.isDisposed()) {
301 this.control.setVisible(false);
303 this.control = control;
304 if (this.control != null) {
305 int index = parent.indexOf (this);
306 if (index == parent.getSelectionIndex ()){
307 this.control.setBounds(parent.getClientArea ());
308 this.control.setVisible(true);
310 int selectedIndex = parent.getSelectionIndex();
311 Control selectedControl = null;
312 if (selectedIndex != -1) {
313 selectedControl = parent.getItem(selectedIndex).control;
315 if (this.control != selectedControl) {
316 this.control.setVisible(false);
322 * Sets the image that is displayed if the tab item is disabled.
323 * Null will clear the image.
325 * @param image the image to be displayed when the item is disabled or null
327 * @exception SWTException <ul>
328 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
329 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
332 * @deprecated This image is not used
335 public void setDisabledImage (Image image) {
337 if (image != null && image.isDisposed ()) {
338 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
340 this.disabledImage = image;
342 boolean setFocus () {
343 return control != null && !control.isDisposed() && control.setFocus ();
346 * Sets the font that the receiver will use to paint textual information
347 * for this item to the font specified by the argument, or to the default font
348 * for that kind of control if the argument is null.
350 * @param font the new font (or null)
352 * @exception IllegalArgumentException <ul>
353 * <li>ERROR_INVALID_ARGUMENT - if the argument has been disposed</li>
355 * @exception SWTException <ul>
356 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
357 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
362 public void setFont (Font font){
364 if (font != null && font.isDisposed ()) {
365 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
367 if (font == null && this.font == null) return;
368 if (font != null && font.equals(this.font)) return;
370 parent.updateFolder(CTabFolder.UPDATE_TAB_HEIGHT | CTabFolder.REDRAW_TABS);
373 public void setImage (Image image) {
375 if (image != null && image.isDisposed ()) {
376 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
378 Image oldImage = getImage();
379 if (image == null && oldImage == null) return;
380 if (image != null && image.equals(oldImage)) return;
381 super.setImage(image);
382 parent.updateFolder(CTabFolder.UPDATE_TAB_HEIGHT | CTabFolder.REDRAW_TABS);
385 * Sets to <code>true</code> to indicate that the receiver's close button should be shown.
386 * If the parent (CTabFolder) was created with SWT.CLOSE style, changing this value has
389 * @param close the new state of the close button
391 * @exception SWTException <ul>
392 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
393 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
398 public void setShowClose(boolean close) {
400 if (showClose == close) return;
402 parent.updateFolder(CTabFolder.REDRAW_TABS);
405 * Sets the text to display on the tab.
406 * A carriage return '\n' allows to display multi line text.
408 * @param string the tab name
410 * @exception SWTException <ul>
411 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
412 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
416 public void setText (String string) {
418 if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
419 if (string.equals(getText())) return;
420 super.setText(string);
421 shortenedText = null;
422 shortenedTextWidth = 0;
423 parent.updateFolder(CTabFolder.UPDATE_TAB_HEIGHT | CTabFolder.REDRAW_TABS);
426 * Sets the receiver's tool tip text to the argument, which
427 * may be null indicating that the default tool tip for the
428 * control will be shown. For a control that has a default
429 * tool tip, such as the Tree control on Windows, setting
430 * the tool tip text to an empty string replaces the default,
431 * causing no tool tip text to be shown.
433 * The mnemonic indicator (character '&') is not displayed in a tool tip.
434 * To display a single '&' in the tool tip, the character '&' can be
435 * escaped by doubling it in the string.
438 * @param string the new tool tip text (or null)
440 * @exception SWTException <ul>
441 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
442 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
445 public void setToolTipText (String string) {
447 toolTipText = string;