menubutton.java

来自「具有不同语法高亮的编辑器实例」· Java 代码 · 共 225 行

JAVA
225
字号
/*
 * 07/14/2004
 *
 * MenuButton.java - A button that, when clicked, presents a popup menu.
 * Copyright (C) 2004 Robert Futrell
 * email@address.com
 * www.website.com
 *
 * This program 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.
 *
 * This program 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.*;
import java.awt.event.ActionEvent;
import javax.swing.*;


/**
 * A button that, when clicked, presents the user with a popup menu.
 *
 * @author Robert Futrell
 * @version 0.9
 */
public class MenuButton extends JButton {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4800815697376616488L;

	private JPopupMenu popupMenu;
	private CompoundIcon compoundIcon;


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


	/**
	 * Constructor.
	 */
	public MenuButton(Icon icon) {
		super();
		popupMenu = new JPopupMenu();
		setAction(new AbstractAction() {
					public void actionPerformed(ActionEvent e) {
						Point location = getLocation();
						location.y = getHeight();
						popupMenu.show(MenuButton.this, 0,location.y);
					}
				});
		setIcon(icon); // Set AFTER setAction so it isn't overridden.
	}


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


	/**
	 * Adds a menu item to the popup menu.
	 *
	 * @param menuItem The menu item to add.
	 */
	public void addMenuItem(JMenuItem menuItem) {
		popupMenu.add(menuItem);
	}


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


	/**
	 * Adds a separator to the popup menu.
	 */
	public void addSeparator() {
		popupMenu.addSeparator();
	}


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


	/**
	 * Overridden to give just a little extra space since our icon is longer.
	 * FIXME:  Figure out why we really have to do this...
	 */
	public Dimension getPreferredSize() {
		Dimension size = super.getPreferredSize();
		if (size!=null)
			size.width += CompoundIcon.SPACING;
		return size;
	}


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


	/**
	 * Sets the icon to be used for this button.  This is overridden so that
	 * a little "down arrow" is put beside of the icon to signify that there
	 * is a pulldown menu.
	 *
	 * @param icon The icon to use for this button.
	 */
	public void setIcon(Icon icon) {
		if (compoundIcon==null)
			compoundIcon = new CompoundIcon(icon);
		else
			compoundIcon.setOtherIcon(icon);
		super.setIcon(compoundIcon);
	}


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


	/**
	 * Overridden so the popup menu gets updated as well.
	 */
	public void updateUI() {
		super.updateUI();
		if (popupMenu!=null) // First time this is called, popupMenu is null.
			SwingUtilities.updateComponentTreeUI(popupMenu);
	}


/*****************************************************************************/
/************************* PRIVATE INNER CLASSES *****************************/
/*****************************************************************************/


	/**
	 * An icon that combines another icon with a down-arrow icon.
	 */
	class CompoundIcon implements Icon {

		private Icon arrowIcon;
		private Icon otherIcon;
		private static final int SPACING	= 3;

		public CompoundIcon(Icon icon) {
			setOtherIcon(icon);
			arrowIcon = new DownArrowIcon();
		}

		public int getIconHeight() {
			if (otherIcon==null)
				return arrowIcon.getIconHeight();
			return Math.max(otherIcon.getIconHeight(), arrowIcon.getIconHeight());
		}

		public int getIconWidth() {
			if (otherIcon==null)
				return arrowIcon.getIconWidth();
			return otherIcon.getIconWidth() + SPACING + arrowIcon.getIconWidth();
		}

		public void paintIcon(Component c, Graphics g, int x, int y) {
			if (otherIcon!=null) {
				otherIcon.paintIcon(c, g, x,y);
				x += otherIcon.getIconWidth() + SPACING;
			}
			y = (c.getHeight()-arrowIcon.getIconHeight())/2;
			arrowIcon.paintIcon(c, g, x,y);
		}

		public void setOtherIcon(Icon otherIcon) {
			this.otherIcon = otherIcon;
		}

	}


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


	/**
	 * The arrow implying that there is a menu underneath this button.
	 */
	class DownArrowIcon implements Icon {

		int[] xcoord;
		int[] ycoord;
		private static final int SIZE		= 6;

		public DownArrowIcon() {
			xcoord = new int[3];
			ycoord = new int[3];
		}

		public int getIconHeight() {
			return SIZE;
		}

		public int getIconWidth() {
			return SIZE;
		}

		public void paintIcon(Component c, Graphics g, int x, int y) {
			g.setColor(Color.BLACK);
			xcoord[0] = x;
			xcoord[1] = x + SIZE;
			xcoord[2] = (xcoord[0] + xcoord[1])/2;
			ycoord[0] = ycoord[1] = y;
			ycoord[2] = y + SIZE;
			g.fillPolygon(xcoord,ycoord, 3);
		}

	}


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

}

⌨️ 快捷键说明

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