### Navigation

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

# Let’s make a Plug-in to count number of Classes in Astah

We’ve created a simple Astah Plug-in. Now let’s make another one. This time make one to count number of Classes in Astah which can be used as one of indexes for an evaluation of the value of designs.

In this sample model as the figure below, there are three classes in this model.

Now let’s make a Plug-in to count the models and let it show the number as below.:

    "There are 3 classes"

## Getting model information

In order to get model information, let’s use Astah API. Astah API is a group of Java interfaces for reading/writing Astah which can be used to count number of classes in this case. Please refer to Astah API User guide which you can find in Astah Install folder for details about Astah API.

We are going to write a code to get the root model of Astah by using ProjectAccessor and get all the classes under the root model recursively. Now let’s modify the “Hello World” Plug-in to make it show the number of Class in the menu instead.

First, create CountClassAction class.

     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
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    /*
     * 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 java.util.ArrayList;
    import java.util.List;
    import javax.swing.JOptionPane;
    import com.change_vision.jude.api.inf.exception.ProjectNotFoundException;
    import com.change_vision.jude.api.inf.model.IClass;
    import com.change_vision.jude.api.inf.model.IModel;
    import com.change_vision.jude.api.inf.model.INamedElement;
    import com.change_vision.jude.api.inf.model.IPackage;
    import com.change_vision.jude.api.inf.project.ProjectAccessor;
    import com.change_vision.jude.api.inf.project.ProjectAccessorFactory;
    import com.change_vision.jude.api.inf.ui.IPluginActionDelegate;
    import com.change_vision.jude.api.inf.ui.IWindow;

    public class CountClassAction implements IPluginActionDelegate {
        public Object run(IWindow window) throws UnExpectedException {
            try {
                ProjectAccessor projectAccessor = ProjectAccessorFactory.getProjectAccessor();
                IModel iCurrentProject = projectAccessor.getProject();
                List<IClass> classeList = new ArrayList<IClass>();
                getAllClasses(iCurrentProject, classeList);
                JOptionPane.showMessageDialog(window.getParent(),
                		"There are " + classeList.size() + " classes.");
            } catch (ProjectNotFoundException e) {
                String message = "Please open a project";
                JOptionPane.showMessageDialog(window.getParent(), message,
                		"Warning", JOptionPane.WARNING_MESSAGE);
                throw new CalculateUnExpectedException();
            } catch (Exception e) {
                JOptionPane.showMessageDialog(window.getParent(),
                		"Exception occured", "Alert", JOptionPane.ERROR_MESSAGE);
                throw new UnExpectedException();
            }
            return null;
        }

        public class CalculateUnExpectedException extends UnExpectedException {
        }

        private void getAllClasses(INamedElement element, List<IClass> classList) throws ClassNotFoundException, ProjectNotFoundException {
            if (element instanceof IPackage) {
                for(INamedElement ownedNamedElement : ((IPackage)element).getOwnedElements()) {
                    getAllClasses(ownedNamedElement, classList);
                }
            } else if (element instanceof IClass) {
                classList.add((IClass)element);
                for(IClass nestedClasses : ((IClass)element).getNestedClasses()) {
                    getAllClasses(nestedClasses, classList);
                }
            }
        }
    }

Now, modify the plugin.xml with this CountClassAction class.

     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.actions.actionSet1">
          <menu
            label="%sample_menu"
            id="sampleMenu"
            path="tool/sampleMenu">
          </menu>
          <action
            label="%count_classes"
            icon="icons/sample.gif"
            class="com.example.actions.CountClassAction"
            tooltip="Count classes"
            menubarPath="tool/sampleMenu/"
            id="com.example.actions.CountClassdAction">
          </action>
        </actionSet>
      </extension>
    </plugin>

At last, add the label for this menu in plugin.properties:

    count_classes=Count classes(C)

And now this new Plug-in should show number of classes in Astah from the menu.

## Debugging the Plug-in

Now let’s debug and also see if it can get a nested class1 and class2 under a pacage0 recursively by running the command below.:

    > astah-debug

This command launches Astah with the 44000 port open for remote debugging which you can connect from IDE.

### Example of remote debugging using Eclipse

If you are using Eclipse, please make the configuration as below.

Clicking [Debug] button executes the remote debugging. Please set a breakpoint in the line of codes if you like. This is all we provide regarding the remote debugging on this tutorial.

### Navigation

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