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

Print this page


   1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


3432                 }
3433                 Action action = map.get(actionKey);
3434 
3435                 if (action != null && action.isEnabled()) {
3436                     action.actionPerformed(new ActionEvent(tabPane,
3437                         ActionEvent.ACTION_PERFORMED, null, e.getWhen(),
3438                         e.getModifiers()));
3439                 }
3440             }
3441         }
3442 
3443         public String toString() {
3444             return "viewport.viewSize=" + viewport.getViewSize() + "\n" +
3445                               "viewport.viewRectangle="+viewport.getViewRect()+"\n"+
3446                               "leadingTabIndex="+leadingTabIndex+"\n"+
3447                               "tabViewPosition=" + tabViewPosition;
3448         }
3449 
3450     }
3451 

3452     private class ScrollableTabViewport extends JViewport implements UIResource {
3453         public ScrollableTabViewport() {
3454             super();
3455             setName("TabbedPane.scrollableViewport");
3456             setScrollMode(SIMPLE_SCROLL_MODE);
3457             setOpaque(tabPane.isOpaque());
3458             Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
3459             if (bgColor == null) {
3460                 bgColor = tabPane.getBackground();
3461             }
3462             setBackground(bgColor);
3463         }
3464     }
3465 

3466     private class ScrollableTabPanel extends JPanel implements UIResource {
3467         public ScrollableTabPanel() {
3468             super(null);
3469             setOpaque(tabPane.isOpaque());
3470             Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
3471             if (bgColor == null) {
3472                 bgColor = tabPane.getBackground();
3473             }
3474             setBackground(bgColor);
3475         }
3476         public void paintComponent(Graphics g) {
3477             super.paintComponent(g);
3478             BasicTabbedPaneUI.this.paintTabArea(g, tabPane.getTabPlacement(),
3479                                                 tabPane.getSelectedIndex());
3480             if (tabScroller.croppedEdge.isParamsSet() && tabContainer == null) {
3481                 Rectangle croppedRect = rects[tabScroller.croppedEdge.getTabIndex()];
3482                 g.translate(croppedRect.x, croppedRect.y);
3483                 tabScroller.croppedEdge.paintComponent(g);
3484                 g.translate(-croppedRect.x, -croppedRect.y);
3485             }
3486         }
3487 
3488         public void doLayout() {
3489             if (getComponentCount() > 0) {
3490                 Component child = getComponent(0);
3491                 child.setBounds(0, 0, getWidth(), getHeight());
3492             }
3493         }
3494     }
3495 

3496     private class ScrollableTabButton extends BasicArrowButton implements UIResource,
3497                                                                             SwingConstants {
3498         public ScrollableTabButton(int direction) {
3499             super(direction,
3500                   UIManager.getColor("TabbedPane.selected"),
3501                   UIManager.getColor("TabbedPane.shadow"),
3502                   UIManager.getColor("TabbedPane.darkShadow"),
3503                   UIManager.getColor("TabbedPane.highlight"));
3504         }
3505     }
3506 
3507 
3508 // Controller: event listeners
3509 
3510     private class Handler implements ChangeListener, ContainerListener,
3511                   FocusListener, MouseListener, MouseMotionListener,
3512                   PropertyChangeListener {
3513         //
3514         // PropertyChangeListener
3515         //


3801             getHandler().focusLost(e);
3802         }
3803     }
3804 
3805     private Vector<View> createHTMLVector() {
3806         Vector<View> htmlViews = new Vector<View>();
3807         int count = tabPane.getTabCount();
3808         if (count>0) {
3809             for (int i=0 ; i<count; i++) {
3810                 String title = tabPane.getTitleAt(i);
3811                 if (BasicHTML.isHTMLString(title)) {
3812                     htmlViews.addElement(BasicHTML.createHTMLView(tabPane, title));
3813                 } else {
3814                     htmlViews.addElement(null);
3815                 }
3816             }
3817         }
3818         return htmlViews;
3819     }
3820 

3821     private class TabContainer extends JPanel implements UIResource {
3822         private boolean notifyTabbedPane = true;
3823 
3824         public TabContainer() {
3825             super(null);
3826             setOpaque(false);
3827         }
3828 
3829         public void remove(Component comp) {
3830             int index = tabPane.indexOfTabComponent(comp);
3831             super.remove(comp);
3832             if (notifyTabbedPane && index != -1) {
3833                 tabPane.setTabComponentAt(index, null);
3834             }
3835         }
3836 
3837         private void removeUnusedTabComponents() {
3838             for (Component c : getComponents()) {
3839                 if (!(c instanceof UIResource)) {
3840                     int index = tabPane.indexOfTabComponent(c);


3845             }
3846         }
3847 
3848         public boolean isOptimizedDrawingEnabled() {
3849             return tabScroller != null && !tabScroller.croppedEdge.isParamsSet();
3850         }
3851 
3852         public void doLayout() {
3853             // We layout tabComponents in JTabbedPane's layout manager
3854             // and use this method as a hook for repainting tabs
3855             // to update tabs area e.g. when the size of tabComponent was changed
3856             if (scrollableTabLayoutEnabled()) {
3857                 tabScroller.tabPanel.repaint();
3858                 tabScroller.updateView();
3859             } else {
3860                 tabPane.repaint(getBounds());
3861             }
3862         }
3863     }
3864 

3865     private class CroppedEdge extends JPanel implements UIResource {
3866         private Shape shape;
3867         private int tabIndex;
3868         private int cropline;
3869         private int cropx, cropy;
3870 
3871         public CroppedEdge() {
3872             setOpaque(false);
3873         }
3874 
3875         public void setParams(int tabIndex, int cropline, int cropx, int cropy) {
3876             this.tabIndex = tabIndex;
3877             this.cropline = cropline;
3878             this.cropx = cropx;
3879             this.cropy = cropy;
3880             Rectangle tabRect = rects[tabIndex];
3881             setBounds(tabRect);
3882             shape = createCroppedTabShape(tabPane.getTabPlacement(), tabRect, cropline);
3883             if (getParent() == null && tabContainer != null) {
3884                 tabContainer.add(this, 0);


   1 /*
   2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


3432                 }
3433                 Action action = map.get(actionKey);
3434 
3435                 if (action != null && action.isEnabled()) {
3436                     action.actionPerformed(new ActionEvent(tabPane,
3437                         ActionEvent.ACTION_PERFORMED, null, e.getWhen(),
3438                         e.getModifiers()));
3439                 }
3440             }
3441         }
3442 
3443         public String toString() {
3444             return "viewport.viewSize=" + viewport.getViewSize() + "\n" +
3445                               "viewport.viewRectangle="+viewport.getViewRect()+"\n"+
3446                               "leadingTabIndex="+leadingTabIndex+"\n"+
3447                               "tabViewPosition=" + tabViewPosition;
3448         }
3449 
3450     }
3451 
3452     @SuppressWarnings("serial") // Superclass is not serializable across versions
3453     private class ScrollableTabViewport extends JViewport implements UIResource {
3454         public ScrollableTabViewport() {
3455             super();
3456             setName("TabbedPane.scrollableViewport");
3457             setScrollMode(SIMPLE_SCROLL_MODE);
3458             setOpaque(tabPane.isOpaque());
3459             Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
3460             if (bgColor == null) {
3461                 bgColor = tabPane.getBackground();
3462             }
3463             setBackground(bgColor);
3464         }
3465     }
3466 
3467     @SuppressWarnings("serial") // Superclass is not serializable across versions
3468     private class ScrollableTabPanel extends JPanel implements UIResource {
3469         public ScrollableTabPanel() {
3470             super(null);
3471             setOpaque(tabPane.isOpaque());
3472             Color bgColor = UIManager.getColor("TabbedPane.tabAreaBackground");
3473             if (bgColor == null) {
3474                 bgColor = tabPane.getBackground();
3475             }
3476             setBackground(bgColor);
3477         }
3478         public void paintComponent(Graphics g) {
3479             super.paintComponent(g);
3480             BasicTabbedPaneUI.this.paintTabArea(g, tabPane.getTabPlacement(),
3481                                                 tabPane.getSelectedIndex());
3482             if (tabScroller.croppedEdge.isParamsSet() && tabContainer == null) {
3483                 Rectangle croppedRect = rects[tabScroller.croppedEdge.getTabIndex()];
3484                 g.translate(croppedRect.x, croppedRect.y);
3485                 tabScroller.croppedEdge.paintComponent(g);
3486                 g.translate(-croppedRect.x, -croppedRect.y);
3487             }
3488         }
3489 
3490         public void doLayout() {
3491             if (getComponentCount() > 0) {
3492                 Component child = getComponent(0);
3493                 child.setBounds(0, 0, getWidth(), getHeight());
3494             }
3495         }
3496     }
3497 
3498     @SuppressWarnings("serial") // Superclass is not serializable across versions
3499     private class ScrollableTabButton extends BasicArrowButton implements UIResource,
3500                                                                             SwingConstants {
3501         public ScrollableTabButton(int direction) {
3502             super(direction,
3503                   UIManager.getColor("TabbedPane.selected"),
3504                   UIManager.getColor("TabbedPane.shadow"),
3505                   UIManager.getColor("TabbedPane.darkShadow"),
3506                   UIManager.getColor("TabbedPane.highlight"));
3507         }
3508     }
3509 
3510 
3511 // Controller: event listeners
3512 
3513     private class Handler implements ChangeListener, ContainerListener,
3514                   FocusListener, MouseListener, MouseMotionListener,
3515                   PropertyChangeListener {
3516         //
3517         // PropertyChangeListener
3518         //


3804             getHandler().focusLost(e);
3805         }
3806     }
3807 
3808     private Vector<View> createHTMLVector() {
3809         Vector<View> htmlViews = new Vector<View>();
3810         int count = tabPane.getTabCount();
3811         if (count>0) {
3812             for (int i=0 ; i<count; i++) {
3813                 String title = tabPane.getTitleAt(i);
3814                 if (BasicHTML.isHTMLString(title)) {
3815                     htmlViews.addElement(BasicHTML.createHTMLView(tabPane, title));
3816                 } else {
3817                     htmlViews.addElement(null);
3818                 }
3819             }
3820         }
3821         return htmlViews;
3822     }
3823 
3824     @SuppressWarnings("serial") // Superclass is not serializable across versions
3825     private class TabContainer extends JPanel implements UIResource {
3826         private boolean notifyTabbedPane = true;
3827 
3828         public TabContainer() {
3829             super(null);
3830             setOpaque(false);
3831         }
3832 
3833         public void remove(Component comp) {
3834             int index = tabPane.indexOfTabComponent(comp);
3835             super.remove(comp);
3836             if (notifyTabbedPane && index != -1) {
3837                 tabPane.setTabComponentAt(index, null);
3838             }
3839         }
3840 
3841         private void removeUnusedTabComponents() {
3842             for (Component c : getComponents()) {
3843                 if (!(c instanceof UIResource)) {
3844                     int index = tabPane.indexOfTabComponent(c);


3849             }
3850         }
3851 
3852         public boolean isOptimizedDrawingEnabled() {
3853             return tabScroller != null && !tabScroller.croppedEdge.isParamsSet();
3854         }
3855 
3856         public void doLayout() {
3857             // We layout tabComponents in JTabbedPane's layout manager
3858             // and use this method as a hook for repainting tabs
3859             // to update tabs area e.g. when the size of tabComponent was changed
3860             if (scrollableTabLayoutEnabled()) {
3861                 tabScroller.tabPanel.repaint();
3862                 tabScroller.updateView();
3863             } else {
3864                 tabPane.repaint(getBounds());
3865             }
3866         }
3867     }
3868 
3869     @SuppressWarnings("serial") // Superclass is not serializable across versions
3870     private class CroppedEdge extends JPanel implements UIResource {
3871         private Shape shape;
3872         private int tabIndex;
3873         private int cropline;
3874         private int cropx, cropy;
3875 
3876         public CroppedEdge() {
3877             setOpaque(false);
3878         }
3879 
3880         public void setParams(int tabIndex, int cropline, int cropx, int cropy) {
3881             this.tabIndex = tabIndex;
3882             this.cropline = cropline;
3883             this.cropx = cropx;
3884             this.cropy = cropy;
3885             Rectangle tabRect = rects[tabIndex];
3886             setBounds(tabRect);
3887             shape = createCroppedTabShape(tabPane.getTabPlacement(), tabRect, cropline);
3888             if (getParent() == null && tabContainer != null) {
3889                 tabContainer.add(this, 0);