KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sshtools > ui > swing > AppAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sshtools.ui.swing;
21
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.net.URL JavaDoc;
25
26 import javax.swing.AbstractAction JavaDoc;
27 import javax.swing.Action JavaDoc;
28 import javax.swing.Icon JavaDoc;
29 import javax.swing.ImageIcon JavaDoc;
30 import javax.swing.event.EventListenerList JavaDoc;
31
32
33 /**
34  *
35  * Extension of {@link AbstractAction}that is used throughout the application
36  *
37  * framework. Provides extra settings for actions that can be used to build
38  *
39  * action components.
40  *
41  * @author $Author: brett $
42  *
43  * @version $Revision: 1.1 $
44  *
45  */

46 public abstract class AppAction extends AbstractAction JavaDoc {
47     /** */
48     public final static String JavaDoc IMAGE_DIR = "/com/sshtools/sshterm/";
49
50     /** */
51     public final static String JavaDoc ON_TOOLBAR = "OnToolBar";
52
53     /** */
54     public final static String JavaDoc TOOLBAR_GROUP = "ToolBarGroup";
55
56     /** */
57     public final static String JavaDoc TOOLBAR_WEIGHT = "ToolBarWeight";
58
59     /** */
60     public final static String JavaDoc ON_MENUBAR = "OnMenuBar";
61
62     /** */
63     public final static String JavaDoc MENU_NAME = "MenuName";
64
65     /** */
66     public final static String JavaDoc MENU_ITEM_GROUP = "MenuItemGroup";
67
68     /** */
69     public final static String JavaDoc MENU_ITEM_WEIGHT = "MmenuItemWeight";
70
71     /** */
72     public final static String JavaDoc TEXT_ON_TOOLBAR = "HideToolbarText";
73
74     /** */
75     public final static String JavaDoc IS_TOGGLE_BUTTON = "IsToggleButton";
76
77     /** */
78     public final static String JavaDoc IS_SELECTED = "IsSelected";
79
80     /** */
81     public final static String JavaDoc LARGE_ICON = "LargeIcon";
82
83     /** */
84     public final static String JavaDoc ON_CONTEXT_MENU = "OnContextMenu";
85
86     /** */
87     public final static String JavaDoc CONTEXT_MENU_GROUP = "ContextMenuGroup";
88
89     /** */
90     public final static String JavaDoc CONTEXT_MENU_WEIGHT = "ContextMenuWeight";
91
92     /** */
93     public final static String JavaDoc MEDIUM_ICON = "ToolIcon";
94
95
96     /** */
97     public final static String JavaDoc CATEGORY = "Category";
98
99     // The listener to action events (usually the main UI)
100
private EventListenerList JavaDoc listeners;
101
102     /**
103      */

104     public AppAction() {
105       this("");
106     }
107
108     /**
109      * @param name
110      */

111     public AppAction(String JavaDoc name) {
112         this(name, null);
113     }
114
115     /**
116      * @param name
117      */

118     public AppAction(String JavaDoc name, Icon JavaDoc smallIcon) {
119         if(name != null) {
120             putValue(AppAction.NAME, name);
121         }
122         if(smallIcon != null) {
123             putValue(AppAction.SMALL_ICON, smallIcon);
124         }
125     }
126
127     /**
128      * @return
129      */

130     public String JavaDoc getActionCommand() {
131         return (String JavaDoc) getValue(Action.ACTION_COMMAND_KEY);
132     }
133
134     /**
135      * @return
136      */

137     public String JavaDoc getShortDescription() {
138         return (String JavaDoc) getValue(Action.SHORT_DESCRIPTION);
139     }
140
141     /**
142      * @return
143      */

144     public String JavaDoc getLongDescription() {
145         return (String JavaDoc) getValue(Action.LONG_DESCRIPTION);
146     }
147
148     /**
149      * @return
150      */

151     public String JavaDoc getName() {
152         return (String JavaDoc) getValue(Action.NAME);
153     }
154
155     /**
156      * @return
157      */

158     public ResourceIcon getSmallIcon() {
159         return (ResourceIcon) getValue(Action.SMALL_ICON);
160     }
161
162     /**
163      * @param evt
164      *
165      */

166     public void actionPerformed(ActionEvent JavaDoc evt) {
167         if (listeners != null) {
168             Object JavaDoc[] listenerList = listeners.getListenerList();
169             // Recreate the ActionEvent and stuff the value of the
170
// ACTION_COMMAND_KEY
171
ActionEvent JavaDoc e = new ActionEvent JavaDoc(evt.getSource(), evt.getID(),
172                     (String JavaDoc) getValue(Action.ACTION_COMMAND_KEY));
173             for (int i = 0; i <= (listenerList.length - 2); i += 2) {
174                 ((ActionListener JavaDoc) listenerList[i + 1]).actionPerformed(e);
175             }
176         }
177     }
178
179     /**
180      * @param l
181      *
182      */

183     public void addActionListener(ActionListener JavaDoc l) {
184         if (listeners == null) {
185             listeners = new EventListenerList JavaDoc();
186         }
187         listeners.add(ActionListener JavaDoc.class, l);
188     }
189
190     /**
191      * @param l
192      *
193      */

194     public void removeActionListener(ActionListener JavaDoc l) {
195         if (listeners == null) { return; }
196         listeners.remove(ActionListener JavaDoc.class, l);
197     }
198
199     /**
200      * @param name
201      *
202      *
203      *
204      * @return
205      */

206     public ImageIcon JavaDoc getIcon(String JavaDoc name) {
207         String JavaDoc imagePath = name.startsWith("/") ? name : (IMAGE_DIR + name);
208         URL JavaDoc url = this.getClass().getResource(imagePath);
209         if (url != null) { return new ImageIcon JavaDoc(url); }
210         return null;
211     }
212
213     public boolean isSelected() {
214         return Boolean.TRUE.equals(getValue(IS_SELECTED));
215     }
216
217
218     public void setSelected(boolean selected) {
219         putValue(IS_SELECTED, Boolean.valueOf(selected));
220     }
221 }
222
Popular Tags