2
* $Id: TargetableAction.java 3475 2009-08-28 08:30:47Z kleopatra $
4
* Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5
* Santa Clara, California 95054, U.S.A. All rights reserved.
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.
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.
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
22
package org.jdesktop.swingx.action;
24
import java.awt.event.ActionEvent;
25
import java.awt.event.ItemEvent;
27
import javax.swing.Icon;
30
* A class that represents a dynamically targetable action. The invocation of this
31
* action will be dispatched to the <code>TargetManager</code> instance.
33
* You would create instances of this class to let the TargetManager handle the
34
* action invocations from the ui components constructed with this action.
35
* The TargetManager could be configured depending on application state to
36
* handle these actions.
39
* @author Mark Davidson
41
public class TargetableAction extends AbstractActionExt {
43
private TargetManager targetManager;
45
public TargetableAction() {
49
public TargetableAction(String name) {
54
* @param name display name of the action
55
* @param command the value of the action command key
57
public TargetableAction(String name, String command) {
62
* @param name display name of the action
63
* @param command the value of the action command key
64
* @param icon icon to display
66
public TargetableAction(String name, String command, Icon icon) {
67
super(name, command, icon);
70
public TargetableAction(String name, Icon icon) {
75
* Set target manager which will handle this command. This action
76
* may be reset to use the singleton instance if set to null.
78
* @param tm the target manager instance to dispatch the actions
80
public void setTargetManager(TargetManager tm) {
81
this.targetManager = tm;
85
* Returns the target manager instance which will be used for action
86
* dispatch. If the target manager has not previously been set then the
87
* singleton instance will be returned.
89
* @return a non null target manager
91
public TargetManager getTargetManager() {
92
if (targetManager == null) {
93
targetManager = TargetManager.getInstance();
101
* Callback for command actions. This event will be redispatched to
102
* the target manager along with the value of the Action.ACTION_COMMAND_KEY
104
* @param evt event which will be forwarded to the TargetManager
107
public void actionPerformed(ActionEvent evt) {
108
if (!isStateAction()) {
109
// Do not process this event if it's a toggle action.
110
getTargetManager().doCommand(getActionCommand(), evt);
115
* Callback for toggle actions. This event will be redispatched to
116
* the target manager along with value of the the Action.ACTION_COMMAND_KEY
118
* @param evt event which will be forwarded to the TargetManager
122
public void itemStateChanged(ItemEvent evt) {
123
// Update all objects that share this item
125
boolean oldValue = isSelected();
127
newValue = evt.getStateChange() == ItemEvent.SELECTED;
129
if (oldValue != newValue) {
130
setSelected(newValue);
132
getTargetManager().doCommand(getActionCommand(), evt);
137
public String toString() {
138
return super.toString();