KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JLFAbstractAction


1 /*
2  * @(#)JLFAbstractAction.java 1.5 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 import java.awt.event.ActionEvent JavaDoc;
38 import java.awt.event.ActionListener JavaDoc;
39
40 import java.net.URL JavaDoc;
41
42 import javax.swing.AbstractAction JavaDoc;
43 import javax.swing.Action JavaDoc;
44 import javax.swing.ImageIcon JavaDoc;
45
46 import javax.swing.event.EventListenerList JavaDoc;
47
48 /**
49  * Abstract Action for the JLF. Defines some useful methods.
50  *
51  */

52 public abstract class JLFAbstractAction extends AbstractAction JavaDoc {
53     
54     // The listener to action events (usually the main UI)
55
private EventListenerList JavaDoc listeners;
56     
57     // Image directory URL
58
public static final String JavaDoc JLF_IMAGE_DIR = "/toolbarButtonGraphics/general/";
59     
60     /**
61      * The key used for storing a large icon for the action,
62      * used for toolbar buttons.
63      * <p>
64      * Note: Eventually this key belongs in the javax.swing.Action interface.
65      */

66     public static final String JavaDoc LARGE_ICON = "LargeIcon";
67     
68     //
69
// These next public methods may belong in the AbstractAction class.
70
//
71

72     /**
73      * Gets the value from the key Action.ACTION_COMMAND_KEY
74      */

75     public String JavaDoc getActionCommand() {
76         return (String JavaDoc)getValue(Action.ACTION_COMMAND_KEY);
77     }
78     
79     /**
80      * Gets the value from the key Action.SHORT_DESCRIPTION
81      */

82     public String JavaDoc getShortDescription() {
83         return (String JavaDoc)getValue(Action.SHORT_DESCRIPTION);
84     }
85     
86     /**
87      * Gets the value from the key Action.LONG_DESCRIPTION
88      */

89     public String JavaDoc getLongDescription() {
90         return (String JavaDoc)getValue(Action.LONG_DESCRIPTION);
91     }
92     
93     /* Should finish the implementation and add get/set methods for all the
94      * javax.swing.Action keys:
95      
96      Action.NAME
97      Action.SMALL_ICON
98      ActionConstants.LARGE_ICON
99      Action.MNEMONIC_KEY
100      */

101     
102     
103     // ActionListener registration and invocation.
104

105     /**
106      * Forwards the ActionEvent to the registered listener.
107      */

108     public void actionPerformed(ActionEvent JavaDoc evt) {
109         if (listeners != null) {
110         Object JavaDoc[] listenerList = listeners.getListenerList();
111         
112         // Recreate the ActionEvent and stuff the value of the ACTION_COMMAND_KEY
113
ActionEvent JavaDoc e = new ActionEvent JavaDoc(evt.getSource(), evt.getID(),
114                         (String JavaDoc)getValue(Action.ACTION_COMMAND_KEY));
115         for (int i = 0; i <= listenerList.length-2; i += 2) {
116         ((ActionListener JavaDoc)listenerList[i+1]).actionPerformed(e);
117         }
118         }
119     }
120     
121     public void addActionListener(ActionListener JavaDoc l) {
122         if (listeners == null) {
123         listeners = new EventListenerList JavaDoc();
124     }
125         listeners.add(ActionListener JavaDoc.class, l);
126     }
127     
128     public void removeActionListener(ActionListener JavaDoc l) {
129     if (listeners == null) {
130         return;
131     }
132         listeners.remove(ActionListener JavaDoc.class, l);
133     }
134     
135     /**
136      * Returns the Icon associated with the name from the resources.
137      * The resouce should be in the path.
138      * @param name Name of the icon file i.e., help16.gif
139      * @return the name of the image or null if the icon is not found.
140      */

141     public ImageIcon JavaDoc getIcon(String JavaDoc name) {
142     if (name != null) {
143         String JavaDoc imagePath = JLF_IMAGE_DIR + name;
144         URL JavaDoc url = this.getClass().getResource(imagePath);
145         if (url != null) return new ImageIcon JavaDoc(url);
146     }
147         return null;
148     }
149 }
150
151
152
Popular Tags