1 11 package org.eclipse.jface.resource; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.Assert; 17 import org.eclipse.core.runtime.IStatus; 18 import org.eclipse.core.runtime.Status; 19 import org.eclipse.jface.util.Policy; 20 import org.eclipse.swt.SWTException; 21 import org.eclipse.swt.graphics.Color; 22 import org.eclipse.swt.graphics.Device; 23 import org.eclipse.swt.graphics.Font; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.graphics.RGB; 26 27 42 public abstract class ResourceManager { 43 44 48 private List disposeExecs = null; 49 50 57 public abstract Device getDevice(); 58 59 87 public abstract Object create(DeviceResourceDescriptor descriptor); 88 89 99 public abstract void destroy(DeviceResourceDescriptor descriptor); 100 101 139 public final Object get(DeviceResourceDescriptor descriptor) { 140 Object result = find(descriptor); 141 142 if (result == null) { 143 result = create(descriptor); 144 } 145 146 return result; 147 } 148 149 168 public final Image createImage(ImageDescriptor descriptor) { 169 Assert.isNotNull(descriptor); 171 172 return (Image)create(descriptor); 173 } 174 175 186 public final Image createImageWithDefault(ImageDescriptor descriptor) { 187 if (descriptor == null) { 188 return getDefaultImage(); 189 } 190 191 try { 192 return (Image) create(descriptor); 193 } catch (DeviceResourceException e) { 194 Policy.getLog().log( 195 new Status(IStatus.WARNING, "org.eclipse.jface", 0, "The image could not be loaded: " + descriptor, e)); 198 return getDefaultImage(); 199 } catch (SWTException e) { 200 Policy.getLog().log( 201 new Status(IStatus.WARNING, "org.eclipse.jface", 0, "The image could not be loaded: " + descriptor, e)); 204 return getDefaultImage(); 205 } 206 } 207 208 217 protected abstract Image getDefaultImage(); 218 219 226 public final void destroyImage(ImageDescriptor descriptor) { 227 destroy(descriptor); 228 } 229 230 242 public final Color createColor(ColorDescriptor descriptor) { 243 return (Color)create(descriptor); 244 } 245 246 258 public final Color createColor(RGB descriptor) { 259 return createColor(new RGBColorDescriptor(descriptor)); 260 } 261 262 269 public final void destroyColor(RGB descriptor) { 270 destroyColor(new RGBColorDescriptor(descriptor)); 271 } 272 273 281 public final void destroyColor(ColorDescriptor descriptor) { 282 destroy(descriptor); 283 } 284 285 297 public final Font createFont(FontDescriptor descriptor) { 298 return (Font)create(descriptor); 299 } 300 301 308 public final void destroyFont(FontDescriptor descriptor) { 309 destroy(descriptor); 310 } 311 312 315 public void dispose() { 316 if (disposeExecs == null) { 317 return; 318 } 319 320 RuntimeException foundException = null; 325 326 Runnable [] execs = (Runnable []) disposeExecs.toArray(new Runnable [disposeExecs.size()]); 327 for (int i = 0; i < execs.length; i++) { 328 Runnable exec = execs[i]; 329 330 try { 331 exec.run(); 332 } catch (RuntimeException e) { 333 foundException = e; 336 } 337 } 338 339 if (foundException != null) { 340 throw foundException; 342 } 343 } 344 345 354 public abstract Object find(DeviceResourceDescriptor descriptor); 355 356 363 public void disposeExec(Runnable r) { 364 Assert.isNotNull(r); 365 366 if (disposeExecs == null) { 367 disposeExecs = new ArrayList (); 368 } 369 370 disposeExecs.add(r); 371 } 372 373 380 public void cancelDisposeExec(Runnable r) { 381 Assert.isNotNull(r); 382 383 if (disposeExecs == null) { 384 return; 385 } 386 387 disposeExecs.remove(r); 388 389 if (disposeExecs.isEmpty()) { 390 disposeExecs = null; 391 } 392 } 393 } 394 | Popular Tags |