KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > actions > ActionFactory


1 /*
2  * $Id: ActionFactory.java,v 1.2 2004/09/17 23:24:57 davidson1 Exp $
3  *
4  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5  * Santa Clara, California 95054, U.S.A. All rights reserved.
6  */

7
8 package org.jdesktop.swing.actions;
9
10 import javax.swing.AbstractAction JavaDoc;
11 import javax.swing.Action JavaDoc;
12 import javax.swing.Icon JavaDoc;
13 import javax.swing.KeyStroke JavaDoc;
14
15 /**
16  * A collection of static methods to make it easier to construct
17  * Actions. Not sure how usefull they are in reality but it saves a
18  * lot of typing.
19  *
20  * @author Mark Davidson
21  */

22 public class ActionFactory {
23
24     /**
25      * Factory Methods for creating BoundActions
26      */

27     public static BoundAction createBoundAction(String JavaDoc id, String JavaDoc name,
28                                                 String JavaDoc mnemonic) {
29         return createBoundAction(id, name, mnemonic, false);
30     }
31
32     public static BoundAction createBoundAction(String JavaDoc id, String JavaDoc name,
33                                                 String JavaDoc mnemonic, boolean toggle) {
34         return createBoundAction(id, name, mnemonic, toggle, null);
35     }
36
37
38     public static BoundAction createBoundAction(String JavaDoc id, String JavaDoc name,
39                                                 String JavaDoc mnemonic, boolean toggle,
40                                                 String JavaDoc group) {
41         return (BoundAction)configureAction(new BoundAction(name, id),
42                                             mnemonic, toggle, group);
43     }
44
45     /**
46      * Factory Methods for creating <code>CompositeAction</code>
47      * @see CompositeAction
48      */

49     public static CompositeAction createCompositeAction(String JavaDoc id, String JavaDoc name,
50                                                         String JavaDoc mnemonic) {
51         return createCompositeAction(id, name, mnemonic, false);
52     }
53
54     public static CompositeAction createCompositeAction(String JavaDoc id, String JavaDoc name,
55                                                         String JavaDoc mnemonic, boolean toggle) {
56         return createCompositeAction(id, name, mnemonic, toggle, null);
57     }
58
59     public static CompositeAction createCompositeAction(String JavaDoc id, String JavaDoc name,
60                                                         String JavaDoc mnemonic, boolean toggle,
61                                                         String JavaDoc group) {
62         return (CompositeAction)configureAction(new CompositeAction(name, id),
63                                                 mnemonic, toggle, group);
64     }
65
66
67     public static ServerAction createServerAction(String JavaDoc id, String JavaDoc name,
68                                                   String JavaDoc mnemonic) {
69         ServerAction action = new ServerAction(name, id);
70         if (mnemonic != null && !mnemonic.equals("")) {
71             action.putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(mnemonic.charAt(0)));
72         }
73         return action;
74     }
75
76
77     /**
78      * These methods are usefull for creating targetable actions
79      */

80     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name) {
81         return createTargetableAction(id, name, null);
82     }
83
84     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
85                                                           String JavaDoc mnemonic) {
86         return createTargetableAction(id, name, mnemonic, false);
87     }
88
89     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
90                                                           String JavaDoc mnemonic, boolean toggle) {
91         return createTargetableAction(id, name, mnemonic, toggle, null);
92     }
93
94     public static TargetableAction createTargetableAction(String JavaDoc id, String JavaDoc name,
95                                                           String JavaDoc mnemonic, boolean toggle,
96                                                           String JavaDoc group) {
97         return (TargetableAction)configureAction(new TargetableAction(name, id),
98                                                  mnemonic, toggle, group);
99     }
100
101     private static Action JavaDoc configureAction(AbstractActionExt action,
102                                           String JavaDoc mnemonic, boolean toggle,
103                                           String JavaDoc group) {
104         action.setMnemonic(mnemonic);
105         String JavaDoc description = action.getName() + " action with comand " + action.getActionCommand();
106         action.setShortDescription(description);
107         action.setLongDescription(description);
108
109         if (toggle) {
110             action.setStateAction();
111         }
112         if (group != null) {
113             action.setGroup(group);
114         }
115         return action;
116     }
117
118     /**
119      * Add additional attributes to the action. If any of these attributes
120      * are null then they will still be set on the action. Many of these
121      * attributes map to the set methods on <code>AbstractActionExt</code>
122      *
123      * @see AbstractActionExt
124      * @param action the action which will all the attributes will be applied
125      */

126     public static void decorateAction(AbstractAction JavaDoc action,
127                                       String JavaDoc shortDesc, String JavaDoc longDesc,
128                                       Icon JavaDoc smallIcon, Icon JavaDoc largeIcon,
129                                       KeyStroke JavaDoc accel) {
130         if (action instanceof AbstractActionExt) {
131             AbstractActionExt a = (AbstractActionExt)action;
132             a.setShortDescription(shortDesc);
133             a.setLongDescription(longDesc);
134             a.setSmallIcon(smallIcon);
135             a.setLargeIcon(largeIcon);
136             a.setAccelerator(accel);
137         }
138         else {
139             action.putValue(Action.SHORT_DESCRIPTION, shortDesc);
140             action.putValue(Action.LONG_DESCRIPTION, longDesc);
141             action.putValue(Action.SMALL_ICON, smallIcon);
142             action.putValue(AbstractActionExt.LARGE_ICON, largeIcon);
143             action.putValue(Action.ACCELERATOR_KEY, accel);
144         }
145     }
146 }
147
Popular Tags