java.net member

Rechercher dans ce site

Loading...
Note:
Most of these plug-ins and articles are available completely for free! If you like them and/or find them useful, please donate any amount to keep this site actively developed

Si vous trouvez ces articles ou plug-ins utiles, n'hesitez pas a contribuer pour encourager l'auteur et garder le site actif n'importe quelle somme est la bienvenue

Using custom images in Eclipse plug-ins

>> 26 August 2011

Note:

This post is a short and fast answer about registering images in ImageRegistry. This is an excerpt of my little ebook Steps 2 Eclipse Plug-in. This book describes, how my  plug-in for Eclipse: XML Parsing in Java Kit, was written.

It's useful to use your own images in an Eclipse plug-in, you can use them later for custom control, to add your logo in a cheat sheet, etc.

Eclipse plug-in must be packaged as a .jar file, for this reason some steps are needed to locate  and use images inside the plug-in.

We will use the Activator class (created automatically, as an option, when defining a plug-in project). This class controls the plug-in life cycle. Its method getDefault(), returns the plug-in instance, when called at run-time





 Steps to register images: (Activator class)
1. Begin by adding an ID to the image you want to use.
This ID (public,static) can be obtained  later from another class. This ID will be the name of the desired image in ImageRegistry

2. Override initializeImageRegistry(ImageRegistry) method to register Images in ImageRegistry

ImageRegistry is a list of images, clients can add or retrieve images from this registry using their names.

ImageDescriptor contains everything needed for creating an image

FileLocator  contains a collection of helper methods for finding files in bundles

3. Get the bundle associated with the plug-in

4. Create an ImageDescriptor from a URL. The custom image is under images folder in the root of the plug-in

5. Save the created ImageDescriptor in ImageRegistry using the image ID declared above.


       
Get image(s) from any user class:
Get the plug-in instance
AbstractUIPlugin plugin=Activator.getDefault();

Get the ImageRegistry associated with the plug-in
ImageRegistry reg=plugin.getImageRegistry();

Get the Image to be used from ImageRegistry
Image logo=reg.get(Activator.LOGO_ID); 







The Activator class
/* ************************************************************************************
 * @Author Kaesar ALNIJRES
 * http://www.java-javafx.com
 * Copyright(c) 2010-2011
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; If not, see
 * **************************************************************************************/

package com.java_javafx.ka.templates.java.xml.templates_and_cheatsheets;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class Activator extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "com.java_javafx.ka.templates.java.xml.templates_and_cheatsheets"; //$NON-NLS-1$

        //Give an ID to the image you want to use, this ID can be obtained  later (public,static)
    public static final String LOGO_ID="logo";
 
   
    // The shared instance
    private static Activator plugin;
   
    //Override initializeImageRegistry to register Images
    @Override
    protected void initializeImageRegistry(ImageRegistry reg)
    {
        super.initializeImageRegistry(reg);
        Bundle bundle=Platform.getBundle(PLUGIN_ID);
       
        ImageDescriptor logo=ImageDescriptor.createFromURL(FileLocator.find(bundle,   new
                                                                                                                 Path("images/logo.gif"), null));
       
        reg.put(LOGO_ID, logo);
       
    }
    /**
     * The constructor
     */
    public Activator() {
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     *
     * @return the shared instance
     */
    public static Activator getDefault() {
        return plugin;
    }

}

0 comments:

Post a Comment

  © Blogger template Simple n' Sweet by Ourblogtemplates.com 2009

Back to TOP