synthtoolbarui.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,086 行 · 第 1/3 页
JAVA
1,086 行
/* * @(#)SynthToolBarUI.java 1.8 03/01/23 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import javax.swing.*;import javax.swing.event.*;import java.awt.*;import java.awt.event.*;import java.beans.*;import javax.swing.border.*;import javax.swing.plaf.*;/** * A Basic L&F implementation of ToolBarUI. This implementation * is a "combined" view/controller. * <p> * * @version 1.8, 01/23/03 (based on BasicToolBarUI v 1.83) * @author Georges Saab * @author Jeff Shapiro */class SynthToolBarUI extends ToolBarUI implements SwingConstants, SynthUI { 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 Icon handleIcon = null; protected Rectangle contentRect = null; protected MouseInputListener dockingListener; protected PropertyChangeListener propertyListener; protected ContainerListener toolBarContListener; protected FocusListener toolBarFocusListener; protected String constraintBeforeFloating = BorderLayout.NORTH; private SynthStyle style; private SynthStyle contentStyle; private SynthStyle dragWindowStyle; private static String FOCUSED_COMP_INDEX = "JToolBar.focusedCompIndex"; public static ComponentUI createUI(JComponent c) { return new SynthToolBarUI(); } protected String getPropertyPrefix() { return "ToolBar"; } public static void loadActionMap(ActionMap map) { // NOTE: this needs to remain static. If you have a need to // have Actions that reference the UI in the ActionMap, // then you'll also need to change the registeration of the // ActionMap. map.put("navigateRight", new RightAction()); map.put("navigateLeft", new LeftAction()); map.put("navigateUp", new UpAction()); map.put("navigateDown", new DownAction()); } public void installUI(JComponent c) { toolBar = (JToolBar)c; contentRect = new Rectangle(); // Set defaults installDefaults(); installComponents(); installListeners(); installKeyboardActions(); // Initialize instance vars dockingSensitivity = 0; floating = false; floatingX = floatingY = 0; floatingToolBar = null; setOrientation(toolBar.getOrientation()); c.setOpaque(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() { toolBar.setLayout(createLayout()); fetchStyle(toolBar); } private void fetchStyle(JToolBar c) { SynthContext context = getContext(c, ENABLED); SynthStyle oldStyle = style; style = SynthLookAndFeel.updateStyle(context, this); if (oldStyle != style) { handleIcon = style.getIcon(context, getPropertyPrefix() + ".handleIcon"); } context.dispose(); context = getContext(c, Region.TOOL_BAR_CONTENT, ENABLED); contentStyle = SynthLookAndFeel.updateStyle(context, this); context.dispose(); context = getContext(c, Region.TOOL_BAR_DRAG_WINDOW, ENABLED); dragWindowStyle = SynthLookAndFeel.updateStyle(context, this); context.dispose(); } protected void uninstallDefaults() { SynthContext context = getContext(toolBar, ENABLED); style.uninstallDefaults(context); context.dispose(); style = null; handleIcon = null; context = getContext(toolBar, Region.TOOL_BAR_CONTENT, ENABLED); contentStyle.uninstallDefaults(context); context.dispose(); contentStyle = null; context = getContext(toolBar, Region.TOOL_BAR_DRAG_WINDOW, ENABLED); dragWindowStyle.uninstallDefaults(context); context.dispose(); dragWindowStyle = null; toolBar.setLayout(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; } } 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, SynthToolBarUI.class, "ToolBar.actionMap"); } InputMap getInputMap(int condition) { if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) { SynthContext context = getContext(toolBar, ENABLED); InputMap map = (InputMap)context.getStyle().get(context, "ToolBar.ancestorInputMap"); context.dispose(); return map; } return null; } protected void uninstallKeyboardActions() { SwingUtilities.replaceUIActionMap(toolBar, null); SwingUtilities.replaceUIInputMap(toolBar, JComponent. WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, null); } protected LayoutManager createLayout() { return new SynthToolBarLayoutManager(); } public SynthContext getContext(JComponent c) { return getContext(c, getComponentState(c)); } private SynthContext getContext(JComponent c, int state) { return SynthContext.getContext(SynthContext.class, c, SynthLookAndFeel.getRegion(c), style, state); } private SynthContext getContext(JComponent c, Region region) { return getContext(c, region, getComponentState(c, region)); } private SynthContext getContext(JComponent c, Region region, int state) { return SynthContext.getContext(SynthContext.class, c, region, dragWindowStyle, state); } private Region getRegion(JComponent c) { return SynthLookAndFeel.getRegion(c); } private int getComponentState(JComponent c) { return SynthLookAndFeel.getComponentState(c); } private int getComponentState(JComponent c, Region region) { return SynthLookAndFeel.getComponentState(c); } public void update(Graphics g, JComponent c) { SynthContext context = getContext(c); SynthLookAndFeel.update(context, g); paint(context, g); context.dispose(); } public void paint(Graphics g, JComponent c) { SynthContext context = getContext(c); paint(context, g); context.dispose(); } protected void paint(SynthContext context, Graphics g) { if (handleIcon != null && toolBar.isFloatable()) { int startX = toolBar.getComponentOrientation().isLeftToRight() ? 0 : toolBar.getWidth() - SynthIcon.getIconWidth(handleIcon, context); SynthIcon.paintIcon(handleIcon, context, g, startX, 0, SynthIcon.getIconWidth(handleIcon, context), SynthIcon.getIconHeight(handleIcon, context)); } SynthContext subcontext = getContext(toolBar, Region.TOOL_BAR_CONTENT); paintContent(subcontext, g, contentRect); subcontext.dispose(); } public void paintContent(SynthContext context, Graphics g, Rectangle bounds) { SynthLookAndFeel.updateSubregion(context, g, bounds); } 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.requestFocus(); break; } } break;
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?