KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jface > resource > JFaceResources


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jface.resource;
12
13 import java.net.URL JavaDoc;
14 import java.text.MessageFormat JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Map JavaDoc;
17 import java.util.MissingResourceException JavaDoc;
18 import java.util.ResourceBundle JavaDoc;
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 /**
34  * Utility methods to access JFace-specific resources.
35  * <p>
36  * All methods declared on this class are static. This class cannot be
37  * instantiated.
38  * </p>
39  * <p>
40  * The following global state is also maintained by this class:
41  * <ul>
42  * <li>a font registry</li>
43  * <li>a color registry</li>
44  * <li>an image registry</li>
45  * <li>a resource bundle</li>
46  * </ul>
47  * </p>
48  */

49 public class JFaceResources {
50
51     /**
52      * The path to the icons in the resources.
53      */

54     private final static String JavaDoc ICONS_PATH = "$nl$/icons/full/";//$NON-NLS-1$
55

56     /**
57      * Map of Display onto DeviceResourceManager. Holds all the resources for
58      * the associated display.
59      */

60     private static final Map JavaDoc registries = new HashMap JavaDoc();
61
62     /**
63      * The symbolic font name for the banner font (value
64      * <code>"org.eclipse.jface.bannerfont"</code>).
65      */

66     public static final String JavaDoc BANNER_FONT = "org.eclipse.jface.bannerfont"; //$NON-NLS-1$
67

68     /**
69      * The JFace resource bundle; eagerly initialized.
70      */

71     private static final ResourceBundle JavaDoc bundle = ResourceBundle
72             .getBundle("org.eclipse.jface.messages"); //$NON-NLS-1$
73

74     /**
75      * The JFace color registry; <code>null</code> until lazily initialized or
76      * explicitly set.
77      */

78     private static ColorRegistry colorRegistry;
79
80     /**
81      * The symbolic font name for the standard font (value
82      * <code>"org.eclipse.jface.defaultfont"</code>).
83      */

84     public static final String JavaDoc DEFAULT_FONT = "org.eclipse.jface.defaultfont"; //$NON-NLS-1$
85

86     /**
87      * The symbolic font name for the dialog font (value
88      * <code>"org.eclipse.jface.dialogfont"</code>).
89      */

90     public static final String JavaDoc DIALOG_FONT = "org.eclipse.jface.dialogfont"; //$NON-NLS-1$
91

92     /**
93      * The JFace font registry; <code>null</code> until lazily initialized or
94      * explicitly set.
95      */

96     private static FontRegistry fontRegistry = null;
97
98     /**
99      * The symbolic font name for the header font (value
100      * <code>"org.eclipse.jface.headerfont"</code>).
101      */

102     public static final String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc format(String JavaDoc key, Object JavaDoc[] 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 JavaDoc 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(toQuery);
201             reg = mgr;
202             registries.put(toQuery, reg);
203             toQuery.disposeExec(new Runnable JavaDoc() {
204                 /*
205                  * (non-Javadoc)
206                  *
207                  * @see java.lang.Runnable#run()
208                  */

209                 public void run() {
210                     mgr.dispose();
211                     registries.remove(toQuery);
212                 }
213             });
214         }
215
216         return reg;
217     }
218
219     /**
220      * Returns the ResourceManager for the current display. May only be called
221      * from a UI thread.
222      *
223      * @since 3.1
224      *
225      * @return the global ResourceManager for the current display
226      */

227     public static ResourceManager getResources() {
228         return getResources(Display.getCurrent());
229     }
230
231     /**
232      * Returns JFace's standard font. Convenience method equivalent to
233      *
234      * <pre>
235      * JFaceResources.getFontRegistry().get(JFaceResources.DEFAULT_FONT)
236      * </pre>
237      *
238      * @return the font
239      */

240     public static Font getDefaultFont() {
241         return getFontRegistry().defaultFont();
242     }
243
244     /**
245      * Returns the descriptor for JFace's standard font. Convenience method
246      * equivalent to
247      *
248      * <pre>
249      * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.DEFAULT_FONT)
250      * </pre>
251      *
252      * @return the font
253      * @since 3.3
254      */

255     public static FontDescriptor getDefaultFontDescriptor() {
256         return getFontRegistry().defaultFontDescriptor();
257     }
258
259     /**
260      * Returns the JFace's dialog font. Convenience method equivalent to
261      *
262      * <pre>
263      * JFaceResources.getFontRegistry().get(JFaceResources.DIALOG_FONT)
264      * </pre>
265      *
266      * @return the font
267      */

268     public static Font getDialogFont() {
269         return getFontRegistry().get(DIALOG_FONT);
270     }
271
272     /**
273      * Returns the descriptor for JFace's dialog font. Convenience method
274      * equivalent to
275      *
276      * <pre>
277      * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.DIALOG_FONT)
278      * </pre>
279      *
280      * @return the font
281      * @since 3.3
282      */

283     public static FontDescriptor getDialogFontDescriptor() {
284         return getFontRegistry().getDescriptor(DIALOG_FONT);
285     }
286
287     /**
288      * Returns the font in JFace's font registry with the given symbolic font
289      * name. Convenience method equivalent to
290      *
291      * <pre>
292      * JFaceResources.getFontRegistry().get(symbolicName)
293      * </pre>
294      *
295      * If an error occurs, return the default font.
296      *
297      * @param symbolicName
298      * the symbolic font name
299      * @return the font
300      */

301     public static Font getFont(String JavaDoc symbolicName) {
302         return getFontRegistry().get(symbolicName);
303     }
304
305     /**
306      * Returns the font descriptor for in JFace's font registry with the given
307      * symbolic name. Convenience method equivalent to
308      *
309      * <pre>
310      * JFaceResources.getFontRegistry().getDescriptor(symbolicName)
311      * </pre>
312      *
313      * If an error occurs, return the default font.
314      *
315      * @param symbolicName
316      * the symbolic font name
317      * @return the font descriptor (never null)
318      * @since 3.3
319      */

320     public static FontDescriptor getFontDescriptor(String JavaDoc symbolicName) {
321         return getFontRegistry().getDescriptor(symbolicName);
322     }
323
324     /**
325      * Returns the font registry for JFace itself. If the value has not been
326      * established by an earlier call to <code>setFontRegistry</code>, is it
327      * initialized to
328      * <code>new FontRegistry("org.eclipse.jface.resource.jfacefonts")</code>.
329      * <p>
330      * Note that several static convenience methods are also provided on this
331      * class for directly accessing JFace's standard fonts.
332      * </p>
333      *
334      * @return the JFace font registry
335      */

336     public static FontRegistry getFontRegistry() {
337         if (fontRegistry == null) {
338             fontRegistry = new FontRegistry(
339                     "org.eclipse.jface.resource.jfacefonts"); //$NON-NLS-1$
340
}
341         return fontRegistry;
342     }
343
344     /**
345      * Returns the JFace's header font. Convenience method equivalent to
346      *
347      * <pre>
348      * JFaceResources.getFontRegistry().get(JFaceResources.HEADER_FONT)
349      * </pre>
350      *
351      * @return the font
352      */

353     public static Font getHeaderFont() {
354         return getFontRegistry().get(HEADER_FONT);
355     }
356
357     /**
358      * Returns the descriptor for JFace's header font. Convenience method
359      * equivalent to
360      *
361      * <pre>
362      * JFaceResources.getFontRegistry().get(JFaceResources.HEADER_FONT)
363      * </pre>
364      *
365      * @return the font descriptor (never null)
366      * @since 3.3
367      */

368     public static FontDescriptor getHeaderFontDescriptor() {
369         return getFontRegistry().getDescriptor(HEADER_FONT);
370     }
371
372     /**
373      * Returns the image in JFace's image registry with the given key, or
374      * <code>null</code> if none. Convenience method equivalent to
375      *
376      * <pre>
377      * JFaceResources.getImageRegistry().get(key)
378      * </pre>
379      *
380      * @param key
381      * the key
382      * @return the image, or <code>null</code> if none
383      */

384     public static Image getImage(String JavaDoc key) {
385         return getImageRegistry().get(key);
386     }
387
388     /**
389      * Returns the image registry for JFace itself.
390      * <p>
391      * Note that the static convenience method <code>getImage</code> is also
392      * provided on this class.
393      * </p>
394      *
395      * @return the JFace image registry
396      */

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     /**
407      * Initialize default images in JFace's image registry.
408      *
409      */

410     private static void initializeDefaultImages() {
411
412         Object JavaDoc bundle = null;
413         try {
414             bundle = JFaceActivator.getBundle();
415         } catch (NoClassDefFoundError JavaDoc exception) {
416             // Test to see if OSGI is present
417
}
418         declareImage(bundle, Wizard.DEFAULT_IMAGE, ICONS_PATH + "page.gif", //$NON-NLS-1$
419
Wizard.class, "images/page.gif"); //$NON-NLS-1$
420

421         // register default images for dialogs
422
declareImage(bundle, Dialog.DLG_IMG_MESSAGE_INFO, ICONS_PATH
423                 + "message_info.gif", Dialog.class, "images/message_info.gif"); //$NON-NLS-1$ //$NON-NLS-2$
424
declareImage(bundle, Dialog.DLG_IMG_MESSAGE_WARNING, ICONS_PATH
425                 + "message_warning.gif", Dialog.class, //$NON-NLS-1$
426
"images/message_warning.gif"); //$NON-NLS-1$
427
declareImage(bundle, Dialog.DLG_IMG_MESSAGE_ERROR, ICONS_PATH
428                 + "message_error.gif", Dialog.class, "images/message_error.gif");//$NON-NLS-1$ //$NON-NLS-2$
429
declareImage(bundle, Dialog.DLG_IMG_HELP, ICONS_PATH
430                 + "help.gif", Dialog.class, "images/help.gif");//$NON-NLS-1$ //$NON-NLS-2$
431
declareImage(
432                 bundle,
433                 TitleAreaDialog.DLG_IMG_TITLE_BANNER,
434                 ICONS_PATH + "title_banner.png", TitleAreaDialog.class, "images/title_banner.gif");//$NON-NLS-1$ //$NON-NLS-2$
435
declareImage(
436                 bundle,
437                 PreferenceDialog.PREF_DLG_TITLE_IMG,
438                 ICONS_PATH + "pref_dialog_title.gif", PreferenceDialog.class, "images/pref_dialog_title.gif");//$NON-NLS-1$ //$NON-NLS-2$
439

440     }
441
442     /**
443      * Declares a JFace image given the path of the image file (relative to the
444      * JFace plug-in). This is a helper method that creates the image descriptor
445      * and passes it to the main <code>declareImage</code> method.
446      *
447      * @param bundle
448      * the {@link Bundle} or <code>null</code> of the Bundle cannot
449      * be found
450      * @param key
451      * the symbolic name of the image
452      * @param path
453      * the path of the image file relative to the base of the
454      * workbench plug-ins install directory
455      * @param fallback
456      * the {@link Class} where the fallback implementation of the
457      * image is relative to
458      * @param fallbackPath
459      * the path relative to the fallback {@link Class}
460      *
461      */

462     private static final void declareImage(Object JavaDoc bundle, String JavaDoc key,
463             String JavaDoc path, Class JavaDoc fallback, String JavaDoc fallbackPath) {
464         
465
466         ImageDescriptor descriptor = null;
467
468         if (bundle != null) {
469             URL JavaDoc url = FileLocator.find((Bundle JavaDoc) bundle, new Path(path), null);
470             if (url != null)
471                 descriptor = ImageDescriptor.createFromURL(url);
472         }
473
474         // If we failed then load from the backup file
475
if (descriptor == null)
476             descriptor = ImageDescriptor.createFromFile(fallback, fallbackPath);
477
478         imageRegistry.put(key, descriptor);
479     }
480
481     /**
482      * Returns the resource object with the given key in JFace's resource
483      * bundle. If there isn't any value under the given key, the key is
484      * returned.
485      *
486      * @param key
487      * the resource name
488      * @return the string
489      */

490     public static String JavaDoc getString(String JavaDoc key) {
491         try {
492             return bundle.getString(key);
493         } catch (MissingResourceException JavaDoc e) {
494             return key;
495         }
496     }
497
498     /**
499      * Returns a list of string values corresponding to the given list of keys.
500      * The lookup is done with <code>getString</code>. The values are in the
501      * same order as the keys.
502      *
503      * @param keys
504      * a list of keys
505      * @return a list of corresponding string values
506      */

507     public static String JavaDoc[] getStrings(String JavaDoc[] keys) {
508         Assert.isNotNull(keys);
509         int length = keys.length;
510         String JavaDoc[] result = new String JavaDoc[length];
511         for (int i = 0; i < length; i++) {
512             result[i] = getString(keys[i]);
513         }
514         return result;
515     }
516
517     /**
518      * Returns JFace's text font. Convenience method equivalent to
519      *
520      * <pre>
521      * JFaceResources.getFontRegistry().get(JFaceResources.TEXT_FONT)
522      * </pre>
523      *
524      * @return the font
525      */

526     public static Font getTextFont() {
527         return getFontRegistry().get(TEXT_FONT);
528     }
529
530     /**
531      * Returns the descriptor for JFace's text font. Convenience method
532      * equivalent to
533      *
534      * <pre>
535      * JFaceResources.getFontRegistry().getDescriptor(JFaceResources.TEXT_FONT)
536      * </pre>
537      *
538      * @return the font descriptor (never null)
539      * @since 3.3
540      */

541     public static FontDescriptor getTextFontDescriptor() {
542         return getFontRegistry().getDescriptor(TEXT_FONT);
543     }
544
545     /**
546      * Returns JFace's viewer font. Convenience method equivalent to
547      *
548      * <pre>
549      * JFaceResources.getFontRegistry().get(JFaceResources.VIEWER_FONT)
550      * </pre>
551      *
552      * @return the font
553      * @deprecated This font is not in use
554      */

555     public static Font getViewerFont() {
556         return getFontRegistry().get(VIEWER_FONT);
557     }
558
559     /**
560      * Sets JFace's font registry to the given value. This method may only be
561      * called once; the call must occur before
562      * <code>JFaceResources.getFontRegistry</code> is invoked (either directly
563      * or indirectly).
564      *
565      * @param registry
566      * a font registry
567      */

568     public static void setFontRegistry(FontRegistry registry) {
569         Assert.isTrue(fontRegistry == null,
570                 "Font registry can only be set once."); //$NON-NLS-1$
571
fontRegistry = registry;
572     }
573
574     /*
575      * (non-Javadoc) Declare a private constructor to block instantiation.
576      */

577     private JFaceResources() {
578         // no-op
579
}
580 }
581
Popular Tags