### Navigation

- index
- next |
- previous |
- Astah Plug-in Tutorial v1.0 documentation »

# Let’s make a sample Plug-in, “Hello World”

This section will introduce you how to make a Plug-in that adds a menu to Astah.

## Creating a new project

First, create a new project. Move to the directory where you want to create a Plug-in Project and then run the command below.:

    > astah-generate-project

Next, specify the group id, artifact id, version and etc.

A new folder for the Plug-in project will be generated in a name you specified in the artifact id.

Generated project consists of the files below.

File Name | Description
--- | ---
pom.xml | This includes maven setting such as project build information and dependencies.
Activator.java | This describes the initialization and finalization for Astah Plug-in and will be called when the Plug-in is started or stopped.
plugin.xml | Describe extension point ex. menu, view etc. This chapter uses menu extension point of Astah, details will be explained below.
plugin.properties | Internationalize labels to show UI on Astah
plugin_jp.properties | plugin.properties will be used as default. However if you’d like to write in Japanese, use plugin_jp.properties.

## Launching Astah with the Plug-in in development

Try launching Astah with the Plug-in in development. First, you need to build the project. To do so, move to the directly of the Plug-in project and then run the command below.:

    > astah-build

After its being built successfully, a new folder “target” is created which includes [artifact id]-[version].jar file. This is a Plug-in file. Now let’s launch Astah with this Plug-in by running the command below.:

    > astah-launch

Astah should launch with this Plug-in.

You can see this Plug-in in the list from [Help] - [Plugin List] menu.

Also you will see a menu that is added by this Plug-in under [Tool] menu.

## Importing Plug-in project to IDE

Astah Plug-in SDK is based on Maven, however you would like to use IDE like Eclipse [1] or IntelliJ IDEA [2] to develop Plug-ins instead of using Astah’s Plug-in SDK only.

So we will show you how to import the Plug-in project to Eclipse and IntelliJ.

### Eclipse

You can run maven commands in Eclipse by using m2e.

Warning

However you are not able to set the classpath to JAR while using m2e. So to set the classpath, you need to use astah-mvn command instead of using m2e’s Dependency Management.

#### Installation of Eclipse and m2e

First, install Eclipse and m2e from Eclipse. This package includes m2e.:

    Eclipse IDE for Java Developers

Note

Installation of this package should not effect to anything on your development environment. Therefore we recommend you to use this package. However if you have Eclipse installed already, you can install m2e from m2e.

#### Configuration of Astah Plug-in to MAVEN_HOME on Eclipse

Run Eclipse and then choose workspace and set “Astah Plug-in SDK” to MAVEN_HOME.

Open Eclipse Preference from [Window] - [Preferences] and then select [Maven] - [Installations]. Add Astah Plug-in SDK’s installation directory (ASDK_HOME) as the figure below.

#### Generating config to load classpath for Eclipse

Now, before importing the plug-in project to Eclipse, move to the working directory of Plug-in Project, set the environment variables of M2_HOME to ASDK_HOME and then run the command below.:

    > astah-mvn eclipse:eclipse

Now all is ready. Import the Plug-in project into Eclipse.

### IntelliJ IDEA

IntelliJ IDEA has Maven Integration. Please download IntelliJ IDEA from IntelliJ site.

When you first run IntelliJ IDEA, a [New Project Wizard] appears for the set up. If you have used IntelliJ IDEA already, please read Configuation MAVEN_HOME to Astah Plug-in on IntelliJ IDEA and then import the project.

#### Importing a Plug-in project to IntelliJ IDEA

Please refer to IntelliJ’s resource for how to import projects. However you need to set the Astah Plug-in SDK on IntelliJ before you import the Plug-in project.

#### Configuation MAVEN_HOME to Astah Plug-in on IntelliJ IDEA

Let’s set Astah Plug-in SDK to MAVEN_HOME in IntelliJ IDEA.

Run IntelliJ, and then select [File] - [Settings] - [Maven]. Check the [Override] for Maven home directory and then specify Astah Plug-in SDK install folder(ASDK_HOME) as figure below.

When the setting is completed, the classpath should have been set up.

## Adding menus on Astah

To create a Plug-in for Astah, an extension point needs to be defined in plugin.xml with the “point” property of <extension> tag. Currently following two extension points are provided.

Extension Point Name | Description
--- | ---
com.change_vision.astah.ui.actionSets | Adding a menu on Astah
com.change_vision.astah.ui.view | Adding an extended view on Astah

Below is a sample of how you write plugin.xml to add a menu on Astah.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    <?xml version="1.0" encoding="UTF-8"?>
    <plugin>
       <extension point="com.change_vision.astah.ui.actionSets">
          <actionSet
                label="%action_set_label"
                visible="true"
                id="com.example.helloworld.actionSet">
             <menu
                   id="helloworld"
                   label="%menu_group"
                   path="tool/helloworld">
             </menu>
             <action
    			   label="%menu_label"
                   icon="icons/sample.gif"
                   class="com.example.TemplateAction"
                   tooltip="%menu_tooltip"
                   menubarPath="tool/helloworld/"
                   id="com.example.actions.HelloWorldAction">
             </action>
         </actionSet>
       </extension>
    </plugin>

In this sample, <menu> tag is used to add a new menu [Sample Menu] under [Tool]. <action> tag is used to add another menu with Hello World Plug-in under the [Sample Menu]. The location of menu is specified with <action> tag’s menubarpath attribute. The value “tool” is an id of [Tool] menu and it should be specified in the <menu> tag’s path attribute and also <action> tag’s menubarpath attribute. The id of all the menu can be found at Astah Menu ID List. Also <actionSet> tag is used to group all the <action>tags. All the actions when the menu is selected should be specified with <action>’s class attributes.

Here is the summary for the plugin.xml.

Tag Name | Attribute | Required | Description
--- | --- | --- | ---
plugin |  | o |
extension |  | o | Specify extension point
| point | o | Specify com.change_vision.astah.ui.actionSets
actionSet |  | o | Specify the group of menu to menubar
| label |  | (Reserved word)
| visible |  | (Reserved word)
| id |  | Specify group ID
menu |  |  | Add menu group
| label | o | Specify label of group menu
| id | o | Specify id of group menu
| path | o | Specify location of path
action |  |  | Add menu
| label | o | Specify label of menu
| icon |  | (Reserved word)
| class | o | Specify the class of push the menu
| tooltip |  | (Reserved word)
| menubarPath | o | Specify the location of menu
| id | o | Specify the id of menu

We will show about action classes later.

## Displaying another languages

In this sample, <action> tag’s label is “%menu_label”. This notation is to make the label show in different languages depending on the environment to run Astah on. The string, “hello_world” is key and label contents are described in both plugin.properties and plugin_ja.properties.

plugin.properties:

    menu_label=&Hello World

plugin_ja.properties:

    menu_label=こんにちは、ワールド(&H)

If Astah is launched on Japanese environment, the label appears in Japanese as specified in the plugin_ja.properties and it appears in English as specified in plugin.properteis if Astah is launched on other environments.

## Making Action classes

Now you have added a Menu in Astah’s menu bar, now design actions to show “Hello World” message in the menu. To do so, create a class which implements IPluginActionDelegate interface.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    /*
     * Please change this class's package to your genearated Plug-in's package.
     * Plug-in's package namespace => com.example
     *   com.change_vision.astah.extension.plugin => X
     *   com.example                              => O
     *   com.example.internal                     => O
     *   learning                                 => X
     */
    package com.example.actions;

    import javax.swing.JOptionPane;
    import com.change_vision.jude.api.inf.ui.IPluginActionDelegate;
    import com.change_vision.jude.api.inf.ui.IWindow;

    public class HelloWorldAction implements IPluginActionDelegate {

      public Object run(IWindow window) throws UnExpectedException {
          try {
              JOptionPane.showMessageDialog(window.getParent(), "Hello World");

          } catch (Exception e) {
            JOptionPane.showMessageDialog(window.getParent(), "Exception occured", "Alert", JOptionPane.ERROR_MESSAGE);
              throw new UnExpectedException();
          }
          return null;
      }
    }

The run() method of the IPluginActionDelegate interface will be called when the menu is clicked in Astah. In this sample, clicking [Hello World] menu makes a dialog appear with “Hello World” message on. Any exception errors happen during the action’s performance will be thrown as a UnExpectedException, inner class of IPlginActionDelegate Interface.

Footnotes

[1] | http://eclipse.org/

[2] | http://www.jetbrains.com/idea/

### Navigation

- index
- next |
- previous |
- Astah Plug-in Tutorial v1.0 documentation »