📄 basictoolbarui.java
字号:
/* BasicToolBarUI.java -- Copyright (C) 2004, 2005 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING. If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library. Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule. An independent module is a module which is not derived fromor based on this library. If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so. If you do not wish to do so, delete thisexception statement from your version. */package javax.swing.plaf.basic;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Component;import java.awt.Container;import java.awt.Dimension;import java.awt.Graphics;import java.awt.Insets;import java.awt.Point;import java.awt.Rectangle;import java.awt.Window;import java.awt.event.ContainerEvent;import java.awt.event.ContainerListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.beans.PropertyChangeEvent;import java.beans.PropertyChangeListener;import java.util.Hashtable;import javax.swing.JButton;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JToolBar;import javax.swing.KeyStroke;import javax.swing.LookAndFeel;import javax.swing.RootPaneContainer;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.border.Border;import javax.swing.event.MouseInputListener;import javax.swing.plaf.BorderUIResource.EtchedBorderUIResource;import javax.swing.plaf.ComponentUI;import javax.swing.plaf.ToolBarUI;import javax.swing.plaf.UIResource;/** * This is the Basic Look and Feel UI class for JToolBar. */public class BasicToolBarUI extends ToolBarUI implements SwingConstants{ /** Static owner of all DragWindows. * This is package-private to avoid an accessor method. */ static JFrame owner = new JFrame(); /** The border used when the JToolBar is in nonrollover mode. */ private static Border nonRolloverBorder; /** The border used when the JToolBar is in rollover mode. */ private static Border rolloverBorder; /** The last known BorderLayout constraint before floating. */ protected String constraintBeforeFloating; /** The last known orientation of the JToolBar before floating. * This is package-private to avoid an accessor method. */ int lastGoodOrientation; /** The color of the border when it is dockable. */ protected Color dockingBorderColor; /** The background color of the JToolBar when it is dockable. */ protected Color dockingColor; /** The docking listener responsible for mouse events on the JToolBar. */ protected MouseInputListener dockingListener; /** The window used for dragging the JToolBar. */ protected BasicToolBarUI.DragWindow dragWindow; /** The color of the border when it is not dockable. */ protected Color floatingBorderColor; /** The background color of the JToolBar when it is not dockable. */ protected Color floatingColor; /** The index of the focused component. */ protected int focusedCompIndex; /** The PropertyChangeListener for the JToolBar. */ protected PropertyChangeListener propertyListener; /** The JToolBar this UI delegate is responsible for. */ protected JToolBar toolBar; /** The Container listener for the JToolBar. */ protected ContainerListener toolBarContListener; /** The Focus listener for the JToolBar. */ protected FocusListener toolBarFocusListener; /** * @deprecated since JDK1.3. */ protected KeyStroke leftKey; /** * @deprecated since JDK1.3. */ protected KeyStroke rightKey; /** * @deprecated since JDK1.3. */ protected KeyStroke upKey; /** * @deprecated since JDK1.3. */ protected KeyStroke downKey; /** * The floating window that is responsible for holding the JToolBar when it * is dragged outside of its original parent. */ private transient Window floatFrame; /** The original parent of the JToolBar. * This is package-private to avoid an accessor method. */ transient Container origParent; /** A hashtable of components and their original borders. * This is package-private to avoid an accessor method. */ transient Hashtable borders; /** A window listener for the floatable frame. */ private transient WindowListener windowListener; /** A set of cached bounds of the JToolBar. * This is package-private to avoid an accessor method. */ transient Dimension cachedBounds; /** The cached orientation of the JToolBar. * This is package-private to avoid an accessor method. */ transient int cachedOrientation; /** * This method creates a new <code>BasicToolBarUI</code> object for the given JToolBar. */ public BasicToolBarUI() { // Do nothing here. } /** * This method returns whether the JToolBar can dock at the given position. * * @param c The component to try to dock in. * @param p The position of the mouse cursor relative to the given * component. * * @return Whether the JToolBar can dock. */ public boolean canDock(Component c, Point p) { return areaOfClick(c, p) != -1; } /** * This helper method returns the position of the JToolBar if it can dock. * * @param c The component to try to dock in. * @param p The position of the mouse cursor relative to the given * component. * * @return One of the SwingConstants directions or -1 if the JToolBar can't * dock. */ private int areaOfClick(Component c, Point p) { // Has to dock in immediate parent, not eventual root container. Rectangle pBounds = c.getBounds(); // XXX: In Sun's implementation, the space the toolbar has to dock is dependent on the size it had last. Dimension d = toolBar.getSize(); int limit = Math.min(d.width, d.height); // The order of checking is 1. top 2. bottom 3. left 4. right if (! pBounds.contains(p)) return -1; if (p.y < limit) return SwingConstants.NORTH; if (p.y > (pBounds.height - limit)) return SwingConstants.SOUTH; if (p.x < limit) return SwingConstants.WEST; if (p.x > (pBounds.width - limit)) return SwingConstants.EAST; return -1; } /** * This method creates a new DockingListener for the JToolBar. * * @return A new DockingListener for the JToolBar. */ protected MouseInputListener createDockingListener() { return new DockingListener(toolBar); } /** * This method creates a new DragWindow for the given JToolBar. * * @param toolbar The JToolBar to create a DragWindow for. * * @return A new DragWindow. */ protected BasicToolBarUI.DragWindow createDragWindow(JToolBar toolbar) { return new DragWindow(); } /** * This method creates a new floating frame for the JToolBar. By default, * this UI uses createFloatingWindow instead. This method of creating a * floating frame is deprecated. * * @param toolbar The JToolBar to create a floating frame for. * * @return A new floating frame. */ protected JFrame createFloatingFrame(JToolBar toolbar) { // FIXME: Though deprecated, this should still work. return null; } /** * This method creates a new floating window for the JToolBar. This is the * method used by default to create a floating container for the JToolBar. * * @param toolbar The JToolBar to create a floating window for. * * @return A new floating window. */ protected RootPaneContainer createFloatingWindow(JToolBar toolbar) { // This one is used by default though. return new ToolBarDialog(); } /** * This method creates a new WindowListener for the JToolBar. * * @return A new WindowListener. */ protected WindowListener createFrameListener() { return new FrameListener(); } /** * This method creates a new nonRolloverBorder for JButtons when the * JToolBar's rollover property is set to false. * * @return A new NonRolloverBorder. */ protected Border createNonRolloverBorder() { return new EtchedBorderUIResource(); } /** * This method creates a new PropertyChangeListener for the JToolBar. * * @return A new PropertyChangeListener. */ protected PropertyChangeListener createPropertyListener() { return new PropertyListener(); } /** * This method creates a new rollover border for JButtons when the * JToolBar's rollover property is set to true. * * @return A new rollover border. */ protected Border createRolloverBorder() { return new EtchedBorderUIResource() { public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { if (c instanceof JButton) { if (((JButton) c).getModel().isRollover()) super.paintBorder(c, g, x, y, width, height); } } }; } /** * This method creates a new Container listener for the JToolBar. * * @return A new Container listener. */ protected ContainerListener createToolBarContListener() { return new ToolBarContListener(); } /** * This method creates a new FocusListener for the JToolBar. * * @return A new FocusListener for the JToolBar. */ protected FocusListener createToolBarFocusListener() { return new ToolBarFocusListener(); } /** * This method creates a new UI delegate for the given JComponent. * * @param c The JComponent to create a UI delegate for. * * @return A new UI delegate. */ public static ComponentUI createUI(JComponent c) { return new BasicToolBarUI(); } /** * This method is called to drag the DragWindow around when the JToolBar is * being dragged around. * * @param position The mouse cursor coordinates relative to the JToolBar. * @param origin The screen position of the JToolBar. */ protected void dragTo(Point position, Point origin) { int loc = areaOfClick(origParent, SwingUtilities.convertPoint(toolBar, position, origParent)); if (loc != -1) { dragWindow.setBorderColor(dockingBorderColor); dragWindow.setBackground(dockingColor); } else { dragWindow.setBorderColor(floatingBorderColor); dragWindow.setBackground(floatingColor); } int w = 0; int h = 0; boolean tmp = ((loc == SwingConstants.NORTH) || (loc == SwingConstants.SOUTH) || (loc == -1)); cachedOrientation = toolBar.getOrientation(); cachedBounds = toolBar.getSize(); if (((cachedOrientation == SwingConstants.HORIZONTAL) && tmp) || ((cachedOrientation == VERTICAL) && ! tmp)) { w = cachedBounds.width; h = cachedBounds.height; } else { w = cachedBounds.height; h = cachedBounds.width; } Point p = dragWindow.getOffset(); Insets insets = toolBar.getInsets(); dragWindow.setBounds((origin.x + position.x) - p.x - ((insets.left + insets.right) / 2), (origin.y + position.y) - p.y - ((insets.top + insets.bottom) / 2), w, h); if (! dragWindow.isVisible()) dragWindow.show(); } /** * This method is used at the end of a drag session to place the frame in * either its original parent as a docked JToolBar or in its floating * frame. * * @param position The position of the mouse cursor relative to the * JToolBar. * @param origin The screen position of the JToolBar before the drag session * started. */ protected void floatAt(Point position, Point origin) { Point p = new Point(position); int aoc = areaOfClick(origParent, SwingUtilities.convertPoint(toolBar, p, origParent)); Container oldParent = toolBar.getParent(); oldParent.remove(toolBar); oldParent.doLayout(); oldParent.repaint(); Container newParent; if (aoc == -1) newParent = ((RootPaneContainer) floatFrame).getContentPane(); else { floatFrame.hide(); newParent = origParent; } String constraint; switch (aoc) { case SwingConstants.EAST: constraint = BorderLayout.EAST; break; case SwingConstants.NORTH: constraint = BorderLayout.NORTH; break; case SwingConstants.SOUTH: constraint = BorderLayout.SOUTH; break; case SwingConstants.WEST: constraint = BorderLayout.WEST; break; default: constraint = BorderLayout.CENTER;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -