Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
4 | * | |
73005152 BH |
5 | * All rights reserved. This program and the accompanying materials |
6 | * are made available under the terms of the Eclipse Public License v1.0 | |
7 | * which accompanies this distribution, and is available at | |
8 | * http://www.eclipse.org/legal/epl-v10.html | |
73005152 BH |
9 | * |
10 | * Contributors: | |
11 | * IBM - Initial API and implementation | |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.impl; | |
15 | ||
e6ace8bb | 16 | import java.net.MalformedURLException; |
73005152 BH |
17 | import java.net.URL; |
18 | ||
19 | import org.eclipse.jface.resource.ImageDescriptor; | |
8fd82db5 | 20 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
73005152 BH |
21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage; |
22 | import org.eclipse.swt.graphics.Image; | |
23 | ||
24 | /** | |
df0b8ff4 BH |
25 | * Default implementation of the IImage interface. |
26 | * | |
27 | * @version 1.0 | |
73005152 BH |
28 | * @author sveyrier |
29 | * | |
30 | */ | |
31 | public class ImageImpl implements IImage { | |
32 | ||
df0b8ff4 BH |
33 | // ------------------------------------------------------------------------ |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
eb63f5ff BH |
37 | /** |
38 | * The image reference | |
39 | */ | |
40 | protected Image fImage = null; | |
73005152 | 41 | |
df0b8ff4 BH |
42 | // ------------------------------------------------------------------------ |
43 | // Constructors | |
44 | // ------------------------------------------------------------------------ | |
45 | /** | |
46 | * Default constructor. | |
47 | * | |
48 | * @param file A file name of image file. | |
49 | */ | |
73005152 | 50 | public ImageImpl(String file) { |
eb63f5ff | 51 | fImage = createResourceImage(file); |
73005152 BH |
52 | } |
53 | ||
df0b8ff4 BH |
54 | /** |
55 | * Copy constructor | |
56 | * | |
eb63f5ff | 57 | * @param image THe image to copy |
df0b8ff4 | 58 | */ |
eb63f5ff BH |
59 | public ImageImpl(Image image) { |
60 | fImage = image; | |
73005152 | 61 | } |
df0b8ff4 BH |
62 | |
63 | // ------------------------------------------------------------------------ | |
64 | // Methods | |
65 | // ------------------------------------------------------------------------ | |
66 | /** | |
67 | * Returns Image object from file name. | |
68 | * | |
eb63f5ff | 69 | * @param name File name of image file |
df0b8ff4 BH |
70 | * @return image object or <code>null</code> |
71 | */ | |
eb63f5ff BH |
72 | public Image getResourceImage(String name) { |
73 | return createResourceImage(name); | |
73005152 BH |
74 | } |
75 | ||
df0b8ff4 BH |
76 | /* |
77 | * (non-Javadoc) | |
78 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage#getImage() | |
79 | */ | |
73005152 BH |
80 | @Override |
81 | public Object getImage() { | |
eb63f5ff | 82 | return fImage; |
73005152 BH |
83 | } |
84 | ||
df0b8ff4 BH |
85 | /* |
86 | * (non-Javadoc) | |
87 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage#dispose() | |
88 | */ | |
73005152 BH |
89 | @Override |
90 | public void dispose() { | |
eb63f5ff BH |
91 | if (fImage != null) { |
92 | fImage.dispose(); | |
93 | } | |
94 | } | |
95 | ||
96 | /** | |
97 | * Returns Image object from file name. | |
98 | * | |
99 | * @param name File name of image file | |
100 | * @return image object or <code>null</code> | |
101 | */ | |
102 | private Image createResourceImage(String name) { | |
103 | try { | |
104 | URL BASIC_URL = new URL("platform", "localhost", "plugin");//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ | |
105 | URL url = new URL(BASIC_URL, "plugin/org.eclipse.linuxtools.tmf.ui/icons/" + name);//$NON-NLS-1$ | |
106 | ImageDescriptor img = ImageDescriptor.createFromURL(url); | |
107 | return img.createImage(); | |
108 | } catch (MalformedURLException e) { | |
8fd82db5 | 109 | Activator.getDefault().logError("Error opening image file", e); //$NON-NLS-1$ |
df0b8ff4 | 110 | } |
eb63f5ff | 111 | return null; |
73005152 | 112 | } |
eb63f5ff | 113 | |
73005152 | 114 | } |