~ubuntu-branches/ubuntu/trusty/libswingx-java/trusty

« back to all changes in this revision

Viewing changes to src/java/org/jdesktop/swingx/action/TargetableAction.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-03-06 00:28:45 UTC
  • mfrom: (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110306002845-escned3cbqp5qx0t
Tags: 1:1.6.2-1
* New upstream release.
* Switch to maven as build system:
  - d/control: drop ant, add maven-debian-helper
  - d/rules: use maven.mk
* d/patches/pom.diff: drop, uneeded since upstream fixed its dependencies.
* d/watch: update to use java.net directly.
* d/rules: force debian version for JARs (Closes: #603495).
* d/copyright: Update to lastest DEP-5 r166.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * $Id: TargetableAction.java 3475 2009-08-28 08:30:47Z kleopatra $
3
 
 *
4
 
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
5
 
 * Santa Clara, California 95054, U.S.A. All rights reserved.
6
 
 *
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.
11
 
 * 
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.
16
 
 * 
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
20
 
 */
21
 
 
22
 
package org.jdesktop.swingx.action;
23
 
 
24
 
import java.awt.event.ActionEvent;
25
 
import java.awt.event.ItemEvent;
26
 
 
27
 
import javax.swing.Icon;
28
 
 
29
 
/**
30
 
 * A class that represents a dynamically targetable action. The invocation of this
31
 
 * action will be dispatched to the <code>TargetManager</code> instance.
32
 
 * <p>
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.
37
 
 *
38
 
 * @see TargetManager
39
 
 * @author Mark Davidson
40
 
 */
41
 
public class TargetableAction extends AbstractActionExt {
42
 
 
43
 
    private TargetManager targetManager;
44
 
 
45
 
    public TargetableAction() {
46
 
        this("action");
47
 
    }
48
 
 
49
 
    public TargetableAction(String name) {
50
 
        super(name);
51
 
    }
52
 
 
53
 
    /**
54
 
     * @param name display name of the action
55
 
     * @param command the value of the action command key
56
 
     */
57
 
    public TargetableAction(String name, String command) {
58
 
        super(name, command);
59
 
    }
60
 
 
61
 
    /**
62
 
     * @param name display name of the action
63
 
     * @param command the value of the action command key
64
 
     * @param icon icon to display
65
 
     */
66
 
    public TargetableAction(String name, String command, Icon icon) {
67
 
        super(name, command, icon);
68
 
    }
69
 
 
70
 
    public TargetableAction(String name, Icon icon) {
71
 
        super(name, icon);
72
 
    }
73
 
 
74
 
    /**
75
 
     * Set target manager which will handle this command. This action
76
 
     * may be reset to use the singleton instance if set to null.
77
 
     *
78
 
     * @param tm the target manager instance to dispatch the actions
79
 
     */
80
 
    public void setTargetManager(TargetManager tm) {
81
 
        this.targetManager = tm;
82
 
    }
83
 
 
84
 
    /**
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.
88
 
     *
89
 
     * @return a non null target manager
90
 
     */
91
 
    public TargetManager getTargetManager() {
92
 
        if (targetManager == null) {
93
 
            targetManager = TargetManager.getInstance();
94
 
        }
95
 
        return targetManager;
96
 
    }
97
 
 
98
 
    // Callbacks...
99
 
 
100
 
    /**
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
103
 
     *
104
 
     * @param evt event which will be forwarded to the TargetManager
105
 
     * @see TargetManager
106
 
     */
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);
111
 
        }
112
 
    }
113
 
 
114
 
    /**
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
117
 
     *
118
 
     * @param evt event which will be forwarded to the TargetManager
119
 
     * @see TargetManager
120
 
     */
121
 
    @Override
122
 
    public void itemStateChanged(ItemEvent evt) {
123
 
        // Update all objects that share this item
124
 
        boolean newValue;
125
 
        boolean oldValue = isSelected();
126
 
 
127
 
        newValue = evt.getStateChange() == ItemEvent.SELECTED;
128
 
 
129
 
        if (oldValue != newValue) {
130
 
            setSelected(newValue);
131
 
 
132
 
            getTargetManager().doCommand(getActionCommand(), evt);
133
 
        }
134
 
    }
135
 
 
136
 
    @Override
137
 
    public String toString() {
138
 
        return super.toString();
139
 
    }
140
 
}