1443 lines · 48.8 KB · Java
RawDownload
/* * Copyright (c) 1997, 2008, 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 javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*; import java.beans.*; import java.util.Hashtable;import java.util.HashMap; import javax.swing.border.*;import javax.swing.plaf.*;import sun.swing.DefaultLookup;import sun.swing.UIAction;  /** * A Basic L&F implementation of ToolBarUI.  This implementation * is a "combined" view/controller. * <p> * * @author Georges Saab * @author Jeff Shapiro */public class BasicToolBarUI extends ToolBarUI implements SwingConstants{    protected JToolBar toolBar;    private boolean floating;    private int floatingX;    private int floatingY;    private JFrame floatingFrame;    private RootPaneContainer floatingToolBar;    protected DragWindow dragWindow;    private Container dockingSource;    private int dockingSensitivity = 0;    protected int focusedCompIndex = -1;     protected Color dockingColor = null;    protected Color floatingColor = null;    protected Color dockingBorderColor = null;    protected Color floatingBorderColor = null;     protected MouseInputListener dockingListener;    protected PropertyChangeListener propertyListener;     protected ContainerListener toolBarContListener;    protected FocusListener toolBarFocusListener;    private Handler handler;     protected String constraintBeforeFloating = BorderLayout.NORTH;     // Rollover button implementation.    private static String IS_ROLLOVER = "JToolBar.isRollover";    private static Border rolloverBorder;    private static Border nonRolloverBorder;    private static Border nonRolloverToggleBorder;    private boolean rolloverBorders = false;     private HashMap<AbstractButton, Border> borderTable = new HashMap<AbstractButton, Border>();    private Hashtable<AbstractButton, Boolean> rolloverTable = new Hashtable<AbstractButton, Boolean>();      /**     * 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;      private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex";     public static ComponentUI createUI( JComponent c )    {        return new BasicToolBarUI();    }     public void installUI( JComponent c )    {        toolBar = (JToolBar) c;         // Set defaults        installDefaults();        installComponents();        installListeners();        installKeyboardActions();         // Initialize instance vars        dockingSensitivity = 0;        floating = false;        floatingX = floatingY = 0;        floatingToolBar = null;         setOrientation( toolBar.getOrientation() );        LookAndFeel.installProperty(c, "opaque", Boolean.TRUE);         if ( c.getClientProperty( FOCUSED_COMP_INDEX ) != null )        {            focusedCompIndex = ( (Integer) ( c.getClientProperty( FOCUSED_COMP_INDEX ) ) ).intValue();        }    }     public void uninstallUI( JComponent c )    {         // Clear defaults        uninstallDefaults();        uninstallComponents();        uninstallListeners();        uninstallKeyboardActions();         // Clear instance vars        if (isFloating())            setFloating(false, null);         floatingToolBar = null;        dragWindow = null;        dockingSource = null;         c.putClientProperty( FOCUSED_COMP_INDEX, Integer.valueOf( focusedCompIndex ) );    }     protected void installDefaults( )    {        LookAndFeel.installBorder(toolBar,"ToolBar.border");        LookAndFeel.installColorsAndFont(toolBar,                                              "ToolBar.background",                                              "ToolBar.foreground",                                              "ToolBar.font");        // Toolbar specific defaults        if ( dockingColor == null || dockingColor instanceof UIResource )            dockingColor = UIManager.getColor("ToolBar.dockingBackground");        if ( floatingColor == null || floatingColor instanceof UIResource )            floatingColor = UIManager.getColor("ToolBar.floatingBackground");        if ( dockingBorderColor == null ||             dockingBorderColor instanceof UIResource )            dockingBorderColor = UIManager.getColor("ToolBar.dockingForeground");        if ( floatingBorderColor == null ||             floatingBorderColor instanceof UIResource )            floatingBorderColor = UIManager.getColor("ToolBar.floatingForeground");         // ToolBar rollover button borders        Object rolloverProp = toolBar.getClientProperty( IS_ROLLOVER );        if (rolloverProp == null) {            rolloverProp = UIManager.get("ToolBar.isRollover");        }        if ( rolloverProp != null ) {            rolloverBorders = ((Boolean)rolloverProp).booleanValue();        }         if (rolloverBorder == null) {            rolloverBorder = createRolloverBorder();        }        if (nonRolloverBorder == null) {            nonRolloverBorder = createNonRolloverBorder();        }        if (nonRolloverToggleBorder == null) {            nonRolloverToggleBorder = createNonRolloverToggleBorder();        }          setRolloverBorders( isRolloverBorders() );    }     protected void uninstallDefaults( )    {        LookAndFeel.uninstallBorder(toolBar);        dockingColor = null;        floatingColor = null;        dockingBorderColor = null;        floatingBorderColor = null;         installNormalBorders(toolBar);         rolloverBorder = null;        nonRolloverBorder = null;        nonRolloverToggleBorder = null;    }     protected void installComponents( )    {    }     protected void uninstallComponents( )    {    }     protected void installListeners( )    {        dockingListener = createDockingListener( );         if ( dockingListener != null )        {            toolBar.addMouseMotionListener( dockingListener );            toolBar.addMouseListener( dockingListener );        }         propertyListener = createPropertyListener();  // added in setFloating        if (propertyListener != null) {            toolBar.addPropertyChangeListener(propertyListener);        }         toolBarContListener = createToolBarContListener();        if ( toolBarContListener != null ) {            toolBar.addContainerListener( toolBarContListener );        }         toolBarFocusListener = createToolBarFocusListener();         if ( toolBarFocusListener != null )        {            // Put focus listener on all components in toolbar            Component[] components = toolBar.getComponents();             for (Component component : components) {                component.addFocusListener(toolBarFocusListener);            }        }    }     protected void uninstallListeners( )    {        if ( dockingListener != null )        {            toolBar.removeMouseMotionListener(dockingListener);            toolBar.removeMouseListener(dockingListener);             dockingListener = null;        }         if ( propertyListener != null )        {            toolBar.removePropertyChangeListener(propertyListener);            propertyListener = null;  // removed in setFloating        }         if ( toolBarContListener != null )        {            toolBar.removeContainerListener( toolBarContListener );            toolBarContListener = null;        }         if ( toolBarFocusListener != null )        {            // Remove focus listener from all components in toolbar            Component[] components = toolBar.getComponents();             for (Component component : components) {                component.removeFocusListener(toolBarFocusListener);            }             toolBarFocusListener = null;        }        handler = null;    }     protected void installKeyboardActions( )    {        InputMap km = getInputMap(JComponent.                                  WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);         SwingUtilities.replaceUIInputMap(toolBar, JComponent.                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,                                         km);     LazyActionMap.installLazyActionMap(toolBar, BasicToolBarUI.class,            "ToolBar.actionMap");    }     InputMap getInputMap(int condition) {        if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) {            return (InputMap)DefaultLookup.get(toolBar, this,                    "ToolBar.ancestorInputMap");        }        return null;    }     static void loadActionMap(LazyActionMap map) {        map.put(new Actions(Actions.NAVIGATE_RIGHT));        map.put(new Actions(Actions.NAVIGATE_LEFT));        map.put(new Actions(Actions.NAVIGATE_UP));        map.put(new Actions(Actions.NAVIGATE_DOWN));    }     protected void uninstallKeyboardActions( )    {        SwingUtilities.replaceUIActionMap(toolBar, null);        SwingUtilities.replaceUIInputMap(toolBar, JComponent.                                         WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,                                         null);    }     protected void navigateFocusedComp( int direction )    {        int nComp = toolBar.getComponentCount();        int j;         switch ( direction )        {            case EAST:            case SOUTH:                 if ( focusedCompIndex < 0 || focusedCompIndex >= nComp ) break;                 j = focusedCompIndex + 1;                 while ( j != focusedCompIndex )                {                    if ( j >= nComp ) j = 0;                    Component comp = toolBar.getComponentAtIndex( j++ );                     if ( comp != null && comp.isFocusTraversable() && comp.isEnabled() )                    {                        comp.requestFocus();                        break;                    }                }                 break;             case WEST:            case NORTH:                 if ( focusedCompIndex < 0 || focusedCompIndex >= nComp ) break;                 j = focusedCompIndex - 1;                 while ( j != focusedCompIndex )                {                    if ( j < 0 ) j = nComp - 1;                    Component comp = toolBar.getComponentAtIndex( j-- );                     if ( comp != null && comp.isFocusTraversable() && comp.isEnabled() )                    {                        comp.requestFocus();                        break;                    }                }                 break;             default:                break;        }    }     /**     * Creates a rollover border for toolbar components. The     * rollover border will be installed if rollover borders are     * enabled.     * <p>     * Override this method to provide an alternate rollover border.     *     * @since 1.4     */    protected Border createRolloverBorder() {        Object border = UIManager.get("ToolBar.rolloverBorder");        if (border != null) {            return (Border)border;        }        UIDefaults table = UIManager.getLookAndFeelDefaults();        return new CompoundBorder(new BasicBorders.RolloverButtonBorder(                                           table.getColor("controlShadow"),                                           table.getColor("controlDkShadow"),                                           table.getColor("controlHighlight"),                                           table.getColor("controlLtHighlight")),                                  new BasicBorders.RolloverMarginBorder());    }     /**     * Creates the non rollover border for toolbar components. This     * border will be installed as the border for components added     * to the toolbar if rollover borders are not enabled.     * <p>     * Override this method to provide an alternate rollover border.     *     * @since 1.4     */    protected Border createNonRolloverBorder() {        Object border = UIManager.get("ToolBar.nonrolloverBorder");        if (border != null) {            return (Border)border;        }        UIDefaults table = UIManager.getLookAndFeelDefaults();        return new CompoundBorder(new BasicBorders.ButtonBorder(                                           table.getColor("Button.shadow"),                                           table.getColor("Button.darkShadow"),                                           table.getColor("Button.light"),                                           table.getColor("Button.highlight")),                                  new BasicBorders.RolloverMarginBorder());    }     /**     * Creates a non rollover border for Toggle buttons in the toolbar.     */    private Border createNonRolloverToggleBorder() {        UIDefaults table = UIManager.getLookAndFeelDefaults();        return new CompoundBorder(new BasicBorders.RadioButtonBorder(                                           table.getColor("ToggleButton.shadow"),                                           table.getColor("ToggleButton.darkShadow"),                                           table.getColor("ToggleButton.light"),                                           table.getColor("ToggleButton.highlight")),                                  new BasicBorders.RolloverMarginBorder());    }     /**     * No longer used, use BasicToolBarUI.createFloatingWindow(JToolBar)     * @see #createFloatingWindow     */    protected JFrame createFloatingFrame(JToolBar toolbar) {        Window window = SwingUtilities.getWindowAncestor(toolbar);        JFrame frame = new JFrame(toolbar.getName(),                                  (window != null) ? window.getGraphicsConfiguration() : null) {            // Override createRootPane() to automatically resize            // the frame when contents change            protected JRootPane createRootPane() {                JRootPane rootPane = new JRootPane() {                    private boolean packing = false;                     public void validate() {                        super.validate();                        if (!packing) {                            packing = true;                            pack();                            packing = false;                        }                    }                };                rootPane.setOpaque(true);                return rootPane;            }        };        frame.getRootPane().setName("ToolBar.FloatingFrame");        frame.setResizable(false);        WindowListener wl = createFrameListener();        frame.addWindowListener(wl);        return frame;    } 

Lines 1–500 of 1443.