/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft LLC. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.tutorial;
import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.IdeScript;
import com.togethersoft.openapi.ide.message.IdeMessageManager;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessagePage;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.ide.project.IdeProjectManagerAccess;
import com.togethersoft.openapi.rwi.RwiModelAccess;
import com.togethersoft.openapi.rwi.RwiNode;
import com.togethersoft.openapi.rwi.RwiMember;
import com.togethersoft.openapi.rwi.RwiLink;
import com.togethersoft.openapi.rwi.RwiDiagram;
import com.togethersoft.openapi.rwi.RwiShapeType;
import com.togethersoft.openapi.rwi.RwiPackage;
import com.togethersoft.openapi.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiModel;
import com.togethersoft.openapi.rwi.enum.RwiNodeEnumeration;
import com.togethersoft.openapi.rwi.enum.RwiPackageEnumeration;
/**
* This script shows how to iterate the model. It starts from the root packages and iterates their subpackages
* showing the hierarchy of packages in the model. You can use this example as a template for your model-iterating scripts.
* We do not make a nice JTree output in order not to complicate the script.
* @author TogetherSoft LLC
*/
public class Lesson4 implements IdeScript {
public void run(IdeContext context) {
//in this example we don't need any selection information contained in the IdeContext
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson4 script: started");
//checking if project is opened.
if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null) {
IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No open project");
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson4 script: finished");
return;
}
RwiModel model = RwiModelAccess.getModel();
RwiPackageEnumeration roots = model.rootPackages(RwiProperty.MODEL);
IdeMessageManagerAccess.getMessageManager().setPaneVisible(true); //making the message pane visible
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "The heirarchy of packages:");
while (roots.hasMoreElements()) {
RwiPackage nextRootPackage = roots.nextRwiPackage();
packageProcessing(nextRootPackage, 0);
}
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson4 script: finished");
}
/**
* This method recursively prerforms the same action upon the RwiPackage passed to it as an argument and
* upon all its subpackages.
*/
private void packageProcessing(RwiPackage rwiPackage, int depth) {
doActionUponPackage(rwiPackage, depth); //put the desired actions in this method. The second argument is used only for this sample script
RwiPackageEnumeration subPackages = rwiPackage.subpackages(); //getting the subpackges
while (subPackages.hasMoreElements()) {
RwiPackage nextPackage = subPackages.nextRwiPackage();
packageProcessing(nextPackage, depth + 1); //processing the next subpackage
}
}
/**
* This method perfoms certain actions upon the RwiPackage passed to it as an argument.
* In this method, for example, you can get and analyze all the nodes or diagrams in the package etc.
*/
private void doActionUponPackage(RwiPackage rwiPackage, int depth) {
//outputting the name of the package
StringBuffer temp = new StringBuffer();
for (int i = 0; i < depth; i++) {
temp.append(" ");
}
if (depth == 0) {
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Root package"); //for root packages RwiProperty.FULL_NAME is ""
}
else {
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION,
temp.toString() + rwiPackage.getProperty(RwiProperty.FULL_NAME));
}
//do some actions upon all the classes/interfaces in this package
RwiNodeEnumeration nodes = rwiPackage.nodes(); //getting all the RwiNodes in this package
while (nodes.hasMoreElements()) {
RwiNode nextNode = nodes.nextRwiNode();
doActionUponNode(nextNode);
}
}
/** This method perfoms certain actions upon the RwiNode representing a class/interfaces. */
private void doActionUponNode(RwiNode node) {
if (RwiShapeType.CLASS.equals(node.getProperty(RwiProperty.SHAPE_TYPE))) {
if (node.hasProperty(RwiProperty.INTERFACE)) {
//it is an interface. Put the desired actions for interfaces here
}
else {
//it is a class. Put the desired actions for classes here
}
}
}
}
|