KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: TargetableAction.java,v 1.3 2005/01/08 19:45:49 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 java.awt.event.ActionEvent JavaDoc;
11 import java.awt.event.ItemEvent JavaDoc;
12
13 import javax.swing.Action JavaDoc;
14 import javax.swing.Icon JavaDoc;
15
16 /**
17  * A class that represents a dynamically targetable action. The invocation of this
18  * action will be dispatched to the <code>TargetManager</code> instance.
19  * <p>
20  * You would create instances of this class to let the TargetManager handle the
21  * action invocations from the ui components constructed with this action.
22  * The TargetManager could be configured depending on application state to
23  * handle these actions.
24  *
25  * @see TargetManager
26  * @author Mark Davidson
27  */

28 public class TargetableAction extends AbstractActionExt {
29
30     private TargetManager targetManager;
31
32     public TargetableAction() {
33         this("action");
34     }
35
36     public TargetableAction(String JavaDoc name) {
37         super(name);
38     }
39
40     /**
41      * @param name display name of the action
42      * @param command the value of the action command key
43      */

44     public TargetableAction(String JavaDoc name, String JavaDoc command) {
45         super(name, command);
46     }
47
48     /**
49      * @param name display name of the action
50      * @param command the value of the action command key
51      * @param icon icon to display
52      */

53     public TargetableAction(String JavaDoc name, String JavaDoc command, Icon JavaDoc icon) {
54         super(name, command, icon);
55     }
56
57     public TargetableAction(String JavaDoc name, Icon JavaDoc icon) {
58         super(name, icon);
59     }
60
61     /**
62      * Set target manager which will handle this command. This action
63      * may be reset to use the singleton instance if set to null.
64      *
65      * @param tm the target manager instance to dispatch the actions
66      */

67     public void setTargetManager(TargetManager tm) {
68         this.targetManager = tm;
69     }
70
71     /**
72      * Returns the target manager instance which will be used for action
73      * dispatch. If the target manager has not previously been set then the
74      * singleton instance will be returned.
75      *
76      * @return a non null target manager
77      */

78     public TargetManager getTargetManager() {
79         if (targetManager == null) {
80             targetManager = TargetManager.getInstance();
81         }
82         return targetManager;
83     }
84
85     // Callbacks...
86

87     /**
88      * Callback for command actions. This event will be redispatched to
89      * the target manager along with the value of the Action.ACTION_COMMAND_KEY
90      *
91      * @param evt event which will be forwarded to the TargetManager
92      * @see TargetManager
93      */

94     public void actionPerformed(ActionEvent JavaDoc evt) {
95         if (!isStateAction()) {
96             // Do not process this event if it's a toggle action.
97
getTargetManager().doCommand(getActionCommand(), evt);
98         }
99     }
100
101     /**
102      * Callback for toggle actions. This event will be redispatched to
103      * the target manager along with value of the the Action.ACTION_COMMAND_KEY
104      *
105      * @param evt event which will be forwarded to the TargetManager
106      * @see TargetManager
107      */

108     public void itemStateChanged(ItemEvent JavaDoc evt) {
109         // Update all objects that share this item
110
boolean newValue;
111         boolean oldValue = isSelected();
112
113         if (evt.getStateChange() == ItemEvent.SELECTED) {
114             newValue = true;
115         } else {
116             newValue = false;
117         }
118
119         if (oldValue != newValue) {
120             setSelected(newValue);
121
122             getTargetManager().doCommand(getActionCommand(), evt);
123         }
124     }
125
126     public String JavaDoc toString() {
127         return super.toString();
128     }
129 }
130
Popular Tags