3933 lines · 149.0 KB · Java
RawDownload
/* * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code 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 * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.swing.plaf.basic; import sun.swing.SwingUtilities2; import javax.swing.*;import javax.swing.event.*;import javax.swing.plaf.*;import javax.swing.text.View; import java.awt.*;import java.awt.event.*;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeEvent;import java.util.Vector;import java.util.Hashtable; import sun.swing.DefaultLookup;import sun.swing.UIAction; /** * A Basic L&F implementation of TabbedPaneUI. * * @author Amy Fowler * @author Philip Milne * @author Steve Wilson * @author Tom Santos * @author Dave Moore */public class BasicTabbedPaneUI extends TabbedPaneUI implements SwingConstants {  // Instance variables initialized at installation     protected JTabbedPane tabPane;     protected Color highlight;    protected Color lightHighlight;    protected Color shadow;    protected Color darkShadow;    protected Color focus;    private   Color selectedColor;     protected int textIconGap;     protected int tabRunOverlay;     protected Insets tabInsets;    protected Insets selectedTabPadInsets;    protected Insets tabAreaInsets;    protected Insets contentBorderInsets;    private boolean tabsOverlapBorder;    private boolean tabsOpaque = true;    private boolean contentOpaque = true;     /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke upKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke downKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke leftKey;    /**     * As of Java 2 platform v1.3 this previously undocumented field is no     * longer used.     * Key bindings are now defined by the LookAndFeel, please refer to     * the key bindings specification for further details.     *     * @deprecated As of Java 2 platform v1.3.     */    @Deprecated    protected KeyStroke rightKey;  // Transient variables (recalculated each time TabbedPane is layed out)     protected int tabRuns[] = new int[10];    protected int runCount = 0;    protected int selectedRun = -1;    protected Rectangle rects[] = new Rectangle[0];    protected int maxTabHeight;    protected int maxTabWidth; // Listeners     protected ChangeListener tabChangeListener;    protected PropertyChangeListener propertyChangeListener;    protected MouseListener mouseListener;    protected FocusListener focusListener; // Private instance data     private Insets currentPadInsets = new Insets(0,0,0,0);    private Insets currentTabAreaInsets = new Insets(0,0,0,0);     private Component visibleComponent;    // PENDING(api): See comment for ContainerHandler    private Vector<View> htmlViews;     private Hashtable<Integer, Integer> mnemonicToIndexMap;     /**     * InputMap used for mnemonics. Only non-null if the JTabbedPane has     * mnemonics associated with it. Lazily created in initMnemonics.     */    private InputMap mnemonicInputMap;     // For use when tabLayoutPolicy = SCROLL_TAB_LAYOUT    private ScrollableTabSupport tabScroller;     private TabContainer tabContainer;     /**     * A rectangle used for general layout calculations in order     * to avoid constructing many new Rectangles on the fly.     */    protected transient Rectangle calcRect = new Rectangle(0,0,0,0);     /**     * Tab that has focus.     */    private int focusIndex;     /**     * Combined listeners.     */    private Handler handler;     /**     * Index of the tab the mouse is over.     */    private int rolloverTabIndex;     /**     * This is set to true when a component is added/removed from the tab     * pane and set to false when layout happens.  If true it indicates that     * tabRuns is not valid and shouldn't be used.     */    private boolean isRunsDirty;     private boolean calculatedBaseline;    private int baseline; // UI creation     public static ComponentUI createUI(JComponent c) {        return new BasicTabbedPaneUI();    }     static void loadActionMap(LazyActionMap map) {        map.put(new Actions(Actions.NEXT));        map.put(new Actions(Actions.PREVIOUS));        map.put(new Actions(Actions.RIGHT));        map.put(new Actions(Actions.LEFT));        map.put(new Actions(Actions.UP));        map.put(new Actions(Actions.DOWN));        map.put(new Actions(Actions.PAGE_UP));        map.put(new Actions(Actions.PAGE_DOWN));        map.put(new Actions(Actions.REQUEST_FOCUS));        map.put(new Actions(Actions.REQUEST_FOCUS_FOR_VISIBLE));        map.put(new Actions(Actions.SET_SELECTED));        map.put(new Actions(Actions.SELECT_FOCUSED));        map.put(new Actions(Actions.SCROLL_FORWARD));        map.put(new Actions(Actions.SCROLL_BACKWARD));    } // UI Installation/De-installation     public void installUI(JComponent c) {        this.tabPane = (JTabbedPane)c;         calculatedBaseline = false;        rolloverTabIndex = -1;        focusIndex = -1;        c.setLayout(createLayoutManager());        installComponents();        installDefaults();        installListeners();        installKeyboardActions();    }     public void uninstallUI(JComponent c) {        uninstallKeyboardActions();        uninstallListeners();        uninstallDefaults();        uninstallComponents();        c.setLayout(null);         this.tabPane = null;    }     /**     * Invoked by <code>installUI</code> to create     * a layout manager object to manage     * the <code>JTabbedPane</code>.     *     * @return a layout manager object     *     * @see TabbedPaneLayout     * @see javax.swing.JTabbedPane#getTabLayoutPolicy     */    protected LayoutManager createLayoutManager() {        if (tabPane.getTabLayoutPolicy() == JTabbedPane.SCROLL_TAB_LAYOUT) {            return new TabbedPaneScrollLayout();        } else { /* WRAP_TAB_LAYOUT */            return new TabbedPaneLayout();        }    }     /* In an attempt to preserve backward compatibility for programs     * which have extended BasicTabbedPaneUI to do their own layout, the     * UI uses the installed layoutManager (and not tabLayoutPolicy) to     * determine if scrollTabLayout is enabled.     */    private boolean scrollableTabLayoutEnabled() {        return (tabPane.getLayout() instanceof TabbedPaneScrollLayout);    }     /**     * Creates and installs any required subcomponents for the JTabbedPane.     * Invoked by installUI.     *     * @since 1.4     */    protected void installComponents() {        if (scrollableTabLayoutEnabled()) {            if (tabScroller == null) {                tabScroller = new ScrollableTabSupport(tabPane.getTabPlacement());                tabPane.add(tabScroller.viewport);            }        }        installTabContainer();    }     private void installTabContainer() {         for (int i = 0; i < tabPane.getTabCount(); i++) {             Component tabComponent = tabPane.getTabComponentAt(i);             if (tabComponent != null) {                 if(tabContainer == null) {                     tabContainer = new TabContainer();                 }                 tabContainer.add(tabComponent);             }         }         if(tabContainer == null) {             return;         }         if (scrollableTabLayoutEnabled()) {             tabScroller.tabPanel.add(tabContainer);         } else {             tabPane.add(tabContainer);         }    }     /**     * Creates and returns a JButton that will provide the user     * with a way to scroll the tabs in a particular direction. The     * returned JButton must be instance of UIResource.     *     * @param direction One of the SwingConstants constants:     * SOUTH, NORTH, EAST or WEST     * @return Widget for user to     * @see javax.swing.JTabbedPane#setTabPlacement     * @see javax.swing.SwingConstants     * @throws IllegalArgumentException if direction is not one of     *         NORTH, SOUTH, EAST or WEST     * @since 1.5     */    protected JButton createScrollButton(int direction) {        if (direction != SOUTH && direction != NORTH && direction != EAST &&                                  direction != WEST) {            throw new IllegalArgumentException("Direction must be one of: " +                                               "SOUTH, NORTH, EAST or WEST");        }        return new ScrollableTabButton(direction);    }     /**     * Removes any installed subcomponents from the JTabbedPane.     * Invoked by uninstallUI.     *     * @since 1.4     */    protected void uninstallComponents() {        uninstallTabContainer();        if (scrollableTabLayoutEnabled()) {            tabPane.remove(tabScroller.viewport);            tabPane.remove(tabScroller.scrollForwardButton);            tabPane.remove(tabScroller.scrollBackwardButton);            tabScroller = null;        }    }     private void uninstallTabContainer() {         if(tabContainer == null) {             return;         }         // Remove all the tabComponents, making sure not to notify         // the tabbedpane.         tabContainer.notifyTabbedPane = false;         tabContainer.removeAll();         if(scrollableTabLayoutEnabled()) {             tabContainer.remove(tabScroller.croppedEdge);             tabScroller.tabPanel.remove(tabContainer);         } else {           tabPane.remove(tabContainer);         }         tabContainer = null;    }     protected void installDefaults() {        LookAndFeel.installColorsAndFont(tabPane, "TabbedPane.background",                                    "TabbedPane.foreground", "TabbedPane.font");        highlight = UIManager.getColor("TabbedPane.light");        lightHighlight = UIManager.getColor("TabbedPane.highlight");        shadow = UIManager.getColor("TabbedPane.shadow");        darkShadow = UIManager.getColor("TabbedPane.darkShadow");        focus = UIManager.getColor("TabbedPane.focus");        selectedColor = UIManager.getColor("TabbedPane.selected");         textIconGap = UIManager.getInt("TabbedPane.textIconGap");        tabInsets = UIManager.getInsets("TabbedPane.tabInsets");        selectedTabPadInsets = UIManager.getInsets("TabbedPane.selectedTabPadInsets");        tabAreaInsets = UIManager.getInsets("TabbedPane.tabAreaInsets");        tabsOverlapBorder = UIManager.getBoolean("TabbedPane.tabsOverlapBorder");        contentBorderInsets = UIManager.getInsets("TabbedPane.contentBorderInsets");        tabRunOverlay = UIManager.getInt("TabbedPane.tabRunOverlay");        tabsOpaque = UIManager.getBoolean("TabbedPane.tabsOpaque");        contentOpaque = UIManager.getBoolean("TabbedPane.contentOpaque");        Object opaque = UIManager.get("TabbedPane.opaque");        if (opaque == null) {            opaque = Boolean.FALSE;        }        LookAndFeel.installProperty(tabPane, "opaque", opaque);         // Fix for 6711145 BasicTabbedPanuUI should not throw a NPE if these        // keys are missing. So we are setting them to there default values here        // if the keys are missing.        if (tabInsets == null) tabInsets = new Insets(0,4,1,4);        if (selectedTabPadInsets == null) selectedTabPadInsets = new Insets(2,2,2,1);        if (tabAreaInsets == null) tabAreaInsets = new Insets(3,2,0,2);        if (contentBorderInsets == null) contentBorderInsets = new Insets(2,2,3,3);    }     protected void uninstallDefaults() {        highlight = null;        lightHighlight = null;        shadow = null;        darkShadow = null;        focus = null;        tabInsets = null;        selectedTabPadInsets = null;        tabAreaInsets = null;        contentBorderInsets = null;    }     protected void installListeners() {        if ((propertyChangeListener = createPropertyChangeListener()) != null) {            tabPane.addPropertyChangeListener(propertyChangeListener);        }        if ((tabChangeListener = createChangeListener()) != null) {            tabPane.addChangeListener(tabChangeListener);        }        if ((mouseListener = createMouseListener()) != null) {            tabPane.addMouseListener(mouseListener);        }        tabPane.addMouseMotionListener(getHandler());        if ((focusListener = createFocusListener()) != null) {            tabPane.addFocusListener(focusListener);        }        tabPane.addContainerListener(getHandler());        if (tabPane.getTabCount()>0) {            htmlViews = createHTMLVector();        }    }     protected void uninstallListeners() {        if (mouseListener != null) {            tabPane.removeMouseListener(mouseListener);            mouseListener = null;        }        tabPane.removeMouseMotionListener(getHandler());        if (focusListener != null) {            tabPane.removeFocusListener(focusListener);            focusListener = null;        }         tabPane.removeContainerListener(getHandler());        if (htmlViews!=null) {            htmlViews.removeAllElements();            htmlViews = null;        }        if (tabChangeListener != null) {            tabPane.removeChangeListener(tabChangeListener);            tabChangeListener = null;        }        if (propertyChangeListener != null) {            tabPane.removePropertyChangeListener(propertyChangeListener);            propertyChangeListener = null;        }        handler = null;    }     protected MouseListener createMouseListener() {        return getHandler();    }     protected FocusListener createFocusListener() {        return getHandler();    }     protected ChangeListener createChangeListener() {        return getHandler();    }     protected PropertyChangeListener createPropertyChangeListener() {        return getHandler();    }     private Handler getHandler() {        if (handler == null) {            handler = new Handler();        }        return handler;    }     protected void installKeyboardActions() {        InputMap km = getInputMap(JComponent.                                  WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);         SwingUtilities.replaceUIInputMap(tabPane, JComponent.                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,                                         km);        km = getInputMap(JComponent.WHEN_FOCUSED);        SwingUtilities.replaceUIInputMap(tabPane, JComponent.WHEN_FOCUSED, km);         LazyActionMap.installLazyActionMap(tabPane, BasicTabbedPaneUI.class,                                           "TabbedPane.actionMap");        updateMnemonics();    }     InputMap getInputMap(int condition) {        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {            return (InputMap)DefaultLookup.get(tabPane, this,                                               "TabbedPane.ancestorInputMap");        }        else if (condition == JComponent.WHEN_FOCUSED) {            return (InputMap)DefaultLookup.get(tabPane, this,                                               "TabbedPane.focusInputMap");        }        return null;    } 

Lines 1–500 of 3933.