View Question

selectionlistener

Hi, Could you give me an example how to use the method addSelectionListener? I want to run a method every time I click on an element in Astah.

Thanks in advance

Product: Astah Professional

Hi Ano,

Thanks for your post. Here's a sample code:


package com.example;

import javax.swing.JOptionPane;

import org.osgi.framework.BundleActivator;

import org.osgi.framework.BundleContext;

import com.change_vision.jude.api.inf.AstahAPI;

import com.change_vision.jude.api.inf.exception.InvalidUsingException;

import com.change_vision.jude.api.inf.project.ProjectAccessor;

import com.change_vision.jude.api.inf.view.IDiagramViewManager;

import com.change_vision.jude.api.inf.view.IEntitySelectionEvent;

import com.change_vision.jude.api.inf.view.IEntitySelectionListener;

public class Activator implements BundleActivator {

      MySelectionListener selectionListener = new MySelectionListener();

public void start(BundleContext context) {
    try {
        getDiagramViewManager().addEntitySelectionListener(selectionListener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void stop(BundleContext context) {
    try {
        getDiagramViewManager().removeEntitySelectionListener(selectionListener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private IDiagramViewManager getDiagramViewManager()
        throws ClassNotFoundException, InvalidUsingException {
    AstahAPI api = AstahAPI.getAstahAPI();
    ProjectAccessor projectAccessor = api.getProjectAccessor();
    IDiagramViewManager vm = projectAccessor.getViewManager().getDiagramViewManager();
    return vm;
}

/**
 * My SelectionListener.
 */
public class MySelectionListener implements IEntitySelectionListener {
    @Override
    public void entitySelectionChanged(IEntitySelectionEvent arg0) {
        // any code
        JOptionPane.showMessageDialog(null, "Hello Astah World", "infomation",
                JOptionPane.PLAIN_MESSAGE);
    }
}

}


  • Activator.java runs when plug-in is loaded. Put Initialization process in Activator#start and End-processing in Activator#stop.
  • IEntitySelectionListener is a listener to receive events when any element is selected on Astah. So create a class to implement it and write an action that you want it to execute when an element is selected in entitySelectionChanged().
  • In the Activator#start, pass the class that implements IEntitySelectionListener for the IDiagramViewManager#addEntitySelectionListener, and delete it in the Activator#stop

PS: Sorry about the space in-between import lines at the top of the sample code. I couldn't format them right in this form for some reasons..

Cheers, SJ

Answer Question

This content has the following tags: