Package jsynoptic.base

Source Code of jsynoptic.base.Plugin

/* ========================
* JSynoptic : a free Synoptic editor
* ========================
*
* Project Info:  http://jsynoptic.sourceforge.net/index.html
*
* This program 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, 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* (C) Copyright 2001-2003, by :
*     Corporate:
*         Astrium SAS
*         EADS CRC
*     Individual:
*         Nicolas Brodu
*         Claude Cazenave
*
* $Id: Plugin.java,v 1.20 2009/01/19 17:24:32 ogor Exp $
*
* Changes
* -------
* 25-Sep-2003 : Initial public release (NB)
* 20-Jan-2004 : Added source templates (NB) 
* 30-Jan-2004 : Added hooks for desktop and source components (CC)
*
*/
package jsynoptic.base;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JMenuBar;
import javax.swing.JTabbedPane;
import javax.swing.JToolBar;
import javax.swing.filechooser.FileFilter;

import jsynoptic.ui.ShapeCreator;

import simtools.data.DataSource;
import simtools.data.DataSourceCollection;
import simtools.diagram.DiagramComponent;
import simtools.shapes.AbstractShape;
import simtools.shapes.ShapesContainer;
import simtools.ui.DesktopCardPanel;
import simtools.ui.NamedElementContainer;

/**
* This is the base class for JSynoptic plugins
*
* @author Nicolas Brodu
*
* @version 1.0 2001
*/
public class Plugin {

    public static Plugin load(String className) {
        Class c;
        try {
            c = Class.forName(className);
        } catch (ClassNotFoundException cnfe) {
            return null;
        }
        Object o;
        try {
            o = c.newInstance();
        } catch (Exception e) {
            return null;
        }
        if (o == null) {
            return null;
        }
        if (o instanceof Plugin) {
            return (Plugin) o;
        }
        return null;
    }


    /**
     * Subclasses can overload those functions to return their plugins for all
     * managed types.
     */
    /**
     * Give the plugin an occasion to add its menus
     *
     * @param mb
     *            The menubar in which to add menus. Typically, the plugin is
     *            expected to call tb.add on each of its menus, and should add
     *            MenuListeners on them too.
     */
    public void setMenu(JMenuBar mb) {
    }

    /**
     * Give the plugin an occasion to add its menus
     *
     * @param mb
     *            The menubar in which to add menus. Typically, the plugin is
     *            expected to call tb.add on each of its menus, and should add
     *            MenuListeners on them too.
     */
    public void setMenu(Object mb) {
        if (mb instanceof JMenuBar) {
            setMenu((JMenuBar) mb);
        }
    }

    /**
     * Give the plugin an occasion to add its toolbar items
     *
     * @param tb
     *            The toolbar in which to add items. Typically, the plugin is
     *            expected to call tb.add on each of its items, and should add
     *            ActionListeners on them too.
     */
    public void setToolBarItem(JToolBar tb) {
    }

    /**
     * Give the plugin an occasion to work when adding a new component
     *
     * @param c he new Component
     * @deprecated replaced by getTemplates()
     */
    public void newComponent(java.awt.Component c) {
    }
   
    /**
     * Give the plugin an occasion to work at source panel initialisation
     *
     * @deprecated replaced by newSourceComponent(NamedElementContainer)
     * @param c
     *            The sources container
     */
    public void newSourceComponent(JTabbedPane c) {
    }

    /**
     * Give the plugin an occasion to work at desktop initialisation
     *
     * @param c
     *            The desktop
     */
    public void newDesktopComponent(DesktopCardPanel c) {
    }

    /**
     * Give the plugin an occasion to work at source panel initialisation
     *
     * @param c
     *            The sources container
     */
    public void newSourceComponent(NamedElementContainer c) {
    }

    public static final int OPEN = 1;
    public static final int SAVE = 2;
    public static final int IMPORT = 3;
    public static final int EXPORT = 4;

   
    /**
     * Shape factory: this plug-in may bring its own shapes to put in a shape list.
     * It will then be asked to create a new shape when requested.
     *
     * @return An array of shape names, or null if this plug-in does not provide any shape.
     *
     * @deprecated use getShapeCreators() instead
     */
    public String[] getShapes() {
        return null;
    }
   
   
    /**
    * Shape factory: this plug-in may bring its own shapes to put in a shape creator list.
    * It will then be asked to create a new shape when requested.
    *
    * @return An list of shape creators, or null if this plug-in does not provide any shape.
    */
    public List getShapeCreators(){
        List res = null;
       
        // By default ensure old plug-ins compatibility
        String[] shapes = getShapes();
        if (shapes != null){
            res = new ArrayList();
            for(int i=0;i<shapes.length; i++){
                res.add(new ShapeCreator(this, shapes[i]));
            }
        }
        return res;
    }

   
    /**
    *  Template factory: this plugin may bring templates to put in the template
     * list.
     * @return An array of templates, or null if this plugin does not provide
     *         any template.
     */
    public Template[] getTemplates() {
        return null;
    }

   
    /**
     * The plug-in is asked to create a new shape with default dimensions, for one of the names provided by  getShapes().
     * @deprecated use createShape(String name) instead.
     */
    public AbstractShape createShape(String name, DiagramComponent diagram) {
        return createShape(name);
    }
   
    /**
     * The plug-in is asked to create a new shape with default dimensions, for one of the names provided by  getShapes().
     * @param name The name of the shape to create.
     * @return the created shape
     */
    public AbstractShape createShape(String name) {
        return null;
    }

    /**
     * The plug-in is asked to create a new shape , for one of the names provided by  getShapes().
     * New shape  top-left corner is  specified as  (<code>x</code>,&nbsp;<code>y</code>),
     * width and height  are specified by the arguments of the same name.
     *
     * @param name The name of the shape to create.
     * @param     x the specified x coordinate
     * @param     y the specified y coordinate
     * @param     width    the width of the <code>Rectangle</code>
     * @param     height   the height of the <code>Rectangle</code>
     * @return the created shape
     */
    public AbstractShape createShape(String name, int x, int y, int width, int height) {
        return null;
    }

    /**
     * Enables a plugin to show an option panel when the user selects a file
     * using one of the plugin file filters.
     *
     * @param filter
     * @return a component to display, or null if there is no option for this
     *         filter (default)
     */
    public JComponent getOptionPanelForFilter(FileFilter filter) {
        return null;
    }

    /**
     * The information returned here will be displayed in the about box. You
     * should return an HTML 3.2 text content. This text will be included in a
     * <li> tag. Returning null is permitted, and disables completely this
     * plugin information. Default is to use the class name, without the package
     * prefix.
     *
     * @return the information about this plugin, including authors, license,
     *         etc..
     */
    public String about() {
        String name = this.getClass().getName();
        return name.substring(name.lastIndexOf('.') + 1);
    }

    /**
     * Source factory: this plugin may bring its own source templates to put in
     * the predefined sources list. It will then be asked to create a new source
     * when requested. How it does it is at the plugin discretion (for example,
     * if the source template needs arguments, it's up to the plugin to open a
     * dialog box/whatever to get them).
     *
     * @return An array of source template names, or null if this plugin does
     *         not provide any source template (default).
     * @see getShapes()
     */
    public String[] getSources() {
        return null;
    }

    /**
     * The plugin is asked to create a new source from one of the templates it
     * declared with getSources(). The newly created source will be inserted at
     * top level in the source tree, unless the plugin already set it in a
     * collection.
     *
     * @param name
     *            The name of the source template.
     * @param instanceName
     *            The desired name of the source created from this template.
     * @return the newly created source, or null if this plugin cannot create a
     *         source of this type
     */
    public DataSource createSource(String name, String instanceName) {
        return null;
    }

    /**
     * Source factory: this plugin may bring its own data sources with its own
     * icons to display them in the source panel
     *
     * @return An array or null if no specific icons. Each row contains in the
     *         first column the data source class and the second column contains
     *         the icon to display it.
     */
    public Object[][] getDataSourceIcons() {
        return null;
    }

    /**
     * Some plugins need a particular treatment during Jsynoptic closing For
     * instance when an external process was created in a plugin, it needs to be
     * destroyed
     */
    public void exit() {
        // By default : do nothing
    }

    /**
     * Help factory: this plugin may bring its own help which will be displayed
     * into JSynoptic help contents.
     *
     * @return The root HelpNode or null if this plugin cannot provide any help.
     */
    public HelpNode getHelp() {
        return null;
    }
   

    /**
     * Give the plugin an occasion to add its file filters. If a file is
     * selected by the user and it is accepted by one of the FileFilters
     * returned by this function, then the processFile method will be called on
     * that file. In graphic mode, the file filters will end up in a
     * JFileChooser. In batch mode, the file filters will be used directly on a
     * user-specified file. In any case, this should be transparent for the
     * plugin.
     *
     * @param action
     *            What to do with the file. Currently OPEN or SAVE.
     * @return An array containing all the file filters added by this plugin
     */
    public FileFilter[] getFileFilters(int action) {
        return null;
    }

    /**
     * See getFileFilters for usage description.
     *
     * @param f
     *            The file to process
     * @param action
     *            What to do with the file. Currently OPEN or SAVE.
     * @return true if the action could be performed successfully.
     */
    public boolean processFile(File f, int action) {
        return false;
    }

    /**
     * Given the plugin the possibility to apply some changes on a given synoptic
     * @param sc
     *          The synoptic to process
     * @param actionType
     *          Kind of action to apply to the synoptic
     * @param background
     *          If true, run transformation in another thread
     * @return
     *          false if actionType is not provided by Plugin or if synoptic process failed
     */
    public boolean processSynoptic(ShapesContainer sc, String actionType, boolean background) {
        return false;
    }

    /**
     * Given the plugin the possibility to apply some changes on all loaded data sources
     * @param dsc
     *          The data source collection to process
     * @param actionType
     *          Kind of action to apply to data source collection
     * @return
     *          false if actionType is not provided by Plugin or if data process failed
     */
    public boolean processDataSourceCollection(DataSourceCollection dsc, String actionType) {
        return false;
    }
}
TOP

Related Classes of jsynoptic.base.Plugin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.