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