?? basictoolbarui.java
字號:
/* * @(#)BasicToolBarUI.java 1.97 06/08/25 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */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;import sun.swing.BorderProvider;/** * A Basic L&F implementation of ToolBarUI. This implementation * is a "combined" view/controller. * <p> * * @version 1.97 08/25/06 * @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 borderTable = new HashMap(); private Hashtable rolloverTable = new Hashtable(); /** * 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() == true) setFloating(false, null); floatingToolBar = null; dragWindow = null; dockingSource = null; c.putClientProperty( FOCUSED_COMP_INDEX, new Integer( 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 ( int i = 0; i < components.length; ++i ) { components[ i ].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 ( int i = 0; i < components.length; ++i ) { components[ i ].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() {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -