1 14 package org.eclipse.jface.resource; 15 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.Map ; 19 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.core.runtime.Assert; 22 import org.eclipse.swt.SWT; 23 import org.eclipse.swt.graphics.Device; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.ImageData; 26 import org.eclipse.swt.widgets.Display; 27 28 46 public class ImageRegistry { 47 50 private Display display; 51 52 private ResourceManager manager; 53 54 private Map table; 55 56 private Runnable disposeRunnable = new Runnable () { 57 public void run() { 58 dispose(); 59 } 60 }; 61 62 65 private static class Entry { 66 67 protected Image image; 68 69 70 protected ImageDescriptor descriptor; 71 } 72 73 private static class OriginalImageDescriptor extends ImageDescriptor { 74 private Image original; 75 private int refCount = 0; 76 private Device originalDisplay; 77 78 82 public OriginalImageDescriptor(Image original, Device originalDisplay) { 83 this.original = original; 84 this.originalDisplay = originalDisplay; 85 } 86 87 public Object createResource(Device device) throws DeviceResourceException { 88 if (device == originalDisplay) { 89 refCount++; 90 return original; 91 } 92 return super.createResource(device); 93 } 94 95 public void destroyResource(Object toDispose) { 96 if (original == toDispose) { 97 refCount--; 98 if (refCount == 0) { 99 original.dispose(); 100 original = null; 101 } 102 } else { 103 super.destroyResource(toDispose); 104 } 105 } 106 107 110 public ImageData getImageData() { 111 return original.getImageData(); 112 } 113 } 114 115 122 public ImageRegistry() { 123 this(Display.getCurrent()); 124 } 125 126 133 public ImageRegistry(ResourceManager manager) { 134 Assert.isNotNull(manager); 135 Device dev = manager.getDevice(); 136 if (dev instanceof Display) { 137 this.display = (Display)dev; 138 } 139 this.manager = manager; 140 manager.disposeExec(disposeRunnable); 141 } 142 143 150 public ImageRegistry(Display display) { 151 this(JFaceResources.getResources(display)); 152 } 153 154 161 public Image get(String key) { 162 163 if (key == null) { 165 return null; 166 } 167 168 if (display != null) { 169 178 int swtKey = -1; 179 if (key.equals(Dialog.DLG_IMG_INFO)) { 180 swtKey = SWT.ICON_INFORMATION; 181 } 182 if (key.equals(Dialog.DLG_IMG_QUESTION)) { 183 swtKey = SWT.ICON_QUESTION; 184 } 185 if (key.equals(Dialog.DLG_IMG_WARNING)) { 186 swtKey = SWT.ICON_WARNING; 187 } 188 if (key.equals(Dialog.DLG_IMG_ERROR)) { 189 swtKey = SWT.ICON_ERROR; 190 } 191 if (swtKey != -1) { 194 final Image[] image = new Image[1]; 195 final int id = swtKey; 196 display.syncExec(new Runnable () { 197 public void run() { 198 image[0] = display.getSystemImage(id); 199 } 200 }); 201 return image[0]; 202 } 203 } 204 205 Entry entry = getEntry(key); 206 if (entry == null) { 207 return null; 208 } 209 210 if (entry.image == null) { 211 entry.image = manager.createImageWithDefault(entry.descriptor); 212 } 213 214 return entry.image; 215 } 216 217 225 public ImageDescriptor getDescriptor(String key) { 226 Entry entry = getEntry(key); 227 if (entry == null) { 228 return null; 229 } 230 231 return entry.descriptor; 232 } 233 234 245 public void put(String key, ImageDescriptor descriptor) { 246 Entry entry = getEntry(key); 247 if (entry == null) { 248 entry = new Entry(); 249 getTable().put(key, entry); 250 } 251 252 if (entry.image != null) { 253 throw new IllegalArgumentException ( 254 "ImageRegistry key already in use: " + key); } 256 257 entry.descriptor = descriptor; 258 } 259 260 274 public void put(String key, Image image) { 275 Entry entry = getEntry(key); 276 277 if (entry == null) { 278 entry = new Entry(); 279 putEntry(key, entry); 280 } 281 282 if (entry.image != null || entry.descriptor != null) { 283 throw new IllegalArgumentException ( 284 "ImageRegistry key already in use: " + key); } 286 287 entry.image = image; 291 entry.descriptor = new OriginalImageDescriptor(image, manager.getDevice()); 292 293 try { 294 manager.create(entry.descriptor); 295 } catch (DeviceResourceException e) { 296 } 297 } 298 299 305 public void remove(String key) { 306 ImageDescriptor descriptor = getDescriptor(key); 307 if (descriptor != null) { 308 manager.destroy(descriptor); 309 getTable().remove(key); 310 } 311 } 312 313 private Entry getEntry(String key) { 314 return (Entry) getTable().get(key); 315 } 316 317 private void putEntry(String key, Entry entry) { 318 getTable().put(key, entry); 319 } 320 321 private Map getTable() { 322 if (table == null) { 323 table = new HashMap (10); 324 } 325 return table; 326 } 327 328 334 public void dispose() { 335 manager.cancelDisposeExec(disposeRunnable); 336 337 if (table != null) { 338 for (Iterator i = table.values().iterator(); i.hasNext();) { 339 Entry entry = (Entry) i.next(); 340 if (entry.image != null) { 341 manager.destroyImage(entry.descriptor); 342 } 343 } 344 table = null; 345 } 346 display = null; 347 } 348 } 349 | Popular Tags |