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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: BoundAction.java,v 1.3 2005/10/10 18:02:40 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.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.ItemEvent;
  25. import java.awt.event.ItemListener;
  26. import java.beans.EventHandler;
  27. import java.beans.Statement;
  28. import java.util.EventListener;
  29. import javax.swing.Icon;
  30. import javax.swing.event.EventListenerList;
  31. /**
  32.  * A class that represents the many type of actions that this framework supports.
  33.  * <p>
  34.  * The command invocation of this action may be delegated to another action or item state
  35.  * listener. If there isn't an explicit binding then the command is forwarded to 
  36.  * the TargetManager.
  37.  *
  38.  * @author Mark Davidson
  39.  */
  40. public class BoundAction extends AbstractActionExt {
  41.     // Holds the listeners
  42.     private EventListenerList listeners;
  43.     public BoundAction() {
  44. this("BoundAction");
  45.     }
  46.     public BoundAction(String name) {
  47. super(name);
  48.     }
  49.     /**
  50.      * @param name display name of the action
  51.      * @param command the value of the action command key
  52.      */
  53.     public BoundAction(String name, String command) {
  54. super(name, command);
  55.     }
  56.     public BoundAction(String name, Icon icon) {
  57. super(name, icon);
  58.     }
  59.     /**
  60.      * @param name display name of the action
  61.      * @param command the value of the action command key
  62.      * @param icon icon to display
  63.      */
  64.     public BoundAction(String name, String command, Icon icon) {
  65. super(name, command, icon);
  66.     }
  67.     /**
  68.      * The callback string will be called to register the action callback.
  69.      * Note the toggle property must be set if this is a state action before
  70.      * this method is called.
  71.      * For example, 
  72.      * <pre>
  73.      *     &lt;exec&gt;com.sun.foo.FubarHandler#handleBar&lt;/exec&gt;
  74.      * </pre>
  75.      * will register
  76.      * <pre>
  77.      *     registerCallback(com.sun.foo.FubarHandler(), "handleBar");
  78.      * </pre>
  79.      */
  80.     public void setCallback(String callback) {
  81. String[] elems = callback.split("#", 2);
  82. if (elems.length == 2) {
  83.     try {
  84. Class clz = Class.forName(elems[0]);
  85. // May throw a security exception in an Applet
  86. // context.
  87. Object obj = clz.newInstance();
  88. registerCallback(obj, elems[1]);
  89.     } catch (Exception ex) {
  90. System.out.println("ERROR: setCallback(" + callback
  91.    + ") - " + ex.getMessage());
  92.     }
  93. }
  94.     }
  95.     /**
  96.      * Registers a callback method when the Action corresponding to
  97.      * the action id is invoked. When a Component that was constructed from the
  98.      * Action identified by the action id invokes actionPerformed then the method
  99.      * named will be invoked on the handler Object.
  100.      * <p>
  101.      * If the Action represented by the action id is a StateChangeAction, then
  102.      * the method passed should take an int as an argument. The value of
  103.      * getStateChange() on the ItemEvent object will be passed as the parameter.
  104.      *
  105.      * @param handler the object which will be perform the action
  106.      * @param method the name of the method on the handler which will be called.
  107.      */
  108.     public void registerCallback(Object handler, String method) {
  109. if (isStateAction()) {
  110.     // Create a handler for toogle type actions.
  111.     addItemListener(new BooleanInvocationHandler(handler, method));
  112. } else {
  113.     // Create a new ActionListener using the dynamic proxy api.
  114.     addActionListener((ActionListener)EventHandler.create(ActionListener.class,
  115.   handler, method));
  116. }
  117.     }
  118.     
  119.     /**
  120.      * The callback for the toggle/state changed action that invokes a method 
  121.      * with a boolean argument on a target.
  122.      *
  123.      * TODO: should reimplement this class as something that can be persistable.
  124.      */
  125.     private class BooleanInvocationHandler implements ItemListener {
  126. private Statement falseStatement;
  127. private Statement trueStatement;
  128. public BooleanInvocationHandler(Object target, String methodName) {
  129.     // Create the true and false statements.
  130.     falseStatement = new Statement(target, methodName, 
  131.    new Object[] { Boolean.FALSE });
  132.     
  133.     trueStatement = new Statement(target, methodName, 
  134.   new Object[] { Boolean.TRUE });
  135. }
  136. public void itemStateChanged(ItemEvent evt) {
  137.     Statement statement = (evt.getStateChange() == ItemEvent.DESELECTED) ? 
  138. falseStatement : trueStatement;
  139.     
  140.     try {
  141. statement.execute();
  142.     } catch (Exception ex) {
  143. ex.printStackTrace();
  144.     }
  145. }
  146.     }
  147.     // Listener registration...
  148.     private void addListener(Class clz, EventListener listener) {
  149. if (listeners == null) {
  150.     listeners = new EventListenerList();
  151. }
  152. listeners.add(clz, listener);
  153.     }
  154.     private void removeListener(Class clz, EventListener listener) {
  155. if (listeners != null) {
  156.     listeners.remove(clz, listener);
  157. }
  158.     }
  159.     private EventListener[] getListeners(Class clz) {
  160. if (listeners == null) {
  161.     return null;
  162. }
  163. return listeners.getListeners(clz);
  164.     }
  165.     /**
  166.      * Add an action listener which will be invoked when this action is invoked.
  167.      */
  168.     public void addActionListener(ActionListener listener) {
  169. addListener(ActionListener.class, listener);
  170.     }
  171.     public void removeActionListener(ActionListener listener) {
  172. removeListener(ActionListener.class, listener);
  173.     }
  174.     public ActionListener[] getActionListeners() {
  175. return (ActionListener[])getListeners(ActionListener.class);
  176.     }
  177.     /**
  178.      * Add an item listener which will be invoked for toggle actions.
  179.      */
  180.     public void addItemListener(ItemListener listener) {
  181. addListener(ItemListener.class, listener);
  182.     }
  183.     public void removeItemListener(ItemListener listener) {
  184. removeListener(ItemListener.class, listener);
  185.     }
  186.     public ItemListener[] getItemListeners() {
  187. return (ItemListener[])getListeners(ItemListener.class);
  188.     }
  189.     // Callbacks...
  190.     /**
  191.      * Callback for command actions.
  192.      */
  193.     public void actionPerformed(ActionEvent evt) {
  194. ActionListener[] alist = getActionListeners();
  195. if (alist != null) {
  196.     for (int i = 0 ; i < alist.length; i++) {
  197. alist[i].actionPerformed(evt);
  198.     }
  199. }
  200.     }
  201.     /**
  202.      * Callback for toggle actions.
  203.      */
  204.     public void itemStateChanged(ItemEvent evt) {
  205.         // Update all objects that share this item
  206. boolean newValue;
  207. boolean oldValue = isSelected();
  208.         if (evt.getStateChange() == ItemEvent.SELECTED) {
  209.     newValue = true;
  210. } else {
  211.     newValue = false;
  212. }
  213. if (oldValue != newValue) {
  214.     setSelected(newValue);
  215.     // Forward the event to the delgate for handling.
  216.     ItemListener[] ilist = getItemListeners();
  217.     if (ilist != null) {
  218. for (int i = 0; i < ilist.length; i++) {
  219.     ilist[i].itemStateChanged(evt);
  220. }
  221.     }
  222. }
  223.     }
  224. }