1 7 8 package javax.swing; 9 10 import java.awt.*; 11 import java.awt.event.*; 12 import java.beans.*; 13 import java.util.*; 14 import javax.swing.event.*; 15 import javax.swing.plaf.*; 16 import javax.accessibility.*; 17 18 import java.io.Serializable ; 19 import java.io.ObjectOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.IOException ; 22 23 64 public class JTabbedPane extends JComponent 65 implements Serializable , Accessible, SwingConstants { 66 67 71 public static final int WRAP_TAB_LAYOUT = 0; 72 73 79 public static final int SCROLL_TAB_LAYOUT = 1; 80 81 82 86 private static final String uiClassID = "TabbedPaneUI"; 87 88 92 protected int tabPlacement = TOP; 93 94 private int tabLayoutPolicy; 95 96 97 protected SingleSelectionModel model; 98 99 private boolean haveRegistered; 100 101 105 protected ChangeListener changeListener = null; 106 107 Vector pages; 108 109 115 protected transient ChangeEvent changeEvent = null; 116 117 122 public JTabbedPane() { 123 this(TOP, WRAP_TAB_LAYOUT); 124 } 125 126 134 public JTabbedPane(int tabPlacement) { 135 this(tabPlacement, WRAP_TAB_LAYOUT); 136 } 137 138 153 public JTabbedPane(int tabPlacement, int tabLayoutPolicy) { 154 setTabPlacement(tabPlacement); 155 setTabLayoutPolicy(tabLayoutPolicy); 156 pages = new Vector(1); 157 setModel(new DefaultSingleSelectionModel ()); 158 updateUI(); 159 } 160 161 167 public TabbedPaneUI getUI() { 168 return (TabbedPaneUI)ui; 169 } 170 171 182 public void setUI(TabbedPaneUI ui) { 183 super.setUI(ui); 184 for (int i = 0; i < getTabCount(); i++) { 186 Icon icon = ((Page)pages.elementAt(i)).disabledIcon; 187 if (icon instanceof UIResource) { 188 setDisabledIconAt(i, null); 189 } 190 } 191 } 192 193 198 public void updateUI() { 199 setUI((TabbedPaneUI)UIManager.getUI(this)); 200 } 201 202 203 211 public String getUIClassID() { 212 return uiClassID; 213 } 214 215 216 220 protected class ModelListener implements ChangeListener, Serializable { 221 public void stateChanged(ChangeEvent e) { 222 fireStateChanged(); 223 } 224 } 225 226 233 protected ChangeListener createChangeListener() { 234 return new ModelListener(); 235 } 236 237 244 public void addChangeListener(ChangeListener l) { 245 listenerList.add(ChangeListener.class, l); 246 } 247 248 255 public void removeChangeListener(ChangeListener l) { 256 listenerList.remove(ChangeListener.class, l); 257 } 258 259 267 public ChangeListener[] getChangeListeners() { 268 return (ChangeListener[])listenerList.getListeners( 269 ChangeListener.class); 270 } 271 272 280 protected void fireStateChanged() { 281 Object [] listeners = listenerList.getListenerList(); 283 for (int i = listeners.length-2; i>=0; i-=2) { 286 if (listeners[i]==ChangeListener.class) { 287 if (changeEvent == null) 289 changeEvent = new ChangeEvent(this); 290 ((ChangeListener)listeners[i+1]).stateChanged(changeEvent); 291 } 292 } 293 } 294 295 300 public SingleSelectionModel getModel() { 301 return model; 302 } 303 304 313 public void setModel(SingleSelectionModel model) { 314 SingleSelectionModel oldModel = getModel(); 315 316 if (oldModel != null) { 317 oldModel.removeChangeListener(changeListener); 318 changeListener = null; 319 } 320 321 this.model = model; 322 323 if (model != null) { 324 changeListener = createChangeListener(); 325 model.addChangeListener(changeListener); 326 } 327 328 firePropertyChange("model", oldModel, model); 329 repaint(); 330 } 331 332 336 public int getTabPlacement() { 337 return tabPlacement; 338 } 339 340 365 public void setTabPlacement(int tabPlacement) { 366 if (tabPlacement != TOP && tabPlacement != LEFT && 367 tabPlacement != BOTTOM && tabPlacement != RIGHT) { 368 throw new IllegalArgumentException ("illegal tab placement: must be TOP, BOTTOM, LEFT, or RIGHT"); 369 } 370 if (this.tabPlacement != tabPlacement) { 371 int oldValue = this.tabPlacement; 372 this.tabPlacement = tabPlacement; 373 firePropertyChange("tabPlacement", oldValue, tabPlacement); 374 revalidate(); 375 repaint(); 376 } 377 } 378 379 385 public int getTabLayoutPolicy() { 386 return tabLayoutPolicy; 387 } 388 389 419 public void setTabLayoutPolicy(int tabLayoutPolicy) { 420 if (tabLayoutPolicy != WRAP_TAB_LAYOUT && tabLayoutPolicy != SCROLL_TAB_LAYOUT) { 421 throw new IllegalArgumentException ("illegal tab layout policy: must be WRAP_TAB_LAYOUT or SCROLL_TAB_LAYOUT"); 422 } 423 if (this.tabLayoutPolicy != tabLayoutPolicy) { 424 int oldValue = this.tabLayoutPolicy; 425 this.tabLayoutPolicy = tabLayoutPolicy; 426 firePropertyChange("tabLayoutPolicy", oldValue, tabLayoutPolicy); 427 revalidate(); 428 repaint(); 429 } 430 } 431 432 439 public int getSelectedIndex() { 440 return model.getSelectedIndex(); 441 } 442 443 460 public void setSelectedIndex(int index) { 461 if (index != -1) { 462 checkIndex(index); 463 } 464 setSelectedIndexImpl(index); 465 } 466 467 468 private void setSelectedIndexImpl(int index) { 469 int oldIndex = model.getSelectedIndex(); 470 Page oldPage = null, newPage = null; 471 if ((oldIndex >= 0) && (oldIndex != index)) { 472 oldPage = (Page) pages.elementAt(oldIndex); 473 } 474 if ((index >= 0) && (oldIndex != index)) { 475 newPage = (Page) pages.elementAt(index); 476 } 477 478 model.setSelectedIndex(index); 479 480 String oldName = null; 481 String newName = null; 482 483 if (oldPage != null) { 484 oldPage.firePropertyChange( 485 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 486 AccessibleState.SELECTED, null); 487 AccessibleContext ac = oldPage.getAccessibleContext(); 489 if (ac != null) { 490 oldName = ac.getAccessibleName(); 491 } 492 493 } 494 if (newPage != null) { 495 newPage.firePropertyChange( 496 AccessibleContext.ACCESSIBLE_STATE_PROPERTY, 497 null, AccessibleState.SELECTED); 498 AccessibleContext ac = newPage.getAccessibleContext(); 500 if (ac != null) { 501 newName = ac.getAccessibleName(); 502 } 503 504 } 505 506 if (newName != null) { 508 getAccessibleContext().firePropertyChange( 509 AccessibleContext.ACCESSIBLE_NAME_PROPERTY, oldName, newName); 510 } 511 512 } 513 514 521 public Component getSelectedComponent() { 522 int index = getSelectedIndex(); 523 if (index == -1) { 524 return null; 525 } 526 return getComponentAt(index); 527 } 528 529 541 public void setSelectedComponent(Component c) { 542 int index = indexOfComponent(c); 543 if (index != -1) { 544 setSelectedIndex(index); 545 } else { 546 throw new IllegalArgumentException ("component not found in tabbed pane"); 547 } 548 } 549 550 566 public void insertTab(String title, Icon icon, Component component, String tip, int index) { 567 int newIndex = index; 568 569 int removeIndex = indexOfComponent(component); 576 if (component != null && removeIndex != -1) { 577 removeTabAt(removeIndex); 578 if (newIndex > removeIndex) { 579 newIndex--; 580 } 581 } 582 583 int selectedIndex = getSelectedIndex(); 584 585 pages.insertElementAt(new Page(this, title != null? title : "", icon, null, 586 component, tip), newIndex); 587 588 589 if (component != null) { 590 addImpl(component, null, -1); 591 component.setVisible(false); 592 } 593 594 if (pages.size() == 1) { 595 setSelectedIndex(0); 596 } 597 598 if (selectedIndex >= newIndex) { 599 setSelectedIndex(selectedIndex + 1); 600 } 601 602 if (!haveRegistered && tip != null) { 603 ToolTipManager.sharedInstance().registerComponent(this); 604 haveRegistered = true; 605 } 606 607 if (accessibleContext != null) { 608 accessibleContext.firePropertyChange( 609 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 610 null, component); 611 } 612 revalidate(); 613 repaint(); 614 } 615 616 630 public void addTab(String title, Icon icon, Component component, String tip) { 631 insertTab(title, icon, component, tip, pages.size()); 632 } 633 634 646 public void addTab(String title, Icon icon, Component component) { 647 insertTab(title, icon, component, null, pages.size()); 648 } 649 650 661 public void addTab(String title, Component component) { 662 insertTab(title, null, component, null, pages.size()); 663 } 664 665 677 public Component add(Component component) { 678 if (!(component instanceof UIResource)) { 679 addTab(component.getName(), component); 680 } else { 681 super.add(component); 682 } 683 return component; 684 } 685 686 697 public Component add(String title, Component component) { 698 if (!(component instanceof UIResource)) { 699 addTab(title, component); 700 } else { 701 super.add(title, component); 702 } 703 return component; 704 } 705 706 718 public Component add(Component component, int index) { 719 if (!(component instanceof UIResource)) { 720 insertTab(component.getName(), null, component, null, 723 index == -1? getTabCount() : index); 724 } else { 725 super.add(component, index); 726 } 727 return component; 728 } 729 730 743 public void add(Component component, Object constraints) { 744 if (!(component instanceof UIResource)) { 745 if (constraints instanceof String ) { 746 addTab((String )constraints, component); 747 } else if (constraints instanceof Icon ) { 748 addTab(null, (Icon )constraints, component); 749 } else { 750 add(component); 751 } 752 } else { 753 super.add(component, constraints); 754 } 755 } 756 757 771 public void add(Component component, Object constraints, int index) { 772 if (!(component instanceof UIResource)) { 773 774 Icon icon = constraints instanceof Icon ? (Icon )constraints : null; 775 String title = constraints instanceof String ? (String )constraints : null; 776 insertTab(title, icon, component, null, index == -1? getTabCount() : index); 779 } else { 780 super.add(component, constraints, index); 781 } 782 } 783 784 796 public void removeTabAt(int index) { 797 checkIndex(index); 798 799 int tabCount = getTabCount(); 803 int selected = getSelectedIndex(); 804 if (selected >= (tabCount - 1)) { 805 setSelectedIndexImpl(selected - 1); 806 } 807 808 Component component = getComponentAt(index); 809 810 if (accessibleContext != null) { 811 accessibleContext.firePropertyChange( 812 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 813 component, null); 814 } 815 816 pages.removeElementAt(index); 817 818 putClientProperty("__index_to_remove__", new Integer (index)); 824 825 if (component != null) { 829 Component components[] = getComponents(); 830 for (int i = components.length; --i >= 0; ) { 831 if (components[i] == component) { 832 super.remove(i); 833 component.setVisible(true); 834 break; 835 } 836 } 837 } 838 839 revalidate(); 840 repaint(); 841 } 842 843 852 public void remove(Component component) { 853 int index = indexOfComponent(component); 854 if (index != -1) { 855 removeTabAt(index); 856 } else { 857 Component children[] = getComponents(); 860 for (int i=0; i < children.length; i++) { 861 if (component == children[i]) { 862 super.remove(i); 863 break; 864 } 865 } 866 } 867 } 868 869 879 public void remove(int index) { 880 removeTabAt(index); 881 } 882 883 890 public void removeAll() { 891 setSelectedIndexImpl(-1); 892 893 int tabCount = getTabCount(); 894 while (tabCount-- > 0) { 897 removeTabAt(tabCount); 898 } 899 } 900 901 906 public int getTabCount() { 907 return pages.size(); 908 } 909 910 921 public int getTabRunCount() { 922 if (ui != null) { 923 return ((TabbedPaneUI)ui).getTabRunCount(this); 924 } 925 return 0; 926 } 927 928 929 931 940 public String getTitleAt(int index) { 941 return ((Page)pages.elementAt(index)).title; 942 } 943 944 954 public Icon getIconAt(int index) { 955 return ((Page)pages.elementAt(index)).icon; 956 } 957 958 973 public Icon getDisabledIconAt(int index) { 974 Page page = ((Page)pages.elementAt(index)); 975 if (page.disabledIcon == null) { 976 page.disabledIcon = UIManager.getLookAndFeel().getDisabledIcon(this, page.icon); 977 } 978 return page.disabledIcon; 979 } 980 981 991 public String getToolTipTextAt(int index) { 992 return ((Page)pages.elementAt(index)).tip; 993 } 994 995 1006 public Color getBackgroundAt(int index) { 1007 return ((Page)pages.elementAt(index)).getBackground(); 1008 } 1009 1010 1021 public Color getForegroundAt(int index) { 1022 return ((Page)pages.elementAt(index)).getForeground(); 1023 } 1024 1025 1037 public boolean isEnabledAt(int index) { 1038 return ((Page)pages.elementAt(index)).isEnabled(); 1039 } 1040 1041 1051 public Component getComponentAt(int index) { 1052 return ((Page)pages.elementAt(index)).component; 1053 } 1054 1055 1071 public int getMnemonicAt(int tabIndex) { 1072 checkIndex(tabIndex); 1073 1074 Page page = (Page)pages.elementAt(tabIndex); 1075 return page.getMnemonic(); 1076 } 1077 1078 1092 public int getDisplayedMnemonicIndexAt(int tabIndex) { 1093 checkIndex(tabIndex); 1094 1095 Page page = (Page)pages.elementAt(tabIndex); 1096 return page.getDisplayedMnemonicIndex(); 1097 } 1098 1099 1114 public Rectangle getBoundsAt(int index) { 1115 checkIndex(index); 1116 if (ui != null) { 1117 return ((TabbedPaneUI)ui).getTabBounds(this, index); 1118 } 1119 return null; 1120 } 1121 1122 1123 1125 1141 public void setTitleAt(int index, String title) { 1142 Page page = (Page)pages.elementAt(index); 1143 String oldTitle =page.title; 1144 page.title = title; 1145 1146 if (oldTitle != title) { 1147 firePropertyChange("indexForTitle", -1, index); 1148 } 1149 page.updateDisplayedMnemonicIndex(); 1150 if ((oldTitle != title) && (accessibleContext != null)) { 1151 accessibleContext.firePropertyChange( 1152 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 1153 oldTitle, title); 1154 } 1155 if (title == null || oldTitle == null || 1156 !title.equals(oldTitle)) { 1157 revalidate(); 1158 repaint(); 1159 } 1160 } 1161 1162 1183 public void setIconAt(int index, Icon icon) { 1184 Page page = (Page)pages.elementAt(index); 1185 Icon oldIcon = page.icon; 1186 if (icon != oldIcon) { 1187 page.icon = icon; 1188 1189 1193 if (page.disabledIcon instanceof UIResource) { 1194 page.disabledIcon = null; 1195 } 1196 1197 if (accessibleContext != null) { 1199 accessibleContext.firePropertyChange( 1200 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 1201 oldIcon, icon); 1202 } 1203 revalidate(); 1204 repaint(); 1205 } 1206 } 1207 1208 1224 public void setDisabledIconAt(int index, Icon disabledIcon) { 1225 Icon oldIcon = ((Page)pages.elementAt(index)).disabledIcon; 1226 ((Page)pages.elementAt(index)).disabledIcon = disabledIcon; 1227 if (disabledIcon != oldIcon && !isEnabledAt(index)) { 1228 revalidate(); 1229 repaint(); 1230 } 1231 } 1232 1233 1248 public void setToolTipTextAt(int index, String toolTipText) { 1249 String oldToolTipText =((Page)pages.elementAt(index)).tip; 1250 ((Page)pages.elementAt(index)).tip = toolTipText; 1251 1252 if ((oldToolTipText != toolTipText) && (accessibleContext != null)) { 1253 accessibleContext.firePropertyChange( 1254 AccessibleContext.ACCESSIBLE_VISIBLE_DATA_PROPERTY, 1255 oldToolTipText, toolTipText); 1256 } 1257 if (!haveRegistered && toolTipText != null) { 1258 ToolTipManager.sharedInstance().registerComponent(this); 1259 haveRegistered = true; 1260 } 1261 } 1262 1263 1280 public void setBackgroundAt(int index, Color background) { 1281 Color oldBg = ((Page)pages.elementAt(index)).background; 1282 ((Page)pages.elementAt(index)).setBackground(background); 1283 if (background == null || oldBg == null || 1284 !background.equals(oldBg)) { 1285 Rectangle tabBounds = getBoundsAt(index); 1286 if (tabBounds != null) { 1287 repaint(tabBounds); 1288 } 1289 } 1290 } 1291 1292 1310 public void setForegroundAt(int index, Color foreground) { 1311 Color oldFg = ((Page)pages.elementAt(index)).foreground; 1312 ((Page)pages.elementAt(index)).setForeground(foreground); 1313 if (foreground == null || oldFg == null || 1314 !foreground.equals(oldFg)) { 1315 Rectangle tabBounds = getBoundsAt(index); 1316 if (tabBounds != null) { 1317 repaint(tabBounds); 1318 } 1319 } 1320 } 1321 1322 1333 public void setEnabledAt(int index, boolean enabled) { 1334 boolean oldEnabled = ((Page)pages.elementAt(index)).isEnabled(); 1335 ((Page)pages.elementAt(index)).setEnabled(enabled); 1336 if (enabled != oldEnabled) { 1337 revalidate(); 1338 repaint(); 1339 } 1340 } 1341 1342 1356 public void setComponentAt(int index, Component component) { 1357 Page page = (Page)pages.elementAt(index); 1358 if (component != page.component) { 1359 if (page.component != null) { 1360 synchronized(getTreeLock()) { 1363 int count = getComponentCount(); 1364 Component children[] = getComponents(); 1365 for (int i = 0; i < count; i++) { 1366 if (children[i] == page.component) { 1367 super.remove(i); 1368 } 1369 } 1370 } 1371 } 1372 page.component = component; 1373 component.setVisible(getSelectedIndex() == index); 1374 addImpl(component, null, -1); 1375 1376 revalidate(); 1377 } 1378 } 1379 1380 1418 public void setDisplayedMnemonicIndexAt(int tabIndex, int mnemonicIndex) { 1419 checkIndex(tabIndex); 1420 1421 Page page = (Page)pages.elementAt(tabIndex); 1422 1423 page.setDisplayedMnemonicIndex(mnemonicIndex); 1424 } 1425 1426 1456 public void setMnemonicAt(int tabIndex, int mnemonic) { 1457 checkIndex(tabIndex); 1458 1459 Page page = (Page)pages.elementAt(tabIndex); 1460 page.setMnemonic(mnemonic); 1461 1462 firePropertyChange("mnemonicAt", null, null); 1463 } 1464 1465 1467 1475 public int indexOfTab(String title) { 1476 for(int i = 0; i < getTabCount(); i++) { 1477 if (getTitleAt(i).equals(title == null? "" : title)) { 1478 return i; 1479 } 1480 } 1481 return -1; 1482 } 1483 1484 1492 public int indexOfTab(Icon icon) { 1493 for(int i = 0; i < getTabCount(); i++) { 1494 Icon tabIcon = getIconAt(i); 1495 if ((tabIcon != null && tabIcon.equals(icon)) || 1496 (tabIcon == null && tabIcon == icon)) { 1497 return i; 1498 } 1499 } 1500 return -1; 1501 } 1502 1503 1511 public int indexOfComponent(Component component) { 1512 for(int i = 0; i < getTabCount(); i++) { 1513 Component c = getComponentAt(i); 1514 if ((c != null && c.equals(component)) || 1515 (c == null && c == component)) { 1516 return i; 1517 } 1518 } 1519 return -1; 1520 } 1521 1522 1533 public int indexAtLocation(int x, int y) { 1534 if (ui != null) { 1535 return ((TabbedPaneUI)ui).tabForCoordinate(this, x, y); 1536 } 1537 return -1; 1538 } 1539 1540 1541 1549 public String getToolTipText(MouseEvent event) { 1550 if (ui != null) { 1551 int index = ((TabbedPaneUI)ui).tabForCoordinate(this, event.getX(), event.getY()); 1552 1553 if (index != -1) { 1554 return ((Page)pages.elementAt(index)).tip; 1555 } 1556 } 1557 return super.getToolTipText(event); 1558 } 1559 1560 private void checkIndex(int index) { 1561 if (index < 0 || index >= pages.size()) { 1562 throw new IndexOutOfBoundsException ("Index: "+index+", Tab count: "+pages.size()); 1563 } 1564 } 1565 1566 1567 1572 private void writeObject(ObjectOutputStream s) throws IOException { 1573 s.defaultWriteObject(); 1574 if (getUIClassID().equals(uiClassID)) { 1575 byte count = JComponent.getWriteObjCounter(this); 1576 JComponent.setWriteObjCounter(this, --count); 1577 if (count == 0 && ui != null) { 1578 ui.installUI(this); 1579 } 1580 } 1581 } 1582 1583 1587 void compWriteObjectNotify() { 1588 super.compWriteObjectNotify(); 1589 if (getToolTipText() == null && haveRegistered) { 1592 ToolTipManager.sharedInstance().unregisterComponent(this); 1593 } 1594 } 1595 1596 1601 private void readObject(ObjectInputStream s) 1602 throws IOException , ClassNotFoundException 1603 { 1604 s.defaultReadObject(); 1605 if ((ui != null) && (getUIClassID().equals(uiClassID))) { 1606 ui.installUI(this); 1607 } 1608 if (getToolTipText() == null && haveRegistered) { 1611 ToolTipManager.sharedInstance().registerComponent(this); 1612 } 1613 } 1614 1615 1616 1626 protected String paramString() { 1627 String tabPlacementString; 1628 if (tabPlacement == TOP) { 1629 tabPlacementString = "TOP"; 1630 } else if (tabPlacement == BOTTOM) { 1631 tabPlacementString = "BOTTOM"; 1632 } else if (tabPlacement == LEFT) { 1633 tabPlacementString = "LEFT"; 1634 } else if (tabPlacement == RIGHT) { 1635 tabPlacementString = "RIGHT"; 1636 } else tabPlacementString = ""; 1637 String haveRegisteredString = (haveRegistered ? 1638 "true" : "false"); 1639 1640 return super.paramString() + 1641 ",haveRegistered=" + haveRegisteredString + 1642 ",tabPlacement=" + tabPlacementString; 1643 } 1644 1645 1649 1658 public AccessibleContext getAccessibleContext() { 1659 if (accessibleContext == null) { 1660 accessibleContext = new AccessibleJTabbedPane(); 1661 } 1662 return accessibleContext; 1663 } 1664 1665 1680 protected class AccessibleJTabbedPane extends AccessibleJComponent 1681 implements AccessibleSelection, ChangeListener { 1682 1683 1686 public AccessibleJTabbedPane() { 1687 super(); 1688 JTabbedPane.this.model.addChangeListener(this); 1689 } 1690 1691 public void stateChanged(ChangeEvent e) { 1692 Object o = e.getSource(); 1693 firePropertyChange(AccessibleContext.ACCESSIBLE_SELECTION_PROPERTY, 1694 null, o); 1695 } 1696 1697 1703 public AccessibleRole getAccessibleRole() { 1704 return AccessibleRole.PAGE_TAB_LIST; 1705 } 1706 1707 1712 public int getAccessibleChildrenCount() { 1713 return getTabCount(); 1714 } 1715 1716 1723 public Accessible getAccessibleChild(int i) { 1724 if (i < 0 || i >= getTabCount()) { 1725 return null; 1726 } 1727 return (Accessible) pages.elementAt(i); 1728 } 1729 1730 1739 public AccessibleSelection getAccessibleSelection() { 1740 return this; 1741 } 1742 1743 1751 public Accessible getAccessibleAt(Point p) { 1752 int tab = ((TabbedPaneUI) ui).tabForCoordinate(JTabbedPane.this, 1753 p.x, p.y); 1754 if (tab == -1) { 1755 tab = getSelectedIndex(); 1756 } 1757 return getAccessibleChild(tab); 1758 } 1759 1760 public int getAccessibleSelectionCount() { 1761 return 1; 1762 } 1763 1764 public Accessible getAccessibleSelection(int i) { 1765 int index = getSelectedIndex(); 1766 if (index == -1) { 1767 return null; 1768 } 1769 return (Accessible) pages.elementAt(index); 1770 } 1771 1772 public boolean isAccessibleChildSelected(int i) { 1773 return (i == getSelectedIndex()); 1774 } 1775 1776 public void addAccessibleSelection(int i) { 1777 setSelectedIndex(i); 1778 } 1779 1780 public void removeAccessibleSelection(int i) { 1781 } 1783 1784 public void clearAccessibleSelection() { 1785 } 1787 1788 public void selectAllAccessibleSelection() { 1789 } 1791 } 1792 1793 private class Page extends AccessibleContext 1794 implements Serializable , Accessible, AccessibleComponent { 1795 String title; 1796 Color background; 1797 Color foreground; 1798 Icon icon; 1799 Icon disabledIcon; 1800 JTabbedPane parent; 1801 Component component; 1802 String tip; 1803 boolean enabled = true; 1804 boolean needsUIUpdate; 1805 int mnemonic = -1; 1806 int mnemonicIndex = -1; 1807 1808 Page(JTabbedPane parent, 1809 String title, Icon icon, Icon disabledIcon, Component component, String tip) { 1810 this.title = title; 1811 this.icon = icon; 1812 this.disabledIcon = disabledIcon; 1813 this.parent = parent; 1814 this.setAccessibleParent(parent); 1815 this.component = component; 1816 this.tip = tip; 1817 if (component instanceof Accessible) { 1818 AccessibleContext ac; 1819 ac = ((Accessible) component).getAccessibleContext(); 1820 if (ac != null) { 1821 ac.setAccessibleParent(this); 1822 } 1823 } 1824 } 1825 1826 void setMnemonic(int mnemonic) { 1827 this.mnemonic = mnemonic; 1828 updateDisplayedMnemonicIndex(); 1829 } 1830 1831 int getMnemonic() { 1832 return mnemonic; 1833 } 1834 1835 1838 void setDisplayedMnemonicIndex(int mnemonicIndex) { 1839 if (this.mnemonicIndex != mnemonicIndex) { 1840 if (mnemonicIndex != -1 && (title == null || 1841 mnemonicIndex < 0 || 1842 mnemonicIndex >= title.length())) { 1843 throw new IllegalArgumentException ( 1844 "Invalid mnemonic index: " + mnemonicIndex); 1845 } 1846 this.mnemonicIndex = mnemonicIndex; 1847 JTabbedPane.this.firePropertyChange("displayedMnemonicIndexAt", 1848 null, null); 1849 } 1850 } 1851 1852 1855 int getDisplayedMnemonicIndex() { 1856 return this.mnemonicIndex; 1857 } 1858 1859 void updateDisplayedMnemonicIndex() { 1860 setDisplayedMnemonicIndex( 1861 SwingUtilities.findDisplayedMnemonicIndex(title, mnemonic)); 1862 } 1863 1864 1868 public AccessibleContext getAccessibleContext() { 1869 return this; 1870 } 1871 1872 1873 1875 public String getAccessibleName() { 1876 if (accessibleName != null) { 1877 return accessibleName; 1878 } else if (title != null) { 1879 return title; 1880 } 1881 return null; 1882 } 1883 1884 public String getAccessibleDescription() { 1885 if (accessibleDescription != null) { 1886 return accessibleDescription; 1887 } else if (tip != null) { 1888 return tip; 1889 } 1890 return null; 1891 } 1892 1893 public AccessibleRole getAccessibleRole() { 1894 return AccessibleRole.PAGE_TAB; 1895 } 1896 1897 public AccessibleStateSet getAccessibleStateSet() { 1898 AccessibleStateSet states; 1899 states = parent.getAccessibleContext().getAccessibleStateSet(); 1900 states.add(AccessibleState.SELECTABLE); 1901 int i = parent.indexOfTab(title); 1902 if (i == parent.getSelectedIndex()) { 1903 states.add(AccessibleState.SELECTED); 1904 } 1905 return states; 1906 } 1907 1908 public int getAccessibleIndexInParent() { 1909 return parent.indexOfTab(title); 1910 } 1911 1912 public int getAccessibleChildrenCount() { 1913 if (component instanceof Accessible) { 1914 return 1; 1915 } else { 1916 return 0; 1917 } 1918 } 1919 1920 public Accessible getAccessibleChild(int i) { 1921 if (component instanceof Accessible) { 1922 return (Accessible) component; 1923 } else { 1924 return null; 1925 } 1926 } 1927 1928 public Locale getLocale() { 1929 return parent.getLocale(); 1930 } 1931 1932 public AccessibleComponent getAccessibleComponent() { 1933 return this; 1934 } 1935 1936 1937 1939 public Color getBackground() { 1940 return background != null? background : parent.getBackground(); 1941 } 1942 1943 public void setBackground(Color c) { 1944 background = c; 1945 } 1946 1947 public Color getForeground() { 1948 return foreground != null? foreground : parent.getForeground(); 1949 } 1950 1951 public void setForeground(Color c) { 1952 foreground = c; 1953 } 1954 1955 public Cursor getCursor() { 1956 return parent.getCursor(); 1957 } 1958 1959 public void setCursor(Cursor c) { 1960 parent.setCursor(c); 1961 } 1962 1963 public Font getFont() { 1964 return parent.getFont(); 1965 } 1966 1967 public void setFont(Font f) { 1968 parent.setFont(f); 1969 } 1970 1971 public FontMetrics getFontMetrics(Font f) { 1972 return parent.getFontMetrics(f); 1973 } 1974 1975 public boolean isEnabled() { 1976 return enabled; 1977 } 1978 1979 public void setEnabled(boolean b) { 1980 enabled = b; 1981 } 1982 1983 public boolean isVisible() { 1984 return parent.isVisible(); 1985 } 1986 1987 public void setVisible(boolean b) { 1988 parent.setVisible(b); 1989 } 1990 1991 public boolean isShowing() { 1992 return parent.isShowing(); 1993 } 1994 1995 public boolean contains(Point p) { 1996 Rectangle r = getBounds(); 1997 return r.contains(p); 1998 } 1999 2000 public Point getLocationOnScreen() { 2001 Point parentLocation = parent.getLocationOnScreen(); 2002 Point componentLocation = getLocation(); 2003 componentLocation.translate(parentLocation.x, parentLocation.y); 2004 return componentLocation; 2005 } 2006 2007 public Point getLocation() { 2008 Rectangle r = getBounds(); 2009 return new Point(r.x, r.y); 2010 } 2011 2012 public void setLocation(Point p) { 2013 } 2015 2016 public Rectangle getBounds() { 2017 return parent.getUI().getTabBounds(parent, 2018 parent.indexOfTab(title)); 2019 } 2020 2021 public void setBounds(Rectangle r) { 2022 } 2024 2025 public Dimension getSize() { 2026 Rectangle r = getBounds(); 2027 return new Dimension(r.width, r.height); 2028 } 2029 2030 public void setSize(Dimension d) { 2031 } 2033 2034 public Accessible getAccessibleAt(Point p) { 2035 if (component instanceof Accessible) { 2036 return (Accessible) component; 2037 } else { 2038 return null; 2039 } 2040 } 2041 2042 public boolean isFocusTraversable() { 2043 return false; 2044 } 2045 2046 public void requestFocus() { 2047 } 2049 2050 public void addFocusListener(FocusListener l) { 2051 } 2053 2054 public void removeFocusListener(FocusListener l) { 2055 } 2057 2058 2067 public AccessibleIcon [] getAccessibleIcon() { 2068 AccessibleIcon accessibleIcon = null; 2069 if (enabled && icon instanceof ImageIcon ) { 2070 AccessibleContext ac = 2071 ((ImageIcon )icon).getAccessibleContext(); 2072 accessibleIcon = (AccessibleIcon)ac; 2073 } else if (!enabled && disabledIcon instanceof ImageIcon ) { 2074 AccessibleContext ac = 2075 ((ImageIcon )disabledIcon).getAccessibleContext(); 2076 accessibleIcon = (AccessibleIcon)ac; 2077 } 2078 if (accessibleIcon != null) { 2079 AccessibleIcon [] returnIcons = new AccessibleIcon[1]; 2080 returnIcons[0] = accessibleIcon; 2081 return returnIcons; 2082 } else { 2083 return null; 2084 } 2085 } 2086 } 2087} 2088 | Popular Tags |