Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5eede25823c4b73332211d9e953db3849e0c4f29 (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
/*****************************************************************************
 * Copyright (c) 2010 ATOS ORIGIN.
 *
 * 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:
 *  Tristan Faure (ATOS ORIGIN INTEGRATION) tristan.faure@atosorigin.com - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.toolbox.utils;

import java.io.IOException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.papyrus.infra.widgets.toolbox.Activator;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.widgets.Display;


/**
 * A class retrieving the icons used in papyrus toolbox
 *
 * @author tfaure
 *
 */
public class ToolbooxImageUtils {

	/**
	 * Returns an image according to {@link ISharedImages}
	 *
	 * @param id
	 *            , the constant
	 * @return
	 */
	public static Image getImage(int id) {
		StringBuffer path = new StringBuffer("/icons/");
		switch (id) {
		case ISharedImages.IMG_RUN:
			path = path.append("run.gif");
			break;
		default:
			break;
		}
		String key = Activator.PLUGIN_ID + path;
		Image result = JFaceResources.getImageRegistry().get(key);
		if (result == null) {
			URL url = Activator.getDefault().getBundle().getEntry(path.toString());
			try {
				result = new Image(Display.getDefault(), url.openStream());
				JFaceResources.getImageRegistry().put(key, result);
			} catch (IOException e) {
			}
		}
		return result;
	}

	/**
	 * Returns an image descriptor according to {@link ISharedImages}
	 *
	 * @param id
	 *            , the constant
	 * @return
	 */
	public static ImageDescriptor getImageDescriptor(final int id) {
		return new ImageDescriptor() {

			@Override
			public ImageData getImageData() {
				return getImage(id).getImageData();
			}
		};
	}
}

Back to the top