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

📄 customizabletoolbar.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * 04/13/2004
 *
 * CustomizableToolBar.java - A toolbar with a popup right-click
 *         menu allowing the user to toggle docking and (in the
 *         future) add/remove buttons and separators.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This file is a part of RText.
 *
 * RText is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * RText 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 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.fife.ui;

import java.awt.Component;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.plaf.ToolBarUI;
import javax.swing.plaf.basic.BasicToolBarUI;


/**
 * An extension of <code>JToolBar</code> that adds a right-click popup
 * menu allowing the user to toggle docking and add or remove buttons
 * and separators.
 * <p>To use this class, create a subclass of it, and at the end of its
 * constructor, call <code>makeCustomizable</code>.
 *
 * @author Robert Futrell
 * @version 0.1
 */
public class CustomizableToolBar extends JToolBar implements PopupMenuListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1254972284055511491L;

	/**
	 * The popup menu for the toolbar.
	 */
	private JPopupMenu popupMenu;

	/**
	 * The menu for adding/removing toolbar buttons.
	 */
	private JMenu addRemoveMenu;

	/**
	 * The mouse listener that listens for right-clicks on this toolbar.
	 */
	private MouseInputAdapter mia;


/*****************************************************************************/


	/**
	 * Creates a new toolbar.
	 */
	public CustomizableToolBar() {
		super();
	}


/*****************************************************************************/


	/**
	 * Creates a new toolbar with the specified orientation.
	 *
	 * @param orientation The orientation preferred (either
	 *                    <code>HORIZONTAL</code> or <code>VERTICAL</code>.
	 */
	public CustomizableToolBar(int orientation) {
		super(orientation);
	}


/*****************************************************************************/


	/**
	 * Creates a new toolbar.
	 *
	 * @param name The name for the tool bar.
	 */
	public CustomizableToolBar(String name) {
		super(name);
	}


/*****************************************************************************/


	/**
	 * Creates a new toolbar.
	 *
	 * @param name The name for the tool bar.
	 * @param orientation The orientation preferred (either
	 *                    <code>HORIZONTAL</code> or <code>VERTICAL</code>.
	 */
	public CustomizableToolBar(String name, int orientation) {
		super(name, orientation);
	}


/*****************************************************************************/


	/**
	 * Creates the popup menu.
	 */
	private void createPopupMenu() {

		popupMenu = new JPopupMenu();
		AbstractAction lockAction = new AbstractAction("Lock the toolbar") {
							public void actionPerformed(ActionEvent e) {

								/* 8.5.2004/pwy: the assumption that getUI returns a BasicToolBarUI
								is not coorect on all platforms. The method used below is more portable. 
									CustomizableToolBar cbt = CustomizableToolBar.this;
									cbt.setFloatable(!cbt.isFloatable());
								05/13/2004 - ref:  The proposed fix actually doesn't work quite right on
								platforms supporting BasicToolBarUI - if you undock the toolbar, then
								right-click and choose "Lock the toolbar" from the popup menu, then close
								the window the floating toolbar is in, you can never get the toolbar to
								reappear in the application!  Thus, I'm simply going to check for the UI
								being a subclass of BasicToolBarUI for now... */

								ToolBarUI ui = getUI();
								if (ui instanceof BasicToolBarUI) {
									boolean floating = ((BasicToolBarUI)ui).isFloating();
									if (floating==true) {
										((BasicToolBarUI)ui).setFloating(false, new Point(0,0));
										setFloatable(false);
									}
									else
										setFloatable(!isFloatable());
								}
								else { // ???
									CustomizableToolBar cbt = CustomizableToolBar.this;
									cbt.setFloatable(!cbt.isFloatable());
								}

							}
						};
		JCheckBoxMenuItem lockMenuItem = new JCheckBoxMenuItem(lockAction);
		lockMenuItem.setMnemonic(KeyEvent.VK_L);
		popupMenu.add(lockMenuItem);

		popupMenu.addSeparator();

		addRemoveMenu = new JMenu("Add or Remove Buttons");
		addRemoveMenu.setMnemonic(KeyEvent.VK_A);
		addRemoveMenu.getPopupMenu().addPopupMenuListener(this);
		popupMenu.add(addRemoveMenu);

	}


/*****************************************************************************/


	/**
	 * This should be called at the end of the constructor of any toolbar
	 * that overrides this class.  This is the method that sets up the
	 * popup menu for the toolbar.  If you don't call this method, then
	 * a <code>CustomizableToolBar</code> behaves no differently than a
	 * <code>JToolBar</code>.
	 */
	public void makeCustomizable() {

		// Remove an old mouse listener if MakeCustomizable() has been called before.
		if (mia!=null) {
			removeMouseListener(mia);
			mia = null;
		}

		// Create the action that listens for right-clicks for customization.
		mia = new MouseInputAdapter() {
							public void mousePressed(MouseEvent e) {
								maybeShowPopup(e);
							}
							public void mouseReleased(MouseEvent e) {
								maybeShowPopup(e);
							}
							public void maybeShowPopup(MouseEvent e) {
								if (e.isPopupTrigger()) {
									if (popupMenu==null)
										createPopupMenu();
									popupMenu.show(e.getComponent(), e.getX(), e.getY());
									e.consume();
								}
							}};

		// Add the mouse listener to all parts of the toolbar so it always works.
		addMouseListener(mia);	// Add to the toolbar itself.
		Component c = getComponentAtIndex(0);
		for (int i=0; c!=null; c=getComponentAtIndex(++i)) {
			c.addMouseListener(mia);
		}

	}


/*****************************************************************************/


	/**
	 * Called whenever the add/remove popup menu is cancelled.
	 */
	public void popupMenuCanceled(PopupMenuEvent e) {
	}


/*****************************************************************************/


	/**
	 * Called whenever the add/remove popup menu is going to become
	 * invisible.
	 */
	public void popupMenuWillBecomeInvisible(PopupMenuEvent e)  {
	}


/*****************************************************************************/


	/**
	 * Called whenever the add/remove popup menu is going to become visible.
	 */
	public void popupMenuWillBecomeVisible(PopupMenuEvent e) {

		JPopupMenu popupMenu = addRemoveMenu.getPopupMenu();
		popupMenu.removeAll();

		JCheckBoxMenuItem cbMenuItem;

		Component[] components = getComponents();
		int num = components.length;
		for (int i=0; i<num; i++) {
			if (components[i] instanceof JButton) {
				final JButton button = (JButton)components[i];
				String title = button.getText();
				if (title==null)
					title = button.getToolTipText();
				if (title==null)
					title = "<Unknown>";
				cbMenuItem = new JCheckBoxMenuItem(new AbstractAction(title) {
									public void actionPerformed(ActionEvent e) {
										button.setVisible(!button.isVisible());
										repaint();
									}
								});
				cbMenuItem.setSelected(button.isVisible());
				popupMenu.add(cbMenuItem);
			}
		}
		popupMenu.addSeparator();
		popupMenu.add(new JMenuItem(new AbstractAction("Reset Toolbar") {
									public void actionPerformed(ActionEvent e) {
										Component[] c = getComponents();
										int num = c.length;
										for (int i=0; i<num; i++)
											if (c[i] instanceof JButton)
												c[i].setVisible(true);
									}
								}));

	}


/*****************************************************************************/


	/**
	 * Overridden so the popup menu gets its UI redone too.
	 */
	public void updateUI() {
		super.updateUI();
		if (popupMenu!=null) {
			SwingUtilities.updateComponentTreeUI(popupMenu);
		}
	}


/*****************************************************************************/

}

⌨️ 快捷键说明

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