testdev/ite/src/com.nokia.testfw.resultview/src/com/nokia/testfw/resultview/ResultViewPlugin.java
changeset 1 96906a986c3b
equal deleted inserted replaced
0:f1112f777ce9 1:96906a986c3b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.testfw.resultview;
       
    18 
       
    19 import java.net.MalformedURLException;
       
    20 import java.net.URL;
       
    21 import java.util.Hashtable;
       
    22 
       
    23 import org.eclipse.core.runtime.IStatus;
       
    24 import org.eclipse.core.runtime.Status;
       
    25 import org.eclipse.jface.resource.ImageDescriptor;
       
    26 import org.eclipse.swt.graphics.Image;
       
    27 import org.eclipse.ui.plugin.AbstractUIPlugin;
       
    28 import org.osgi.framework.Bundle;
       
    29 import org.osgi.framework.BundleContext;
       
    30 
       
    31 
       
    32 /**
       
    33  * The activator class controls the plug-in life cycle
       
    34  */
       
    35 public class ResultViewPlugin extends AbstractUIPlugin {
       
    36 
       
    37 	// The plug-in ID
       
    38 	public static final String PLUGIN_ID = "com.nokia.testfw.resultview";
       
    39 	
       
    40 	private static final String IMAGE_PATH = "icons/full/";
       
    41 
       
    42 	// The shared instance
       
    43 	private static ResultViewPlugin plugin;
       
    44 	
       
    45 	private static Hashtable<String, Image> images;
       
    46 	
       
    47 	/**
       
    48 	 * The constructor
       
    49 	 */
       
    50 	public ResultViewPlugin() {
       
    51 		images = new Hashtable<String, Image>();
       
    52 	}
       
    53 
       
    54 	/*
       
    55 	 * (non-Javadoc)
       
    56 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
       
    57 	 */
       
    58 	public void start(BundleContext context) throws Exception {
       
    59 		super.start(context);
       
    60 		plugin = this;
       
    61 	}
       
    62 
       
    63 	/*
       
    64 	 * (non-Javadoc)
       
    65 	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
       
    66 	 */
       
    67 	public void stop(BundleContext context) throws Exception {
       
    68 		plugin = null;
       
    69 		super.stop(context);
       
    70 		//dispose image
       
    71 		for (Image image : images.values()) {
       
    72 			if (!image.isDisposed()) {
       
    73 				image.dispose();
       
    74 			}
       
    75 		}
       
    76 	}
       
    77 
       
    78 	/**
       
    79 	 * Returns the shared instance
       
    80 	 *
       
    81 	 * @return the shared instance
       
    82 	 */
       
    83 	public static ResultViewPlugin getDefault() {
       
    84 		return plugin;
       
    85 	}
       
    86 	
       
    87 	/**
       
    88      * retrieve an image based on its name (from the plug-in standard image folder)
       
    89      * @param imageName
       
    90      * @return <code>ImageDescriptor</code> of the image if found or the standard missing image descvritor
       
    91      */
       
    92     public static ImageDescriptor getImageDescriptor(String imageName) {
       
    93         return getImageDescriptor(ResultViewPlugin.getDefault().getBundle(), imageName);
       
    94     }
       
    95     
       
    96     public static Image getImage(String imageName) {
       
    97     	Image image = images.get(imageName);
       
    98 		if (image == null) {
       
    99 			ImageDescriptor desc = getImageDescriptor(imageName);
       
   100 			image = desc.createImage();
       
   101 			images.put(imageName, image);
       
   102 		}
       
   103 		return image;
       
   104     }
       
   105 
       
   106     /**
       
   107      * retrieve an image based on its name (from the plug-in standard image folder)
       
   108      * @param imageName
       
   109      * @return <code>ImageDescriptor</code> of the image if found or the standard missing image descriptor
       
   110      */
       
   111     public static final ImageDescriptor getImageDescriptor(Bundle bundle,
       
   112         String imageName) {
       
   113         URL url = getImageUrl(bundle, imageName);
       
   114 
       
   115         return (url != null) ? ImageDescriptor.createFromURL(url)
       
   116                              : ImageDescriptor.getMissingImageDescriptor();
       
   117     }
       
   118  
       
   119     
       
   120     /**
       
   121      * retrieve the URL of an image file based on its name (from the plug-in standard image folder)
       
   122      * @param imageName
       
   123      * @return <code>URL</code> of the image if found or null if not found
       
   124      */
       
   125 //    private static URL getImageUrl(String imageName) {
       
   126 //        return getImageUrl(ResultViewPlugin.getDefault().getBundle(), imageName);
       
   127 //    }
       
   128 
       
   129     public static final URL getImageUrl(Bundle bundle, String imageName) {
       
   130         if (bundle == null) {
       
   131             bundle = ResultViewPlugin.getDefault().getBundle();
       
   132         }
       
   133 
       
   134         try {
       
   135             URL baseURL = bundle.getEntry("/");
       
   136             return new URL(baseURL, IMAGE_PATH + imageName);
       
   137         } catch (MalformedURLException e) {
       
   138             //            LOG.warn("image URL invalid (image probably missing)", e);
       
   139             return null;
       
   140         }
       
   141     }
       
   142     
       
   143     public static void log(Throwable e) {
       
   144 		log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "Error", e)); //$NON-NLS-1$
       
   145 	}
       
   146 
       
   147 	public static void log(IStatus status) {
       
   148 		getDefault().getLog().log(status);
       
   149 	}
       
   150 
       
   151 	public static void log(int status, String msg) {
       
   152 		Status s = new Status(status, PLUGIN_ID, status, msg, null);
       
   153 		log(s);
       
   154 	}
       
   155 
       
   156 
       
   157 }