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

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: ActionFactory.java,v 1.2 2005/10/10 18:02:42 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 javax.swing.AbstractAction;
  23. import javax.swing.Action;
  24. import javax.swing.Icon;
  25. import javax.swing.KeyStroke;
  26. import org.jdesktop.swingx.action.TargetableAction;
  27. import org.jdesktop.swingx.action.ServerAction;
  28. /**
  29.  * A collection of static methods to make it easier to construct
  30.  * Actions. Not sure how usefull they are in reality but it saves a
  31.  * lot of typing.
  32.  *
  33.  * @author Mark Davidson
  34.  */
  35. public class ActionFactory {
  36.     /**
  37.      * Factory Methods for creating BoundActions
  38.      */
  39.     public static BoundAction createBoundAction(String id, String name,
  40.                                                 String mnemonic) {
  41.         return createBoundAction(id, name, mnemonic, false);
  42.     }
  43.     public static BoundAction createBoundAction(String id, String name,
  44.                                                 String mnemonic, boolean toggle) {
  45.         return createBoundAction(id, name, mnemonic, toggle, null);
  46.     }
  47.     public static BoundAction createBoundAction(String id, String name,
  48.                                                 String mnemonic, boolean toggle,
  49.                                                 String group) {
  50.         return (BoundAction)configureAction(new BoundAction(name, id),
  51.                                             mnemonic, toggle, group);
  52.     }
  53.     /**
  54.      * Factory Methods for creating <code>CompositeAction</code>
  55.      * @see CompositeAction
  56.      */
  57.     public static CompositeAction createCompositeAction(String id, String name,
  58.                                                         String mnemonic) {
  59.         return createCompositeAction(id, name, mnemonic, false);
  60.     }
  61.     public static CompositeAction createCompositeAction(String id, String name,
  62.                                                         String mnemonic, boolean toggle) {
  63.         return createCompositeAction(id, name, mnemonic, toggle, null);
  64.     }
  65.     public static CompositeAction createCompositeAction(String id, String name,
  66.                                                         String mnemonic, boolean toggle,
  67.                                                         String group) {
  68.         return (CompositeAction)configureAction(new CompositeAction(name, id),
  69.                                                 mnemonic, toggle, group);
  70.     }
  71.     public static ServerAction createServerAction(String id, String name,
  72.                                                   String mnemonic) {
  73.         ServerAction action = new ServerAction(name, id);
  74.         if (mnemonic != null && !mnemonic.equals("")) {
  75.             action.putValue(Action.MNEMONIC_KEY, new Integer(mnemonic.charAt(0)));
  76.         }
  77.         return action;
  78.     }
  79.     /**
  80.      * These methods are usefull for creating targetable actions
  81.      */
  82.     public static TargetableAction createTargetableAction(String id, String name) {
  83.         return createTargetableAction(id, name, null);
  84.     }
  85.     public static TargetableAction createTargetableAction(String id, String name,
  86.                                                           String mnemonic) {
  87.         return createTargetableAction(id, name, mnemonic, false);
  88.     }
  89.     public static TargetableAction createTargetableAction(String id, String name,
  90.                                                           String mnemonic, boolean toggle) {
  91.         return createTargetableAction(id, name, mnemonic, toggle, null);
  92.     }
  93.     public static TargetableAction createTargetableAction(String id, String name,
  94.                                                           String mnemonic, boolean toggle,
  95.                                                           String group) {
  96.         return (TargetableAction)configureAction(new TargetableAction(name, id),
  97.                                                  mnemonic, toggle, group);
  98.     }
  99.     private static Action configureAction(AbstractActionExt action,
  100.                                           String mnemonic, boolean toggle,
  101.                                           String group) {
  102.         action.setMnemonic(mnemonic);
  103.         String description = action.getName() + " action with comand " + action.getActionCommand();
  104.         action.setShortDescription(description);
  105.         action.setLongDescription(description);
  106.         if (toggle) {
  107.             action.setStateAction();
  108.         }
  109.         if (group != null) {
  110.             action.setGroup(group);
  111.         }
  112.         return action;
  113.     }
  114.     /**
  115.      * Add additional attributes to the action. If any of these attributes
  116.      * are null then they will still be set on the action. Many of these
  117.      * attributes map to the set methods on <code>AbstractActionExt</code>
  118.      *
  119.      * @see AbstractActionExt
  120.      * @param action the action which will all the attributes will be applied
  121.      */
  122.     public static void decorateAction(AbstractAction action,
  123.                                       String shortDesc, String longDesc,
  124.                                       Icon smallIcon, Icon largeIcon,
  125.                                       KeyStroke accel) {
  126.         if (action instanceof AbstractActionExt) {
  127.             AbstractActionExt a = (AbstractActionExt)action;
  128.             a.setShortDescription(shortDesc);
  129.             a.setLongDescription(longDesc);
  130.             a.setSmallIcon(smallIcon);
  131.             a.setLargeIcon(largeIcon);
  132.             a.setAccelerator(accel);
  133.         }
  134.         else {
  135.             action.putValue(Action.SHORT_DESCRIPTION, shortDesc);
  136.             action.putValue(Action.LONG_DESCRIPTION, longDesc);
  137.             action.putValue(Action.SMALL_ICON, smallIcon);
  138.             action.putValue(AbstractActionExt.LARGE_ICON, largeIcon);
  139.             action.putValue(Action.ACCELERATOR_KEY, accel);
  140.         }
  141.     }
  142. }