2015-05-05 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / libjava / classpath / javax / swing / JTabbedPane.java
blob18055e8a9a8f5dfcbbc1c41183a6b2104cf1ff65
1 /* JTabbedPane.java --
2 Copyright (C) 2002, 2004, 2005, 2006, Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.swing;
41 import gnu.java.lang.CPStringBuilder;
43 import java.awt.Color;
44 import java.awt.Component;
45 import java.awt.Point;
46 import java.awt.Rectangle;
47 import java.awt.event.MouseEvent;
48 import java.io.Serializable;
49 import java.util.Locale;
50 import java.util.Vector;
52 import javax.accessibility.Accessible;
53 import javax.accessibility.AccessibleContext;
54 import javax.accessibility.AccessibleRole;
55 import javax.accessibility.AccessibleSelection;
56 import javax.accessibility.AccessibleState;
57 import javax.accessibility.AccessibleStateSet;
58 import javax.swing.event.ChangeEvent;
59 import javax.swing.event.ChangeListener;
60 import javax.swing.plaf.TabbedPaneUI;
61 import javax.swing.plaf.UIResource;
63 /**
64 * This is a container for components where only one component is displayed at
65 * a given time and the displayed component can be switched by clicking on
66 * tabs.
68 * <p>
69 * Tabs can be oriented in several ways. They can be above, below, left and
70 * right of the component. Tabs can either wrap around (by creating multiple
71 * rows of tabs) or they can be scrolled (where only a subset of the tabs
72 * can be seen at once). More tabs can be added by calling the
73 * add/addTab/insertTab methods.
74 * </p>
76 public class JTabbedPane extends JComponent implements Serializable,
77 Accessible,
78 SwingConstants
80 /**
81 * Accessibility support for <code>JTabbedPane</code>.
83 protected class AccessibleJTabbedPane extends JComponent.AccessibleJComponent
84 implements AccessibleSelection, ChangeListener
86 /**
87 * The serialization UID.
89 private static final long serialVersionUID = 7610530885966830483L;
91 /**
92 * Creates a new AccessibleJTabbedPane object.
94 public AccessibleJTabbedPane()
96 super();
99 /**
100 * Receives notification when the selection state of the
101 * <code>JTabbedPane</code> changes and fires appropriate property change
102 * events to interested listeners.
104 * @param e the change event describing the change
106 public void stateChanged(ChangeEvent e)
108 // I couldn't figure out what else should be done here.
109 Object source = e.getSource();
110 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
111 null, source);
115 * Returns the accessible role of the <code>JTabbedPane</code>, which is
116 * {@link AccessibleRole#PAGE_TAB_LIST}.
118 * @return the accessible role of the <code>JTabbedPane</code>
120 public AccessibleRole getAccessibleRole()
122 return AccessibleRole.PAGE_TAB_LIST;
126 * Returns the number of accessible child components of the
127 * <code>JTabbedPane</code>.
129 * @return the number of accessible child components of the
130 * <code>JTabbedPane</code>
132 public int getAccessibleChildrenCount()
134 return getTabCount();
138 * Returns the accessible child component at the specified index.
140 * @param i the index of the child component to fetch
142 * @return the accessible child component at the specified index
144 public Accessible getAccessibleChild(int i)
146 // Testing shows that the reference implementation returns instances
147 // of page here.
148 Accessible child = null;
149 if (i >= 0 && i < tabs.size())
150 child = (Page) tabs.get(i);
151 return child;
155 * Returns the current selection state of the <code>JTabbedPane</code>
156 * as AccessibleSelection object.
158 * @return the current selection state of the <code>JTabbedPane</code>
160 public AccessibleSelection getAccessibleSelection()
162 return this;
166 * Returns the accessible child component at the specified coordinates.
167 * If there is no child component at this location, then return the
168 * currently selected tab.
170 * @param p the coordinates at which to look up the child component
172 * @return the accessible child component at the specified coordinates or
173 * the currently selected tab if there is no child component at
174 * this location
176 public Accessible getAccessibleAt(Point p)
178 int tabIndex = indexAtLocation(p.x, p.y);
179 if (tabIndex >= 0)
180 return getAccessibleChild(tabIndex);
181 else
182 return getAccessibleSelection(0);
186 * Returns the number of selected child components of the
187 * <code>JTabbedPane</code>. The reference implementation appears
188 * to return <code>1</code> always and we do the same.
190 * @return <code>1</code>
192 public int getAccessibleSelectionCount()
194 return 1;
198 * Returns the selected tab, or <code>null</code> if there is no
199 * selection.
201 * @param i the selection index (ignored here).
203 * @return The selected tab, or <code>null</code>.
205 public Accessible getAccessibleSelection(int i)
207 Accessible result = null;
208 int selected = getSelectedIndex();
209 if (selected >= 0)
210 result = (Page) tabs.get(selected);
211 return result;
215 * Returns <code>true</code> if the specified child is selected,
216 * and <code>false</code> otherwise.
218 * @param i the child index.
220 * @return A boolean.
222 public boolean isAccessibleChildSelected(int i)
224 return i == getSelectedIndex();
228 * Selects the specified tab.
230 * @param i the index of the item to select.
232 public void addAccessibleSelection(int i)
234 setSelectedIndex(i);
238 * Does nothing - it makes no sense to remove a selection for a
239 * tabbed pane, since one tab must always be selected.
241 * @param i the item index.
243 * @see #addAccessibleSelection(int)
245 public void removeAccessibleSelection(int i)
247 // do nothing
251 * Does nothing - it makes no sense to clear the selection for
252 * a tabbed pane, since one tab must always be selected.
254 * @see #addAccessibleSelection(int)
256 public void clearAccessibleSelection()
258 // do nothing
262 * Does nothing - it makes no sense to select all for a tabbed
263 * pane, since only one tab can be selected at a time.
265 * @see #addAccessibleSelection(int)
267 public void selectAllAccessibleSelection()
269 // do nothing
274 * A helper class that listens for changes to the model.
276 protected class ModelListener implements ChangeListener, Serializable
278 private static final long serialVersionUID = 497359819958114132L;
281 * Creates a new ModelListener object.
283 protected ModelListener()
285 // Nothing to do here.
289 * This method is called whenever the model is changed.
291 * @param e The ChangeEvent that is passed from the model.
293 public void stateChanged(ChangeEvent e)
295 // Propagate to our listeners.
296 fireStateChanged();
301 * A private class that holds all the information for each tab.
303 private class Page
304 extends AccessibleContext
305 implements Accessible
307 /** The tooltip string. */
308 private String tip;
310 /** The component associated with the tab. */
311 private Component component;
313 /** The active icon associated with the tab. */
314 private transient Icon icon;
316 /** The disabled icon associated with the tab. */
317 private transient Icon disabledIcon;
319 /** The tab's enabled status. */
320 private transient boolean enabled = true;
322 /** The string painted on the tab. */
323 private transient String title;
325 /** The background color of the tab. */
326 private transient Color bg;
328 /** The foreground color of the tab. */
329 private transient Color fg;
331 /** The mnemonic associated with the tab. */
332 private transient int mnemonicKey;
334 /** The index of the underlined character in the string. */
335 private transient int underlinedChar = -1;
338 * Creates a new data storage for the tab.
340 * @param title The string displayed on the tab.
341 * @param icon The active icon displayed on the tab.
342 * @param component The component associated with the tab.
343 * @param tip The tooltip associated with the tab.
345 protected Page(String title, Icon icon, Component component, String tip)
347 this.title = title;
348 this.icon = icon;
349 this.component = component;
350 this.tip = tip;
354 * This method returns the component associated with the tab.
356 * @return The component associated with the tab.
358 public Component getComponent()
360 return component;
364 * This method sets the component associated with the tab.
366 * @param c The component associated with the tab.
368 public void setComponent(Component c)
370 int i = indexOfComponent(component);
371 insertTab(title, icon, c, tip, i);
372 component = c;
373 removeTabAt(i);
377 * This method returns the tooltip string.
379 * @return The tooltip string.
381 public String getTip()
383 return tip;
387 * This method sets the tooltip string.
389 * @param tip The tooltip string.
391 public void setTip(String tip)
393 this.tip = tip;
397 * This method returns the background color.
399 * @return The background color.
401 public Color getBackground()
403 Color background;
404 if (bg == null)
405 background = JTabbedPane.this.getBackground();
406 else
407 background = bg;
408 return background;
412 * This method sets the background color.
414 * @param background The background color.
416 public void setBackground(Color background)
418 bg = background;
422 * This method returns the foreground color.
424 * @return The foreground color.
426 public Color getForeground()
428 Color foreground;
429 if (fg == null)
430 foreground = JTabbedPane.this.getForeground();
431 else
432 foreground = fg;
433 return foreground;
437 * This method sets the foreground color.
439 * @param foreground The foreground color.
441 public void setForeground(Color foreground)
443 fg = foreground;
447 * This method returns the title associated with the tab.
449 * @return The title of the tab.
451 public String getTitle()
453 return title;
456 private static final long serialVersionUID = 1614381073220130939L;
459 * This method sets the title of the tab.
461 * @param text The title of the tab.
463 public void setTitle(String text)
465 title = text;
466 if (title != null && title.length() <= underlinedChar)
467 setDisplayedMnemonicIndex(title.length() - 1);
471 * This method returns the active icon.
473 * @return The active icon.
475 public Icon getIcon()
477 return icon;
481 * This method sets the active icon.
483 * @param icon The active icon.
485 public void setIcon(Icon icon)
487 this.icon = icon;
491 * This method returns the disabled icon.
493 * @return The disabled icon.
495 public Icon getDisabledIcon()
497 if (disabledIcon == null && icon instanceof ImageIcon)
498 setDisabledIcon(icon);
499 return disabledIcon;
503 * This method sets the disabled icon.
505 * @param disabledIcon The disabled icon.
507 public void setDisabledIcon(Icon disabledIcon)
509 this.disabledIcon = disabledIcon;
513 * This method returns whether the tab is enabled.
515 * @return Whether the tab is enabled.
517 public boolean isEnabled()
519 return enabled;
523 * This method sets whether the tab is enabled.
525 * @param enabled Whether this tab is enabled.
527 public void setEnabled(boolean enabled)
529 this.enabled = enabled;
533 * This method returns the mnemonic.
535 * @return The mnemonic.
537 public int getMnemonic()
539 return mnemonicKey;
543 * This method sets the mnemonic. If the title is set, it will update the
544 * mnemonicIndex.
546 * @param key The mnemonic.
548 public void setMnemonic(int key)
550 setMnemonic((char) key);
554 * This method sets the mnemonic. If the title is set, it will update the
555 * mnemonicIndex.
557 * @param aChar The mnemonic.
559 public void setMnemonic(char aChar)
561 mnemonicKey = aChar;
562 if (title != null)
563 setDisplayedMnemonicIndex(title.indexOf(mnemonicKey));
567 * This method returns the mnemonicIndex.
569 * @return The mnemonicIndex.
571 public int getDisplayedMnemonicIndex()
573 return underlinedChar;
577 * This method sets the mnemonicIndex.
579 * @param index The mnemonicIndex.
581 * @throws IllegalArgumentException If index less than -1 || index greater
582 * or equal to title.length.
584 public void setDisplayedMnemonicIndex(int index)
585 throws IllegalArgumentException
587 if (index < -1 || title != null && index >= title.length())
588 throw new IllegalArgumentException();
590 if (title == null || mnemonicKey == 0 || (index > -1 && title.charAt(index) != mnemonicKey))
591 index = -1;
593 underlinedChar = index;
597 * Returns the accessible context, which is this object itself.
599 * @return the accessible context, which is this object itself
601 public AccessibleContext getAccessibleContext()
603 return this;
607 * Returns the accessible name for this tab.
609 * @return The accessible name.
611 public String getAccessibleName()
613 if (accessibleName != null)
614 return accessibleName;
615 else
616 return title;
620 * Returns the accessible role of this tab, which is always
621 * {@link AccessibleRole#PAGE_TAB}.
623 * @return the accessible role of this tab
625 public AccessibleRole getAccessibleRole()
627 return AccessibleRole.PAGE_TAB;
631 * Returns the accessible state set of this object.
633 * @return the accessible state set of this object
635 public AccessibleStateSet getAccessibleStateSet()
637 AccessibleContext parentCtx = JTabbedPane.this.getAccessibleContext();
638 AccessibleStateSet state = parentCtx.getAccessibleStateSet();
639 state.add(AccessibleState.SELECTABLE);
640 if (component == getSelectedComponent())
641 state.add(AccessibleState.SELECTED);
642 return state;
646 * Returns the index of this tab inside its parent.
648 * @return the index of this tab inside its parent
650 public int getAccessibleIndexInParent()
652 // TODO: Not sure if the title is unambiguous, but I can't figure
653 // another way of doing this.
654 return indexOfTab(title);
658 * Returns the number of accessible children, which is always one (the
659 * component of this tab).
661 * @return the number of accessible children
663 public int getAccessibleChildrenCount()
665 return 1;
669 * Returns the accessible child of this tab, which is the component
670 * displayed by the tab.
672 * @return the accessible child of this tab
674 public Accessible getAccessibleChild(int i)
676 // A quick test shows that this method always returns the component
677 // displayed by the tab, regardless of the index.
678 return (Accessible) component;
682 * Returns the locale of this accessible object.
684 * @return the locale of this accessible object
686 public Locale getLocale()
688 // TODO: Is this ok?
689 return Locale.getDefault();
693 private static final long serialVersionUID = 1614381073220130939L;
695 /** The changeEvent used to fire changes to listeners. */
696 protected ChangeEvent changeEvent;
698 /** The listener that listens to the model. */
699 protected ChangeListener changeListener;
701 /** The model that describes this JTabbedPane. */
702 protected SingleSelectionModel model;
704 /** Indicates that the TabbedPane is in scrolling mode. */
705 public static final int SCROLL_TAB_LAYOUT = 1;
707 /** Indicates that the TabbedPane is in wrap mode. */
708 public static final int WRAP_TAB_LAYOUT = 0;
710 /** The current tabPlacement of the TabbedPane. */
711 protected int tabPlacement = SwingConstants.TOP;
713 /** The current tabLayoutPolicy of the TabbedPane. */
714 private transient int layoutPolicy;
716 /** The list of tabs associated with the TabbedPane. */
717 transient Vector tabs = new Vector();
720 * Creates a new JTabbedPane object with tabs on top and using wrap tab
721 * layout.
723 public JTabbedPane()
725 this(SwingConstants.TOP, WRAP_TAB_LAYOUT);
729 * Creates a new JTabbedPane object using wrap tab layout and the given
730 * <code>tabPlacement</code>, where <code>tabPlacement</code> can be one
731 * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
732 * {@link #RIGHT}.
734 * @param tabPlacement where the tabs will be placed
736 public JTabbedPane(int tabPlacement)
738 this(tabPlacement, WRAP_TAB_LAYOUT);
742 * Creates a new JTabbedPane object with the given <code>tabPlacement</code>
743 * and <code>tabLayoutPolicy</code>. The <code>tabPlacement</code> can be one
744 * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
745 * {@link #RIGHT}. The <code>tabLayoutPolicy</code> can be either
746 * {@link #SCROLL_TAB_LAYOUT} or {@link #WRAP_TAB_LAYOUT}.
748 * @param tabPlacement where the tabs will be placed
749 * @param tabLayoutPolicy the way tabs will be placed
751 * @throws IllegalArgumentException If tabLayoutPolicy or tabPlacement are
752 * not valid.
754 public JTabbedPane(int tabPlacement, int tabLayoutPolicy)
756 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
757 && tabPlacement != LEFT)
758 throw new IllegalArgumentException("tabPlacement is not valid.");
759 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
760 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
761 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
762 this.tabPlacement = tabPlacement;
763 layoutPolicy = tabLayoutPolicy;
765 setModel(new DefaultSingleSelectionModel());
767 updateUI();
771 * This method returns the UI used to display the JTabbedPane.
773 * @return The UI used to display the JTabbedPane.
775 public TabbedPaneUI getUI()
777 return (TabbedPaneUI) ui;
781 * This method sets the UI used to display the JTabbedPane.
783 * @param ui The UI used to display the JTabbedPane.
785 public void setUI(TabbedPaneUI ui)
787 super.setUI(ui);
791 * This method restores the UI to the defaults given by the UIManager.
793 public void updateUI()
795 setUI((TabbedPaneUI) UIManager.getUI(this));
799 * This method returns a string identifier that is used to determine which
800 * UI will be used with the JTabbedPane.
802 * @return A string identifier for the UI.
804 public String getUIClassID()
806 return "TabbedPaneUI";
810 * This method creates a ChangeListener that is used to listen to the model
811 * for events.
813 * @return A ChangeListener to listen to the model.
815 protected ChangeListener createChangeListener()
817 return new ModelListener();
821 * This method adds a ChangeListener to the JTabbedPane.
823 * @param l The ChangeListener to add.
825 public void addChangeListener(ChangeListener l)
827 listenerList.add(ChangeListener.class, l);
831 * This method removes a ChangeListener to the JTabbedPane.
833 * @param l The ChangeListener to remove.
835 public void removeChangeListener(ChangeListener l)
837 listenerList.remove(ChangeListener.class, l);
841 * This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
843 protected void fireStateChanged()
845 Object[] changeListeners = listenerList.getListenerList();
846 if (changeEvent == null)
847 changeEvent = new ChangeEvent(this);
848 for (int i = changeListeners.length - 2; i >= 0; i -= 2)
850 if (changeListeners[i] == ChangeListener.class)
851 ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
856 * This method returns all ChangeListeners registered with the JTabbedPane.
858 * @return The ChangeListeners registered with the JTabbedPane.
860 public ChangeListener[] getChangeListeners()
862 return (ChangeListener[]) super.getListeners(ChangeListener.class);
866 * This method returns the model used with the JTabbedPane.
868 * @return The JTabbedPane's model.
870 public SingleSelectionModel getModel()
872 return model;
876 * This method changes the model property of the JTabbedPane.
878 * @param m The new model to use with the JTabbedPane.
880 public void setModel(SingleSelectionModel m)
882 if (m != model)
884 SingleSelectionModel oldModel = this.model;
885 if (oldModel != null && changeListener != null)
886 oldModel.removeChangeListener(changeListener);
888 model = m;
890 if (model != null)
892 if (changeListener == null)
893 changeListener = createChangeListener();
894 model.addChangeListener(changeListener);
896 firePropertyChange("model", oldModel, this.model);
901 * This method returns the tabPlacement.
903 * @return The tabPlacement used with the JTabbedPane.
905 public int getTabPlacement()
907 return tabPlacement;
911 * This method changes the tabPlacement property of the JTabbedPane.
913 * @param tabPlacement The tabPlacement to use.
915 * @throws IllegalArgumentException If tabPlacement is not one of TOP,
916 * BOTTOM, LEFT, or RIGHT.
918 public void setTabPlacement(int tabPlacement)
920 if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
921 && tabPlacement != LEFT)
922 throw new IllegalArgumentException("tabPlacement is not valid.");
923 if (tabPlacement != this.tabPlacement)
925 int oldPlacement = this.tabPlacement;
926 this.tabPlacement = tabPlacement;
927 firePropertyChange("tabPlacement", oldPlacement, this.tabPlacement);
932 * This method returns the tabLayoutPolicy.
934 * @return The tabLayoutPolicy.
936 public int getTabLayoutPolicy()
938 return layoutPolicy;
942 * This method changes the tabLayoutPolicy property of the JTabbedPane.
944 * @param tabLayoutPolicy The tabLayoutPolicy to use.
946 * @throws IllegalArgumentException If tabLayoutPolicy is not one of
947 * SCROLL_TAB_LAYOUT or WRAP_TAB_LAYOUT.
949 public void setTabLayoutPolicy(int tabLayoutPolicy)
951 if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
952 && tabLayoutPolicy != WRAP_TAB_LAYOUT)
953 throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
954 if (tabLayoutPolicy != layoutPolicy)
956 int oldPolicy = layoutPolicy;
957 layoutPolicy = tabLayoutPolicy;
958 firePropertyChange("tabLayoutPolicy", oldPolicy, layoutPolicy);
963 * This method returns the index of the tab that is currently selected.
965 * @return The index of the selected tab.
967 public int getSelectedIndex()
969 return model.getSelectedIndex();
973 * This method checks the index.
975 * @param index The index to check.
976 * @param start DOCUMENT ME!
977 * @param end DOCUMENT ME!
979 * @throws IndexOutOfBoundsException DOCUMENT ME!
981 private void checkIndex(int index, int start, int end)
983 if (index < start || index >= end)
984 throw new IndexOutOfBoundsException("Index < " + start + " || Index >= "
985 + end);
989 * This method sets the selected index. This method will hide the old
990 * component and show the new component.
992 * @param index The index to set it at.
994 public void setSelectedIndex(int index)
996 checkIndex(index, -1, tabs.size());
997 if (index != getSelectedIndex())
999 // Hiding and showing the involved components
1000 // is done by the JTabbedPane's UI.
1001 model.setSelectedIndex(index);
1006 * This method returns the component at the selected index.
1008 * @return The component at the selected index.
1010 public Component getSelectedComponent()
1012 int selectedIndex = getSelectedIndex();
1013 Component selected = null;
1014 if (selectedIndex >= 0)
1015 selected = getComponentAt(selectedIndex);
1016 return selected;
1020 * This method sets the component at the selected index.
1022 * @param c The component associated with the selected index.
1024 public void setSelectedComponent(Component c)
1026 if (c.getParent() == this)
1027 setSelectedIndex(indexOfComponent(c));
1028 else
1029 setComponentAt(getSelectedIndex(), c);
1033 * This method inserts tabs into JTabbedPane. This includes adding the
1034 * component to the JTabbedPane and hiding it.
1036 * @param title the title of the tab; may be <code>null</code>
1037 * @param icon the tab's icon; may be <code>null</code>
1038 * @param component the component associated with the tab
1039 * @param tip the tooltip for the tab
1040 * @param index the index to insert the tab at
1042 public void insertTab(String title, Icon icon, Component component,
1043 String tip, int index)
1045 if (title == null)
1046 title = "";
1047 Page p = new Page(title, icon, component, tip);
1048 tabs.insertElementAt(p, index);
1050 // Hide the component so we don't see it. Do it before we parent it
1051 // so we don't trigger a repaint.
1052 if (component != null)
1054 component.hide();
1055 super.add(component);
1058 if (getSelectedIndex() == -1)
1060 setSelectedIndex(0);
1061 fireStateChanged();
1064 revalidate();
1065 repaint();
1069 * This method adds a tab to the JTabbedPane.
1071 * @param title the title of the tab; may be <code>null</code>
1072 * @param icon the icon for the tab; may be <code>null</code>
1073 * @param component the associated component
1074 * @param tip the associated tooltip
1076 public void addTab(String title, Icon icon, Component component, String tip)
1078 insertTab(title, icon, component, tip, tabs.size());
1082 * This method adds a tab to the JTabbedPane.
1084 * @param title the title of the tab; may be <code>null</code>
1085 * @param icon the icon for the tab; may be <code>null</code>
1086 * @param component the associated component
1088 public void addTab(String title, Icon icon, Component component)
1090 insertTab(title, icon, component, null, tabs.size());
1094 * This method adds a tab to the JTabbedPane.
1096 * @param title the title of the tab; may be <code>null</code>
1097 * @param component the associated component
1099 public void addTab(String title, Component component)
1101 insertTab(title, null, component, null, tabs.size());
1105 * This method adds a tab to the JTabbedPane. The title of the tab is the
1106 * Component's name. If the Component is an instance of UIResource, it
1107 * doesn't add the tab and instead add the component directly to the
1108 * JTabbedPane.
1110 * @param component The associated component.
1112 * @return The Component that was added.
1114 public Component add(Component component)
1116 if (component instanceof UIResource)
1117 super.add(component);
1118 else
1119 insertTab(component.getName(), null, component, null, tabs.size());
1121 return component;
1125 * This method adds a tab to the JTabbedPane. If the Component is an
1126 * instance of UIResource, it doesn't add the tab and instead add the
1127 * component directly to the JTabbedPane.
1129 * @param title the title of the tab; may be <code>null</code>
1130 * @param component the associated component
1132 * @return The Component that was added.
1134 public Component add(String title, Component component)
1136 if (component instanceof UIResource)
1137 super.add(component);
1138 else
1139 insertTab(title, null, component, null, tabs.size());
1140 return component;
1144 * This method adds a tab to the JTabbedPane. If the Component is an
1145 * instance of UIResource, it doesn't add the tab and instead add the
1146 * component directly to the JTabbedPane.
1148 * @param component The associated component.
1149 * @param index The index to insert the tab at.
1151 * @return The Component that was added.
1153 public Component add(Component component, int index)
1155 if (component instanceof UIResource)
1156 super.add(component);
1157 else
1158 insertTab(component.getName(), null, component, null, index);
1159 return component;
1163 * This method adds a tab to the JTabbedPane. If the Component is an
1164 * instance of UIResource, it doesn't add the tab and instead add the
1165 * component directly to the JTabbedPane. If the constraints object is an
1166 * icon, it will be used as the tab's icon. If the constraints object is a
1167 * string, we will use it as the title.
1169 * @param component The associated component.
1170 * @param constraints The constraints object.
1172 public void add(Component component, Object constraints)
1174 add(component, constraints, tabs.size());
1178 * This method adds a tab to the JTabbedPane. If the Component is an
1179 * instance of UIResource, it doesn't add the tab and instead add the
1180 * component directly to the JTabbedPane. If the constraints object is an
1181 * icon, it will be used as the tab's icon. If the constraints object is a
1182 * string, we will use it as the title.
1184 * @param component The associated component.
1185 * @param constraints The constraints object.
1186 * @param index The index to insert the tab at.
1188 public void add(Component component, Object constraints, int index)
1190 if (component instanceof UIResource)
1191 super.add(component);
1192 else
1194 if (constraints instanceof String)
1195 insertTab((String) constraints, null, component, null, index);
1196 else
1197 insertTab(component.getName(),
1198 (constraints instanceof Icon) ? (Icon) constraints : null,
1199 component, null, index);
1204 * Removes the tab at index. After the component associated with
1205 * index is removed, its visibility is reset to true to ensure it
1206 * will be visible if added to other containers.
1208 * @param index The index of the tab to remove.
1210 public void removeTabAt(int index)
1212 checkIndex(index, 0, tabs.size());
1214 // We need to adjust the selection if we remove a tab that comes
1215 // before the selected tab or if the selected tab is removed.
1216 // This decrements the selected index by 1 if any of this is the case.
1217 // Note that this covers all cases:
1218 // - When the selected tab comes after the removed tab, this simply
1219 // adjusts the selection so that after the removal the selected tab
1220 // is still the same.
1221 // - When we remove the currently selected tab, then the tab before the
1222 // selected tab gets selected.
1223 // - When the last tab is removed, then we have an index==0, which gets
1224 // decremented to -1, which means no selection, which is 100% perfect.
1225 int selectedIndex = getSelectedIndex();
1226 if (selectedIndex >= index)
1227 setSelectedIndex(selectedIndex - 1);
1229 Component comp = getComponentAt(index);
1231 // Remove the tab object.
1232 tabs.remove(index);
1234 // Remove the component. I think we cannot assume that the tab order
1235 // is equal to the component order, so we iterate over the children
1236 // here to find the and remove the correct component.
1237 if (comp != null)
1239 Component[] children = getComponents();
1240 for (int i = children.length - 1; i >= 0; --i)
1242 if (children[i] == comp)
1244 super.remove(i);
1245 comp.setVisible(true);
1246 break;
1250 revalidate();
1251 repaint();
1255 * Removes the specified Component from the JTabbedPane.
1257 * @param component The Component to remove.
1259 public void remove(Component component)
1261 // Since components implementing UIResource
1262 // are not added as regular tabs by the add()
1263 // methods we have to take special care when
1264 // removing these object. Especially
1265 // Container.remove(Component) cannot be used
1266 // because it will call JTabbedPane.remove(int)
1267 // later which is overridden and can only
1268 // handle tab components.
1269 // This implementation can even cope with a
1270 // situation that someone called insertTab()
1271 // with a component that implements UIResource.
1272 int index = indexOfComponent(component);
1274 // If the component is not a tab component
1275 // find out its Container-given index
1276 // and call that class' implementation
1277 // directly.
1278 if (index == -1)
1280 Component[] cs = getComponents();
1281 for (int i = 0; i< cs.length; i++)
1282 if (cs[i] == component)
1283 super.remove(i);
1285 else
1286 removeTabAt(index);
1290 * Removes the tab and component which corresponds to the specified index.
1292 * @param index The index of the tab to remove.
1294 public void remove(int index)
1296 removeTabAt(index);
1300 * This method removes all tabs and associated components from the
1301 * JTabbedPane.
1303 public void removeAll()
1305 setSelectedIndex(-1);
1306 for (int i = getTabCount() - 1; i >= 0; i--)
1307 removeTabAt(i);
1311 * This method returns how many tabs are in the JTabbedPane.
1313 * @return The number of tabs in the JTabbedPane.
1315 public int getTabCount()
1317 return tabs.size();
1321 * This method returns the number of runs used to paint the JTabbedPane.
1323 * @return The number of runs.
1325 public int getTabRunCount()
1327 return ((TabbedPaneUI) ui).getTabRunCount(this);
1331 * This method returns the tab title given the index.
1333 * @param index The index of the tab.
1335 * @return The title for the tab.
1337 public String getTitleAt(int index)
1339 checkIndex(index, 0, tabs.size());
1340 return ((Page) tabs.elementAt(index)).getTitle();
1344 * This method returns the active icon given the index.
1346 * @param index The index of the tab.
1348 * @return The active icon for the tab.
1350 public Icon getIconAt(int index)
1352 checkIndex(index, 0, tabs.size());
1353 return ((Page) tabs.elementAt(index)).getIcon();
1357 * This method returns the disabled icon given the index.
1359 * @param index The index of the tab.
1361 * @return The disabled icon for the tab.
1363 public Icon getDisabledIconAt(int index)
1365 checkIndex(index, 0, tabs.size());
1366 return ((Page) tabs.elementAt(index)).getDisabledIcon();
1370 * This method returns the tooltip string for the tab.
1372 * @param index The index of the tab.
1374 * @return The tooltip string for the tab.
1376 public String getToolTipTextAt(int index)
1378 checkIndex(index, 0, tabs.size());
1379 return ((Page) tabs.elementAt(index)).getTip();
1383 * This method returns the foreground color for the tab.
1385 * @param index The index of the tab.
1387 * @return The foreground color for the tab.
1389 public Color getForegroundAt(int index)
1391 checkIndex(index, 0, tabs.size());
1392 return ((Page) tabs.elementAt(index)).getForeground();
1396 * This method returns the background color for the tab.
1398 * @param index The index of the tab.
1400 * @return The background color for the tab.
1402 public Color getBackgroundAt(int index)
1404 checkIndex(index, 0, tabs.size());
1405 return ((Page) tabs.elementAt(index)).getBackground();
1409 * This method returns the component associated with the tab.
1411 * @param index The index of the tab.
1413 * @return The component associated with the tab.
1415 public Component getComponentAt(int index)
1417 checkIndex(index, 0, tabs.size());
1418 return ((Page) tabs.elementAt(index)).getComponent();
1422 * This method returns whether this tab is enabled. Disabled tabs cannot be
1423 * selected.
1425 * @param index The index of the tab.
1427 * @return Whether the tab is enabled.
1429 public boolean isEnabledAt(int index)
1431 checkIndex(index, 0, tabs.size());
1432 return ((Page) tabs.elementAt(index)).isEnabled();
1436 * This method returns the mnemonic for the tab.
1438 * @param tabIndex The index of the tab.
1440 * @return The mnemonic for the tab.
1442 public int getMnemonicAt(int tabIndex)
1444 checkIndex(tabIndex, 0, tabs.size());
1445 return ((Page) tabs.elementAt(tabIndex)).getMnemonic();
1449 * This method returns the mnemonic index for the tab.
1451 * @param tabIndex The index of the tab.
1453 * @return The mnemonic index for the tab.
1455 public int getDisplayedMnemonicIndexAt(int tabIndex)
1457 checkIndex(tabIndex, 0, tabs.size());
1458 return ((Page) tabs.elementAt(tabIndex)).getDisplayedMnemonicIndex();
1462 * This method returns the bounds of the tab given the index.
1464 * @param index The index of the tab.
1466 * @return A rectangle describing the bounds of the tab.
1468 public Rectangle getBoundsAt(int index)
1470 checkIndex(index, 0, tabs.size());
1471 return ((TabbedPaneUI) ui).getTabBounds(this, index);
1475 * This method sets the title of the tab.
1477 * @param index The index of the tab.
1478 * @param title The new title.
1480 public void setTitleAt(int index, String title)
1482 checkIndex(index, 0, tabs.size());
1483 ((Page) tabs.elementAt(index)).setTitle(title);
1487 * This method sets the icon of the tab.
1489 * @param index The index of the tab.
1490 * @param icon The new icon.
1492 public void setIconAt(int index, Icon icon)
1494 checkIndex(index, 0, tabs.size());
1495 ((Page) tabs.elementAt(index)).setIcon(icon);
1499 * This method sets the disabled icon of the tab.
1501 * @param index The index of the tab.
1502 * @param disabledIcon The new disabled icon.
1504 public void setDisabledIconAt(int index, Icon disabledIcon)
1506 checkIndex(index, 0, tabs.size());
1507 ((Page) tabs.elementAt(index)).setDisabledIcon(disabledIcon);
1511 * This method sets the tooltip text of the tab.
1513 * @param index The index of the tab.
1514 * @param toolTipText The tooltip text.
1516 public void setToolTipTextAt(int index, String toolTipText)
1518 checkIndex(index, 0, tabs.size());
1519 ((Page) tabs.elementAt(index)).setTip(toolTipText);
1523 * This method sets the background color of the tab.
1525 * @param index The index of the tab.
1526 * @param background The background color of the tab.
1528 public void setBackgroundAt(int index, Color background)
1530 checkIndex(index, 0, tabs.size());
1531 ((Page) tabs.elementAt(index)).setBackground(background);
1535 * This method sets the foreground color of the tab.
1537 * @param index The index of the tab.
1538 * @param foreground The foreground color of the tab.
1540 public void setForegroundAt(int index, Color foreground)
1542 checkIndex(index, 0, tabs.size());
1543 ((Page) tabs.elementAt(index)).setForeground(foreground);
1547 * This method sets whether the tab is enabled.
1549 * @param index The index of the tab.
1550 * @param enabled Whether the tab is enabled.
1552 public void setEnabledAt(int index, boolean enabled)
1554 checkIndex(index, 0, tabs.size());
1555 ((Page) tabs.elementAt(index)).setEnabled(enabled);
1559 * This method sets the component associated with the tab.
1561 * @param index The index of the tab.
1562 * @param component The component associated with the tab.
1564 public void setComponentAt(int index, Component component)
1566 checkIndex(index, 0, tabs.size());
1567 ((Page) tabs.elementAt(index)).setComponent(component);
1571 * This method sets the displayed mnemonic index of the tab.
1573 * @param tabIndex The index of the tab.
1574 * @param mnemonicIndex The mnemonic index.
1576 public void setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)
1578 checkIndex(tabIndex, 0, tabs.size());
1579 ((Page) tabs.elementAt(tabIndex)).setDisplayedMnemonicIndex(mnemonicIndex);
1583 * This method sets the mnemonic for the tab.
1585 * @param tabIndex The index of the tab.
1586 * @param mnemonic The mnemonic.
1588 public void setMnemonicAt(int tabIndex, int mnemonic)
1590 checkIndex(tabIndex, 0, tabs.size());
1591 ((Page) tabs.elementAt(tabIndex)).setMnemonic(mnemonic);
1595 * This method finds the index of a tab given the title.
1597 * @param title The title that belongs to a tab.
1599 * @return The index of the tab that has the title or -1 if not found.
1601 public int indexOfTab(String title)
1603 int index = -1;
1604 for (int i = 0; i < tabs.size(); i++)
1606 if (((Page) tabs.elementAt(i)).getTitle().equals(title))
1608 index = i;
1609 break;
1612 return index;
1616 * This method finds the index of a tab given the icon.
1618 * @param icon The icon that belongs to a tab.
1620 * @return The index of the tab that has the icon or -1 if not found.
1622 public int indexOfTab(Icon icon)
1624 int index = -1;
1625 for (int i = 0; i < tabs.size(); i++)
1627 if (((Page) tabs.elementAt(i)).getIcon() == icon)
1629 index = i;
1630 break;
1633 return index;
1637 * This method finds the index of a tab given the component.
1639 * @param component A component associated with a tab.
1641 * @return The index of the tab that has this component or -1 if not found.
1643 public int indexOfComponent(Component component)
1645 int index = -1;
1646 for (int i = 0; i < tabs.size(); i++)
1648 if (((Page) tabs.elementAt(i)).getComponent() == component)
1650 index = i;
1651 break;
1654 return index;
1658 * This method returns a tab index given an (x,y) location. The origin of
1659 * the (x,y) pair will be the JTabbedPane's top left position. The tab
1660 * returned will be the one that contains the point. This method is
1661 * delegated to the UI.
1663 * @param x The x coordinate of the point.
1664 * @param y The y coordinate of the point.
1666 * @return The index of the tab that contains the point.
1668 public int indexAtLocation(int x, int y)
1670 return ((TabbedPaneUI) ui).tabForCoordinate(this, x, y);
1674 * This method returns the tooltip text given a mouse event.
1676 * @param event The mouse event.
1678 * @return The tool tip text that is associated with this mouse event.
1680 public String getToolTipText(MouseEvent event)
1682 int index = indexAtLocation(event.getX(), event.getY());
1683 return ((Page) tabs.elementAt(index)).getTip();
1687 * Returns a string describing the attributes for the
1688 * <code>JTabbedPane</code> component, for use in debugging. The return
1689 * value is guaranteed to be non-<code>null</code>, but the format of the
1690 * string may vary between implementations.
1692 * @return A string describing the attributes of the
1693 * <code>JTabbedPane</code>.
1695 protected String paramString()
1697 CPStringBuilder sb = new CPStringBuilder(super.paramString());
1698 sb.append(",tabPlacement=");
1699 if (tabPlacement == TOP)
1700 sb.append("TOP");
1701 if (tabPlacement == BOTTOM)
1702 sb.append("BOTTOM");
1703 if (tabPlacement == LEFT)
1704 sb.append("LEFT");
1705 if (tabPlacement == RIGHT)
1706 sb.append("RIGHT");
1707 return sb.toString();
1711 * Returns the object that provides accessibility features for this
1712 * <code>JTabbedPane</code> component.
1714 * @return The accessible context (an instance of
1715 * {@link AccessibleJTabbedPane}).
1717 public AccessibleContext getAccessibleContext()
1719 if (accessibleContext == null)
1721 AccessibleJTabbedPane ctx = new AccessibleJTabbedPane();
1722 addChangeListener(ctx);
1723 accessibleContext = ctx;
1726 return accessibleContext;