Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 30f628ce0782745bbc90d9c0c5bc9c7bcabbb97e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*******************************************************************************
 * Copyright (c) 2004, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.impl.util;

import java.net.URL;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.internal.intro.impl.IIntroConstants;
import org.eclipse.ui.internal.intro.impl.IntroPlugin;
import org.osgi.framework.Bundle;

/**
 * Convenience class for Images.
 */
public final class ImageUtil {

    /**
     * Image keys, to be used by plugin (intro) registry.
     */
    // Default images
    public static final String DEFAULT_ROOT_LINK = "rootLink"; //$NON-NLS-1$
    public static final String DEFAULT_SMALL_ROOT_LINK = "rootLinkSmall"; //$NON-NLS-1$
    public static final String DEFAULT_FORM_BG = "formBg"; //$NON-NLS-1$
    public static final String DEFAULT_LINK = "link"; //$NON-NLS-1$

    // Standby images
    public static final String BACK = "back"; //$NON-NLS-1$
    public static final String HELP_TOPIC = "helpTopic"; //$NON-NLS-1$

    // Launch bar image
    public static final String RESTORE_WELCOME = "restoreWelcome"; //$NON-NLS-1$

    // Viewer images
    public static final String INTRO_MODEL_LEAF = "leaf"; //$NON-NLS-1$
    public static final String INTRO_MODEL_CONTAINER = "container"; //$NON-NLS-1$
    public static final String OPEN_ITNRO_VIEW = "introView"; //$NON-NLS-1$

    public static final String CONFIG_EXTENSION = "configExtension";//$NON-NLS-1$

    // Image location
    public static final String ICONS_PATH = "$nl$/icons/"; //$NON-NLS-1$

    /**
     * Convenience method to create an image descriptor from the Intro plugin.
     *
     * Method assumes that images are under the "icons" directory, so don't
     * append that directory name for "imageName".
     */
    public static ImageDescriptor createImageDescriptor(String imageName) {
        return createImageDescriptor(Platform
            .getBundle(IIntroConstants.PLUGIN_ID), ICONS_PATH + imageName);
    }

    /**
     * Convenience method to create an image descriptor.
     *
     */
    public static ImageDescriptor createImageDescriptor(Bundle bundle,
            String imageName) {
        try {
            URL imageUrl = FileLocator.find(bundle, new Path(imageName), null);
            if (imageUrl != null) {
                ImageDescriptor desc = ImageDescriptor.createFromURL(imageUrl);
                return desc;
            }
        } catch (Exception e) {
            // Should never be here.
            Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
        }
        Log.warning("could not create Image Descriptor for: " + imageName //$NON-NLS-1$
                + " in bundle: " + bundle.getSymbolicName()); //$NON-NLS-1$
        return ImageDescriptor.getMissingImageDescriptor();
    }

    /**
     * Convenience method to create an image descriptor.
     *
     */
    public static ImageDescriptor createImageDescriptor(IPath base,
            String imageName) {
        try {
            URL imageUrl = new URL(base.append(imageName).toOSString());
            if (imageUrl != null) {
                ImageDescriptor desc = ImageDescriptor.createFromURL(imageUrl);
                return desc;
            }
        } catch (Exception e) {
            // Should never be here.
            Log.error("could not create Image Descriptor", e); //$NON-NLS-1$
        }
        Log.warning("could not create Image Descriptor for: " + imageName); //$NON-NLS-1$
        return ImageDescriptor.getMissingImageDescriptor();
    }

    /**
     * Convenience method to create an image from the Intro plugin.
     *
     * Method assumes that images are under the "icons" directory, so don't
     * append that directory name for "imageName".
     */
    public static Image createImage(String imageName) {
        try {
            ImageDescriptor imageDsc = createImageDescriptor(imageName);
            return imageDsc.createImage();
        } catch (Exception e) {
            // Should never be here.
            Log.error("could not create Image", e); //$NON-NLS-1$
            return ImageDescriptor.getMissingImageDescriptor().createImage();
        }
    }

    /**
     * Util method for image re-use in Intro Plugin.
     *
     * @param key
     * @return
     */
    public static Image getImage(String key) {
        // INTRO: Image registry should not have the same life span
        // as the intro plug-in. It should be disposed when
        // presentation is disposed, otherwise images will
        // stay around once Inro has been loaded.
        return IntroPlugin.getDefault().getVolatileImageRegistry().get(key);
    }

    public static boolean hasImage(String key) {
        ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
        return (registry.getDescriptor(key) != null);
    }

    /**
     * Register an image descriptor in the Intro Plugin image registry. Has no
     * effect if the key has already been registered.
     *
     * @param key
     * @param imageName
     */
    public static void registerImage(String key, String imageName) {
        ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
        if (registry.getDescriptor(key) != null)
            // key has already been registered. do nothing.
            return;
        registry.put(key, createImageDescriptor(imageName));
    }

    public static void registerImage(String key, Bundle bundle, String imageName) {

        ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
        if (registry.getDescriptor(key) != null)
            // key has already been registered. do nothing.
            return;
        registry.put(key, createImageDescriptor(bundle, imageName));
    }

    public static void registerImage(String key, IPath base, String imageName) {
        ImageRegistry registry = IntroPlugin.getDefault().getVolatileImageRegistry();
        if (registry.getDescriptor(key) != null)
            // key has already been registered. do nothing.
            return;
        registry.put(key, createImageDescriptor(base, imageName));
    }
}

Back to the top