frameworkplugins/com.nokia.s60tools.ui/src/com/nokia/s60tools/ui/internal/S60ToolsUiPlugin.java
changeset 0 61163b28edca
equal deleted inserted replaced
-1:000000000000 0:61163b28edca
       
     1 /*
       
     2 * Copyright (c) 2006 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  
       
    18  
       
    19 package com.nokia.s60tools.ui.internal;
       
    20 
       
    21 import java.io.File;
       
    22 import java.io.IOException;
       
    23 import java.net.URL;
       
    24 
       
    25 import org.eclipse.core.runtime.FileLocator;
       
    26 import org.eclipse.jface.resource.ImageDescriptor;
       
    27 import org.eclipse.jface.resource.ImageRegistry;
       
    28 import org.eclipse.swt.graphics.Image;
       
    29 import org.eclipse.swt.widgets.Display;
       
    30 import org.eclipse.ui.plugin.AbstractUIPlugin;
       
    31 import org.osgi.framework.BundleContext;
       
    32 
       
    33 import com.nokia.s60tools.ui.UiUtils;
       
    34 import com.nokia.s60tools.ui.actions.OpenPreferencePageAction;
       
    35 import com.nokia.s60tools.ui.actions.S60ToolsBaseAction;
       
    36 
       
    37 /**
       
    38  * The main plugin class to be used in the desktop.
       
    39  */
       
    40 public class S60ToolsUiPlugin extends AbstractUIPlugin {
       
    41 
       
    42 	/**
       
    43 	 * Shared plug-in ID.
       
    44 	 */
       
    45 	public static final String PLUGIN_ID = "com.nokia.s60tools.ui";
       
    46 
       
    47 	/**
       
    48 	 * The shared plug-in instance.
       
    49 	 */
       
    50 	private static S60ToolsUiPlugin plugin;
       
    51 	
       
    52 	/**
       
    53 	 * Plugin installation location.
       
    54 	 */
       
    55 	private static String pluginInstallLocation;
       
    56 	
       
    57 	/**
       
    58 	 * The constructor.
       
    59 	 */
       
    60 	public S60ToolsUiPlugin() {
       
    61 		plugin = this;
       
    62 	}
       
    63 
       
    64 	/**
       
    65 	 * This method is called upon plug-in activation
       
    66 	 */
       
    67 	public void start(BundleContext context) throws Exception {
       
    68 		super.start(context);
       
    69 		pluginInstallLocation = getPluginInstallPath();
       
    70 	}
       
    71 
       
    72 	/**
       
    73 	 * This method is called when the plug-in is stopped
       
    74 	 */
       
    75 	public void stop(BundleContext context) throws Exception {
       
    76 		super.stop(context);
       
    77 		plugin = null;
       
    78 	}
       
    79 
       
    80 	/**
       
    81 	 * Returns the shared instance.
       
    82 	 */
       
    83 	public static S60ToolsUiPlugin getDefault() {
       
    84 		return plugin;
       
    85 	}
       
    86 
       
    87 	/**
       
    88 	 * Returns an image descriptor for the image file at the given
       
    89 	 * plug-in relative path.
       
    90 	 *
       
    91 	 * @param path the path
       
    92 	 * @return the image descriptor
       
    93 	 */
       
    94 	public static ImageDescriptor getImageDescriptor(String path) {
       
    95 		return AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); //$NON-NLS-1$
       
    96 	}
       
    97 
       
    98 	/**
       
    99 	 * 
       
   100 	 * @return plugin install path
       
   101 	 * @throws IOException
       
   102 	 */
       
   103 	public String getPluginInstallPath() throws IOException{
       
   104 		 // URL to the plugin's root ("/")
       
   105 		URL relativeURL = getBundle().getEntry("/"); //$NON-NLS-1$
       
   106 		//	Converting into local path
       
   107 		URL localURL = FileLocator.toFileURL(relativeURL);
       
   108 		//	Getting install location in correct form
       
   109 		File f = new File(localURL.getPath());
       
   110 		String pluginInstallLocation = f.getAbsolutePath();
       
   111 				
       
   112 		return pluginInstallLocation;		
       
   113 	}
       
   114 	
       
   115 	/**
       
   116 	 * Gets images path relative to given plugin install path.
       
   117 	 * @return Path were image resources are located.
       
   118 	 */
       
   119 	private String getImagesPath(){
       
   120 		return pluginInstallLocation
       
   121 				+ File.separatorChar
       
   122 				+ "icons"; //$NON-NLS-1$
       
   123 	}
       
   124 
       
   125 	/* (non-Javadoc)
       
   126      * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeImageRegistry(org.eclipse.jface.resource.ImageRegistry)
       
   127      */
       
   128     protected void initializeImageRegistry(ImageRegistry imgReg) {
       
   129     	
       
   130     	//
       
   131     	// Storing images to plugin's image registry
       
   132     	//
       
   133     	Display disp = Display.getCurrent();
       
   134     	
       
   135     	Image img = new Image( disp, getImagesPath() + File.separator + "empty_banner.png" );        	 //$NON-NLS-1$
       
   136         imgReg.put( UiUtils.NOKIA_TOOLS_BANNER, img );
       
   137         
       
   138     	img = new Image( disp, getImagesPath() + File.separator + "s60tools_action_icon.png" );        	 //$NON-NLS-1$
       
   139         imgReg.put( S60ToolsBaseAction.S60TOOLS_ACTION_ICON, img );
       
   140         
       
   141     	img = new Image( disp, getImagesPath() + File.separator + "preferences.png"); //$NON-NLS-1$
       
   142         imgReg.put( OpenPreferencePageAction.IMG_PREFERENCE_PAGE, img );        
       
   143         
       
   144     }
       
   145     
       
   146 	/**
       
   147 	 * Returns image descriptor for the given key from the plugin's image registry.
       
   148 	 * @param key Key to search descriptor for.
       
   149 	 * @return Image descriptor for the given key from the plugin's image registry.
       
   150 	 */
       
   151 	public static ImageDescriptor getImageDescriptorForKey( String key ){
       
   152     	ImageRegistry imgReg = getDefault().getImageRegistry();
       
   153     	return  imgReg.getDescriptor( key );		
       
   154 	}	
       
   155 
       
   156 	/**
       
   157 	 * Returns image for the given key from the plugin's image registry.
       
   158 	 * @param key Key to search image for.
       
   159 	 * @return Image for the given key from the plugin's image registry.
       
   160 	 */
       
   161 	public static Image getImageForKey( String key ){
       
   162     	ImageRegistry imgReg = getDefault().getImageRegistry();    	
       
   163     	return  imgReg.get(key);		
       
   164 	}
       
   165 	
       
   166 }