~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to profiling/org.eclipse.linuxtools.dataviewers.charts/src/org/eclipse/linuxtools/dataviewers/charts/UIHelper.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 *
 
8
 * Contributors:
 
9
 *  Actuate Corporation  - initial API and implementation
 
10
 *******************************************************************************/
 
11
 
 
12
package org.eclipse.linuxtools.dataviewers.charts;
 
13
 
 
14
import java.io.File;
 
15
import java.io.FileInputStream;
 
16
import java.io.IOException;
 
17
import java.net.MalformedURLException;
 
18
import java.net.URL;
 
19
 
 
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;
 
26
 
 
27
/**
 
28
 * This class has been created to hold methods that provide specific
 
29
 * functionality or services.
 
30
 */
 
31
public final class UIHelper
 
32
{
 
33
 
 
34
        /**
 
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.
 
38
         * 
 
39
         * @param sPluginRelativePath
 
40
         *            The path to the resource relative to the plugin location.
 
41
         * @return URL representing the location of the resource.
 
42
         */
 
43
        public static URL getURL( String sPluginRelativePath )
 
44
        {
 
45
                URL url = null;
 
46
                if ( Platform.getExtensionRegistry( ) != null )
 
47
                {
 
48
                        try
 
49
                        {
 
50
                                url = new URL( Activator.getDefault( )
 
51
                                                .getBundle( )
 
52
                                                .getEntry( "/" ), sPluginRelativePath ); //$NON-NLS-1$
 
53
                        }
 
54
                        catch ( MalformedURLException e )
 
55
                        {
 
56
                                e.printStackTrace( );
 
57
                        }
 
58
                }
 
59
                else
 
60
                {
 
61
                        try
 
62
                        {
 
63
                                url = new URL( "file:///" + new File( sPluginRelativePath ).getAbsolutePath( ) ); //$NON-NLS-1$
 
64
                        }
 
65
                        catch ( MalformedURLException e )
 
66
                        {
 
67
                                e.printStackTrace( );
 
68
                        }
 
69
                }
 
70
 
 
71
                return url;
 
72
        }
 
73
 
 
74
        private static Image createImage( String sPluginRelativePath )
 
75
        {
 
76
                Image img = null;
 
77
                try
 
78
                {
 
79
                        try
 
80
                        {
 
81
                                img = new Image( Display.getCurrent( ),
 
82
                                                getURL( sPluginRelativePath ).openStream( ) );
 
83
                        }
 
84
                        catch ( MalformedURLException e1 )
 
85
                        {
 
86
                                img = new Image( Display.getCurrent( ),
 
87
                                                new FileInputStream( getURL( sPluginRelativePath ).toString( ) ) );
 
88
                        }
 
89
                }
 
90
                catch ( IOException e )
 
91
                {
 
92
                        e.printStackTrace( );
 
93
                }
 
94
 
 
95
                // If still can't load, return a dummy image.
 
96
                if ( img == null )
 
97
                {
 
98
                        img = new Image( Display.getCurrent( ), 1, 1 );
 
99
                }
 
100
                return img;
 
101
        }
 
102
 
 
103
        /**
 
104
         * This is a convenience method to get an imgIcon from a URL.
 
105
         * 
 
106
         * @param sPluginRelativePath
 
107
         *            The URL for the imgIcon.
 
108
         * @return The imgIcon represented by the given URL.
 
109
         * @see #setImageCached( boolean )
 
110
         */
 
111
        public static Image getImage( String sPluginRelativePath )
 
112
        {
 
113
                ImageRegistry registry = JFaceResources.getImageRegistry( );
 
114
                Image image = registry.get( sPluginRelativePath );
 
115
                if ( image == null )
 
116
                {
 
117
                        image = createImage( sPluginRelativePath );
 
118
                        registry.put( sPluginRelativePath, image );
 
119
                }
 
120
                return image;
 
121
        }
 
122
 
 
123
        /**
 
124
         * This is a convenience method to get an imgIcon from a URL.
 
125
         * 
 
126
         * @param sPluginRelativePath
 
127
         *            The URL for the imgIcon.
 
128
         * @return The imgIcon represented by the given URL.
 
129
         * @see #setImageCached( boolean )
 
130
         */
 
131
        public static ImageDescriptor getImageDescriptor( String sPluginRelativePath )
 
132
        {
 
133
                ImageRegistry registry = JFaceResources.getImageRegistry( );
 
134
                ImageDescriptor image = registry.getDescriptor( sPluginRelativePath );
 
135
                if ( image == null )
 
136
                {
 
137
                        registry.put( sPluginRelativePath,
 
138
                                        createImage( sPluginRelativePath ) );
 
139
                        image = registry.getDescriptor( sPluginRelativePath );
 
140
                }
 
141
                return image;
 
142
        }
 
143
}
 
 
b'\\ No newline at end of file'