001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jface.resource;
011:
012: import java.net.URL;
013: import java.text.MessageFormat;
014: import java.util.HashMap;
015: import java.util.Map;
016: import java.util.MissingResourceException;
017: import java.util.ResourceBundle;
018:
019: import org.eclipse.core.runtime.Assert;
020: import org.eclipse.core.runtime.FileLocator;
021: import org.eclipse.core.runtime.Path;
022: import org.eclipse.jface.dialogs.Dialog;
023: import org.eclipse.jface.dialogs.PopupDialog;
024: import org.eclipse.jface.dialogs.TitleAreaDialog;
025: import org.eclipse.jface.internal.JFaceActivator;
026: import org.eclipse.jface.preference.PreferenceDialog;
027: import org.eclipse.jface.wizard.Wizard;
028: import org.eclipse.swt.graphics.Font;
029: import org.eclipse.swt.graphics.Image;
030: import org.eclipse.swt.widgets.Display;
031: import org.osgi.framework.Bundle;
032:
033: /**
034: * Utility methods to access JFace-specific resources.
035: * <p>
036: * All methods declared on this class are static. This class cannot be
037: * instantiated.
038: * </p>
039: * <p>
040: * The following global state is also maintained by this class:
041: * <ul>
042: * <li>a font registry</li>
043: * <li>a color registry</li>
044: * <li>an image registry</li>
045: * <li>a resource bundle</li>
046: * </ul>
047: * </p>
048: */
049: public class JFaceResources {
050:
051: /**
052: * The path to the icons in the resources.
053: */
054: private final static String ICONS_PATH = "$nl$/icons/full/";//$NON-NLS-1$
055:
056: /**
057: * Map of Display onto DeviceResourceManager. Holds all the resources for
058: * the associated display.
059: */
060: private static final Map registries = new HashMap();
061:
062: /**
063: * The symbolic font name for the banner font (value
064: * <code>"org.eclipse.jface.bannerfont"</code>).
065: */
066: public static final String BANNER_FONT = "org.eclipse.jface.bannerfont"; //$NON-NLS-1$
067:
068: /**
069: * The JFace resource bundle; eagerly initialized.
070: */
071: private static final ResourceBundle bundle = ResourceBundle
072: .getBundle("org.eclipse.jface.messages"); //$NON-NLS-1$
073:
074: /**
075: * The JFace color registry; <code>null</code> until lazily initialized or
076: * explicitly set.
077: */
078: private static ColorRegistry colorRegistry;
079:
080: /**
081: * The symbolic font name for the standard font (value
082: * <code>"org.eclipse.jface.defaultfont"</code>).
083: */
084: public static final String DEFAULT_FONT = "org.eclipse.jface.defaultfont"; //$NON-NLS-1$
085:
086: /**
087: * The symbolic font name for the dialog font (value
088: * <code>"org.eclipse.jface.dialogfont"</code>).
089: */
090: public static final String DIALOG_FONT = "org.eclipse.jface.dialogfont"; //$NON-NLS-1$
091:
092: /**
093: * The JFace font registry; <code>null</code> until lazily initialized or
094: * explicitly set.
095: */
096: private static FontRegistry fontRegistry = null;
097:
098: /**
099: * The symbolic font name for the header font (value
100: * <code>"org.eclipse.jface.headerfont"</code>).
101: */
102: public static final String HEADER_FONT = "org.eclipse.jface.headerfont"; //$NON-NLS-1$
103:
104: /**
105: * The JFace image registry; <code>null</code> until lazily initialized.
106: */
107: private static ImageRegistry imageRegistry = null;
108:
109: /**
110: * The symbolic font name for the text font (value
111: * <code>"org.eclipse.jface.textfont"</code>).
112: */
113: public static final String TEXT_FONT = "org.eclipse.jface.textfont"; //$NON-NLS-1$
114:
115: /**
116: * The symbolic font name for the viewer font (value
117: * <code>"org.eclipse.jface.viewerfont"</code>).
118: *
119: * @deprecated This font is not in use
120: */
121: public static final String VIEWER_FONT = "org.eclipse.jface.viewerfont"; //$NON-NLS-1$
122:
123: /**
124: * The symbolic font name for the window font (value
125: * <code>"org.eclipse.jface.windowfont"</code>).
126: *
127: * @deprecated This font is not in use
128: */
129: public static final String WINDOW_FONT = "org.eclipse.jface.windowfont"; //$NON-NLS-1$
130:
131: /**
132: * Returns the formatted message for the given key in JFace's resource
133: * bundle.
134: *
135: * @param key
136: * the resource name
137: * @param args
138: * the message arguments
139: * @return the string
140: */
141: public static String format(String key, Object[] args) {
142: return MessageFormat.format(getString(key), args);
143: }
144:
145: /**
146: * Returns the JFace's banner font. Convenience method equivalent to
147: *
148: * <pre>
149: * JFaceResources.getFontRegistry().get(JFaceResources.BANNER_FONT)
150: * </pre>
151: *
152: * @return the font
153: */
154: public static Font getBannerFont() {
155: return getFontRegistry().get(BANNER_FONT);
156: }
157:
158: /**
159: * Returns the resource bundle for JFace itself. The resouble bundle is
160: * obtained from
161: * <code>ResourceBundle.getBundle("org.eclipse.jface.jface_nls")</code>.
162: * <p>
163: * Note that several static convenience methods are also provided on this
164: * class for directly accessing resources in this bundle.
165: * </p>
166: *
167: * @return the resource bundle
168: */
169: public static ResourceBundle getBundle() {
170: return bundle;
171: }
172:
173: /**
174: * Returns the color registry for JFace itself.
175: * <p>
176: *
177: * @return the <code>ColorRegistry</code>.
178: * @since 3.0
179: */
180: public static ColorRegistry getColorRegistry() {
181: if (colorRegistry == null) {
182: colorRegistry = new ColorRegistry();
183: }
184: return colorRegistry;
185: }
186:
187: /**
188: * Returns the global resource manager for the given display
189: *
190: * @since 3.1
191: *
192: * @param toQuery
193: * display to query
194: * @return the global resource manager for the given display
195: */
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(
201: toQuery);
202: reg = mgr;
203: registries.put(toQuery, reg);
204: toQuery.disposeExec(new Runnable() {
205: /*
206: * (non-Javadoc)
207: *
208: * @see java.lang.Runnable#run()
209: */
210: public void run() {
211: mgr.dispose();
212: registries.remove(toQuery);
213: }
214: });
215: }
216:
217: return reg;
218: }
219:
220: /**
221: * Returns the ResourceManager for the current display. May only be called
222: * from a UI thread.
223: *
224: * @since 3.1
225: *
226: * @return the global ResourceManager for the current display
227: */
228: public static ResourceManager getResources() {
229: return getResources(Display.getCurrent());
230: }
231:
232: /**
233: * Returns JFace's standard font. Convenience method equivalent to
234: *
235: * <pre>
236: * JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT)
237: * </pre>
238: *
239: * @return the font
240: */
241: public static Font getDefaultFont() {
242: return getFontRegistry().defaultFont();
243: }
244:
245: /**
246: * Returns the descriptor for JFace's standard font. Convenience method
247: * equivalent to
248: *
249: * <pre>
250: * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.DEFAULT_FONT)
251: * </pre>
252: *
253: * @return the font
254: * @since 3.3
255: */
256: public static FontDescriptor getDefaultFontDescriptor() {
257: return getFontRegistry().defaultFontDescriptor();
258: }
259:
260: /**
261: * Returns the JFace's dialog font. Convenience method equivalent to
262: *
263: * <pre>
264: * JFaceResources.getFontRegistry().get(JFaceResources.DIALOG_FONT)
265: * </pre>
266: *
267: * @return the font
268: */
269: public static Font getDialogFont() {
270: return getFontRegistry().get(DIALOG_FONT);
271: }
272:
273: /**
274: * Returns the descriptor for JFace's dialog font. Convenience method
275: * equivalent to
276: *
277: * <pre>
278: * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.DIALOG_FONT)
279: * </pre>
280: *
281: * @return the font
282: * @since 3.3
283: */
284: public static FontDescriptor getDialogFontDescriptor() {
285: return getFontRegistry().getDescriptor(DIALOG_FONT);
286: }
287:
288: /**
289: * Returns the font in JFace's font registry with the given symbolic font
290: * name. Convenience method equivalent to
291: *
292: * <pre>
293: * JFaceResources.getFontRegistry().get(symbolicName)
294: * </pre>
295: *
296: * If an error occurs, return the default font.
297: *
298: * @param symbolicName
299: * the symbolic font name
300: * @return the font
301: */
302: public static Font getFont(String symbolicName) {
303: return getFontRegistry().get(symbolicName);
304: }
305:
306: /**
307: * Returns the font descriptor for in JFace's font registry with the given
308: * symbolic name. Convenience method equivalent to
309: *
310: * <pre>
311: * JFaceResources.getFontRegistry().getDescriptor(symbolicName)
312: * </pre>
313: *
314: * If an error occurs, return the default font.
315: *
316: * @param symbolicName
317: * the symbolic font name
318: * @return the font descriptor (never null)
319: * @since 3.3
320: */
321: public static FontDescriptor getFontDescriptor(String symbolicName) {
322: return getFontRegistry().getDescriptor(symbolicName);
323: }
324:
325: /**
326: * Returns the font registry for JFace itself. If the value has not been
327: * established by an earlier call to <code>setFontRegistry</code>, is it
328: * initialized to
329: * <code>new FontRegistry("org.eclipse.jface.resource.jfacefonts")</code>.
330: * <p>
331: * Note that several static convenience methods are also provided on this
332: * class for directly accessing JFace's standard fonts.
333: * </p>
334: *
335: * @return the JFace font registry
336: */
337: public static FontRegistry getFontRegistry() {
338: if (fontRegistry == null) {
339: fontRegistry = new FontRegistry(
340: "org.eclipse.jface.resource.jfacefonts"); //$NON-NLS-1$
341: }
342: return fontRegistry;
343: }
344:
345: /**
346: * Returns the JFace's header font. Convenience method equivalent to
347: *
348: * <pre>
349: * JFaceResources.getFontRegistry().get(JFaceResources.HEADER_FONT)
350: * </pre>
351: *
352: * @return the font
353: */
354: public static Font getHeaderFont() {
355: return getFontRegistry().get(HEADER_FONT);
356: }
357:
358: /**
359: * Returns the descriptor for JFace's header font. Convenience method
360: * equivalent to
361: *
362: * <pre>
363: * JFaceResources.getFontRegistry().get(JFaceResources.HEADER_FONT)
364: * </pre>
365: *
366: * @return the font descriptor (never null)
367: * @since 3.3
368: */
369: public static FontDescriptor getHeaderFontDescriptor() {
370: return getFontRegistry().getDescriptor(HEADER_FONT);
371: }
372:
373: /**
374: * Returns the image in JFace's image registry with the given key, or
375: * <code>null</code> if none. Convenience method equivalent to
376: *
377: * <pre>
378: * JFaceResources.getImageRegistry().get(key)
379: * </pre>
380: *
381: * @param key
382: * the key
383: * @return the image, or <code>null</code> if none
384: */
385: public static Image getImage(String key) {
386: return getImageRegistry().get(key);
387: }
388:
389: /**
390: * Returns the image registry for JFace itself.
391: * <p>
392: * Note that the static convenience method <code>getImage</code> is also
393: * provided on this class.
394: * </p>
395: *
396: * @return the JFace image registry
397: */
398: public static ImageRegistry getImageRegistry() {
399: if (imageRegistry == null) {
400: imageRegistry = new ImageRegistry(getResources(Display
401: .getCurrent()));
402: initializeDefaultImages();
403: }
404: return imageRegistry;
405: }
406:
407: /**
408: * Initialize default images in JFace's image registry.
409: *
410: */
411: private static void initializeDefaultImages() {
412:
413: Object bundle = null;
414: try {
415: bundle = JFaceActivator.getBundle();
416: } catch (NoClassDefFoundError exception) {
417: // Test to see if OSGI is present
418: }
419: declareImage(bundle, Wizard.DEFAULT_IMAGE, ICONS_PATH
420: + "page.gif", //$NON-NLS-1$
421: Wizard.class, "images/page.gif"); //$NON-NLS-1$
422:
423: // register default images for dialogs
424: declareImage(
425: bundle,
426: Dialog.DLG_IMG_MESSAGE_INFO,
427: ICONS_PATH + "message_info.gif", Dialog.class, "images/message_info.gif"); //$NON-NLS-1$ //$NON-NLS-2$
428: declareImage(bundle, Dialog.DLG_IMG_MESSAGE_WARNING, ICONS_PATH
429: + "message_warning.gif", Dialog.class, //$NON-NLS-1$
430: "images/message_warning.gif"); //$NON-NLS-1$
431: declareImage(
432: bundle,
433: Dialog.DLG_IMG_MESSAGE_ERROR,
434: ICONS_PATH + "message_error.gif", Dialog.class, "images/message_error.gif");//$NON-NLS-1$ //$NON-NLS-2$
435: declareImage(bundle, Dialog.DLG_IMG_HELP, ICONS_PATH
436: + "help.gif", Dialog.class, "images/help.gif");//$NON-NLS-1$ //$NON-NLS-2$
437: declareImage(
438: bundle,
439: TitleAreaDialog.DLG_IMG_TITLE_BANNER,
440: ICONS_PATH + "title_banner.png", TitleAreaDialog.class, "images/title_banner.gif");//$NON-NLS-1$ //$NON-NLS-2$
441: declareImage(
442: bundle,
443: PreferenceDialog.PREF_DLG_TITLE_IMG,
444: ICONS_PATH + "pref_dialog_title.gif", PreferenceDialog.class, "images/pref_dialog_title.gif");//$NON-NLS-1$ //$NON-NLS-2$
445: declareImage(
446: bundle,
447: PopupDialog.POPUP_IMG_MENU,
448: ICONS_PATH + "popup_menu.gif", PopupDialog.class, "images/popup_menu.gif");//$NON-NLS-1$ //$NON-NLS-2$
449: declareImage(
450: bundle,
451: PopupDialog.POPUP_IMG_MENU_DISABLED,
452: ICONS_PATH + "popup_menu_disabled.gif", PopupDialog.class, "images/popup_menu_disabled.gif");//$NON-NLS-1$ //$NON-NLS-2$
453: }
454:
455: /**
456: * Declares a JFace image given the path of the image file (relative to the
457: * JFace plug-in). This is a helper method that creates the image descriptor
458: * and passes it to the main <code>declareImage</code> method.
459: *
460: * @param bundle
461: * the {@link Bundle} or <code>null</code> of the Bundle cannot
462: * be found
463: * @param key
464: * the symbolic name of the image
465: * @param path
466: * the path of the image file relative to the base of the
467: * workbench plug-ins install directory
468: * @param fallback
469: * the {@link Class} where the fallback implementation of the
470: * image is relative to
471: * @param fallbackPath
472: * the path relative to the fallback {@link Class}
473: *
474: */
475: private static final void declareImage(Object bundle, String key,
476: String path, Class fallback, String fallbackPath) {
477:
478: ImageDescriptor descriptor = null;
479:
480: if (bundle != null) {
481: URL url = FileLocator.find((Bundle) bundle, new Path(path),
482: null);
483: if (url != null)
484: descriptor = ImageDescriptor.createFromURL(url);
485: }
486:
487: // If we failed then load from the backup file
488: if (descriptor == null)
489: descriptor = ImageDescriptor.createFromFile(fallback,
490: fallbackPath);
491:
492: imageRegistry.put(key, descriptor);
493: }
494:
495: /**
496: * Returns the resource object with the given key in JFace's resource
497: * bundle. If there isn't any value under the given key, the key is
498: * returned.
499: *
500: * @param key
501: * the resource name
502: * @return the string
503: */
504: public static String getString(String key) {
505: try {
506: return bundle.getString(key);
507: } catch (MissingResourceException e) {
508: return key;
509: }
510: }
511:
512: /**
513: * Returns a list of string values corresponding to the given list of keys.
514: * The lookup is done with <code>getString</code>. The values are in the
515: * same order as the keys.
516: *
517: * @param keys
518: * a list of keys
519: * @return a list of corresponding string values
520: */
521: public static String[] getStrings(String[] keys) {
522: Assert.isNotNull(keys);
523: int length = keys.length;
524: String[] result = new String[length];
525: for (int i = 0; i < length; i++) {
526: result[i] = getString(keys[i]);
527: }
528: return result;
529: }
530:
531: /**
532: * Returns JFace's text font. Convenience method equivalent to
533: *
534: * <pre>
535: * JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT)
536: * </pre>
537: *
538: * @return the font
539: */
540: public static Font getTextFont() {
541: return getFontRegistry().get(TEXT_FONT);
542: }
543:
544: /**
545: * Returns the descriptor for JFace's text font. Convenience method
546: * equivalent to
547: *
548: * <pre>
549: * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.TEXT_FONT)
550: * </pre>
551: *
552: * @return the font descriptor (never null)
553: * @since 3.3
554: */
555: public static FontDescriptor getTextFontDescriptor() {
556: return getFontRegistry().getDescriptor(TEXT_FONT);
557: }
558:
559: /**
560: * Returns JFace's viewer font. Convenience method equivalent to
561: *
562: * <pre>
563: * JFaceResources.getFontRegistry().get(JFaceResources.VIEWER_FONT)
564: * </pre>
565: *
566: * @return the font
567: * @deprecated This font is not in use
568: */
569: public static Font getViewerFont() {
570: return getFontRegistry().get(VIEWER_FONT);
571: }
572:
573: /**
574: * Sets JFace's font registry to the given value. This method may only be
575: * called once; the call must occur before
576: * <code>JFaceResources.getFontRegistry</code> is invoked (either directly
577: * or indirectly).
578: *
579: * @param registry
580: * a font registry
581: */
582: public static void setFontRegistry(FontRegistry registry) {
583: Assert.isTrue(fontRegistry == null,
584: "Font registry can only be set once."); //$NON-NLS-1$
585: fontRegistry = registry;
586: }
587:
588: /*
589: * (non-Javadoc) Declare a private constructor to block instantiation.
590: */
591: private JFaceResources() {
592: // no-op
593: }
594: }
|