1 11 package org.eclipse.jface.resource; 12 13 import java.net.URL ; 14 import java.text.MessageFormat ; 15 import java.util.HashMap ; 16 import java.util.Map ; 17 import java.util.MissingResourceException ; 18 import java.util.ResourceBundle ; 19 20 import org.eclipse.core.runtime.Assert; 21 import org.eclipse.core.runtime.FileLocator; 22 import org.eclipse.core.runtime.Path; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.TitleAreaDialog; 25 import org.eclipse.jface.internal.JFaceActivator; 26 import org.eclipse.jface.preference.PreferenceDialog; 27 import org.eclipse.jface.wizard.Wizard; 28 import org.eclipse.swt.graphics.Font; 29 import org.eclipse.swt.graphics.Image; 30 import org.eclipse.swt.widgets.Display; 31 import org.osgi.framework.Bundle; 32 33 49 public class JFaceResources { 50 51 54 private final static String ICONS_PATH = "$nl$/icons/full/"; 56 60 private static final Map registries = new HashMap (); 61 62 66 public static final String BANNER_FONT = "org.eclipse.jface.bannerfont"; 68 71 private static final ResourceBundle bundle = ResourceBundle 72 .getBundle("org.eclipse.jface.messages"); 74 78 private static ColorRegistry colorRegistry; 79 80 84 public static final String DEFAULT_FONT = "org.eclipse.jface.defaultfont"; 86 90 public static final String DIALOG_FONT = "org.eclipse.jface.dialogfont"; 92 96 private static FontRegistry fontRegistry = null; 97 98 102 public static final String HEADER_FONT = "org.eclipse.jface.headerfont"; 104 107 private static ImageRegistry imageRegistry = null; 108 109 113 public static final String TEXT_FONT = "org.eclipse.jface.textfont"; 115 121 public static final String VIEWER_FONT = "org.eclipse.jface.viewerfont"; 123 129 public static final String WINDOW_FONT = "org.eclipse.jface.windowfont"; 131 141 public static String format(String key, Object [] args) { 142 return MessageFormat.format(getString(key), args); 143 } 144 145 154 public static Font getBannerFont() { 155 return getFontRegistry().get(BANNER_FONT); 156 } 157 158 169 public static ResourceBundle getBundle() { 170 return bundle; 171 } 172 173 180 public static ColorRegistry getColorRegistry() { 181 if (colorRegistry == null) { 182 colorRegistry = new ColorRegistry(); 183 } 184 return colorRegistry; 185 } 186 187 196 public static ResourceManager getResources(final Display toQuery) { 197 ResourceManager reg = (ResourceManager) registries.get(toQuery); 198 199 if (reg == null) { 200 final DeviceResourceManager mgr = new DeviceResourceManager(toQuery); 201 reg = mgr; 202 registries.put(toQuery, reg); 203 toQuery.disposeExec(new Runnable () { 204 209 public void run() { 210 mgr.dispose(); 211 registries.remove(toQuery); 212 } 213 }); 214 } 215 216 return reg; 217 } 218 219 227 public static ResourceManager getResources() { 228 return getResources(Display.getCurrent()); 229 } 230 231 240 public static Font getDefaultFont() { 241 return getFontRegistry().defaultFont(); 242 } 243 244 255 public static FontDescriptor getDefaultFontDescriptor() { 256 return getFontRegistry().defaultFontDescriptor(); 257 } 258 259 268 public static Font getDialogFont() { 269 return getFontRegistry().get(DIALOG_FONT); 270 } 271 272 283 public static FontDescriptor getDialogFontDescriptor() { 284 return getFontRegistry().getDescriptor(DIALOG_FONT); 285 } 286 287 301 public static Font getFont(String symbolicName) { 302 return getFontRegistry().get(symbolicName); 303 } 304 305 320 public static FontDescriptor getFontDescriptor(String symbolicName) { 321 return getFontRegistry().getDescriptor(symbolicName); 322 } 323 324 336 public static FontRegistry getFontRegistry() { 337 if (fontRegistry == null) { 338 fontRegistry = new FontRegistry( 339 "org.eclipse.jface.resource.jfacefonts"); } 341 return fontRegistry; 342 } 343 344 353 public static Font getHeaderFont() { 354 return getFontRegistry().get(HEADER_FONT); 355 } 356 357 368 public static FontDescriptor getHeaderFontDescriptor() { 369 return getFontRegistry().getDescriptor(HEADER_FONT); 370 } 371 372 384 public static Image getImage(String key) { 385 return getImageRegistry().get(key); 386 } 387 388 397 public static ImageRegistry getImageRegistry() { 398 if (imageRegistry == null) { 399 imageRegistry = new ImageRegistry( 400 getResources(Display.getCurrent())); 401 initializeDefaultImages(); 402 } 403 return imageRegistry; 404 } 405 406 410 private static void initializeDefaultImages() { 411 412 Object bundle = null; 413 try { 414 bundle = JFaceActivator.getBundle(); 415 } catch (NoClassDefFoundError exception) { 416 } 418 declareImage(bundle, Wizard.DEFAULT_IMAGE, ICONS_PATH + "page.gif", Wizard.class, "images/page.gif"); 421 declareImage(bundle, Dialog.DLG_IMG_MESSAGE_INFO, ICONS_PATH 423 + "message_info.gif", Dialog.class, "images/message_info.gif"); declareImage(bundle, Dialog.DLG_IMG_MESSAGE_WARNING, ICONS_PATH 425 + "message_warning.gif", Dialog.class, "images/message_warning.gif"); declareImage(bundle, Dialog.DLG_IMG_MESSAGE_ERROR, ICONS_PATH 428 + "message_error.gif", Dialog.class, "images/message_error.gif"); declareImage(bundle, Dialog.DLG_IMG_HELP, ICONS_PATH 430 + "help.gif", Dialog.class, "images/help.gif"); declareImage( 432 bundle, 433 TitleAreaDialog.DLG_IMG_TITLE_BANNER, 434 ICONS_PATH + "title_banner.png", TitleAreaDialog.class, "images/title_banner.gif"); declareImage( 436 bundle, 437 PreferenceDialog.PREF_DLG_TITLE_IMG, 438 ICONS_PATH + "pref_dialog_title.gif", PreferenceDialog.class, "images/pref_dialog_title.gif"); 440 } 441 442 462 private static final void declareImage(Object bundle, String key, 463 String path, Class fallback, String fallbackPath) { 464 465 466 ImageDescriptor descriptor = null; 467 468 if (bundle != null) { 469 URL url = FileLocator.find((Bundle ) bundle, new Path(path), null); 470 if (url != null) 471 descriptor = ImageDescriptor.createFromURL(url); 472 } 473 474 if (descriptor == null) 476 descriptor = ImageDescriptor.createFromFile(fallback, fallbackPath); 477 478 imageRegistry.put(key, descriptor); 479 } 480 481 490 public static String getString(String key) { 491 try { 492 return bundle.getString(key); 493 } catch (MissingResourceException e) { 494 return key; 495 } 496 } 497 498 507 public static String [] getStrings(String [] keys) { 508 Assert.isNotNull(keys); 509 int length = keys.length; 510 String [] result = new String [length]; 511 for (int i = 0; i < length; i++) { 512 result[i] = getString(keys[i]); 513 } 514 return result; 515 } 516 517 526 public static Font getTextFont() { 527 return getFontRegistry().get(TEXT_FONT); 528 } 529 530 541 public static FontDescriptor getTextFontDescriptor() { 542 return getFontRegistry().getDescriptor(TEXT_FONT); 543 } 544 545 555 public static Font getViewerFont() { 556 return getFontRegistry().get(VIEWER_FONT); 557 } 558 559 568 public static void setFontRegistry(FontRegistry registry) { 569 Assert.isTrue(fontRegistry == null, 570 "Font registry can only be set once."); fontRegistry = registry; 572 } 573 574 577 private JFaceResources() { 578 } 580 } 581 | Popular Tags |