⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolbarui.java

📁 用于java swing的皮肤软件
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
package com.digitprop.tonic;

import javax.swing.*;
import javax.swing.event.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.*;
import java.awt.IllegalComponentStateException;

import java.beans.*;

import java.util.Hashtable;
import java.util.HashMap;

import javax.swing.border.*;
import javax.swing.plaf.*;
import javax.swing.plaf.basic.*;


/** UI delegate for JToolBars.
 * 
 *  @author Markus Fischer
 *
 *  	<p>This software is under the <a href="http://www.gnu.org/copyleft/lesser.html" target="_blank">GNU Lesser General Public License</a>
 */

/*
 * ------------------------------------------------------------------------
 * Copyright (C) 2004 Markus Fischer
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation.
 * 
 * This library 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
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free 
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
 * MA 02111-1307  USA
 * 
 * You can contact the author at:
 *    Markus Fischer
 *    www.digitprop.com
 *    info@digitprop.com
 * ------------------------------------------------------------------------
 */
public class ToolBarUI extends BasicToolBarUI 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;

	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= true;

	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.
	 */
	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.
	 */
	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.
	 */
	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.
	 */
	protected KeyStroke rightKey;

	private static String FOCUSED_COMP_INDEX= "JToolBar.focusedCompIndex";

	public static ComponentUI createUI(JComponent c)
	{
		return new ToolBarUI();
	}

	public void installUI(JComponent c)
	{
		toolBar= (JToolBar) c;

		if(toolBar.getOrientation()==JToolBar.HORIZONTAL)
			toolBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		else
			toolBar.setLayout(new VerticalFlowLayout(VerticalFlowLayout.TOP, 0, 0));
		
		// 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();
		}
		
		// Set the borders of contained buttons
		for(int i=0; i<toolBar.getComponentCount(); i++)
		{
			Component child=toolBar.getComponent(i);
			
			if (toolBarFocusListener != null)
			{
				child.addFocusListener(toolBarFocusListener);
			}

			setButtonSize(child);
		
			if (isRolloverBorders())
			{
				setBorderToRollover(child);
			}
			else
			{
				setBorderToNonRollover(child);
			}			
		}
	}

	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 setOrientedBorder()
	{
		if(toolBar.getOrientation()==JToolBar.HORIZONTAL)
			LookAndFeel.installBorder(toolBar, "ToolBar.border");
		else
			LookAndFeel.installBorder(toolBar, "ToolBar.verticalBorder");
	}
	
	
	protected void installDefaults()
	{
		setOrientedBorder();
		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)
		{
			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;
		}
	}

	protected void installKeyboardActions()
	{
		InputMap km= getMyInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

		SwingUtilities.replaceUIInputMap(
			toolBar,
			JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
			km);
		ActionMap am= getMyActionMap();

		if (am != null)
		{
			SwingUtilities.replaceUIActionMap(toolBar, am);
		}
	}

	InputMap getMyInputMap(int condition)
	{
		if (condition == JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT)
		{
			return (InputMap) UIManager.get("ToolBar.ancestorInputMap");
		}
		return null;
	}

	ActionMap getMyActionMap()
	{
		ActionMap map= (ActionMap) UIManager.get("ToolBar.actionMap");

		if (map == null)
		{
			map= createMyActionMap();
			if (map != null)
			{
				UIManager.getLookAndFeelDefaults().put("ToolBar.actionMap", map);
			}
		}
		return map;
	}

	ActionMap createMyActionMap()
	{
		ActionMap map= new ActionMapUIResource();
		map.put("navigateRight", new RightAction());
		map.put("navigateLeft", new LeftAction());
		map.put("navigateUp", new UpAction());
		map.put("navigateDown", new DownAction());
		return map;
	}

	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.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.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()
	{
		return new ToolBarBorder(); //BorderFactory.createEmptyBorder(0, 0, 0, 0);
//		UIDefaults table= UIManager.getLookAndFeelDefaults();
//		return new CompoundBorder(
//			new BasicBorders.RolloverButtonBorder(
//				table.getColor("controlShadow"),
//				table.getColor("controlDkShadow"),
//				table.getColor("controlHighlight"),
//				table.getColor("controlLtHighlight")),
//			new ToolBarMarginBorder());
	}

	/**
	 * 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()
	{
		return new ToolBarBorder(); //BorderFactory.createEmptyBorder(0, 0, 0, 0);
//		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 ToolBarMarginBorder());
	}

	/**
	 * Creates a non rollover border for Toggle buttons in the toolbar.
	 */
	private Border createNonRolloverToggleBorder()
	{
		return new ToolBarBorder(); //BorderFactory.createEmptyBorder(0, 0, 0, 0);
		
//		UIDefaults table= UIManager.getLookAndFeelDefaults();
//		return new CompoundBorder(
//			new BasicBorders.RadioButtonBorder(
//				table.getColor("ToggleButton.shadow"),
//				table.getColor("ToggleButton.darkShadow"),

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -