KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > images > IconHelper


1 package org.oddjob.images;
2
3 import java.io.IOException JavaDoc;
4 import java.io.ObjectInputStream JavaDoc;
5 import java.io.ObjectOutputStream JavaDoc;
6 import java.io.Serializable JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.List JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import org.oddjob.Iconic;
13 import org.oddjob.util.OddjobConstantException;
14
15
16 /**
17  * Icons for the JobTree. All from <a HREF="http://www.cit.gu.edu.au/images/Images.html">Anthony's WWW Images</a>.
18  * <a HREF="http://java.sun.com/developer/techDocs/hi/repository/">Java Look and Feel Graphics</a>.
19  *
20  * @author Rob Gordon
21  */

22
23 public class IconHelper implements Serializable JavaDoc {
24
25     private static final long serialVersionUID = 20050927;
26
27     public static final String JavaDoc NULL = "null";
28     public static final String JavaDoc INITIALIZING = "initializing";
29     public static final String JavaDoc READY = "ready";
30     public static final String JavaDoc EXECUTING = "executing";
31     public static final String JavaDoc COMPLETE = "complete";
32     public static final String JavaDoc NOT_COMPLETE = "notcomplete";
33     public static final String JavaDoc EXCEPTION = "exception";
34     public static final String JavaDoc SLEEPING = "sleeping";
35     public static final String JavaDoc STOPPING = "stopping";
36     public static final String JavaDoc STOPPED= "stopped";
37     public static final String JavaDoc INVALID = "invalid";
38     
39     public static final IconTip nullIcon
40         = new IconTip(
41                 IconHelper.class.getResource("diamond.gif"),
42                 "Null Icon");
43     
44     public static final IconTip initializingIcon
45         = new IconTip(
46                 IconHelper.class.getResource("triangle.gif"),
47                 "Initialising");
48
49     public static final IconTip readyIcon
50         = new IconTip(
51                 IconHelper.class.getResource("right_blue.gif"),
52                 "Ready");
53
54     public static final IconTip executingIcon
55         = new IconTip(
56                 IconHelper.class.getResource("triangle_green.gif"),
57                 "Executing");
58
59     public static final IconTip completeIcon
60         = new IconTip(
61                 IconHelper.class.getResource("tick_green.gif"),
62                 "Complete");
63
64     public static final IconTip notCompleteIcon
65         = new IconTip(
66                 IconHelper.class.getResource("cross.gif"),
67                 "Not Complete");
68
69     public static final IconTip stoppingIcon
70         = new IconTip(
71                 IconHelper.class.getResource("triangle_red.gif"),
72                 "Stopping");
73
74     public static final IconTip stoppedIcon
75     = new IconTip(
76             IconHelper.class.getResource("square_red.gif"),
77             "Stopped");
78
79     public static final IconTip sleepingIcon
80         = new IconTip(
81                 IconHelper.class.getResource("dot_blue.gif"),
82                 "Sleeping");
83
84     public static final IconTip invalidIcon
85         = new IconTip(
86                 IconHelper.class.getResource("cross_red.gif"),
87                 "Invalid");
88
89     public static final IconTip exceptionIcon
90         = new IconTip(
91                 IconHelper.class.getResource("asterix_red.gif"),
92                 "Exception");
93
94     private static Map JavaDoc defaultIconMap = new HashMap JavaDoc();
95
96     static {
97         defaultIconMap.put(NULL, nullIcon);
98         defaultIconMap.put(INITIALIZING, initializingIcon);
99         defaultIconMap.put(READY ,readyIcon);
100         defaultIconMap.put(EXECUTING, executingIcon);
101         defaultIconMap.put(COMPLETE, completeIcon);
102         defaultIconMap.put(NOT_COMPLETE, notCompleteIcon);
103         defaultIconMap.put(SLEEPING, sleepingIcon);
104         defaultIconMap.put(STOPPING, stoppingIcon);
105         defaultIconMap.put(STOPPED, stoppedIcon);
106         defaultIconMap.put(INVALID, invalidIcon);
107         defaultIconMap.put(EXCEPTION, exceptionIcon);
108     }
109
110     private transient Object JavaDoc source;
111     private volatile IconEvent lastEvent;
112     private transient List JavaDoc listeners = new ArrayList JavaDoc();
113     private transient Map JavaDoc iconMap;
114     private transient Iconic mirror;
115     
116     /**
117      * Constructor.
118      *
119      * @param source The source for events.
120      */

121     public IconHelper(Object JavaDoc source) {
122         this.source = source;
123         lastEvent = new IconEvent(source, READY);
124         iconMap = defaultIconMap;
125     }
126     
127     public void setIconMap(Map JavaDoc iconMap) {
128         this.iconMap = iconMap;
129     }
130     
131     public void setMirror(Iconic mirror) {
132         this.mirror = mirror;
133         this.iconMap = new HashMap JavaDoc();
134     }
135     
136     /**
137      * Retrun an IconTip for the given id.
138      *
139      * @param iconId The id.
140      * @return The iconTip never null.
141      *
142      * @throws NoSuchIconException If the icon doesn't exist.
143      */

144     public IconTip iconForId(String JavaDoc iconId) throws NoSuchIconException {
145         IconTip iconTip = (IconTip)iconMap.get(iconId);
146         if (iconTip != null) {
147             return iconTip;
148         }
149         if (mirror != null) {
150             iconTip = mirror.iconForId(iconId);
151             iconMap.put(iconId, iconTip);
152             return iconTip;
153         }
154         throw new NoSuchIconException("No icon for " + iconId);
155     }
156
157     /**
158      * Change the icon by firing an iconEvent.
159      *
160      * @param iconId The icon id.
161      */

162     public void changeIcon(String JavaDoc iconId) {
163         // check icon
164
iconForId(iconId);
165         
166         // create a local last event so that another thread
167
// doesn't change the event mid notification. Copy the
168
// list of lisetners so that we don't need to
169
// hold the monitor lock when we notify them.
170
IconEvent localEvent = new IconEvent(source, iconId);
171         IconListener[] la = null;
172         synchronized (listeners) {
173             lastEvent = localEvent;
174             la = (IconListener[]) listeners.toArray(new IconListener[0]);
175         }
176         for (int i = 0; i < la.length; ++i) {
177             la[i].iconEvent(localEvent);
178         }
179     }
180
181     /**
182      * Get the current/last icon id.
183      *
184      * @return An iconId.
185      */

186     public String JavaDoc currentId() {
187         return lastEvent.getIconId();
188     }
189     
190     public void addIconListener(IconListener listener) {
191         if (lastEvent == null) {
192             throw new IllegalStateException JavaDoc("No icon set in " + source);
193         }
194         listener.iconEvent(lastEvent);
195         synchronized (listeners) {
196             listeners.add(listener);
197         }
198     }
199     
200     public void removeIconListener(IconListener listener) {
201         synchronized (listeners) {
202             listeners.remove(listener);
203         }
204     }
205     
206     /**
207      * Typically only called after deserialisation.
208      *
209      * @param source
210      */

211     synchronized public void setSource(Object JavaDoc source) {
212         if (this.source != null) {
213             throw new OddjobConstantException("Source can not be changed.");
214         }
215         this.source = source;
216         lastEvent = new IconEvent(source, lastEvent.getIconId());
217     }
218     
219     private void writeObject(ObjectOutputStream JavaDoc s)
220     throws IOException JavaDoc {
221         s.defaultWriteObject();
222     }
223     
224     private void readObject(ObjectInputStream JavaDoc s)
225     throws IOException JavaDoc, ClassNotFoundException JavaDoc {
226         s.defaultReadObject();
227         listeners = new ArrayList JavaDoc();
228         iconMap = defaultIconMap;
229     }
230     
231     public static IconTip getIconTip(String JavaDoc id) {
232         return (IconTip) defaultIconMap.get(id);
233     }
234 }
235
Popular Tags