1
/*******************************************************************************
2
* Copyright (c) 2005 Actuate Corporation.
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
9
* Actuate Corporation - initial API and implementation
10
*******************************************************************************/
12
package org.eclipse.linuxtools.dataviewers.charts;
15
import java.io.FileInputStream;
16
import java.io.IOException;
17
import java.net.MalformedURLException;
20
import org.eclipse.core.runtime.Platform;
21
import org.eclipse.jface.resource.ImageDescriptor;
22
import org.eclipse.jface.resource.ImageRegistry;
23
import org.eclipse.jface.resource.JFaceResources;
24
import org.eclipse.swt.graphics.Image;
25
import org.eclipse.swt.widgets.Display;
28
* This class has been created to hold methods that provide specific
29
* functionality or services.
31
public final class UIHelper
35
* This method returns an URL for a resource given its plugin relative path.
36
* It is intended to be used to abstract out the usage of the UI as a plugin
37
* or standalone component when it comes to accessing resources.
39
* @param sPluginRelativePath
40
* The path to the resource relative to the plugin location.
41
* @return URL representing the location of the resource.
43
public static URL getURL( String sPluginRelativePath )
46
if ( Platform.getExtensionRegistry( ) != null )
50
url = new URL( Activator.getDefault( )
52
.getEntry( "/" ), sPluginRelativePath ); //$NON-NLS-1$
54
catch ( MalformedURLException e )
63
url = new URL( "file:///" + new File( sPluginRelativePath ).getAbsolutePath( ) ); //$NON-NLS-1$
65
catch ( MalformedURLException e )
74
private static Image createImage( String sPluginRelativePath )
81
img = new Image( Display.getCurrent( ),
82
getURL( sPluginRelativePath ).openStream( ) );
84
catch ( MalformedURLException e1 )
86
img = new Image( Display.getCurrent( ),
87
new FileInputStream( getURL( sPluginRelativePath ).toString( ) ) );
90
catch ( IOException e )
95
// If still can't load, return a dummy image.
98
img = new Image( Display.getCurrent( ), 1, 1 );
104
* This is a convenience method to get an imgIcon from a URL.
106
* @param sPluginRelativePath
107
* The URL for the imgIcon.
108
* @return The imgIcon represented by the given URL.
109
* @see #setImageCached( boolean )
111
public static Image getImage( String sPluginRelativePath )
113
ImageRegistry registry = JFaceResources.getImageRegistry( );
114
Image image = registry.get( sPluginRelativePath );
117
image = createImage( sPluginRelativePath );
118
registry.put( sPluginRelativePath, image );
124
* This is a convenience method to get an imgIcon from a URL.
126
* @param sPluginRelativePath
127
* The URL for the imgIcon.
128
* @return The imgIcon represented by the given URL.
129
* @see #setImageCached( boolean )
131
public static ImageDescriptor getImageDescriptor( String sPluginRelativePath )
133
ImageRegistry registry = JFaceResources.getImageRegistry( );
134
ImageDescriptor image = registry.getDescriptor( sPluginRelativePath );
137
registry.put( sPluginRelativePath,
138
createImage( sPluginRelativePath ) );
139
image = registry.getDescriptor( sPluginRelativePath );
b'\\ No newline at end of file'