AbstractActionExt.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:12k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: AbstractActionExt.java,v 1.3 2005/10/10 18:02:44 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.action;
  22. import java.awt.event.ItemListener;
  23. import java.beans.PropertyChangeListener;
  24. import javax.swing.AbstractAction;
  25. import javax.swing.Action;
  26. import javax.swing.Icon;
  27. import javax.swing.KeyStroke;
  28. /**
  29.  * Extends the concept of the Action to include toggle or group states.
  30.  *
  31.  */
  32. public abstract class AbstractActionExt extends AbstractAction
  33.     implements ItemListener {
  34.     /**
  35.      * The key for the large icon
  36.      */
  37.     public static final String LARGE_ICON = "__LargeIcon__";
  38.     /**
  39.      * The key for the button group
  40.      */
  41.     public static final String GROUP = "__Group__";
  42.     /**
  43.      * The key for the flag which indicates that this is a state type.
  44.      */
  45.     public static final String IS_STATE = "__State__";
  46.     /**
  47.      * Specified whether the action is selected; the default is false
  48.      */
  49.     private boolean selected = false;
  50.     /**
  51.      * Copy constuctor copies the state.
  52.      */
  53.     public AbstractActionExt(AbstractActionExt action) {
  54.         Object[] keys = action.getKeys();
  55.         for (int i = 0; i < keys.length; i++) {
  56.             putValue((String)keys[i], action.getValue((String)keys[i]));
  57.         }
  58.         this.selected = action.selected;
  59.         this.enabled = action.enabled;
  60.         // Copy change listeners.
  61.         PropertyChangeListener[] listeners = action.getPropertyChangeListeners();
  62.         for (int i = 0; i < listeners.length; i++) {
  63.             addPropertyChangeListener(listeners[i]);
  64.         }
  65.     }
  66.     public AbstractActionExt(String name) {
  67.         super(name);
  68.     }
  69.     public AbstractActionExt(String name, Icon icon) {
  70.         super(name, icon);
  71.     }
  72.     /**
  73.      * Constructs an Action with the label and command
  74.      *
  75.      * @param name name of the action usually used as a label
  76.      * @param command command key of the action
  77.      */
  78.     public AbstractActionExt(String name, String command) {
  79.         this(name);
  80.         setActionCommand(command);
  81.     }
  82.     /**
  83.      * @param name display name of the action
  84.      * @param command the value of the action command key
  85.      * @param icon icon to display
  86.      */
  87.     public AbstractActionExt(String name, String command, Icon icon) {
  88.         super(name, icon);
  89.         setActionCommand(command);
  90.     }
  91.     /**
  92.      * Returns a short desciption of the action.
  93.      *
  94.      * @return the short description or null
  95.      */
  96.     public String getShortDescription()  {
  97.         return (String)getValue(Action.SHORT_DESCRIPTION);
  98.     }
  99.     /**
  100.      * Sets the short desciption of the action. This will also
  101.      * set the long description value is it is null.
  102.      * <p>
  103.      * This is a convenience method for <code>putValue</code> with the
  104.      * <code>Action.SHORT_DESCRIPTION</code> key.
  105.      *
  106.      * @param desc the short description; can be <code>null</code>w
  107.      * @see Action#SHORT_DESCRIPTION
  108.      * @see Action#putValue
  109.      */
  110.     public void setShortDescription(String desc) {
  111.         putValue(Action.SHORT_DESCRIPTION, desc);
  112.         if (desc != null && getLongDescription() == null) {
  113.             setLongDescription(desc);
  114.         }
  115.     }
  116.     /**
  117.      * Returns a long desciption of the action.
  118.      *
  119.      * @return the long description or null
  120.      */
  121.     public String getLongDescription()  {
  122.         return (String)getValue(Action.LONG_DESCRIPTION);
  123.     }
  124.     /**
  125.      * Sets the long desciption of the action. This will also set the
  126.      * value of the short description if that value is null.
  127.      * <p>
  128.      * This is a convenience method for <code>putValue</code> with the
  129.      * <code>Action.LONG_DESCRIPTION</code> key.
  130.      *
  131.      * @param desc the long description; can be <code>null</code>
  132.      * @see Action#LONG_DESCRIPTION
  133.      * @see Action#putValue
  134.      */
  135.     public void setLongDescription(String desc) {
  136.         putValue(Action.LONG_DESCRIPTION, desc);
  137.         if (desc != null && getShortDescription() == null) {
  138.             setLongDescription(desc);
  139.         }
  140.     }
  141.     /**
  142.      * Returns a small icon which represents the action.
  143.      *
  144.      * @return the small icon or null
  145.      */
  146.     public Icon getSmallIcon() {
  147.         return (Icon)getValue(SMALL_ICON);
  148.     }
  149.     /**
  150.      * Sets the small icon which represents the action.
  151.      * <p>
  152.      * This is a convenience method for <code>putValue</code> with the
  153.      * <code>Action.SMALL_ICON</code> key.
  154.      *
  155.      * @param icon the small icon; can be <code>null</code>
  156.      * @see Action#SMALL_ICON
  157.      * @see Action#putValue
  158.      */
  159.     public void setSmallIcon(Icon icon) {
  160.         putValue(SMALL_ICON, icon);
  161.     }
  162.     /**
  163.      * Returns a large icon which represents the action.
  164.      *
  165.      * @return the large icon or null
  166.      */
  167.     public Icon getLargeIcon() {
  168.         return (Icon)getValue(LARGE_ICON);
  169.     }
  170.     /**
  171.      * Sets the large icon which represents the action.
  172.      * <p>
  173.      * This is a convenience method for <code>putValue</code> with the
  174.      * <code>LARGE_ICON</code> key.
  175.      *
  176.      * @param icon the large icon; can be <code>null</code>
  177.      * @see #LARGE_ICON
  178.      * @see Action#putValue
  179.      */
  180.     public void setLargeIcon(Icon icon) {
  181.         putValue(LARGE_ICON, icon);
  182.     }
  183.     /**
  184.      * Sets the name of the action.
  185.      * <p>
  186.      * This is a convenience method for <code>putValue</code> with the
  187.      * <code>Action.NAME</code> key.
  188.      *
  189.      * @param name the name of the action; can be <code>null</code>
  190.      * @see Action#NAME
  191.      * @see Action#putValue
  192.      */
  193.     public void setName(String name) {
  194.         putValue(Action.NAME, name);
  195.     }
  196.     /**
  197.      * Returns the name of the action.
  198.      *
  199.      * @return the name of the action or null
  200.      */
  201.     public String getName() {
  202.         return (String)getValue(Action.NAME);
  203.     }
  204.     public void setMnemonic(String mnemonic) {
  205.         if (mnemonic != null && mnemonic.length() > 0) {
  206.             putValue(Action.MNEMONIC_KEY, new Integer(mnemonic.charAt(0)));
  207.         }
  208.     }
  209.     /**
  210.      * Sets the mnemonic key code for the action.
  211.      * <p>
  212.      * This is a convenience method for <code>putValue</code> with the
  213.      * <code>Action.MNEMONIC_KEY</code> key.
  214.      * <p>
  215.      * This method does not validate the value. Please see
  216.      * {@link javax.swing.AbstractButton#setMnemonic(int)} for details
  217.      * concerning the value of the mnemonic.
  218.      *
  219.      * @param mnemonic an int key code mnemonic or 0
  220.      * @see javax.swing.AbstractButton#setMnemonic(int)
  221.      * @see Action#MNEMONIC_KEY
  222.      * @see Action#putValue
  223.      */
  224.     public void setMnemonic(int mnemonic) {
  225.         putValue(Action.MNEMONIC_KEY, new Integer(mnemonic));
  226.     }
  227.     /**
  228.      * Return the mnemonic key code for the action.
  229.      *
  230.      * @return the mnemonic or 0
  231.      */
  232.     public int getMnemonic() {
  233.         Integer value = (Integer)getValue(Action.MNEMONIC_KEY);
  234.         if (value != null) {
  235.             return value.intValue();
  236.         }
  237.         return '';
  238.     }
  239.     /**
  240.      * Sets the action command key. The action command key
  241.      * is used to identify the action.
  242.      * <p>
  243.      * This is a convenience method for <code>putValue</code> with the
  244.      * <code>Action.ACTION_COMMAND_KEY</code> key.
  245.      *
  246.      * @param key the action command
  247.      * @see Action#ACTION_COMMAND_KEY
  248.      * @see Action#putValue
  249.      */
  250.     public void setActionCommand(Object key) {
  251.         putValue(Action.ACTION_COMMAND_KEY, key);
  252.     }
  253.     /**
  254.      * Returns the action command.
  255.      *
  256.      * @return the action command or null
  257.      */
  258.     public Object getActionCommand() {
  259.         return getValue(Action.ACTION_COMMAND_KEY);
  260.     }
  261.     /**
  262.      * Returns the key stroke which represents an accelerator
  263.      * for the action.
  264.      *
  265.      * @return the key stroke or null
  266.      */
  267.     public KeyStroke getAccelerator() {
  268.         return (KeyStroke)getValue(Action.ACCELERATOR_KEY);
  269.     }
  270.     /**
  271.      * Sets the key stroke which represents an accelerator
  272.      * for the action.
  273.      * <p>
  274.      * This is a convenience method for <code>putValue</code> with the
  275.      * <code>Action.ACCELERATOR_KEY</code> key.
  276.      *
  277.      * @param key the key stroke; can be <code>null</code>
  278.      * @see Action#ACCELERATOR_KEY
  279.      * @see Action#putValue
  280.      */
  281.     public void setAccelerator(KeyStroke key) {
  282.         putValue(Action.ACCELERATOR_KEY, key);
  283.     }
  284.     /**
  285.      * Sets the group identity of the state action. This is used to
  286.      * identify the action as part of a button group.
  287.      */
  288.     public void setGroup(Object group) {
  289.         putValue(GROUP, group);
  290.     }
  291.     public Object getGroup() {
  292.         return getValue(GROUP);
  293.     }
  294.     /**
  295.      * Will perform cleanup on the object.
  296.      * Should be called when finished with the Action. This should be used if
  297.      * a new action is constructed from the properties of an old action.
  298.      * The old action properties should be disposed.
  299.      */
  300.     public void dispose() {
  301.         PropertyChangeListener[] listeners = getPropertyChangeListeners();
  302.         for (int i = 0; i < listeners.length; i++) {
  303.             removePropertyChangeListener(listeners[i]);
  304.         }
  305.     }
  306.     // Properties etc....
  307.     /**
  308.      * Inicates if this action has states. If this method returns
  309.      * true then the this will send ItemEvents to ItemListeners
  310.      * when the control constructed with this action in invoked.
  311.      *
  312.      * @return true if this can handle states
  313.      */
  314.     public boolean isStateAction() {
  315.         Boolean state = (Boolean)getValue(IS_STATE);
  316.         if (state != null) {
  317.             return state.booleanValue();
  318.         }
  319.         return false;
  320.     }
  321.     /**
  322.      * Set the state property to true.
  323.      */
  324.     public void setStateAction() {
  325.         setStateAction(true);
  326.     }
  327.     /**
  328.      * Set the state property.
  329.      *
  330.      * @param state if true then this action will fire ItemEvents
  331.      */
  332.     public void setStateAction(boolean state) {
  333.         putValue(IS_STATE, Boolean.valueOf(state));
  334.     }
  335.     /**
  336.      * @return true if the action is in the selected state
  337.      */
  338.     public boolean isSelected()  {
  339.         return selected;
  340.     }
  341.     /**
  342.      * Changes the state of the action
  343.      * @param newValue true to set the action as selected of the action.
  344.      */
  345.     public synchronized void setSelected(boolean newValue) {
  346.         boolean oldValue = this.selected;
  347.         if (oldValue != newValue) {
  348.             this.selected = newValue;
  349.             firePropertyChange("selected", Boolean.valueOf(oldValue),
  350.                                Boolean.valueOf(newValue));
  351.         }
  352.     }
  353.     public String toString() {
  354.         StringBuffer buffer = new StringBuffer("[");
  355.         // RG: Fix for J2SE 5.0; Can't cascade append() calls because
  356.         // return type in StringBuffer and AbstractStringBuilder are different
  357.         buffer.append(this.getClass().toString());
  358.         buffer.append(":");
  359.         try {
  360.             Object[] keys = getKeys();
  361.             for (int i = 0; i < keys.length; i++) {
  362.                 buffer.append(keys[i]);
  363.                 buffer.append('=');
  364.                 buffer.append(getValue( (String) keys[i]).toString());
  365.                 if (i < keys.length - 1) {
  366.                     buffer.append(',');
  367.                 }
  368.             }
  369.             buffer.append(']');
  370.         }
  371.         catch (Exception ex) {  // RG: append(char) throws IOException in J2SE 5.0
  372.             /** @todo Log it */
  373.         }
  374.         return buffer.toString();
  375.     }
  376. }