按钮创建

JButton aMinus = new JButton();
String Minus = Msg.getMsg(Env.getCtx(), "Minus");
DialogButton dbMinus = new DialogButton(Minus, "Minus", Env
				.getImageIcon("Minus24.gif"), 0);
dbMinus.getInputMap(CButton.WHEN_IN_FOCUSED_WINDOW).put(
				KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, Event.CTRL_MASK),
				Minus);
dbMinus.getActionMap().put(Minus, dbMinus.getAction());
aMinus = dbMinus;
aMinus.addActionListener(this);

public void init(){
	b_New = createButtonAction("New", null);
	b_New.setText("New");
}
/**
 * Create Action Button
 * 
 * @param action
 *            action
 * @return button
 */
protected CButton createButtonAction(String action, KeyStroke accelerator) {
	AppsAction act = new AppsAction(action, accelerator, false);
	act.setDelegate(this);
	CButton button = (CButton) act.getButton();
	button.setPreferredSize(new Dimension(80, 40));
	button.setMinimumSize(getPreferredSize());
	button.setMaximumSize(getPreferredSize());
	button.setFocusable(false);
	return button;
} // getButtonAction
package org.compiere.apps;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.compiere.swing.*;
import org.compiere.util.*;

/**
 *  Application Action.
 *		Creates Action with MenuItem and Button
 *		The ActionCommand is translated for display
 *		If translated text contains &, the next character is the Mnemonic
 *
 *  @author 	Jorg Janke
 *  @version 	$Id: AppsAction.java,v 1.1 2008/01/14 04:53:11 cly Exp $
 */
public final class AppsAction extends AbstractAction
{
	/**
	 *  Application Action
	 *
	 *  @param   action base action command - used as AD_Message for Text and Icon name
	 *  @param   accelerator optional keystroke for accelerator
	 *  @param   toggle is toggle action (maintains state)
	 */
	public AppsAction (String action, KeyStroke accelerator, boolean toggle)
	{
		super();
		m_action = action;

		//	Data
		String text = Msg.getMsg(Env.getCtx(), action);
		int pos = text.indexOf("&");
		if (pos != -1)					//	We have a nemonic
		{
			Character ch = new Character(text.toUpperCase().charAt(pos+1));
			text = text.substring(0, pos) + text.substring(pos+1);
			putValue(Action.MNEMONIC_KEY, new Integer(ch.hashCode()));
		}
		//
		Icon small = getIcon(action, true);
		Icon large = getIcon(action, false);
		Icon smallPressed = null;
		Icon largePressed = null;

		m_toggle = toggle;
		//  ToggleIcons have the pressed name with X
		if (toggle)
		{
			smallPressed = getIcon(action+"X", true);
			if (smallPressed == null)
				smallPressed = small;
			largePressed = getIcon(action+"X", false);
			if (largePressed == null)
				largePressed = large;
		}

		//	Attributes
		putValue(Action.NAME, text);						//	Display
		putValue(Action.SMALL_ICON, small);                 //  Icon
		putValue(Action.SHORT_DESCRIPTION, text);			//	Tooltip
		putValue(Action.ACTION_COMMAND_KEY, m_action);      //  ActionCammand
		putValue(Action.ACCELERATOR_KEY, accelerator);      //  KeyStroke
	//	putValue(Action.MNEMONIC_KEY, new Integer(0));      //  Mnemonic
	//	putValue(Action.DEFAULT, text);						//	Not Used

		//	Create Button
		if (toggle)
		{
			m_button = new CToggleButton(this);
			m_button.setSelectedIcon(largePressed);
		}
		else
			m_button = new CButton(this);
		m_button.setName(action);
		//	Correcting Action items
		if (large != null)
		{
			m_button.setIcon(large);
			m_button.setText(null);
		}
		m_button.setActionCommand(m_action);
		m_button.setMargin(BUTTON_INSETS);
		m_button.setSize(BUTTON_SIZE);

		//	Create Menu
		if (toggle)
		{
			m_menu = new JCheckBoxMenuItem(this);
			m_menu.setSelectedIcon(smallPressed);
		}
		else
			m_menu = new JMenuItem(this);
		m_menu.setAccelerator(accelerator);
		m_menu.setActionCommand(m_action);
	}	//	Action
	
	/**
	 *  Application Action
	 *
	 *  @param   action base action command - used as AD_Message for Text and Icon name
	 *  @param   accelerator optional keystroke for accelerator
	 *  @param   toggle is toggle action (maintains state)
	 */
	public AppsAction (String action, KeyStroke accelerator, String toolTipText, boolean toggle)
	{
		super();
		m_action = action;

		//	Data
		if (toolTipText == null)
			toolTipText = Msg.getMsg(Env.getCtx(), action);
		int pos = toolTipText.indexOf('&');
		if (pos != -1  && toolTipText.length() > pos)	//	We have a nemonic - creates ALT-_
		{
			Character ch = new Character(toolTipText.toUpperCase().charAt(pos+1));
			if (ch != ' ')
			{
				toolTipText = toolTipText.substring(0, pos) + toolTipText.substring(pos+1);
				putValue(Action.MNEMONIC_KEY, new Integer(ch.hashCode()));
			}
		}
		//
		Icon small = getIcon(action, true);
		Icon large = getIcon(action, false);
		Icon smallPressed = null;
		Icon largePressed = null;

		m_toggle = toggle;
		//  ToggleIcons have the pressed name with X
		if (toggle)
		{
			smallPressed = getIcon(action+"X", true);
			if (smallPressed == null)
				smallPressed = small;
			largePressed = getIcon(action+"X", false);
			if (largePressed == null)
				largePressed = large;
		}

		//	Attributes
		putValue(Action.NAME, toolTipText);						//	Display
		putValue(Action.SMALL_ICON, small);                 //  Icon
		putValue(Action.SHORT_DESCRIPTION, toolTipText);			//	Tooltip
		putValue(Action.ACTION_COMMAND_KEY, m_action);      //  ActionCammand
		putValue(Action.ACCELERATOR_KEY, accelerator);      //  KeyStroke
	//	putValue(Action.MNEMONIC_KEY, new Integer(0));      //  Mnemonic
	//	putValue(Action.DEFAULT, text);						//	Not Used

		//	Create Button
		if (toggle)
		{
			m_button = new CToggleButton(this);
			m_button.setSelectedIcon(largePressed);
		}
		else
			m_button = new CButton(this);
		m_button.setName(action);
		//	Correcting Action items
		if (large != null)
		{
			m_button.setIcon(large);
			m_button.setText(null);
		}
		m_button.setActionCommand(m_action);
		m_button.setMargin(BUTTON_INSETS);
		m_button.setSize(BUTTON_SIZE);

		//	Create Menu
		if (toggle)
		{
			m_menu = new JCheckBoxMenuItem(this);
			m_menu.setSelectedIcon(smallPressed);
		}
		else
			m_menu = new JMenuItem(this);
		m_menu.setAccelerator(accelerator);
		m_menu.setActionCommand(m_action);
	}	//	Action

	/** Button Size     			*/
	public static final Dimension	BUTTON_SIZE = new Dimension(28,28);
	/** Button Insets   			*/
	public static final Insets		BUTTON_INSETS = new Insets(0, 0, 0, 0);
	/** CButton or CToggelButton	*/
	private AbstractButton 	m_button;
	/**	Menu						*/
	private JMenuItem		m_menu;

	private String			m_action;
	private ActionListener	m_delegate = null;
	private boolean 		m_toggle = false;
	private boolean			m_pressed = false;

	/**
	 *	Get Icon with name action
	 *  @param name name
	 *  @param small small
	 *  @return Icon
	 */
	private ImageIcon getIcon(String name, boolean small)
	{
		String fullName = name + (small ? "16.gif" : "24.gif");
		return Env.getImageIcon(fullName);
	}	//	getIcon

	/**
	 *	Get Name/ActionCommand
	 *  @return ActionName
	 */
	public String getName()
	{
		return m_action;
	}	//	getName

	/**
	 *	Return Button
	 *  @return Button
	 */
	public AbstractButton getButton()
	{
		return m_button;
	}	//	getButton

	/**
	 *	Return MenuItem
	 *  @return MenuItem
	 */
	public JMenuItem getMenuItem()
	{
		return m_menu;
	}	//	getMenuItem

	/**
	 *	Set Delegate to receive the actionPerformed calls
	 *  @param al listener
	 */
	public void setDelegate(ActionListener al)
	{
		m_delegate = al;
	}	//	setDelegate

	/**
	 *	Toggle
	 *  @param pressed pressed
	 */
	public void setPressed (boolean pressed)
	{
		if (!m_toggle)
			return;
		m_pressed = pressed;

		//	Set Button
		m_button.setSelected(pressed);
		//	Set Menu
		m_menu.setSelected(pressed);
	}	//	setPressed

	/**
	 *	IsPressed
	 *  @return true if pressed
	 */
	public boolean isPressed()
	{
		return m_pressed;
	}	//	isPressed

	/**
	 *	ActionListener
	 *  @param e Event
	 */
	public void actionPerformed(ActionEvent e)
	{
	//	log.info( "AppsAction.actionPerformed", e.getActionCommand());
		//	Toggle Items
		if (m_toggle)
			setPressed(!m_pressed);
		//	Inform
		if (m_delegate != null)
			m_delegate.actionPerformed(e);
	}	//	actionPerformed

	/**
	 *	Dispose
	 */
	public void dispose()
	{
		m_button = null;
		m_menu = null;
	}	//	dispose

	/**
	 *  toString
	 *  @return String Representation
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer("AppsAction - ");
		sb.append(m_action)
			.append(" Acc=")
			.append(getValue(Action.ACCELERATOR_KEY));
		return sb.toString();
	}   //  toString

}	//	AppsAction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值