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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: TargetableAction.java,v 1.3 2005/10/10 18:02:41 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.ItemEvent;
  24. import javax.swing.Icon;
  25. /**
  26.  * A class that represents a dynamically targetable action. The invocation of this
  27.  * action will be dispatched to the <code>TargetManager</code> instance.
  28.  * <p>
  29.  * You would create instances of this class to let the TargetManager handle the
  30.  * action invocations from the ui components constructed with this action.
  31.  * The TargetManager could be configured depending on application state to
  32.  * handle these actions.
  33.  *
  34.  * @see TargetManager
  35.  * @author Mark Davidson
  36.  */
  37. public class TargetableAction extends AbstractActionExt {
  38.     private TargetManager targetManager;
  39.     public TargetableAction() {
  40.         this("action");
  41.     }
  42.     public TargetableAction(String name) {
  43.         super(name);
  44.     }
  45.     /**
  46.      * @param name display name of the action
  47.      * @param command the value of the action command key
  48.      */
  49.     public TargetableAction(String name, String command) {
  50.         super(name, command);
  51.     }
  52.     /**
  53.      * @param name display name of the action
  54.      * @param command the value of the action command key
  55.      * @param icon icon to display
  56.      */
  57.     public TargetableAction(String name, String command, Icon icon) {
  58.         super(name, command, icon);
  59.     }
  60.     public TargetableAction(String name, Icon icon) {
  61.         super(name, icon);
  62.     }
  63.     /**
  64.      * Set target manager which will handle this command. This action
  65.      * may be reset to use the singleton instance if set to null.
  66.      *
  67.      * @param tm the target manager instance to dispatch the actions
  68.      */
  69.     public void setTargetManager(TargetManager tm) {
  70.         this.targetManager = tm;
  71.     }
  72.     /**
  73.      * Returns the target manager instance which will be used for action
  74.      * dispatch. If the target manager has not previously been set then the
  75.      * singleton instance will be returned.
  76.      *
  77.      * @return a non null target manager
  78.      */
  79.     public TargetManager getTargetManager() {
  80.         if (targetManager == null) {
  81.             targetManager = TargetManager.getInstance();
  82.         }
  83.         return targetManager;
  84.     }
  85.     // Callbacks...
  86.     /**
  87.      * Callback for command actions. This event will be redispatched to
  88.      * the target manager along with the value of the Action.ACTION_COMMAND_KEY
  89.      *
  90.      * @param evt event which will be forwarded to the TargetManager
  91.      * @see TargetManager
  92.      */
  93.     public void actionPerformed(ActionEvent evt) {
  94.         if (!isStateAction()) {
  95.             // Do not process this event if it's a toggle action.
  96.             getTargetManager().doCommand(getActionCommand(), evt);
  97.         }
  98.     }
  99.     /**
  100.      * Callback for toggle actions. This event will be redispatched to
  101.      * the target manager along with value of the the Action.ACTION_COMMAND_KEY
  102.      *
  103.      * @param evt event which will be forwarded to the TargetManager
  104.      * @see TargetManager
  105.      */
  106.     public void itemStateChanged(ItemEvent evt) {
  107.         // Update all objects that share this item
  108.         boolean newValue;
  109.         boolean oldValue = isSelected();
  110.         if (evt.getStateChange() == ItemEvent.SELECTED) {
  111.             newValue = true;
  112.         } else {
  113.             newValue = false;
  114.         }
  115.         if (oldValue != newValue) {
  116.             setSelected(newValue);
  117.             getTargetManager().doCommand(getActionCommand(), evt);
  118.         }
  119.     }
  120.     public String toString() {
  121.         return super.toString();
  122.     }
  123. }