KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > HelpUIResources


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.help.ui.internal;
12 import java.net.URL JavaDoc;
13
14 import org.eclipse.core.runtime.FileLocator;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.core.runtime.Platform;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.jface.resource.ImageRegistry;
20 import org.eclipse.swt.graphics.Image;
21 import org.osgi.framework.Bundle;
22 /**
23  * Uses a resource bundle to load images and strings from a property file. This
24  * class needs to properly use the desired locale.
25  */

26 public class HelpUIResources {
27     /**
28      * WorkbenchResources constructor comment.
29      */

30     public HelpUIResources() {
31         super();
32     }
33     /**
34      * Returns a string from a property file
35      */

36     public static URL JavaDoc getImagePath(String JavaDoc name) {
37         IPath path = new Path("$nl$/icons/").append(name); //$NON-NLS-1$
38
return FileLocator.find(HelpUIPlugin.getDefault().getBundle(), path, null);
39     }
40     
41     /**
42      * Returns an image descriptor from a property file
43      * @param name simple image file name
44      * @return the descriptor
45      */

46
47     public static ImageDescriptor getImageDescriptor(String JavaDoc name) {
48         URL JavaDoc imagePath = getImagePath(name);
49         ImageRegistry registry = HelpUIPlugin.getDefault().getImageRegistry();
50         ImageDescriptor desc = registry.getDescriptor(name);
51         if (desc==null) {
52             desc = ImageDescriptor.createFromURL(imagePath);
53             registry.put(name, desc);
54         }
55         return desc;
56     }
57     
58     public static ImageDescriptor getImageDescriptor(String JavaDoc bundleId, String JavaDoc name) {
59         ImageRegistry registry = HelpUIPlugin.getDefault().getImageRegistry();
60         ImageDescriptor desc = registry.getDescriptor(name);
61         if (desc==null) {
62             Bundle bundle = Platform.getBundle(bundleId);
63             if (bundle==null) return null;
64             URL JavaDoc url = FileLocator.find(bundle, new Path(name), null);
65             desc = ImageDescriptor.createFromURL(url);
66             registry.put(name, desc);
67         }
68         return desc;
69     }
70     
71     /**
72      * Returns an image from a property file
73      * @param name simple image file name
74      * @return the new image or <code>null</code> if image
75      * could not be created
76      */

77
78     public static Image getImage(String JavaDoc name) {
79         ImageRegistry registry = HelpUIPlugin.getDefault().getImageRegistry();
80         //Ensure we have the descriptor
81
getImageDescriptor(name);
82         return registry.get(name);
83     }
84     
85     public static Image getImage(URL JavaDoc url) {
86         ImageRegistry registry = HelpUIPlugin.getDefault().getImageRegistry();
87         String JavaDoc name = url.toString();
88         ImageDescriptor desc = registry.getDescriptor(name);
89         if (desc==null) {
90             desc = ImageDescriptor.createFromURL(url);
91             registry.put(name, desc);
92         }
93         return registry.get(name);
94     }
95 }
96
Popular Tags