1 package org.oddjob.images; 2 3 import java.io.IOException ; 4 import java.io.ObjectInputStream ; 5 import java.io.ObjectOutputStream ; 6 import java.io.Serializable ; 7 import java.util.ArrayList ; 8 import java.util.HashMap ; 9 import java.util.List ; 10 import java.util.Map ; 11 12 import org.oddjob.Iconic; 13 import org.oddjob.util.OddjobConstantException; 14 15 16 22 23 public class IconHelper implements Serializable { 24 25 private static final long serialVersionUID = 20050927; 26 27 public static final String NULL = "null"; 28 public static final String INITIALIZING = "initializing"; 29 public static final String READY = "ready"; 30 public static final String EXECUTING = "executing"; 31 public static final String COMPLETE = "complete"; 32 public static final String NOT_COMPLETE = "notcomplete"; 33 public static final String EXCEPTION = "exception"; 34 public static final String SLEEPING = "sleeping"; 35 public static final String STOPPING = "stopping"; 36 public static final String STOPPED= "stopped"; 37 public static final String 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 defaultIconMap = new HashMap (); 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 source; 111 private volatile IconEvent lastEvent; 112 private transient List listeners = new ArrayList (); 113 private transient Map iconMap; 114 private transient Iconic mirror; 115 116 121 public IconHelper(Object source) { 122 this.source = source; 123 lastEvent = new IconEvent(source, READY); 124 iconMap = defaultIconMap; 125 } 126 127 public void setIconMap(Map iconMap) { 128 this.iconMap = iconMap; 129 } 130 131 public void setMirror(Iconic mirror) { 132 this.mirror = mirror; 133 this.iconMap = new HashMap (); 134 } 135 136 144 public IconTip iconForId(String 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 162 public void changeIcon(String iconId) { 163 iconForId(iconId); 165 166 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 186 public String currentId() { 187 return lastEvent.getIconId(); 188 } 189 190 public void addIconListener(IconListener listener) { 191 if (lastEvent == null) { 192 throw new IllegalStateException ("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 211 synchronized public void setSource(Object 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 s) 220 throws IOException { 221 s.defaultWriteObject(); 222 } 223 224 private void readObject(ObjectInputStream s) 225 throws IOException , ClassNotFoundException { 226 s.defaultReadObject(); 227 listeners = new ArrayList (); 228 iconMap = defaultIconMap; 229 } 230 231 public static IconTip getIconTip(String id) { 232 return (IconTip) defaultIconMap.get(id); 233 } 234 } 235 | Popular Tags |