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