KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > designer > ActionManager


1 package com.opensymphony.workflow.designer;
2
3 import java.net.URL JavaDoc;
4 import java.util.*;
5 import javax.swing.*;
6
7 /**
8  * Central repository for all Actions.
9  * On startup, the application is responsible for registering all actions via the {@link #register(java.lang.String, javax.swing.Action)}
10  * method. Once this is done, any action can be retrieved via the {@link #get(java.lang.String)} method.
11  * The action manager will look for an actions.properties file in the same package, and will read all properties
12  * specified in it for a given action.
13  * <p>
14  * The benefit of specifying actions in the external file is that actions themselves need not be aware of their textual or
15  * graphic representation or key bindings.
16  */

17 public final class ActionManager
18 {
19     private static final String JavaDoc OS_NAME_STRING = System.getProperty("os.name").replace(' ', '_').toLowerCase();
20
21   private static final String JavaDoc SMALL_GRAY_ICON = "smallGrayIcon";
22   private static final String JavaDoc DISPLAYED_MNEMONIC_INDEX = "mnemonicIndex";
23
24   private static final ActionManager INSTANCE = new ActionManager();
25
26   private final Map actions;
27   private ResourceBundle bundle;
28
29   private ActionManager()
30   {
31     this.actions = new HashMap(50);
32     bundle = ResourceBundle.getBundle("com.opensymphony.workflow.designer.actions");
33   }
34
35   /**
36    * Register an action.
37    * @param id The action id denotes the set of properties that will be read for the action
38    * @param action The action instance to bind to the specified id.
39    * @return The action, once it has been initialised. If the id is not specified in the
40    * properties file, then null is returned.
41    * @throws NullPointerException if the specified action is null
42    */

43   public static Action register(String JavaDoc id, Action action)
44   {
45     if(action == null)
46       throw new NullPointerException JavaDoc("Registered actions must not be null.");
47
48         boolean exists = ActionReader.readAndPutValues(action, INSTANCE.bundle, id);
49         if(!exists) return null;
50
51     Object JavaDoc oldValue = INSTANCE.actions.put(id, action);
52     if(oldValue != null)
53             System.out.println("WARNING: Duplicate action id: " + id);
54     return action;
55   }
56
57     /**
58      * Remove a registered action
59      * @param id the action id
60      * @return The removed action, if it existed.
61      */

62     public static Action deregister(String JavaDoc id)
63     {
64         return (Action)INSTANCE.actions.remove(id);
65     }
66
67   /**
68    * Get a previously registered action
69    * @param id The action id
70    * @return The action bound to the specified id, or null if no action is bound.
71    */

72   public static Action get(String JavaDoc id)
73   {
74     Action action = (Action)(INSTANCE.actions.get(id));
75     if(null == action)
76     {
77             System.out.println("ERROR: No action found for id: " + id);
78       return null;
79     }
80     return action;
81   }
82
83   /**
84    * Retrieves and answers the small icon for the given <code>id</code>.
85    */

86   public static Icon getIcon(String JavaDoc id)
87   {
88     Action action = get(id);
89     if(action == null)
90       return null;
91     return (Icon)action.getValue(Action.SMALL_ICON);
92   }
93
94   /**
95    * Alias a particular id to another one.
96    * This allows one action to be bound to multiple keys.
97    * @param newKey The new alias to bind to.
98    * @param oldKey The old id to bind to.
99    */

100     public static void alias(String JavaDoc newKey, String JavaDoc oldKey)
101     {
102         Object JavaDoc oldValue = INSTANCE.actions.put(newKey, INSTANCE.actions.get(oldKey));
103         if(oldValue != null)
104             System.out.println("WARNING: Duplicate action id: " + newKey);
105     }
106
107   private static class ActionReader
108   {
109     private static final String JavaDoc LABEL = "label";
110     private static final char MNEMONIC_MARKER = '&';
111     private static final String JavaDoc DOT_STRING = "...";
112     private static final String JavaDoc SHORT_DESCRIPTION = "tooltip";
113     private static final String JavaDoc LONG_DESCRIPTION = "helptext";
114     private static final String JavaDoc ICON = "icon";
115     private static final String JavaDoc GRAY_ICON = ICON + ".gray";
116     private static final String JavaDoc ACCELERATOR = "accelerator";
117         private static final String JavaDoc COMMAND = "command";
118
119         private String JavaDoc id;
120         private String JavaDoc name;
121         private Integer JavaDoc mnemonic;
122         private Integer JavaDoc aMnemonicIndex;
123         private String JavaDoc shortDescription;
124         private String JavaDoc longDescription;
125         private ImageIcon icon;
126         private ImageIcon grayIcon;
127         private KeyStroke accelerator;
128         private String JavaDoc command;
129         private boolean exists = true;
130
131     /**
132      * Reads properties for <code>id</code> in <code>bundle</code>.
133      */

134     static void readValues(ResourceBundle bundle, String JavaDoc id)
135     {
136       new ActionReader(bundle, id);
137     }
138
139     /**
140      * Reads properties for <code>id</code> in <code>bundle</code> and
141      * sets the approriate values in the given <code>action</code>.
142      */

143         static boolean readAndPutValues(Action action, ResourceBundle bundle, String JavaDoc id)
144     {
145       ActionReader reader = new ActionReader(bundle, id);
146             if(!reader.actionExists()) return false;
147       reader.putValues(action);
148             return true;
149     }
150
151     private ActionReader(ResourceBundle bundle, String JavaDoc id)
152     {
153             String JavaDoc iconPath = getString(bundle, id + '.' + ICON, null);
154             if(getString(bundle, id + "." + LABEL, null) == null && iconPath == null)
155             {
156                 exists = false;
157                 return;
158             }
159
160       this.id = id;
161       String JavaDoc nameWithMnemonic = getString(bundle, id + "." + LABEL, id);
162       int index = mnemonicIndex(nameWithMnemonic);
163       name = stripName(nameWithMnemonic, index);
164       mnemonic = stripMnemonic(nameWithMnemonic, index);
165       aMnemonicIndex = new Integer JavaDoc(index);
166
167       shortDescription = getString(bundle, id + '.' + SHORT_DESCRIPTION, defaultShortDescription(name));
168       longDescription = getString(bundle, id + '.' + LONG_DESCRIPTION, name);
169
170             URL JavaDoc iconURL = iconPath != null ? getClass().getClassLoader().getResource(iconPath) : null;
171             if(iconURL == null && iconPath != null)
172             {
173                 System.out.println("WARNING Invalid icon " + iconPath + " specified in actions.properties for action '" + name + "'");
174                 icon = null;
175             }
176             else
177             {
178                 icon = (iconPath == null) ? null : new ImageIcon(iconURL);
179             }
180
181       String JavaDoc grayIconPath = getString(bundle, id + '.' + GRAY_ICON, null);
182       grayIcon = (grayIconPath == null) ? null : new ImageIcon(getClass().getClassLoader().getResource(grayIconPath));
183
184             String JavaDoc shortcut = getString(bundle, id + '.' + ACCELERATOR + '.' + OS_NAME_STRING, null);
185             if(shortcut == null)
186             {
187                 shortcut = getString(bundle, id + '.' + ACCELERATOR, null);
188             }
189       accelerator = getKeyStroke(shortcut);
190
191             command = getString(bundle, id + '.' + COMMAND, null);
192         }
193
194         public boolean actionExists()
195         {
196             return exists;
197     }
198
199     /**
200      * Put the ActionReader's properties as values in the Action.
201      */

202     private void putValues(Action action)
203     {
204       action.putValue(Action.NAME, name);
205       action.putValue(Action.SHORT_DESCRIPTION, shortDescription);
206       action.putValue(Action.LONG_DESCRIPTION, longDescription);
207             if(icon != null)
208         action.putValue(Action.SMALL_ICON, icon);
209             if(grayIcon != null)
210         action.putValue(ActionManager.SMALL_GRAY_ICON, grayIcon);
211             if(accelerator != null)
212         action.putValue(Action.ACCELERATOR_KEY, accelerator);
213             if(mnemonic != null)
214       action.putValue(Action.MNEMONIC_KEY, mnemonic);
215             if(command != null)
216                 action.putValue(Action.ACTION_COMMAND_KEY, command);
217       action.putValue(ActionManager.DISPLAYED_MNEMONIC_INDEX, aMnemonicIndex);
218     }
219
220     private int mnemonicIndex(String JavaDoc nameWithMnemonic)
221     {
222       return nameWithMnemonic.indexOf(MNEMONIC_MARKER);
223     }
224
225     private String JavaDoc stripName(String JavaDoc nameWithMnemonic, int mnemonicIndex)
226     {
227       return mnemonicIndex == -1 ? nameWithMnemonic : nameWithMnemonic.substring(0, mnemonicIndex) + nameWithMnemonic.substring(mnemonicIndex + 1);
228     }
229
230     private Integer JavaDoc stripMnemonic(String JavaDoc nameWithMnemonic, int mnemonicIndex)
231     {
232       return mnemonicIndex == -1 ? null : new Integer JavaDoc(nameWithMnemonic.charAt(mnemonicIndex + 1));
233     }
234
235     private String JavaDoc defaultShortDescription(String JavaDoc nameWithDots)
236     {
237       return nameWithDots.endsWith(DOT_STRING) ? (nameWithDots.substring(0, nameWithDots.length() - DOT_STRING.length())) : nameWithDots;
238     }
239
240         private KeyStroke getKeyStroke(String JavaDoc accelerator)
241     {
242             if(accelerator == null)
243       {
244         return null;
245       }
246       else
247       {
248                 KeyStroke keyStroke = KeyStroke.getKeyStroke(accelerator);
249         if(keyStroke == null)
250                     System.out.println("WARNING: Action " + id + " has an invalid accelerator " + accelerator);
251         return keyStroke;
252       }
253     }
254
255     private String JavaDoc getString(ResourceBundle bundle, String JavaDoc key, String JavaDoc defaultString)
256     {
257       try
258       {
259         return bundle.getString(key);
260       }
261       catch(MissingResourceException e)
262       {
263         return defaultString;
264       }
265     }
266   }
267 }
Popular Tags