Home
Categories
Dictionary
Download
Project Details
Changes Log
Tutorials
FAQ
License

Custom actions



Custom Plugin actions allow to use Plugins to act on Objects created by the application or other Plugins.

An example of usage could be to create a tree in the application and ask plugins to create menus on this tree.

Creation of custom actions

Creating a custom action is perfoemd in the application by the following method:
You must call the MDIPluginsCustomActionType.SINGLE_PLUGIN before registering the Plugins.


For example:
      public class MyApplication extends AbstractMDIApplication {
         public MyApplication {
           super("My Application");
      
           pluginsDir = new File(System.getProperty("user.dir"));

           this.addCustomAction("AddFile", MDIPluginsCustomActionType.SINGLE_PLUGIN);

           // register plugins in the plugins directory (same as main application jar directory)
           this.registerPlugins();

           // create the menus with the Application menu factory
           mfactory = new SimpleMenuFactory();
           super.preparePanels(4, true, true, mfactory);
           this.setSize(500, 500);
         }
      }

Specifying for a Plugin that it supports an action

If custom actions have been specified in the application, each Plugin will be called with the Plugin.supportCustomAction(String) to check if the Plugin may want to do something for this action.

If the Plugin returns false for an action name, it will never be called when executing the associated action.


For example:
  public class OpenFilePlugin extends AbstractMDIPlugin {
     public OpenFilePlugin() {
     }

     @Override
     public boolean supportCustomAction(String actionName) {
        return actionName.equals("AddImage");
     }
  }

Executing an action

When the application (or a Plugin) wants Plugins to be able to do something with some data, it just has to call one of the following methods: The plugins Manager will call the Plugins which have declared that they support the action, and call the Plugin.executeCustomAction(String, Object...) method. If the Plugin returns true, it means that it declares that it did something with the data: For example, suppose that the application created a DefaultMutableTreeNode and looks for Plugins to add sub-nodes to this node:
   DefaultMutableTreeNode theNode = new DefaultMutableTreeNode"TheNode");
   executeCustomAction("AddFile", theNode);
Then we can have in our Plugin:
  public class OpenFilePlugin extends AbstractMDIPlugin {
     public OpenFilePlugin() {
     }

     @Override
     public boolean supportCustomAction(String actionName) {
        return actionName.equals("AddFile");
     }
            
     @Override
     public boolean executeCustomAction(String actionName, Object... arguments) {
        DefaultMutableTreeNode theNode = (DefaultMutableTreeNode) arguments[0];
        DefaultMutableTreeNode node = new DefaultMutableTreeNode("Child Node");
        theNode.add(node);
        return true;
     }
  }

See also


Categories: gui | swing

Copyright 2006-2023 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences