/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft LLC. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.inserttags;
import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.IdeScript;
import com.togethersoft.openapi.ide.project.IdeProjectManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.window.IdeWindowManager;
import com.togethersoft.openapi.ide.window.IdeWindowManagerAccess;
import com.togethersoft.openapi.ide.window.IdeButtonType;
import com.togethersoft.openapi.ide.window.IdeDialogButtonGroup;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.ide.window.IdeDialogType;
import com.togethersoft.openapi.ide.diagram.IdeDiagramManagerAccess;
import com.togethersoft.openapi.ide.diagram.IdeDiagramManager;
import com.togethersoft.openapi.rwi.RwiVisitorAdapter;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiPackage;
import com.togethersoft.openapi.rwi.RwiNode;
import com.togethersoft.openapi.rwi.RwiMember;
import com.togethersoft.openapi.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiDiagram;
import com.togethersoft.openapi.rwi.RwiShapeType;
import com.togethersoft.openapi.rwi.enum.RwiNodeEnumeration;
import com.togethersoft.openapi.rwi.enum.RwiPackageEnumeration;
import java.util.Vector;
import java.awt.Dimension;
import javax.swing.JList;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
/**
* This sample script inserts the specified tag into the selected source elements (packages/classes/interfaces and operations/attributes)
* without this kind of a tag.
* If the user selects a class diagram, this script will process the contents (with subpackages) of its containing package.
*
* Note that if you select a package icon, the selected element is RwiDiagram (see API docs, RwiDiagram)
* and the RwiPackage being represented by it can be obtained via RwiDiagram.getContainingPackage() method (this is done in
* the TagsInserterVisitor.visitDiagram method). That is why if you select a package icon you will get what you want - it will
* insert tags in all possible elements within the package and its subpackages. But if you select a free space in a diagram the
* selected element will also be RwiDiagram (alas), so the script will insert tags not only in this diagram's
* elements, but in the diagram's containing package elements (and elements in its subpackages).
* This is because it applies the same algorithm to the RwiDiagrams.
*
* Pay attention to how we use visitors - it is a very convenient pattern.
*
* @version 3.0.0
* @author TogetherSoft LLC
*/
public class InsertTags implements IdeScript {
public void run(IdeContext context) {
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "InsertTags script: started");
//checking if project is opened.
if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null) {
IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No open project");
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "InsertTags script: finished");
return;
}
//we need an opened diagram to get an element to work with
IdeDiagramManager diagramManager = IdeDiagramManagerAccess.getDiagramManager();
if (diagramManager.getActiveDiagram() == null) {
IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No open diagram");
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson12 script: finished");
return;
}
//the array of selected RwiElements
RwiElement[] selectedRwiElements = context.getRwiElements();
if (selectedRwiElements == null || selectedRwiElements.length == 0) {
IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No selection was made.");
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "InsertTags script: finished");
return;
}
JLabel header = new JLabel("Insert or change tags in " + selectedRwiElements.length + " selected elements");
windowManager = IdeWindowManagerAccess.getWindowManager();
//standard Swing stuff
JTextField tagField = new JTextField(DEFAULT_TAG);
JTextField tagValueField = new JTextField(DEFAULT_TAG_VALUE);
//getting the pressed button
String answer = windowManager.showOptionDialog(null, "Enter the tag and its value", IdeMessageType.INFORMATION, new Object[] {
header, tagField, tagValueField
}, IdeButtonType.OK_CANCEL_BUTTONS, null);
if (!IdeButtonType.OK.equals(answer)) { //not OK has been pressed
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "InsertTags script: finished");
return;
}
TagsInserter tagsVisitor = new TagsInserter(tagField.getText(), tagValueField.getText());
for (int i = 0; i < selectedRwiElements.length; i++) { //working with the selection
selectedRwiElements[i].accept(tagsVisitor); //the visitor inserts the tags
}
showResults(tagsVisitor);
IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "InsertTags script: finished");
}
private void showResults(TagsInserter visitor) {
JScrollPane pane = new JScrollPane(visitor.getResults());
pane.setPreferredSize(new Dimension(400, 200));
windowManager.showMessageDialog("Results", IdeDialogType.INFORMATION, new Object[] {
"Total source elements analyzed: " + visitor.getTotalProcessed(),
"Total tags inserted: " + visitor.getTagsInserted(), pane
});
}
private final static String DEFAULT_TAG = "author"; //tag name
private final static String DEFAULT_TAG_VALUE = "Unknown"; //tag value
private IdeWindowManager windowManager;
/** This visitor insert tags in the various elements. */
private class TagsInserter extends RwiVisitorAdapter {
public TagsInserter(String tag, String tagValue) {
myTag = tag;
myTagValue = tagValue;
processedNamesVector = new Vector();
}
/** Returns a JList with the processed elements */
public JList getResults() {
JList processed = new JList(processedNamesVector);
processed.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
processed.setSelectedIndex(0);
return processed;
}
public int getTotalProcessed() { return myTotalElements; }
public int getTagsInserted() { return myTagsInserted; }
public Object visitPackage(RwiPackage rwiPackage) {
RwiNodeEnumeration rwiNodeEnumeration = rwiPackage.nodes();
//processing nodes...
while (rwiNodeEnumeration.hasMoreElements()) {
RwiNode nextRwiNode = rwiNodeEnumeration.nextRwiNode();
visitNode(nextRwiNode);
}
RwiPackageEnumeration subpackages = rwiPackage.subpackages();
//processing subpackages...
while (subpackages.hasMoreElements()) {
RwiPackage nextSubpackage = subpackages.nextRwiPackage();
visitPackage(nextSubpackage);
}
return null;
}
public Object visitDiagram(RwiDiagram diagram) {
//work only with class diagrams
if (RwiShapeType.CLASS_DIAGRAM.equals(diagram.getProperty(RwiProperty.SHAPE_TYPE))) {
visitPackage(diagram.getContainingPackage());
}
return null;
}
public Object visitNode(RwiNode rwiNode) {
String comment = "";
if (RwiShapeType.CLASS.equals(rwiNode.getProperty(RwiProperty.SHAPE_TYPE))) {
if (rwiNode.hasProperty(RwiProperty.INTERFACE)) {
comment = " (interface) ";
}
else {
comment = " (class) ";
}
addTag(rwiNode, comment);
}
return null;
}
public Object visitMember(RwiMember member) {
String comment = "";
if (RwiShapeType.OPERATION.equals(member.getProperty(RwiProperty.SHAPE_TYPE))) {
comment = " (operation) ";
addTag(member, comment);
} else if (RwiShapeType.ATTRIBUTE.equals(member.getProperty(RwiProperty.SHAPE_TYPE))) {
comment = " (attribute) ";
addTag(member, comment);
}
return null;
}
private void addTag(RwiElement element, String comment) {
//using the RwiPropertyMap.addProperty (see api docs)
String name = element.getProperty(RwiProperty.FULL_NAME);
if (name == null || "".equals(name)) {
name = element.getProperty(RwiProperty.NAME);
}
if (element.canAddProperty(myTag, myTagValue)) {
element.addProperty(myTag, myTagValue);
processedNamesVector.addElement("processed: " + name + comment + " - tag inserted");
myTagsInserted++;
}
else {
processedNamesVector.addElement("processed: " + name + comment);
}
myTotalElements++;
}
private Vector processedNamesVector;
private String myTag; //keeps the tag name
private String myTagValue; //keeps the tag value
private int myTagsInserted;
private int myTotalElements;
}
}
|