Task 1: Creating Actions

 

Solution

To create your own actions, you must create a new action provider and implement each individual action in the relevant action class. Then you must only register the new action provider in the plugin.xml.

 

1. Writing your own Element Action Provider

First, create an action provider class. Since we want to associate actions with Selena elements, the provider class must inherit from the abstract class ElementActionProvider.

In your provider class, declare all single actions and, if necessary, the multi-element actions that you will implement. To register the actions available for a context with the Eclipse environment, overwrite the two abstract methods getSingleActions() and getMultiActions().

Normally, you will only have to implement single actions. The getMultiActions() method then returns null.

 

Example

 

 

 

public class MyElementActionProvider extends ElementActionProvider {

 

  private IElementSingleAction[] thisSingleActions =

                 { new MyFirstSingleAction(), new MySecondSingleAction()};

  private IElementMultiAction[] thisMultiActions =

                 { new MyFirstMultiAction()};

 

  public IElementSingleAction[] getSingleActions(Element element, Element[] context)   {

     return thisSingleActions;

  }

 

  public IElementMultiAction[] getMultiActions(Element[] context) {

    return thisMultiActions;

  }

 

}

 

 

 

2. Writing your own Actions

To create new actions you must create action classes. They must either implement the interface IElementSingleAction (for single actions) or the interface IElementMultAction (for multi-element actions).

Here, you must assign a category to the individual actions and implement the methods isAvailable(), isEnabled(), run(), und getCategory().

 

Example 1

In this example, a single action is implemented and assigned the predefined category Category.CUT. The run method merely creates a simple console output.

 

 

public class MyFirstSingleAction implements IElementSingleAction {

    private final ICategory myCategory = Category.CUT;

 

    public boolean isAvailable(Element element) {

       return true;

    }

 

    public boolean isEnabled(Element element) {

       return true;

    }

 

    public void run(Element element) {

      System.out.println("First SingleAction running on element: "

                          + element.getUniqueName()

                          + " - "

                          + element.getPropertyValue(Property.METACLASS));

    }

 

    public ICategory getCategory() {

       return myCategory;

    }

}

 

 

 

Example 2

This example also implements a single action for which a new category is created dynamically using the method Category.createDisplayableCategory(). The predefined category EDIT is used as the parent category.

The action can only be executed if the selected element belongs to the domain model and the element name contains the character a. The run method again only creates a simple console output.

 

 

public class MySecondSingleAction implements IElementSingleAction {

 

    private final ICategory myCategory = Category.createDisplayableCategory(

                                               Category.EDIT,

                                               "(1)my 'a' Model Action",

                                               "A a model action",

                                               "Active on models containing an 'a'",

                                                null);

 

  public boolean isAvailable(Element element) {

     return element.getUniqueName().getDomain().equals("model");

  }

 

  public boolean isEnabled(Element element) {

    return element.getUniqueName().getName().lastIndexOf('a') != -1;

  }

 

  public void run(Element element) {

    System.out.println("(1)MySecondAction running on: "

                       + element.getUniqueName()

                       + " - "

                       + element.getPropertyValue(Property.METACLASS));

  }

 

  public ICategory getCategory() {

    return myCategory;

  }

}

 

 

 

Example

This example implements a multi-element actions for which a new category is created dynamically. The action can only be activated if two elements were selected. The run method executes the operation on both selected elements.

 

 

public class MyFirstMultiAction implements IElementMultiAction {

    private final ICategory myCategory =

                          Category.createDisplayableCategory(

                                                 Category.DEFAULT,

                                                 "myFirstMultiAction",

                                                 "Two Element Action",

                                                 "Active for two Elements selected",

                                                 null);

 

    public boolean isAvailable(Element[] elements) {

        return true;

    }

 

    public boolean isEnabled(Element[] elements) {

       return elements.length == 2;

    }

 

    public void run(Element[] element) {

       System.out.println("First MultiAction running on: ");

       for (int i = 0; i < element.length; i++) {

           System.out.println(element[i].getUniqueName() + " - "

                             + element[i].getPropertyValue(Property.METACLASS));

       }

    }

 

    public ICategory getCategory() {

        return myCategory;

    }

}

 

 

 

3. Registering the Action Provider

If you have not already done so, add the plug-ins com.tssap.util and com.tssap.selena.model.extension to your plugin.xml as required Plugins.

 

...

<requires>

     ...

    <import plugin="com.tssap.selena.model.extension"/>

    <import plugin="com.tssap.util"/>

    ...

</requires>

...

 

 

To register your action provider in plugin.xml, use the extension point com.tssap.selena.model.provider. Assign ObjectPropertyProvider to your action provider as a metaclass.

 

...

<extension point="com.tssap.selena.model.provider">

   <provider class="com.tssap.mypackage.MyElementActionProvider">

       <meta class="ObjectPropertyProvider">

       </meta>

   </provider>

</extension>

...