KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > adwt > action > CommandAction


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.adwt.action;
8
9 import java.awt.event.ActionEvent JavaDoc;
10 import java.beans.PropertyChangeListener JavaDoc;
11 import java.beans.beancontext.BeanContextServiceAvailableEvent JavaDoc;
12 import java.beans.beancontext.BeanContextServiceRevokedEvent JavaDoc;
13 import java.beans.beancontext.BeanContextServices JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15
16 import javax.swing.AbstractAction JavaDoc;
17 import javax.swing.Action JavaDoc;
18 import javax.swing.Icon JavaDoc;
19 import javax.swing.ImageIcon JavaDoc;
20 import javax.swing.JMenuItem JavaDoc;
21 import javax.swing.KeyStroke JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejtools.adwt.service.MenuBarService;
25 import org.ejtools.adwt.service.ToolBarService;
26 import org.ejtools.beans.beancontext.CustomBeanContextServicesSupport;
27
28 /**
29  * BeanContext that wraps an Action and delegates to a Command object.
30  * <p>
31  * It supports the Command pattern by delegating the <code>ActionEvent</code> event to the <code>execute</code> method of the Command object.
32  * </p>
33  * <p>
34  * It automatically handles the negotiation in ToolBar and MenuBar if these services are present.
35  * </p>
36  * <p>
37  * It supports the following properties :<ul>
38  * <li>Custom Icon</li>
39  * <li>Tooltip</li>
40  * <li>Mnemonic key</li>
41  * <li>Accelerator key</li>
42  * <li>MenuBar presence</li>
43  * <li>Toolbar presence</li>
44  * <li>Menu name</li>
45  * <li>Menu item</li>
46  * <li>Menu position</li>
47  * </ul>
48  * </p>
49  *
50  * @author Laurent Etiemble
51  * @version $Revision: 1.8 $
52  * @see org.ejtools.adwt.action.Command
53  */

54 public class CommandAction extends CustomBeanContextServicesSupport implements Action JavaDoc
55 {
56    /** The wrapped action */
57    protected AbstractAction JavaDoc action = null;
58    /** The command to execute */
59    protected Command command;
60    /** Resource Bundle */
61    protected ResourceBundle JavaDoc resource;
62    /** Default logger */
63    private static Logger logger = Logger.getLogger(CommandAction.class);
64
65    /** Constant for ICON property */
66    public final static String JavaDoc ICON = "Icon";
67    /** Constant for MENU property */
68    public final static String JavaDoc MENU = "Menu";
69    /** Constant for MENU_ITEM property */
70    public final static String JavaDoc MENU_ITEM = "MenuItem";
71    /** Constant for MENU_LAYOUT property */
72    public final static String JavaDoc MENU_LAYOUT = "MenuLayout";
73    /** Constant for TOOLBAR property */
74    public final static String JavaDoc TOOLBAR = "Toolbar";
75    /** Constant for TOOLTIP property */
76    public final static String JavaDoc TOOLTIP = "ToolTip";
77
78
79
80    /**
81     * Constructor used to fully define the CommandAction.
82     *
83     * @param command The command object to delegate the ActionEvent to
84     * @param res The Resource Bundle used for resolving the name and other properties
85     * @param name The name of the CommandAction. Must be a key found in the Resource Bundle
86     */

87    protected CommandAction(Command command, ResourceBundle JavaDoc res, String JavaDoc name)
88    {
89       this.setCommand(command);
90       this.setResourceBundle(res);
91
92       this.action =
93          new AbstractAction JavaDoc(res.getString(name))
94          {
95             public void actionPerformed(ActionEvent JavaDoc e)
96             {
97                getCommand().execute();
98             }
99          };
100
101       this.putValue(TOOLBAR, Boolean.FALSE);
102       this.putValue(MENU_LAYOUT, new Integer JavaDoc(MenuBarService.NO_LAYOUT));
103
104       try
105       {
106          this.putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(resource.getString(name + ".mnemonic")));
107       }
108       catch (Exception JavaDoc e)
109       {
110          // Do nothing
111
}
112       try
113       {
114          this.putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(resource.getString(name + ".accelerator")));
115       }
116       catch (Exception JavaDoc e)
117       {
118          // Do nothing
119
}
120       try
121       {
122          this.putValue(TOOLTIP, resource.getString(name + ".tooltip"));
123       }
124       catch (Exception JavaDoc e)
125       {
126          // Do nothing
127
}
128    }
129
130
131    /**
132     * This method is called whenever the action is acted upon. The call will be delegated to the wrapped action.
133     *
134     * @param e The action event
135     */

136    public void actionPerformed(ActionEvent JavaDoc e)
137    {
138       this.action.actionPerformed(e);
139    }
140
141
142    /**
143     * Adds a PropertyChangeListener. The call will be delegated to the wrapped action.
144     *
145     * @param listener The listener to be added
146     */

147    public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener)
148    {
149       this.action.addPropertyChangeListener(listener);
150    }
151
152
153    /**
154     * Getter for the menuItem attribute
155     *
156     * @return The value
157     */

158    public JMenuItem JavaDoc getMenuItem()
159    {
160       JMenuItem JavaDoc menuitem = (JMenuItem JavaDoc) this.getValue(MENU_ITEM);
161
162       if (menuitem == null)
163       {
164          logger.debug("Building MenuItem");
165
166          menuitem = new JMenuItem JavaDoc(this);
167
168          if (this.getValue(Action.MNEMONIC_KEY) != null)
169          {
170             menuitem.setMnemonic(((Integer JavaDoc) this.getValue(Action.MNEMONIC_KEY)).intValue());
171          }
172          if (this.getValue(Action.ACCELERATOR_KEY) != null)
173          {
174             menuitem.setAccelerator((KeyStroke JavaDoc) this.getValue(Action.ACCELERATOR_KEY));
175          }
176          this.putValue(MENU_ITEM, menuitem);
177       }
178       return menuitem;
179    }
180
181
182    /**
183     * Gets the value attribute of the CommandAction object
184     *
185     * @param key Description of Parameter
186     * @return The value value
187     */

188    public Object JavaDoc getValue(String JavaDoc key)
189    {
190       return this.action.getValue(key);
191    }
192
193
194    /**
195     * Gets the enabled attribute of the CommandAction object
196     *
197     * @return The enabled value
198     */

199    public boolean isEnabled()
200    {
201       return this.action.isEnabled();
202    }
203
204
205    /**
206     * Description of the Method
207     *
208     * @param key Description of Parameter
209     * @param value Description of Parameter
210     */

211    public void putValue(String JavaDoc key, Object JavaDoc value)
212    {
213       this.action.putValue(key, value);
214    }
215
216
217    /**
218     * Removes a PropertyChangeListener. The call will be delegated to the wrapped action.
219     *
220     * @param listener The listener to remove
221     */

222    public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener)
223    {
224       this.action.removePropertyChangeListener(listener);
225    }
226
227
228    /**
229     * Triggered when a service is made available.
230     *
231     * @param bcsae The availability event
232     */

233    public void serviceAvailable(BeanContextServiceAvailableEvent JavaDoc bcsae) { }
234
235
236    /**
237     * Triggered when a service is revoked.
238     *
239     * @param bcsre The revocation event
240     */

241    public void serviceRevoked(BeanContextServiceRevokedEvent JavaDoc bcsre) { }
242
243
244    /**
245     * Enables the Action. The call will be delegated to the wrapped action.
246     *
247     * @param b If true, the action will be enable
248     */

249    public void setEnabled(boolean b)
250    {
251       this.action.setEnabled(b);
252    }
253
254
255    /** Called when the CommandAction is put in a BeanContext */
256    protected void initializeBeanContextResources()
257    {
258       super.initializeBeanContextResources();
259       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
260       this.register(context);
261    }
262
263
264    /**
265     * Description of the Method
266     *
267     * @param context Description of Parameter
268     */

269    protected void register(BeanContextServices JavaDoc context)
270    {
271       // Set up MenuBar
272
if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU))))
273       {
274          logger.debug("Using service MenuBarService");
275          try
276          {
277             MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this);
278             service.register(this);
279          }
280          catch (Exception JavaDoc e)
281          {
282             logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")");
283             e.printStackTrace();
284          }
285       }
286
287       // Set up ToolBar
288
if ((context.hasService(ToolBarService.class)) && (((Boolean JavaDoc) this.getValue(TOOLBAR)).booleanValue()))
289       {
290          logger.debug("Using service ToolBarService");
291          try
292          {
293             ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this);
294             service.register(this);
295          }
296          catch (Exception JavaDoc e)
297          {
298             logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")");
299             e.printStackTrace();
300          }
301       }
302    }
303
304
305    /** Called when the CommandAction is removed from a BeanContext */
306    protected void releaseBeanContextResources()
307    {
308       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
309       this.unregister(context);
310       super.releaseBeanContextResources();
311    }
312
313
314    /**
315     * Description of the Method
316     *
317     * @param context Description of Parameter
318     */

319    protected void unregister(BeanContextServices JavaDoc context)
320    {
321       // Set up MenuBar
322
if ((context.hasService(MenuBarService.class)) && (this.getValue(MENU) != null) && (!"".equals(this.getValue(MENU))))
323       {
324          logger.debug("Using service MenuBarService");
325          try
326          {
327             MenuBarService service = (MenuBarService) context.getService(this, this, MenuBarService.class, this, this);
328             service.unregister(this);
329          }
330          catch (Exception JavaDoc e)
331          {
332             logger.error("Error during utilisation of service MenuBarService (" + e.getMessage() + ")");
333             e.printStackTrace();
334          }
335       }
336
337       // Set up ToolBar
338
if ((context.hasService(ToolBarService.class)) && (((Boolean JavaDoc) this.getValue(TOOLBAR)).booleanValue()))
339       {
340          logger.debug("Using service ToolBarService");
341          try
342          {
343             ToolBarService service = (ToolBarService) context.getService(this, this, ToolBarService.class, this, this);
344             service.unregister(this);
345          }
346          catch (Exception JavaDoc e)
347          {
348             logger.error("Error during utilisation of service ToolBarService (" + e.getMessage() + ")");
349             e.printStackTrace();
350          }
351       }
352    }
353
354
355    /**
356     * Returns the menu property
357     *
358     * @return The menu property
359     */

360    public final String JavaDoc getMenu()
361    {
362       return (String JavaDoc) this.getValue(MENU);
363    }
364
365
366    /**
367     * Returns the menu layout property
368     *
369     * @return The menu layout property
370     */

371    public final int getMenuLayout()
372    {
373       return ((Integer JavaDoc) this.getValue(MENU_LAYOUT)).intValue();
374    }
375
376
377    /**
378     * Returns the Resource Bundle used by this object
379     *
380     * @return The Resource Bundle
381     */

382    public final ResourceBundle JavaDoc getResourceBundle()
383    {
384       return this.resource;
385    }
386
387
388    /**
389     * Returns the toolbar presence property
390     *
391     * @return The toolbar presence property
392     */

393    public final boolean getToolBar()
394    {
395       return ((Boolean JavaDoc) this.getValue(TOOLBAR)).booleanValue();
396    }
397
398
399    /**
400     * Sets the icon attribute of the CommandAction object
401     *
402     * @param icon The new icon value
403     */

404    public final void setIcon(String JavaDoc icon)
405    {
406       if ((icon != null) && (!"".equals(icon)))
407       {
408          try
409          {
410             Icon JavaDoc image = new ImageIcon JavaDoc(getClass().getResource(icon));
411             this.putValue(ICON, image);
412          }
413          catch (Exception JavaDoc e)
414          {
415          }
416       }
417    }
418
419
420    /**
421     * Constructor for the setMenu object
422     *
423     * @param menu Description of Parameter
424     */

425    public final void setMenu(String JavaDoc menu)
426    {
427       if ((menu != null) && (!"".equals(menu)))
428       {
429          this.putValue(MENU, menu);
430       }
431    }
432
433
434    /**
435     * Sets the menuLayout attribute of the CommandAction object
436     *
437     * @param menuLayout The new menuLayout value
438     */

439    public final void setMenuLayout(int menuLayout)
440    {
441       this.putValue(MENU_LAYOUT, new Integer JavaDoc(menuLayout));
442    }
443
444
445    /**
446     * Constructor for the setIcon object
447     *
448     * @param icon Description of Parameter
449     */

450    public final void setSmallIcon(String JavaDoc icon)
451    {
452       if ((icon != null) && (!"".equals(icon)))
453       {
454          try
455          {
456             Icon JavaDoc image = new ImageIcon JavaDoc(getClass().getResource(icon));
457             this.putValue(Action.SMALL_ICON, image);
458          }
459          catch (Exception JavaDoc e)
460          {
461          }
462       }
463    }
464
465
466    /**
467     * Sets the toolBar attribute of the CommandAction object
468     *
469     * @param inToolBar The new toolBar value
470     */

471    public final void setToolBar(boolean inToolBar)
472    {
473       this.putValue(TOOLBAR, new Boolean JavaDoc(inToolBar));
474    }
475
476
477    /**
478     * Returns the wrapped command object
479     *
480     * @return The command object
481     */

482    protected final Command getCommand()
483    {
484       return this.command;
485    }
486
487
488    /**
489     * This method sets the action's command object.
490     *
491     * @param newValue the command for this action to act upon
492     */

493    protected final void setCommand(Command newValue)
494    {
495       this.command = newValue;
496    }
497
498
499    /**
500     * Setter for the resourceBundle attribute
501     *
502     * @param newValue The new value for resourceBundle attribute
503     */

504    protected final void setResourceBundle(ResourceBundle JavaDoc newValue)
505    {
506       this.resource = newValue;
507    }
508 }
509
510
Popular Tags