1 11 package org.eclipse.ui.internal.commands; 12 13 import java.net.URL ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.core.commands.common.EventManager; 20 import org.eclipse.jface.resource.ImageDescriptor; 21 22 43 public final class CommandImageManager extends EventManager { 44 45 48 public static final int TYPE_DEFAULT = 0; 49 50 53 public static final int TYPE_DISABLED = 1; 54 55 59 public static final int TYPE_HOVER = 2; 60 61 67 private final Map imagesById = new HashMap (); 68 69 77 public final void addCommandImageManagerListener( 78 final ICommandImageManagerListener listener) { 79 addListenerObject(listener); 80 } 81 82 96 public final void bind(final String commandId, final int type, 97 final String style, final URL url) { 98 final ImageDescriptor descriptor = ImageDescriptor.createFromURL(url); 99 bind(commandId, type, style, descriptor); 100 } 101 102 116 public final void bind(final String commandId, final int type, 117 final String style, final ImageDescriptor descriptor) { 118 Object [] images = (Object []) imagesById.get(commandId); 119 if (images == null) { 120 images = new Object [3]; 121 imagesById.put(commandId, images); 122 } 123 124 if ((type < 0) || (type >= images.length)) { 125 throw new IllegalArgumentException ( 126 "The type must be one of TYPE_DEFAULT, TYPE_DISABLED and TYPE_HOVER."); } 128 129 final Object typedImage = images[type]; 130 if (style == null) { 131 if ((typedImage == null) || (typedImage instanceof ImageDescriptor)) { 132 images[type] = descriptor; 133 } else if (typedImage instanceof Map ) { 134 final Map styleMap = (Map ) typedImage; 135 styleMap.put(style, descriptor); 136 } 137 } else { 138 if (typedImage instanceof Map ) { 139 final Map styleMap = (Map ) typedImage; 140 styleMap.put(style, descriptor); 141 } else if (typedImage instanceof ImageDescriptor) { 142 final Map styleMap = new HashMap (); 143 styleMap.put(null, typedImage); 144 styleMap.put(style, descriptor); 145 images[type] = descriptor; 146 } 147 } 148 149 fireManagerChanged(new CommandImageManagerEvent(this, 150 new String [] { commandId }, type, style)); 151 } 152 153 156 public final void clear() { 157 imagesById.clear(); 158 if (isListenerAttached()) { 159 final String [] commandIds = (String []) imagesById.keySet().toArray( 160 new String [imagesById.size()]); 161 fireManagerChanged(new CommandImageManagerEvent(this, commandIds, 162 TYPE_DEFAULT, null)); 163 } 164 } 165 166 174 private final void fireManagerChanged(final CommandImageManagerEvent event) { 175 if (event == null) { 176 throw new NullPointerException (); 177 } 178 179 final Object [] listeners = getListeners(); 180 for (int i = 0; i < listeners.length; i++) { 181 final ICommandImageManagerListener listener = (ICommandImageManagerListener) listeners[i]; 182 listener.commandImageManagerChanged(event); 183 } 184 } 185 186 196 public final String generateUnusedStyle(final String commandId) { 197 final Object [] existingImages = (Object []) imagesById.get(commandId); 198 if (existingImages == null) { 199 return null; 200 } 201 202 final Set existingStyles = new HashSet (3); 203 for (int type = 0; type < existingImages.length; type++) { 204 final Object styledImages = existingImages[type]; 205 if (styledImages instanceof ImageDescriptor) { 206 existingStyles.add(null); 207 } else if (styledImages instanceof Map ) { 208 final Map styleMap = (Map ) styledImages; 209 existingStyles.addAll(styleMap.keySet()); 210 } 211 } 212 213 if (!existingStyles.contains(null)) { 214 return null; 215 } 216 217 String generatedStyle = "AUTOGEN:::"; int index = 0; 219 while (existingStyles.contains(generatedStyle)) { 220 generatedStyle += (index++ % 10); 221 } 222 223 return generatedStyle; 224 } 225 226 235 public final ImageDescriptor getImageDescriptor(final String commandId) { 236 return getImageDescriptor(commandId, TYPE_DEFAULT, null); 237 } 238 239 251 public final ImageDescriptor getImageDescriptor(final String commandId, 252 final int type) { 253 return getImageDescriptor(commandId, type, null); 254 } 255 256 270 public final ImageDescriptor getImageDescriptor(final String commandId, 271 final int type, final String style) { 272 if (commandId == null) { 273 throw new NullPointerException (); 274 } 275 276 final Object [] images = (Object []) imagesById.get(commandId); 277 if (images == null) { 278 return null; 279 } 280 281 if ((type < 0) || (type >= images.length)) { 282 throw new IllegalArgumentException ( 283 "The type must be one of TYPE_DEFAULT, TYPE_DISABLED and TYPE_HOVER."); } 285 286 Object typedImage = images[type]; 287 288 if (typedImage == null) { 289 typedImage = images[TYPE_DEFAULT]; 290 } 291 292 if (typedImage instanceof ImageDescriptor) { 293 return (ImageDescriptor) typedImage; 294 } 295 296 if (typedImage instanceof Map ) { 297 final Map styleMap = (Map ) typedImage; 298 Object styledImage = styleMap.get(style); 299 if (styledImage instanceof ImageDescriptor) { 300 return (ImageDescriptor) styledImage; 301 } 302 303 if (style != null) { 304 styledImage = styleMap.get(null); 305 if (styledImage instanceof ImageDescriptor) { 306 return (ImageDescriptor) styledImage; 307 } 308 } 309 } 310 311 return null; 312 } 313 314 325 public final ImageDescriptor getImageDescriptor(final String commandId, 326 final String style) { 327 return getImageDescriptor(commandId, TYPE_DEFAULT, style); 328 } 329 330 336 public final void removeCommandImageManagerListener( 337 final ICommandImageManagerListener listener) { 338 removeListenerObject(listener); 339 } 340 } 341 | Popular Tags |