com.worldline.awltech.i18ntools.editor.Activator.java Source code

Java tutorial

Introduction

Here is the source code for com.worldline.awltech.i18ntools.editor.Activator.java

Source

/**
 * I18N Tools
 *
 * Copyright (C) 2014 Worldline or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
package com.worldline.awltech.i18ntools.editor;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.plugin.AbstractUIPlugin;
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.worldline.awltech.i18ntools.editor"; //$NON-NLS-1$

    // The shared instance
    private static Activator plugin;

    /**
     * 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;
    }

    /**
     * Returns image in plugin
     * 
     * @param pluginId
     *            : Id of the plugin containing thie image
     * @param imageFilePath
     *            : image File Path in plugin
     * @return Image if exists
     */
    public Image getImage(final String pluginId, final String imageFilePath) {
        Image image = Activator.getDefault().getImageRegistry().get(pluginId + ":" + imageFilePath);
        if (image == null) {
            image = this.loadImage(pluginId, imageFilePath);
        }
        return image;
    }

    /**
     * Loads image in Image Registry is not available in it
     * 
     * @param pluginId
     *            : Id of the plugin containing thie image
     * @param imageFilePath
     *            : image File Path in plugin
     * @return Image if loaded
     */
    private synchronized Image loadImage(final String pluginId, final String imageFilePath) {
        final String id = pluginId + ":" + imageFilePath;
        Image image = Activator.getDefault().getImageRegistry().get(id);
        if (image != null) {
            return image;
        }
        final ImageDescriptor imageDescriptor = AbstractUIPlugin.imageDescriptorFromPlugin(pluginId, imageFilePath);
        if (imageDescriptor != null) {
            image = imageDescriptor.createImage();
            Activator.getDefault().getImageRegistry().put(pluginId + ":" + imageFilePath, image);
        }
        return image;
    }

    /**
     * Returns image in this plugin
     * 
     * @param imageFilePath
     *            : image File Path in this plugin
     * @return Image if exists
     */
    public Image getImage(final String imageFilePath) {
        Image image = Activator.getDefault().getImageRegistry().get(Activator.PLUGIN_ID + ":" + imageFilePath);
        if (image == null) {
            image = this.loadImage(Activator.PLUGIN_ID, imageFilePath);
        }
        return image;
    }

}