< prev index next >

src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTabbedPaneUI.java

Print this page




 245         rolloverTabIndex = -1;
 246         focusIndex = -1;
 247         c.setLayout(createLayoutManager());
 248         installComponents();
 249         installDefaults();
 250         installListeners();
 251         installKeyboardActions();
 252     }
 253 
 254     public void uninstallUI(JComponent c) {
 255         uninstallKeyboardActions();
 256         uninstallListeners();
 257         uninstallDefaults();
 258         uninstallComponents();
 259         c.setLayout(null);
 260 
 261         this.tabPane = null;
 262     }
 263 
 264     /**
 265      * Invoked by <code>installUI</code> to create
 266      * a layout manager object to manage
 267      * the <code>JTabbedPane</code>.
 268      *
 269      * @return a layout manager object
 270      *
 271      * @see TabbedPaneLayout
 272      * @see javax.swing.JTabbedPane#getTabLayoutPolicy
 273      */
 274     protected LayoutManager createLayoutManager() {
 275         if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
 276             return new TabbedPaneScrollLayout();
 277         } else { /* WRAP_TAB_LAYOUT */
 278             return new TabbedPaneLayout();
 279         }
 280     }
 281 
 282     /* In an attempt to preserve backward compatibility for programs
 283      * which have extended BasicTabbedPaneUI to do their own layout, the
 284      * UI uses the installed layoutManager (and not tabLayoutPolicy) to
 285      * determine if scrollTabLayout is enabled.
 286      */
 287     private boolean scrollableTabLayoutEnabled() {


 607         mnemonicInputMap.put(KeyStroke.getKeyStroke(mnemonic, BasicLookAndFeel.getFocusAcceleratorKeyMask()),
 608                              "setSelectedIndex");
 609         mnemonicToIndexMap.put(Integer.valueOf(mnemonic), Integer.valueOf(index));
 610     }
 611 
 612     /**
 613      * Installs the state needed for mnemonics.
 614      */
 615     private void initMnemonics() {
 616         mnemonicToIndexMap = new Hashtable<Integer, Integer>();
 617         mnemonicInputMap = new ComponentInputMapUIResource(tabPane);
 618         mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane,
 619                               JComponent.WHEN_IN_FOCUSED_WINDOW));
 620         SwingUtilities.replaceUIInputMap(tabPane,
 621                               JComponent.WHEN_IN_FOCUSED_WINDOW,
 622                                          mnemonicInputMap);
 623     }
 624 
 625     /**
 626      * Sets the tab the mouse is over by location. This is a cover method
 627      * for <code>setRolloverTab(tabForCoordinate(x, y, false))</code>.
 628      */
 629     private void setRolloverTab(int x, int y) {
 630         // NOTE:
 631         // This calls in with false otherwise it could trigger a validate,
 632         // which should NOT happen if the user is only dragging the
 633         // mouse around.
 634         setRolloverTab(tabForCoordinate(tabPane, x, y, false));
 635     }
 636 
 637     /**
 638      * Sets the tab the mouse is currently over to <code>index</code>.
 639      * <code>index</code> will be -1 if the mouse is no longer over any
 640      * tab. No checking is done to ensure the passed in index identifies a
 641      * valid tab.
 642      *
 643      * @param index Index of the tab the mouse is over.
 644      * @since 1.5
 645      */
 646     protected void setRolloverTab(int index) {
 647         rolloverTabIndex = index;
 648     }
 649 
 650     /**
 651      * Returns the tab the mouse is currently over, or {@code -1} if the mouse is no
 652      * longer over any tab.
 653      *
 654      * @return the tab the mouse is currently over, or {@code -1} if the mouse is no
 655      * longer over any tab
 656      * @since 1.5
 657      */
 658     protected int getRolloverTab() {
 659         return rolloverTabIndex;


 754             if (view != null) {
 755                 int viewHeight = (int)view.getPreferredSpan(View.Y_AXIS);
 756                 int baseline = BasicHTML.getHTMLBaseline(
 757                     view, (int)view.getPreferredSpan(View.X_AXIS), viewHeight);
 758                 if (baseline >= 0) {
 759                     return maxTabHeight / 2 - viewHeight / 2 + baseline +
 760                         getBaselineOffset();
 761                 }
 762                 return -1;
 763             }
 764         }
 765         FontMetrics metrics = getFontMetrics();
 766         int fontHeight = metrics.getHeight();
 767         int fontBaseline = metrics.getAscent();
 768         return maxTabHeight / 2 - fontHeight / 2 + fontBaseline +
 769                 getBaselineOffset();
 770     }
 771 
 772     /**
 773      * Returns the amount the baseline is offset by.  This is typically
 774      * the same as <code>getTabLabelShiftY</code>.
 775      *
 776      * @return amount to offset the baseline by
 777      * @since 1.6
 778      */
 779     protected int getBaselineOffset() {
 780         switch(tabPane.getTabPlacement()) {
 781         case JTabbedPane.TOP:
 782             if (tabPane.getTabCount() > 1) {
 783                 return 1;
 784             }
 785             else {
 786                 return -1;
 787             }
 788         case JTabbedPane.BOTTOM:
 789             if (tabPane.getTabCount() > 1) {
 790                 return -1;
 791             }
 792             else {
 793                 return 1;
 794             }


 846         ensureCurrentLayout();
 847 
 848         // Paint content border and tab area
 849         if (tabsOverlapBorder) {
 850             paintContentBorder(g, tabPlacement, selectedIndex);
 851         }
 852         // If scrollable tabs are enabled, the tab area will be
 853         // painted by the scrollable tab panel instead.
 854         //
 855         if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT
 856             paintTabArea(g, tabPlacement, selectedIndex);
 857         }
 858         if (!tabsOverlapBorder) {
 859             paintContentBorder(g, tabPlacement, selectedIndex);
 860         }
 861     }
 862 
 863     /**
 864      * Paints the tabs in the tab area.
 865      * Invoked by paint().
 866      * The graphics parameter must be a valid <code>Graphics</code>
 867      * object.  Tab placement may be either:
 868      * <code>JTabbedPane.TOP</code>, <code>JTabbedPane.BOTTOM</code>,
 869      * <code>JTabbedPane.LEFT</code>, or <code>JTabbedPane.RIGHT</code>.
 870      * The selected index must be a valid tabbed pane tab index (0 to
 871      * tab count - 1, inclusive) or -1 if no tab is currently selected.
 872      * The handling of invalid parameters is unspecified.
 873      *
 874      * @param g the graphics object to use for rendering
 875      * @param tabPlacement the placement for the tabs within the JTabbedPane
 876      * @param selectedIndex the tab index of the selected component
 877      *
 878      * @since 1.4
 879      */
 880     protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
 881         int tabCount = tabPane.getTabCount();
 882 
 883         Rectangle iconRect = new Rectangle(),
 884                   textRect = new Rectangle();
 885         Rectangle clipRect = g.getClipBounds();
 886 
 887         // Paint tabRuns of tabs from back to front
 888         for (int i = runCount - 1; i >= 0; i--) {
 889             int start = tabRuns[i];


1695         int tabCount = tabPane.getTabCount();
1696         for (int i = 0; i < tabCount; i++) {
1697             if (rects[i].contains(p.x, p.y)) {
1698                 return i;
1699             }
1700         }
1701         return -1;
1702     }
1703 
1704     /**
1705      * Returns the bounds of the specified tab in the coordinate space
1706      * of the JTabbedPane component.  This is required because the tab rects
1707      * are by default defined in the coordinate space of the component where
1708      * they are rendered, which could be the JTabbedPane
1709      * (for WRAP_TAB_LAYOUT) or a ScrollableTabPanel (SCROLL_TAB_LAYOUT).
1710      * This method should be used whenever the tab rectangle must be relative
1711      * to the JTabbedPane itself and the result should be placed in a
1712      * designated Rectangle object (rather than instantiating and returning
1713      * a new Rectangle each time). The tab index parameter must be a valid
1714      * tabbed pane tab index (0 to tab count - 1, inclusive).  The destination
1715      * rectangle parameter must be a valid <code>Rectangle</code> instance.
1716      * The handling of invalid parameters is unspecified.
1717      *
1718      * @param tabIndex the index of the tab
1719      * @param dest the rectangle where the result should be placed
1720      * @return the resulting rectangle
1721      *
1722      * @since 1.4
1723      */
1724     protected Rectangle getTabBounds(int tabIndex, Rectangle dest) {
1725         dest.width = rects[tabIndex].width;
1726         dest.height = rects[tabIndex].height;
1727 
1728         if (scrollableTabLayoutEnabled()) { // SCROLL_TAB_LAYOUT
1729             // Need to translate coordinates based on viewport location &
1730             // view position
1731             Point vpp = tabScroller.viewport.getLocation();
1732             Point viewp = tabScroller.viewport.getViewPosition();
1733             dest.x = rects[tabIndex].x + vpp.x - viewp.x;
1734             dest.y = rects[tabIndex].y + vpp.y - viewp.y;
1735 




 245         rolloverTabIndex = -1;
 246         focusIndex = -1;
 247         c.setLayout(createLayoutManager());
 248         installComponents();
 249         installDefaults();
 250         installListeners();
 251         installKeyboardActions();
 252     }
 253 
 254     public void uninstallUI(JComponent c) {
 255         uninstallKeyboardActions();
 256         uninstallListeners();
 257         uninstallDefaults();
 258         uninstallComponents();
 259         c.setLayout(null);
 260 
 261         this.tabPane = null;
 262     }
 263 
 264     /**
 265      * Invoked by {@code installUI} to create
 266      * a layout manager object to manage
 267      * the {@code JTabbedPane}.
 268      *
 269      * @return a layout manager object
 270      *
 271      * @see TabbedPaneLayout
 272      * @see javax.swing.JTabbedPane#getTabLayoutPolicy
 273      */
 274     protected LayoutManager createLayoutManager() {
 275         if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {
 276             return new TabbedPaneScrollLayout();
 277         } else { /* WRAP_TAB_LAYOUT */
 278             return new TabbedPaneLayout();
 279         }
 280     }
 281 
 282     /* In an attempt to preserve backward compatibility for programs
 283      * which have extended BasicTabbedPaneUI to do their own layout, the
 284      * UI uses the installed layoutManager (and not tabLayoutPolicy) to
 285      * determine if scrollTabLayout is enabled.
 286      */
 287     private boolean scrollableTabLayoutEnabled() {


 607         mnemonicInputMap.put(KeyStroke.getKeyStroke(mnemonic, BasicLookAndFeel.getFocusAcceleratorKeyMask()),
 608                              "setSelectedIndex");
 609         mnemonicToIndexMap.put(Integer.valueOf(mnemonic), Integer.valueOf(index));
 610     }
 611 
 612     /**
 613      * Installs the state needed for mnemonics.
 614      */
 615     private void initMnemonics() {
 616         mnemonicToIndexMap = new Hashtable<Integer, Integer>();
 617         mnemonicInputMap = new ComponentInputMapUIResource(tabPane);
 618         mnemonicInputMap.setParent(SwingUtilities.getUIInputMap(tabPane,
 619                               JComponent.WHEN_IN_FOCUSED_WINDOW));
 620         SwingUtilities.replaceUIInputMap(tabPane,
 621                               JComponent.WHEN_IN_FOCUSED_WINDOW,
 622                                          mnemonicInputMap);
 623     }
 624 
 625     /**
 626      * Sets the tab the mouse is over by location. This is a cover method
 627      * for {@code setRolloverTab(tabForCoordinate(x, y, false))}.
 628      */
 629     private void setRolloverTab(int x, int y) {
 630         // NOTE:
 631         // This calls in with false otherwise it could trigger a validate,
 632         // which should NOT happen if the user is only dragging the
 633         // mouse around.
 634         setRolloverTab(tabForCoordinate(tabPane, x, y, false));
 635     }
 636 
 637     /**
 638      * Sets the tab the mouse is currently over to {@code index}.
 639      * {@code index} will be -1 if the mouse is no longer over any
 640      * tab. No checking is done to ensure the passed in index identifies a
 641      * valid tab.
 642      *
 643      * @param index Index of the tab the mouse is over.
 644      * @since 1.5
 645      */
 646     protected void setRolloverTab(int index) {
 647         rolloverTabIndex = index;
 648     }
 649 
 650     /**
 651      * Returns the tab the mouse is currently over, or {@code -1} if the mouse is no
 652      * longer over any tab.
 653      *
 654      * @return the tab the mouse is currently over, or {@code -1} if the mouse is no
 655      * longer over any tab
 656      * @since 1.5
 657      */
 658     protected int getRolloverTab() {
 659         return rolloverTabIndex;


 754             if (view != null) {
 755                 int viewHeight = (int)view.getPreferredSpan(View.Y_AXIS);
 756                 int baseline = BasicHTML.getHTMLBaseline(
 757                     view, (int)view.getPreferredSpan(View.X_AXIS), viewHeight);
 758                 if (baseline >= 0) {
 759                     return maxTabHeight / 2 - viewHeight / 2 + baseline +
 760                         getBaselineOffset();
 761                 }
 762                 return -1;
 763             }
 764         }
 765         FontMetrics metrics = getFontMetrics();
 766         int fontHeight = metrics.getHeight();
 767         int fontBaseline = metrics.getAscent();
 768         return maxTabHeight / 2 - fontHeight / 2 + fontBaseline +
 769                 getBaselineOffset();
 770     }
 771 
 772     /**
 773      * Returns the amount the baseline is offset by.  This is typically
 774      * the same as {@code getTabLabelShiftY}.
 775      *
 776      * @return amount to offset the baseline by
 777      * @since 1.6
 778      */
 779     protected int getBaselineOffset() {
 780         switch(tabPane.getTabPlacement()) {
 781         case JTabbedPane.TOP:
 782             if (tabPane.getTabCount() > 1) {
 783                 return 1;
 784             }
 785             else {
 786                 return -1;
 787             }
 788         case JTabbedPane.BOTTOM:
 789             if (tabPane.getTabCount() > 1) {
 790                 return -1;
 791             }
 792             else {
 793                 return 1;
 794             }


 846         ensureCurrentLayout();
 847 
 848         // Paint content border and tab area
 849         if (tabsOverlapBorder) {
 850             paintContentBorder(g, tabPlacement, selectedIndex);
 851         }
 852         // If scrollable tabs are enabled, the tab area will be
 853         // painted by the scrollable tab panel instead.
 854         //
 855         if (!scrollableTabLayoutEnabled()) { // WRAP_TAB_LAYOUT
 856             paintTabArea(g, tabPlacement, selectedIndex);
 857         }
 858         if (!tabsOverlapBorder) {
 859             paintContentBorder(g, tabPlacement, selectedIndex);
 860         }
 861     }
 862 
 863     /**
 864      * Paints the tabs in the tab area.
 865      * Invoked by paint().
 866      * The graphics parameter must be a valid {@code Graphics}
 867      * object.  Tab placement may be either:
 868      * {@code JTabbedPane.TOP}, {@code JTabbedPane.BOTTOM},
 869      * {@code JTabbedPane.LEFT}, or {@code JTabbedPane.RIGHT}.
 870      * The selected index must be a valid tabbed pane tab index (0 to
 871      * tab count - 1, inclusive) or -1 if no tab is currently selected.
 872      * The handling of invalid parameters is unspecified.
 873      *
 874      * @param g the graphics object to use for rendering
 875      * @param tabPlacement the placement for the tabs within the JTabbedPane
 876      * @param selectedIndex the tab index of the selected component
 877      *
 878      * @since 1.4
 879      */
 880     protected void paintTabArea(Graphics g, int tabPlacement, int selectedIndex) {
 881         int tabCount = tabPane.getTabCount();
 882 
 883         Rectangle iconRect = new Rectangle(),
 884                   textRect = new Rectangle();
 885         Rectangle clipRect = g.getClipBounds();
 886 
 887         // Paint tabRuns of tabs from back to front
 888         for (int i = runCount - 1; i >= 0; i--) {
 889             int start = tabRuns[i];


1695         int tabCount = tabPane.getTabCount();
1696         for (int i = 0; i < tabCount; i++) {
1697             if (rects[i].contains(p.x, p.y)) {
1698                 return i;
1699             }
1700         }
1701         return -1;
1702     }
1703 
1704     /**
1705      * Returns the bounds of the specified tab in the coordinate space
1706      * of the JTabbedPane component.  This is required because the tab rects
1707      * are by default defined in the coordinate space of the component where
1708      * they are rendered, which could be the JTabbedPane
1709      * (for WRAP_TAB_LAYOUT) or a ScrollableTabPanel (SCROLL_TAB_LAYOUT).
1710      * This method should be used whenever the tab rectangle must be relative
1711      * to the JTabbedPane itself and the result should be placed in a
1712      * designated Rectangle object (rather than instantiating and returning
1713      * a new Rectangle each time). The tab index parameter must be a valid
1714      * tabbed pane tab index (0 to tab count - 1, inclusive).  The destination
1715      * rectangle parameter must be a valid {@code Rectangle} instance.
1716      * The handling of invalid parameters is unspecified.
1717      *
1718      * @param tabIndex the index of the tab
1719      * @param dest the rectangle where the result should be placed
1720      * @return the resulting rectangle
1721      *
1722      * @since 1.4
1723      */
1724     protected Rectangle getTabBounds(int tabIndex, Rectangle dest) {
1725         dest.width = rects[tabIndex].width;
1726         dest.height = rects[tabIndex].height;
1727 
1728         if (scrollableTabLayoutEnabled()) { // SCROLL_TAB_LAYOUT
1729             // Need to translate coordinates based on viewport location &
1730             // view position
1731             Point vpp = tabScroller.viewport.getLocation();
1732             Point viewp = tabScroller.viewport.getViewPosition();
1733             dest.x = rects[tabIndex].x + vpp.x - viewp.x;
1734             dest.y = rects[tabIndex].y + vpp.y - viewp.y;
1735 


< prev index next >