astah*でPing監視またはチェックするプラグインをお試し開発

マインドマップを使用してPing監視またはチェックするプラグインをお試し開発。
インベントリやチェックに使えるのではと。

仕組み

  • IPアドレスが記載されているブランチを探索
  • IPアドレスにPing(といってもJavaのisReachable)実施
  • 結果でブランチ文字色変更

コード

TemplateActionにそのままコーディング。

package jp.co.exmaple.astah.example;

import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;

import javax.swing.JOptionPane;

import com.change_vision.jude.api.inf.AstahAPI;
import com.change_vision.jude.api.inf.editor.ITransactionManager;
import com.change_vision.jude.api.inf.exception.InvalidEditingException;
import com.change_vision.jude.api.inf.exception.ProjectNotFoundException;
import com.change_vision.jude.api.inf.model.IDiagram;
import com.change_vision.jude.api.inf.model.IMindMapDiagram;
import com.change_vision.jude.api.inf.model.IModel;
import com.change_vision.jude.api.inf.presentation.INodePresentation;
import com.change_vision.jude.api.inf.presentation.PresentationPropertyConstants.Key;
import com.change_vision.jude.api.inf.project.ProjectAccessor;
import com.change_vision.jude.api.inf.ui.IPluginActionDelegate;
import com.change_vision.jude.api.inf.ui.IWindow;
import com.change_vision.jude.api.inf.view.IDiagramViewManager;

public class TemplateAction implements IPluginActionDelegate {
    private IModel project = null;

    public Object run(IWindow window) throws UnExpectedException {
        try {
            AstahAPI api = AstahAPI.getAstahAPI();
            ProjectAccessor projectAccessor = api.getProjectAccessor();
            project = projectAccessor.getProject();

            IDiagramViewManager diagramViewManager = api.getViewManager().getDiagramViewManager();
            IDiagram currentDiagram = diagramViewManager.getCurrentDiagram();
            if((currentDiagram instanceof IMindMapDiagram) == true) {
                executePing(((IMindMapDiagram)currentDiagram).getRoot(), projectAccessor);
                Arrays.asList(((IMindMapDiagram)currentDiagram).getFloatingTopics()).forEach(inp -> {
                    executePing(inp, projectAccessor);
                });
            }

        }
        catch(ProjectNotFoundException e) {
            String message = "Project is not opened.Please open the project or create new project.";
            JOptionPane.showMessageDialog(window.getParent(), message, "Warning", JOptionPane.WARNING_MESSAGE);
        }
        catch(Exception e) {
            JOptionPane.showMessageDialog(window.getParent(), "Unexpected error has occurred.", "Alert", JOptionPane.ERROR_MESSAGE);
            throw new UnExpectedException();
        }
        return null;
    }

    private void executePing(INodePresentation iNodePresentation, ProjectAccessor projectAccessor) {
        List<INodePresentation> iNodePresentationList = Arrays.asList(iNodePresentation.getChildren());
        iNodePresentationList.forEach(inp -> {
            this.judgePing(inp, projectAccessor);
        });
    }

    private void changeFontColor(INodePresentation inp, String color) {
        try {
            inp.setProperty(Key.FONT_COLOR, color);
        }
        catch (InvalidEditingException e) {
        }
    }

    private void judgePing(INodePresentation inp, ProjectAccessor projectAccessor) {
        ITransactionManager transactionManager = projectAccessor.getTransactionManager();
        transactionManager.beginTransaction();
        InetAddress inetAddress = null;
        try {
            System.out.println(inp.getLabel());
            inetAddress = InetAddress.getByName(inp.getLabel());
        }
        catch(Exception e) {
            System.out.println("InetAddress Exception");
            System.out.println(inetAddress);
        }
        try {
            if(inetAddress != null) {
                if(inetAddress.isReachable(1000) == true) {
                    changeFontColor(inp, "#00FF00");
                    System.out.println(inetAddress.toString() + "ping ok");
                }
                else {
                    changeFontColor(inp, "#FF0000");
                    System.out.println(inetAddress.toString() + "ping ng");
                }
            }
        }
        catch(Exception e) {
            changeFontColor(inp, "#FF0000");
            System.out.println(inetAddress.toString() + "ping ng");
        }
        transactionManager.endTransaction();
        List<INodePresentation> iNodePresentationList = Arrays.asList(inp.getChildren());
        iNodePresentationList.forEach(inpchild -> {
            this.judgePing(inpchild, projectAccessor);
        });

    }
}

astah*astah

Posted by shi-n