JTabbedPane.java 45.9 KB
Newer Older
Tom Tromey committed
1
/* JTabbedPane.java --
2
   Copyright (C) 2002, 2004, 2005, 2006,  Free Software Foundation, Inc.
Tom Tromey committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing;

41 42
import gnu.java.lang.CPStringBuilder;

Tom Tromey committed
43 44 45 46 47 48
import java.awt.Color;
import java.awt.Component;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.io.Serializable;
49
import java.util.Locale;
Tom Tromey committed
50 51 52 53 54 55
import java.util.Vector;

import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.accessibility.AccessibleRole;
import javax.accessibility.AccessibleSelection;
56
import javax.accessibility.AccessibleState;
57
import javax.accessibility.AccessibleStateSet;
Tom Tromey committed
58 59 60 61 62 63
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.TabbedPaneUI;
import javax.swing.plaf.UIResource;

/**
64 65 66
 * This is a container for components where only one component is displayed at
 * a given time and the displayed component can be switched by clicking on
 * tabs.
67
 *
Tom Tromey committed
68 69 70 71 72 73 74 75 76 77 78 79 80
 * <p>
 * Tabs can be oriented in several ways. They can be above, below, left and
 * right of the component. Tabs can either wrap around (by creating multiple
 * rows of tabs) or they can be scrolled (where only a subset of the  tabs
 * can be seen at once). More tabs can be added by calling the
 * add/addTab/insertTab methods.
 * </p>
 */
public class JTabbedPane extends JComponent implements Serializable,
                                                       Accessible,
                                                       SwingConstants
{
  /**
81
   * Accessibility support for <code>JTabbedPane</code>.
Tom Tromey committed
82 83 84 85
   */
  protected class AccessibleJTabbedPane extends JComponent.AccessibleJComponent
    implements AccessibleSelection, ChangeListener
  {
86 87 88
    /**
     * The serialization UID.
     */
Tom Tromey committed
89 90 91 92 93 94 95 96 97 98 99
    private static final long serialVersionUID = 7610530885966830483L;

    /**
     * Creates a new AccessibleJTabbedPane object.
     */
    public AccessibleJTabbedPane()
    {
      super();
    }

    /**
100
     * Receives notification when the selection state of the
101 102
     * <code>JTabbedPane</code> changes and fires appropriate property change
     * events to interested listeners.
Tom Tromey committed
103
     *
104
     * @param e the change event describing the change
Tom Tromey committed
105 106 107
     */
    public void stateChanged(ChangeEvent e)
    {
108 109 110 111
      // I couldn't figure out what else should be done here.
      Object source = e.getSource();
      firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY,
                         null, source);
Tom Tromey committed
112 113 114
    }

    /**
115 116
     * Returns the accessible role of the <code>JTabbedPane</code>, which is
     * {@link AccessibleRole#PAGE_TAB_LIST}.
Tom Tromey committed
117
     *
118
     * @return the accessible role of the <code>JTabbedPane</code>
Tom Tromey committed
119 120 121
     */
    public AccessibleRole getAccessibleRole()
    {
122
      return AccessibleRole.PAGE_TAB_LIST;
Tom Tromey committed
123 124 125
    }

    /**
126 127
     * Returns the number of accessible child components of the
     * <code>JTabbedPane</code>.
Tom Tromey committed
128
     *
129 130
     * @return the number of accessible child components of the
     *         <code>JTabbedPane</code>
Tom Tromey committed
131 132 133
     */
    public int getAccessibleChildrenCount()
    {
134
      return getTabCount();
Tom Tromey committed
135 136 137
    }

    /**
138
     * Returns the accessible child component at the specified index.
Tom Tromey committed
139
     *
140
     * @param i the index of the child component to fetch
Tom Tromey committed
141
     *
142
     * @return the accessible child component at the specified index
Tom Tromey committed
143 144 145
     */
    public Accessible getAccessibleChild(int i)
    {
146 147 148 149 150 151
      // Testing shows that the reference implementation returns instances
      // of page here.
      Accessible child = null;
      if (i >= 0 && i < tabs.size())
        child = (Page) tabs.get(i);
      return child;
Tom Tromey committed
152 153 154
    }

    /**
155 156
     * Returns the current selection state of the <code>JTabbedPane</code>
     * as AccessibleSelection object.
Tom Tromey committed
157
     *
158
     * @return the current selection state of the <code>JTabbedPane</code>
Tom Tromey committed
159 160 161
     */
    public AccessibleSelection getAccessibleSelection()
    {
162
      return this;
Tom Tromey committed
163 164 165
    }

    /**
166 167 168
     * Returns the accessible child component at the specified coordinates.
     * If there is no child component at this location, then return the
     * currently selected tab.
Tom Tromey committed
169
     *
170
     * @param p the coordinates at which to look up the child component
Tom Tromey committed
171
     *
172 173 174
     * @return the accessible child component at the specified coordinates or
     *         the currently selected tab if there is no child component at
     *         this location
Tom Tromey committed
175 176 177
     */
    public Accessible getAccessibleAt(Point p)
    {
178 179 180 181 182
      int tabIndex = indexAtLocation(p.x, p.y);
      if (tabIndex >= 0)
        return getAccessibleChild(tabIndex);
      else
        return getAccessibleSelection(0);
Tom Tromey committed
183 184 185
    }

    /**
186 187
     * Returns the number of selected child components of the
     * <code>JTabbedPane</code>. The reference implementation appears
188
     * to return <code>1</code> always and we do the same.
Tom Tromey committed
189
     *
190
     * @return <code>1</code>
Tom Tromey committed
191 192 193
     */
    public int getAccessibleSelectionCount()
    {
194
      return 1;
Tom Tromey committed
195 196 197
    }

    /**
198
     * Returns the selected tab, or <code>null</code> if there is no
199
     * selection.
Tom Tromey committed
200
     *
201
     * @param i  the selection index (ignored here).
Tom Tromey committed
202
     *
203
     * @return The selected tab, or <code>null</code>.
Tom Tromey committed
204 205 206
     */
    public Accessible getAccessibleSelection(int i)
    {
207 208 209 210 211
      Accessible result = null;
      int selected = getSelectedIndex();
      if (selected >= 0)
        result = (Page) tabs.get(selected);
      return result;
Tom Tromey committed
212 213 214
    }

    /**
215 216
     * Returns <code>true</code> if the specified child is selected,
     * and <code>false</code> otherwise.
Tom Tromey committed
217
     *
218
     * @param i the child index.
Tom Tromey committed
219
     *
220
     * @return A boolean.
Tom Tromey committed
221 222 223
     */
    public boolean isAccessibleChildSelected(int i)
    {
224
      return i == getSelectedIndex();
Tom Tromey committed
225 226 227
    }

    /**
228
     * Selects the specified tab.
Tom Tromey committed
229
     *
230
     * @param i  the index of the item to select.
Tom Tromey committed
231 232 233
     */
    public void addAccessibleSelection(int i)
    {
234
      setSelectedIndex(i);
Tom Tromey committed
235 236 237
    }

    /**
238 239
     * Does nothing - it makes no sense to remove a selection for a
     * tabbed pane, since one tab must always be selected.
Tom Tromey committed
240
     *
241
     * @param i  the item index.
242
     *
243
     * @see #addAccessibleSelection(int)
Tom Tromey committed
244 245 246
     */
    public void removeAccessibleSelection(int i)
    {
247
      // do nothing
Tom Tromey committed
248 249 250
    }

    /**
251 252
     * Does nothing - it makes no sense to clear the selection for
     * a tabbed pane, since one tab must always be selected.
253
     *
254
     * @see #addAccessibleSelection(int)
Tom Tromey committed
255 256 257
     */
    public void clearAccessibleSelection()
    {
258
      // do nothing
Tom Tromey committed
259 260 261
    }

    /**
262 263
     * Does nothing - it makes no sense to select all for a tabbed
     * pane, since only one tab can be selected at a time.
264
     *
265
     * @see #addAccessibleSelection(int)
Tom Tromey committed
266 267 268
     */
    public void selectAllAccessibleSelection()
    {
269
      // do nothing
Tom Tromey committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
    }
  }

  /**
   * A helper class that listens for changes to the model.
   */
  protected class ModelListener implements ChangeListener, Serializable
  {
    private static final long serialVersionUID = 497359819958114132L;

    /**
     * Creates a new ModelListener object.
     */
    protected ModelListener()
    {
285
      // Nothing to do here.
Tom Tromey committed
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
    }

    /**
     * This method is called whenever the model  is changed.
     *
     * @param e The ChangeEvent that is passed from the model.
     */
    public void stateChanged(ChangeEvent e)
    {
      // Propagate to our listeners.
      fireStateChanged();
    }
  }

  /**
   * A private class that holds all the information  for each tab.
   */
  private class Page
304 305
    extends AccessibleContext
    implements Accessible
Tom Tromey committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
  {
    /** The tooltip string. */
    private String tip;

    /** The component associated with the tab. */
    private Component component;

    /** The active icon associated with the tab. */
    private transient Icon icon;

    /** The disabled icon associated with the tab. */
    private transient Icon disabledIcon;

    /** The tab's enabled status. */
    private transient boolean enabled = true;

    /** The string painted on the tab. */
    private transient String title;

    /** The background color of the tab. */
    private transient Color bg;

    /** The foreground color of the tab. */
    private transient Color fg;

    /** The mnemonic associated with the tab. */
    private transient int mnemonicKey;

    /** The index of the underlined character in the string. */
    private transient int underlinedChar = -1;

    /**
     * Creates a new data storage for the tab.
     *
     * @param title The string displayed on the tab.
     * @param icon The active icon displayed on the tab.
     * @param component The component associated with the tab.
     * @param tip The tooltip associated with the tab.
     */
    protected Page(String title, Icon icon, Component component, String tip)
    {
      this.title = title;
      this.icon = icon;
      this.component = component;
      this.tip = tip;
    }

    /**
     * This method returns the component associated with the tab.
     *
     * @return The component associated with the tab.
     */
    public Component getComponent()
    {
      return component;
    }

    /**
     * This method sets the component associated with the tab.
     *
     * @param c The component associated with the tab.
     */
    public void setComponent(Component c)
    {
370 371 372 373
      int i = indexOfComponent(component);
      insertTab(title, icon, c, tip, i);
      component = c;
      removeTabAt(i);
Tom Tromey committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
    }

    /**
     * This method returns the tooltip string.
     *
     * @return The tooltip string.
     */
    public String getTip()
    {
      return tip;
    }

    /**
     * This method sets the tooltip string.
     *
     * @param tip The tooltip string.
     */
    public void setTip(String tip)
    {
      this.tip = tip;
    }

    /**
     * This method returns the background color.
     *
     * @return The background color.
     */
    public Color getBackground()
    {
403 404 405 406 407 408
      Color background;
      if (bg == null)
        background = JTabbedPane.this.getBackground();
      else
        background = bg;
      return background;
Tom Tromey committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
    }

    /**
     * This method sets the background color.
     *
     * @param background The background color.
     */
    public void setBackground(Color background)
    {
      bg = background;
    }

    /**
     * This method returns the foreground color.
     *
     * @return The foreground color.
     */
    public Color getForeground()
    {
428 429 430 431 432 433
      Color foreground;
      if (fg == null)
        foreground = JTabbedPane.this.getForeground();
      else
        foreground = fg;
      return foreground;
Tom Tromey committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
    }

    /**
     * This method sets the foreground color.
     *
     * @param foreground The foreground color.
     */
    public void setForeground(Color foreground)
    {
      fg = foreground;
    }

    /**
     * This method returns the title associated with the tab.
     *
     * @return The title of the tab.
     */
    public String getTitle()
    {
      return title;
    }

    private static final long serialVersionUID = 1614381073220130939L;

    /**
     * This method sets the title of the tab.
     *
     * @param text The title of the tab.
     */
    public void setTitle(String text)
    {
      title = text;
      if (title != null && title.length() <= underlinedChar)
467
        setDisplayedMnemonicIndex(title.length() - 1);
Tom Tromey committed
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    }

    /**
     * This method returns the active icon.
     *
     * @return The active icon.
     */
    public Icon getIcon()
    {
      return icon;
    }

    /**
     * This method sets the active icon.
     *
     * @param icon The active icon.
     */
    public void setIcon(Icon icon)
    {
      this.icon = icon;
    }

    /**
     * This method returns the disabled icon.
     *
     * @return The disabled icon.
     */
    public Icon getDisabledIcon()
    {
      if (disabledIcon == null && icon instanceof ImageIcon)
498
        setDisabledIcon(icon);
Tom Tromey committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
      return disabledIcon;
    }

    /**
     * This method sets the disabled icon.
     *
     * @param disabledIcon The disabled icon.
     */
    public void setDisabledIcon(Icon disabledIcon)
    {
      this.disabledIcon = disabledIcon;
    }

    /**
     * This method returns whether the tab is enabled.
     *
     * @return Whether the tab is enabled.
     */
    public boolean isEnabled()
    {
      return enabled;
    }

    /**
     * This method sets whether the tab is enabled.
     *
     * @param enabled Whether this tab is enabled.
     */
    public void setEnabled(boolean enabled)
    {
      this.enabled = enabled;
    }

    /**
     * This method returns the mnemonic.
     *
     * @return The mnemonic.
     */
    public int getMnemonic()
    {
539
      return mnemonicKey;
Tom Tromey committed
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    }

    /**
     * This method sets the mnemonic. If the title is set, it will update the
     * mnemonicIndex.
     *
     * @param key The mnemonic.
     */
    public void setMnemonic(int key)
    {
      setMnemonic((char) key);
    }

    /**
     * This method sets the mnemonic. If the title is set, it will update the
     * mnemonicIndex.
     *
     * @param aChar The mnemonic.
     */
    public void setMnemonic(char aChar)
    {
      mnemonicKey = aChar;
      if (title != null)
563
        setDisplayedMnemonicIndex(title.indexOf(mnemonicKey));
Tom Tromey committed
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
    }

    /**
     * This method returns the mnemonicIndex.
     *
     * @return The mnemonicIndex.
     */
    public int getDisplayedMnemonicIndex()
    {
      return underlinedChar;
    }

    /**
     * This method sets the mnemonicIndex.
     *
     * @param index The mnemonicIndex.
     *
     * @throws IllegalArgumentException If index less than -1 || index greater
     *         or equal to title.length.
     */
    public void setDisplayedMnemonicIndex(int index)
      throws IllegalArgumentException
    {
      if (index < -1 || title != null && index >= title.length())
588
        throw new IllegalArgumentException();
Tom Tromey committed
589 590

      if (title == null || mnemonicKey == 0 || (index > -1 && title.charAt(index) != mnemonicKey))
591
        index = -1;
Tom Tromey committed
592 593 594

      underlinedChar = index;
    }
595 596 597 598 599 600 601 602 603 604 605 606

    /**
     * Returns the accessible context, which is this object itself.
     *
     * @return the accessible context, which is this object itself
     */
    public AccessibleContext getAccessibleContext()
    {
      return this;
    }

    /**
607
     * Returns the accessible name for this tab.
608
     *
609 610 611 612 613 614 615 616 617
     * @return The accessible name.
     */
    public String getAccessibleName()
    {
      if (accessibleName != null)
        return accessibleName;
      else
        return title;
    }
618

619
    /**
620 621 622 623 624 625 626 627 628 629
     * Returns the accessible role of this tab, which is always
     * {@link AccessibleRole#PAGE_TAB}.
     *
     * @return the accessible role of this tab
     */
    public AccessibleRole getAccessibleRole()
    {
      return AccessibleRole.PAGE_TAB;
    }

630 631 632 633 634
    /**
     * Returns the accessible state set of this object.
     *
     * @return the accessible state set of this object
     */
635 636
    public AccessibleStateSet getAccessibleStateSet()
    {
637
      AccessibleContext parentCtx = JTabbedPane.this.getAccessibleContext();
638 639 640 641 642
      AccessibleStateSet state = parentCtx.getAccessibleStateSet();
      state.add(AccessibleState.SELECTABLE);
      if (component == getSelectedComponent())
        state.add(AccessibleState.SELECTED);
      return state;
643 644
    }

645 646 647 648 649
    /**
     * Returns the index of this tab inside its parent.
     *
     * @return the index of this tab inside its parent
     */
650 651
    public int getAccessibleIndexInParent()
    {
652 653 654
      // TODO: Not sure if the title is unambiguous, but I can't figure
      // another way of doing this.
      return indexOfTab(title);
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
    }

    /**
     * Returns the number of accessible children, which is always one (the
     * component of this tab).
     *
     * @return the number of accessible children
     */
    public int getAccessibleChildrenCount()
    {
      return 1;
    }

    /**
     * Returns the accessible child of this tab, which is the component
     * displayed by the tab.
     *
     * @return the accessible child of this tab
     */
    public Accessible getAccessibleChild(int i)
    {
      // A quick test shows that this method always returns the component
      // displayed by the tab, regardless of the index.
      return (Accessible) component;
    }

    /**
     * Returns the locale of this accessible object.
     *
     * @return the locale of this accessible object
     */
    public Locale getLocale()
    {
      // TODO: Is this ok?
      return Locale.getDefault();
    }
Tom Tromey committed
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
  }

  private static final long serialVersionUID = 1614381073220130939L;

  /** The changeEvent used to fire changes to listeners. */
  protected ChangeEvent changeEvent;

  /** The listener that listens to the model. */
  protected ChangeListener changeListener;

  /** The model that describes this JTabbedPane. */
  protected SingleSelectionModel model;

  /** Indicates that the TabbedPane is in scrolling mode. */
  public static final int SCROLL_TAB_LAYOUT = 1;

  /** Indicates that the TabbedPane is in wrap mode. */
  public static final int WRAP_TAB_LAYOUT = 0;

  /** The current tabPlacement of the TabbedPane. */
  protected int tabPlacement = SwingConstants.TOP;

  /** The current tabLayoutPolicy of the TabbedPane. */
  private transient int layoutPolicy;

  /** The list of tabs associated with the TabbedPane. */
  transient Vector tabs = new Vector();

  /**
   * Creates a new JTabbedPane object with tabs on top and using wrap tab
   * layout.
   */
  public JTabbedPane()
  {
    this(SwingConstants.TOP, WRAP_TAB_LAYOUT);
  }

  /**
   * Creates a new JTabbedPane object using wrap tab layout  and the given
730 731 732
   * <code>tabPlacement</code>, where <code>tabPlacement</code> can be one
   * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
   * {@link #RIGHT}.
Tom Tromey committed
733
   *
734
   * @param tabPlacement where the tabs will be placed
Tom Tromey committed
735 736 737 738 739 740 741
   */
  public JTabbedPane(int tabPlacement)
  {
    this(tabPlacement, WRAP_TAB_LAYOUT);
  }

  /**
742 743 744 745 746
   * Creates a new JTabbedPane object with the given <code>tabPlacement</code>
   * and <code>tabLayoutPolicy</code>. The <code>tabPlacement</code> can be one
   * of the following values: {@link #TOP}, {@link #BOTTOM}, {@link #LEFT} or
   * {@link #RIGHT}. The <code>tabLayoutPolicy</code> can be either
   * {@link #SCROLL_TAB_LAYOUT} or {@link #WRAP_TAB_LAYOUT}.
Tom Tromey committed
747
   *
748 749
   * @param tabPlacement where the tabs will be placed
   * @param tabLayoutPolicy the way tabs will be placed
Tom Tromey committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763
   *
   * @throws IllegalArgumentException If tabLayoutPolicy or tabPlacement are
   *         not valid.
   */
  public JTabbedPane(int tabPlacement, int tabLayoutPolicy)
  {
    if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
        && tabPlacement != LEFT)
      throw new IllegalArgumentException("tabPlacement is not valid.");
    if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
        && tabLayoutPolicy != WRAP_TAB_LAYOUT)
      throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
    this.tabPlacement = tabPlacement;
    layoutPolicy = tabLayoutPolicy;
764

765
    setModel(new DefaultSingleSelectionModel());
Tom Tromey committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

    updateUI();
  }

  /**
   * This method returns the UI used to display the JTabbedPane.
   *
   * @return The UI used to display the JTabbedPane.
   */
  public TabbedPaneUI getUI()
  {
    return (TabbedPaneUI) ui;
  }

  /**
   * This method sets the UI used to display the JTabbedPane.
   *
   * @param ui The UI used to display the JTabbedPane.
   */
  public void setUI(TabbedPaneUI ui)
  {
    super.setUI(ui);
  }

  /**
   * This method restores the UI to the defaults given by the UIManager.
   */
  public void updateUI()
  {
    setUI((TabbedPaneUI) UIManager.getUI(this));
  }

  /**
   * This method returns a string identifier that  is used to determine which
   * UI will be used with  the JTabbedPane.
   *
   * @return A string identifier for the UI.
   */
  public String getUIClassID()
  {
    return "TabbedPaneUI";
  }

  /**
   * This method creates a ChangeListener that is used to  listen to the model
   * for events.
   *
   * @return A ChangeListener to listen to the model.
   */
  protected ChangeListener createChangeListener()
  {
    return new ModelListener();
  }

  /**
   * This method adds a ChangeListener to the JTabbedPane.
   *
   * @param l The ChangeListener to add.
   */
  public void addChangeListener(ChangeListener l)
  {
    listenerList.add(ChangeListener.class, l);
  }

  /**
   * This method removes a ChangeListener to the JTabbedPane.
   *
   * @param l The ChangeListener to remove.
   */
  public void removeChangeListener(ChangeListener l)
  {
    listenerList.remove(ChangeListener.class, l);
  }

  /**
   * This method fires a ChangeEvent to all the JTabbedPane's ChangeListeners.
   */
  protected void fireStateChanged()
  {
    Object[] changeListeners = listenerList.getListenerList();
    if (changeEvent == null)
      changeEvent = new ChangeEvent(this);
    for (int i = changeListeners.length - 2; i >= 0; i -= 2)
      {
850 851
        if (changeListeners[i] == ChangeListener.class)
          ((ChangeListener) changeListeners[i + 1]).stateChanged(changeEvent);
Tom Tromey committed
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877
      }
  }

  /**
   * This method returns all ChangeListeners registered with the JTabbedPane.
   *
   * @return The ChangeListeners registered with the JTabbedPane.
   */
  public ChangeListener[] getChangeListeners()
  {
    return (ChangeListener[]) super.getListeners(ChangeListener.class);
  }

  /**
   * This method returns the model used with the JTabbedPane.
   *
   * @return The JTabbedPane's model.
   */
  public SingleSelectionModel getModel()
  {
    return model;
  }

  /**
   * This method changes the model property of the JTabbedPane.
   *
878
   * @param m The new model to use with the JTabbedPane.
Tom Tromey committed
879
   */
880
  public void setModel(SingleSelectionModel m)
Tom Tromey committed
881
  {
882
    if (m != model)
Tom Tromey committed
883
      {
884
        SingleSelectionModel oldModel = this.model;
885 886 887
        if (oldModel != null && changeListener != null)
          oldModel.removeChangeListener(changeListener);

888
        model = m;
889 890 891 892 893 894 895

        if (model != null)
          {
            if (changeListener == null)
              changeListener = createChangeListener();
            model.addChangeListener(changeListener);
          }
896
        firePropertyChange("model", oldModel, this.model);
Tom Tromey committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
      }
  }

  /**
   * This method returns the tabPlacement.
   *
   * @return The tabPlacement used with the JTabbedPane.
   */
  public int getTabPlacement()
  {
    return tabPlacement;
  }

  /**
   * This method changes the tabPlacement property of the JTabbedPane.
   *
   * @param tabPlacement The tabPlacement to use.
   *
   * @throws IllegalArgumentException If tabPlacement is not one of TOP,
   *         BOTTOM, LEFT, or RIGHT.
   */
  public void setTabPlacement(int tabPlacement)
  {
    if (tabPlacement != TOP && tabPlacement != BOTTOM && tabPlacement != RIGHT
        && tabPlacement != LEFT)
      throw new IllegalArgumentException("tabPlacement is not valid.");
    if (tabPlacement != this.tabPlacement)
      {
925 926 927
        int oldPlacement = this.tabPlacement;
        this.tabPlacement = tabPlacement;
        firePropertyChange("tabPlacement", oldPlacement, this.tabPlacement);
Tom Tromey committed
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
      }
  }

  /**
   * This method returns the tabLayoutPolicy.
   *
   * @return The tabLayoutPolicy.
   */
  public int getTabLayoutPolicy()
  {
    return layoutPolicy;
  }

  /**
   * This method changes the tabLayoutPolicy property of the JTabbedPane.
   *
   * @param tabLayoutPolicy The tabLayoutPolicy to use.
   *
   * @throws IllegalArgumentException If tabLayoutPolicy is not one of
   *         SCROLL_TAB_LAYOUT or WRAP_TAB_LAYOUT.
   */
  public void setTabLayoutPolicy(int tabLayoutPolicy)
  {
    if (tabLayoutPolicy != SCROLL_TAB_LAYOUT
        && tabLayoutPolicy != WRAP_TAB_LAYOUT)
      throw new IllegalArgumentException("tabLayoutPolicy is not valid.");
    if (tabLayoutPolicy != layoutPolicy)
      {
956 957 958
        int oldPolicy = layoutPolicy;
        layoutPolicy = tabLayoutPolicy;
        firePropertyChange("tabLayoutPolicy", oldPolicy, layoutPolicy);
Tom Tromey committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
      }
  }

  /**
   * This method returns the index of the tab that is currently selected.
   *
   * @return The index of the selected tab.
   */
  public int getSelectedIndex()
  {
    return model.getSelectedIndex();
  }

  /**
   * This method checks the index.
   *
   * @param index The index to check.
   * @param start DOCUMENT ME!
   * @param end DOCUMENT ME!
   *
   * @throws IndexOutOfBoundsException DOCUMENT ME!
   */
  private void checkIndex(int index, int start, int end)
  {
    if (index < start || index >= end)
      throw new IndexOutOfBoundsException("Index < " + start + " || Index >= "
                                          + end);
  }

  /**
   * This method sets the selected index. This method will hide the old
   * component and show the new component.
   *
   * @param index The index to set it at.
   */
  public void setSelectedIndex(int index)
  {
    checkIndex(index, -1, tabs.size());
    if (index != getSelectedIndex())
      {
999 1000
        // Hiding and showing the involved components
        // is done by the JTabbedPane's UI.
1001
        model.setSelectedIndex(index);
Tom Tromey committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
      }
  }

  /**
   * This method returns the component at the selected index.
   *
   * @return The component at the selected index.
   */
  public Component getSelectedComponent()
  {
1012 1013 1014 1015 1016
    int selectedIndex = getSelectedIndex();
    Component selected = null;
    if (selectedIndex >= 0)
      selected = getComponentAt(selectedIndex);
    return selected;
Tom Tromey committed
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  }

  /**
   * This method sets the component at the selected index.
   *
   * @param c The component associated with the selected index.
   */
  public void setSelectedComponent(Component c)
  {
    if (c.getParent() == this)
      setSelectedIndex(indexOfComponent(c));
    else
      setComponentAt(getSelectedIndex(), c);
  }

  /**
   * This method inserts tabs into JTabbedPane. This includes adding the
   * component to the JTabbedPane and hiding it.
   *
1036 1037 1038 1039 1040
   * @param title the title of the tab; may be <code>null</code>
   * @param icon the tab's icon; may be <code>null</code>
   * @param component the component associated with the tab
   * @param tip the tooltip for the tab
   * @param index the index to insert the tab at
Tom Tromey committed
1041 1042 1043 1044
   */
  public void insertTab(String title, Icon icon, Component component,
                        String tip, int index)
  {
1045 1046
    if (title == null)
      title = "";
Tom Tromey committed
1047 1048 1049 1050 1051 1052 1053
    Page p = new Page(title, icon, component, tip);
    tabs.insertElementAt(p, index);

    // Hide the component so we don't see it. Do it before we parent it
    // so we don't trigger a repaint.
    if (component != null)
      {
1054 1055
        component.hide();
        super.add(component);
Tom Tromey committed
1056 1057 1058
      }

    if (getSelectedIndex() == -1)
1059 1060 1061 1062
      {
        setSelectedIndex(0);
        fireStateChanged();
      }
Tom Tromey committed
1063

1064
    revalidate();
Tom Tromey committed
1065 1066 1067 1068 1069 1070
    repaint();
  }

  /**
   * This method adds a tab to the JTabbedPane.
   *
1071 1072 1073 1074
   * @param title the title of the tab; may be <code>null</code>
   * @param icon the icon for the tab; may be <code>null</code>
   * @param component the associated component
   * @param tip the associated tooltip
Tom Tromey committed
1075 1076 1077 1078 1079 1080 1081 1082 1083
   */
  public void addTab(String title, Icon icon, Component component, String tip)
  {
    insertTab(title, icon, component, tip, tabs.size());
  }

  /**
   * This method adds a tab to the JTabbedPane.
   *
1084 1085 1086
   * @param title the title of the tab; may be <code>null</code>
   * @param icon the icon for the tab; may be <code>null</code>
   * @param component the associated component
Tom Tromey committed
1087 1088 1089 1090 1091 1092 1093 1094 1095
   */
  public void addTab(String title, Icon icon, Component component)
  {
    insertTab(title, icon, component, null, tabs.size());
  }

  /**
   * This method adds a tab to the JTabbedPane.
   *
1096 1097
   * @param title the title of the tab; may be <code>null</code>
   * @param component the associated component
Tom Tromey committed
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
   */
  public void addTab(String title, Component component)
  {
    insertTab(title, null, component, null, tabs.size());
  }

  /**
   * This method adds a tab to the JTabbedPane. The title of the tab is the
   * Component's name. If the Component is an instance of UIResource, it
   * doesn't add the tab and instead add the component directly to the
   * JTabbedPane.
   *
   * @param component The associated component.
   *
   * @return The Component that was added.
   */
  public Component add(Component component)
  {
    if (component instanceof UIResource)
      super.add(component);
    else
      insertTab(component.getName(), null, component, null, tabs.size());
1120

Tom Tromey committed
1121 1122 1123 1124 1125 1126 1127 1128
    return component;
  }

  /**
   * This method adds a tab to the JTabbedPane. If the Component is an
   * instance of UIResource, it doesn't add the tab and instead add the
   * component directly to the JTabbedPane.
   *
1129 1130
   * @param title the title of the tab; may be <code>null</code>
   * @param component the associated component
Tom Tromey committed
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
   *
   * @return The Component that was added.
   */
  public Component add(String title, Component component)
  {
    if (component instanceof UIResource)
      super.add(component);
    else
      insertTab(title, null, component, null, tabs.size());
    return component;
  }

  /**
   * This method adds a tab to the JTabbedPane. If the Component is an
   * instance of UIResource, it doesn't add the tab and instead add the
   * component directly to the JTabbedPane.
   *
   * @param component The associated component.
   * @param index The index to insert the tab at.
   *
   * @return The Component that was added.
   */
  public Component add(Component component, int index)
  {
    if (component instanceof UIResource)
      super.add(component);
    else
      insertTab(component.getName(), null, component, null, index);
    return component;
  }

  /**
   * This method adds a tab to the JTabbedPane. If the Component is an
   * instance of UIResource, it doesn't add the tab and instead add the
   * component directly to the JTabbedPane. If the constraints object is an
   * icon, it will be used as the tab's icon. If the constraints object is a
   * string, we will use it as the title.
   *
   * @param component The associated component.
   * @param constraints The constraints object.
   */
  public void add(Component component, Object constraints)
  {
    add(component, constraints, tabs.size());
  }

  /**
   * This method adds a tab to the JTabbedPane. If the Component is an
   * instance of UIResource, it doesn't add the tab and instead add the
   * component directly to the JTabbedPane. If the constraints object is an
   * icon, it will be used as the tab's icon. If the constraints object is a
   * string, we will use it as the title.
   *
   * @param component The associated component.
   * @param constraints The constraints object.
   * @param index The index to insert the tab at.
   */
  public void add(Component component, Object constraints, int index)
  {
    if (component instanceof UIResource)
      super.add(component);
    else
      {
1194 1195 1196 1197 1198 1199
        if (constraints instanceof String)
          insertTab((String) constraints, null, component, null, index);
        else
          insertTab(component.getName(),
                    (constraints instanceof Icon) ? (Icon) constraints : null,
                    component, null, index);
Tom Tromey committed
1200 1201 1202 1203
      }
  }

  /**
1204 1205
   * Removes the tab at index. After the component associated with
   * index is removed, its visibility is reset to true to ensure it
1206
   * will be visible if added to other containers.
Tom Tromey committed
1207 1208 1209 1210 1211 1212
   *
   * @param index The index of the tab to remove.
   */
  public void removeTabAt(int index)
  {
    checkIndex(index, 0, tabs.size());
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231

    // We need to adjust the selection if we remove a tab that comes
    // before the selected tab or if the selected tab is removed.
    // This decrements the selected index by 1 if any of this is the case.
    // Note that this covers all cases:
    // - When the selected tab comes after the removed tab, this simply
    //   adjusts the selection so that after the removal the selected tab
    //   is still the same.
    // - When we remove the currently selected tab, then the tab before the
    //   selected tab gets selected.
    // - When the last tab is removed, then we have an index==0, which gets
    //   decremented to -1, which means no selection, which is 100% perfect.
    int selectedIndex = getSelectedIndex();
    if (selectedIndex >= index)
      setSelectedIndex(selectedIndex - 1);

    Component comp = getComponentAt(index);

    // Remove the tab object.
Tom Tromey committed
1232
    tabs.remove(index);
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

    // Remove the component. I think we cannot assume that the tab order
    // is equal to the component order, so we iterate over the children
    // here to find the and remove the correct component.
    if (comp != null)
      {
        Component[] children = getComponents();
        for (int i = children.length - 1; i >= 0; --i)
          {
            if (children[i] == comp)
              {
                super.remove(i);
                comp.setVisible(true);
                break;
              }
          }
      }
    revalidate();
    repaint();
Tom Tromey committed
1252 1253 1254
  }

  /**
1255
   * Removes the specified Component from the JTabbedPane.
Tom Tromey committed
1256 1257 1258 1259 1260
   *
   * @param component The Component to remove.
   */
  public void remove(Component component)
  {
1261 1262 1263
    // Since components implementing UIResource
    // are not added as regular tabs by the add()
    // methods we have to take special care when
1264
    // removing these object. Especially
1265 1266 1267 1268 1269 1270 1271 1272
    // Container.remove(Component) cannot be used
    // because it will call JTabbedPane.remove(int)
    // later which is overridden and can only
    // handle tab components.
    // This implementation can even cope with a
    // situation that someone called insertTab()
    // with a component that implements UIResource.
    int index = indexOfComponent(component);
1273

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
    // If the component is not a tab component
    // find out its Container-given index
    // and call that class' implementation
    // directly.
    if (index == -1)
      {
        Component[] cs = getComponents();
        for (int i = 0; i< cs.length; i++)
          if (cs[i] == component)
            super.remove(i);
      }
    else
      removeTabAt(index);
Tom Tromey committed
1287 1288 1289
  }

  /**
1290
   * Removes the tab and component which corresponds to the specified index.
Tom Tromey committed
1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304
   *
   * @param index The index of the tab to remove.
   */
  public void remove(int index)
  {
    removeTabAt(index);
  }

  /**
   * This method removes all tabs and associated components from the
   * JTabbedPane.
   */
  public void removeAll()
  {
1305 1306
    setSelectedIndex(-1);
    for (int i = getTabCount() - 1; i >= 0; i--)
Tom Tromey committed
1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
      removeTabAt(i);
  }

  /**
   * This method returns how many tabs are in the JTabbedPane.
   *
   * @return The number of tabs in the JTabbedPane.
   */
  public int getTabCount()
  {
    return tabs.size();
  }

  /**
   * This method returns the number of runs used  to paint the JTabbedPane.
   *
   * @return The number of runs.
   */
  public int getTabRunCount()
  {
    return ((TabbedPaneUI) ui).getTabRunCount(this);
  }

  /**
   * This method returns the tab title given the index.
   *
   * @param index The index of the tab.
   *
   * @return The title for the tab.
   */
  public String getTitleAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getTitle();
  }

  /**
   * This method returns the active icon given the index.
   *
   * @param index The index of the tab.
   *
   * @return The active icon for the tab.
   */
  public Icon getIconAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getIcon();
  }

  /**
   * This method returns the disabled icon given the index.
   *
   * @param index The index of the tab.
   *
   * @return The disabled icon for the tab.
   */
  public Icon getDisabledIconAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getDisabledIcon();
  }

  /**
   * This method returns the tooltip string for the tab.
   *
   * @param index The index of the tab.
   *
   * @return The tooltip string for the tab.
   */
  public String getToolTipTextAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getTip();
  }

  /**
   * This method returns the foreground color for the tab.
   *
   * @param index The index of the tab.
   *
   * @return The foreground color for the tab.
   */
  public Color getForegroundAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getForeground();
  }

  /**
   * This method returns the background color for the tab.
   *
   * @param index The index of the tab.
   *
   * @return The background color for the tab.
   */
  public Color getBackgroundAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getBackground();
  }

  /**
   * This method returns the component associated with the tab.
   *
   * @param index The index of the tab.
   *
   * @return The component associated with the tab.
   */
  public Component getComponentAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).getComponent();
  }

  /**
   * This method returns whether this tab is enabled. Disabled tabs cannot be
   * selected.
   *
   * @param index The index of the tab.
   *
   * @return Whether the tab is enabled.
   */
  public boolean isEnabledAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((Page) tabs.elementAt(index)).isEnabled();
  }

  /**
   * This method returns the mnemonic for the tab.
   *
   * @param tabIndex The index of the tab.
   *
   * @return The mnemonic for the tab.
   */
  public int getMnemonicAt(int tabIndex)
  {
    checkIndex(tabIndex, 0, tabs.size());
    return ((Page) tabs.elementAt(tabIndex)).getMnemonic();
  }

  /**
   * This method returns the mnemonic index for the tab.
   *
   * @param tabIndex The index of the tab.
   *
   * @return The mnemonic index for the tab.
   */
  public int getDisplayedMnemonicIndexAt(int tabIndex)
  {
    checkIndex(tabIndex, 0, tabs.size());
    return ((Page) tabs.elementAt(tabIndex)).getDisplayedMnemonicIndex();
  }

  /**
   * This method returns the bounds of the tab given the index.
   *
   * @param index The index of the tab.
   *
   * @return A rectangle describing the bounds of the tab.
   */
  public Rectangle getBoundsAt(int index)
  {
    checkIndex(index, 0, tabs.size());
    return ((TabbedPaneUI) ui).getTabBounds(this, index);
  }

  /**
   * This method sets the title of the tab.
   *
   * @param index The index of the tab.
   * @param title The new title.
   */
  public void setTitleAt(int index, String title)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setTitle(title);
  }

  /**
   * This method sets the icon of the tab.
   *
   * @param index The index of the tab.
   * @param icon The new icon.
   */
  public void setIconAt(int index, Icon icon)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setIcon(icon);
  }

  /**
   * This method sets the disabled icon of the tab.
   *
   * @param index The index of the tab.
   * @param disabledIcon The new disabled icon.
   */
  public void setDisabledIconAt(int index, Icon disabledIcon)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setDisabledIcon(disabledIcon);
  }

  /**
   * This method sets the tooltip text of the tab.
   *
   * @param index The index of the tab.
   * @param toolTipText The tooltip text.
   */
  public void setToolTipTextAt(int index, String toolTipText)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setTip(toolTipText);
  }

  /**
   * This method sets the background color of the tab.
   *
   * @param index The index of the tab.
   * @param background The background color of the tab.
   */
  public void setBackgroundAt(int index, Color background)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setBackground(background);
  }

  /**
   * This method sets the foreground color of the tab.
   *
   * @param index The index of the tab.
   * @param foreground The foreground color of the tab.
   */
  public void setForegroundAt(int index, Color foreground)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setForeground(foreground);
  }

  /**
   * This method sets whether the tab is enabled.
   *
   * @param index The index of the tab.
   * @param enabled Whether the tab is enabled.
   */
  public void setEnabledAt(int index, boolean enabled)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setEnabled(enabled);
  }

  /**
   * This method sets the component associated with the tab.
   *
   * @param index The index of the tab.
   * @param component The component associated with the tab.
   */
  public void setComponentAt(int index, Component component)
  {
    checkIndex(index, 0, tabs.size());
    ((Page) tabs.elementAt(index)).setComponent(component);
  }

  /**
   * This method sets the displayed mnemonic index of the tab.
   *
   * @param tabIndex The index of the tab.
   * @param mnemonicIndex The mnemonic index.
   */
  public void setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex)
  {
    checkIndex(tabIndex, 0, tabs.size());
    ((Page) tabs.elementAt(tabIndex)).setDisplayedMnemonicIndex(mnemonicIndex);
  }

  /**
   * This method sets the mnemonic for the tab.
   *
   * @param tabIndex The index of the tab.
   * @param mnemonic The mnemonic.
   */
  public void setMnemonicAt(int tabIndex, int mnemonic)
  {
    checkIndex(tabIndex, 0, tabs.size());
    ((Page) tabs.elementAt(tabIndex)).setMnemonic(mnemonic);
  }

  /**
   * This method finds the index of a tab given the title.
   *
   * @param title The title that belongs to a tab.
   *
   * @return The index of the tab that has the title or -1 if not found.
   */
  public int indexOfTab(String title)
  {
    int index = -1;
    for (int i = 0; i < tabs.size(); i++)
      {
1606 1607 1608 1609 1610
        if (((Page) tabs.elementAt(i)).getTitle().equals(title))
          {
            index = i;
            break;
          }
Tom Tromey committed
1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626
      }
    return index;
  }

  /**
   * This method finds the index of a tab given the icon.
   *
   * @param icon The icon that belongs to a tab.
   *
   * @return The index of the tab that has the icon or -1 if not found.
   */
  public int indexOfTab(Icon icon)
  {
    int index = -1;
    for (int i = 0; i < tabs.size(); i++)
      {
1627 1628 1629 1630 1631
        if (((Page) tabs.elementAt(i)).getIcon() == icon)
          {
            index = i;
            break;
          }
Tom Tromey committed
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
      }
    return index;
  }

  /**
   * This method finds the index of a tab given the component.
   *
   * @param component A component associated with a tab.
   *
   * @return The index of the tab that has this component or -1 if not found.
   */
  public int indexOfComponent(Component component)
  {
    int index = -1;
    for (int i = 0; i < tabs.size(); i++)
      {
1648 1649 1650 1651 1652
        if (((Page) tabs.elementAt(i)).getComponent() == component)
          {
            index = i;
            break;
          }
Tom Tromey committed
1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
      }
    return index;
  }

  /**
   * This method returns a tab index given an (x,y) location. The origin of
   * the (x,y) pair will be the JTabbedPane's top left position. The  tab
   * returned will be the one that contains the point. This method is
   * delegated to the UI.
   *
   * @param x The x coordinate of the point.
   * @param y The y coordinate of the point.
   *
   * @return The index of the tab that contains the point.
   */
  public int indexAtLocation(int x, int y)
  {
    return ((TabbedPaneUI) ui).tabForCoordinate(this, x, y);
  }

  /**
   * This method returns the tooltip text given a mouse event.
   *
   * @param event The mouse event.
   *
   * @return The tool tip text that is associated with this mouse event.
   */
  public String getToolTipText(MouseEvent event)
  {
    int index = indexAtLocation(event.getX(), event.getY());
    return ((Page) tabs.elementAt(index)).getTip();
  }

  /**
1687 1688 1689
   * Returns a string describing the attributes for the
   * <code>JTabbedPane</code> component, for use in debugging.  The return
   * value is guaranteed to be non-<code>null</code>, but the format of the
1690
   * string may vary between implementations.
Tom Tromey committed
1691
   *
1692
   * @return A string describing the attributes of the
1693
   *     <code>JTabbedPane</code>.
Tom Tromey committed
1694 1695 1696
   */
  protected String paramString()
  {
1697
    CPStringBuilder sb = new CPStringBuilder(super.paramString());
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707
    sb.append(",tabPlacement=");
    if (tabPlacement == TOP)
      sb.append("TOP");
    if (tabPlacement == BOTTOM)
      sb.append("BOTTOM");
    if (tabPlacement == LEFT)
      sb.append("LEFT");
    if (tabPlacement == RIGHT)
      sb.append("RIGHT");
    return sb.toString();
Tom Tromey committed
1708 1709 1710
  }

  /**
1711 1712
   * Returns the object that provides accessibility features for this
   * <code>JTabbedPane</code> component.
Tom Tromey committed
1713
   *
1714
   * @return The accessible context (an instance of
1715
   *         {@link AccessibleJTabbedPane}).
Tom Tromey committed
1716 1717 1718 1719
   */
  public AccessibleContext getAccessibleContext()
  {
    if (accessibleContext == null)
1720 1721 1722 1723 1724 1725
      {
        AccessibleJTabbedPane ctx = new AccessibleJTabbedPane();
        addChangeListener(ctx);
        accessibleContext = ctx;
      }

Tom Tromey committed
1726 1727 1728
    return accessibleContext;
  }
}