FileDocCategorySizeDatePackage
Lesson11.javaAPI DocExample6571Thu Feb 17 20:00:56 GMT 2000com.togethersoft.modules.tutorial

Lesson11

public class Lesson11 extends Object implements com.togethersoft.openapi.ide.IdeScript
This script shows how to use Together's text editor to open a file and make the specific position of this file visible in the editor's pane. You must select a class (interface) or an operation/attribute on a class diagram. Then this script will close the editor's pane and open it again with a file containing selected element. After that it will make the selected element's text visible in the editor's visible area.
author
TogetherSoft LLC

Fields Summary
Constructors Summary
Methods Summary
public static com.togethersoft.openapi.ide.editor.IdeEditoropenAndDisplayRwiElement(com.togethersoft.openapi.rwi.RwiElement element)
You use this method as a template in your editor-related scripts.

param
element RwiElement representing a class/interface or attribute/operation to be opened in the editor. After that this method will make element visible in the editor.
return
IdeEditor containing the file. This editor is made active.

        String fileName = element.getProperty(RwiProperty.FILE); //name of a file with <code>element</code>
        IdeEditorManager editorManager = IdeEditorManagerAccess.getEditorManager();
        IdeEditor editor = editorManager.findEditor(fileName);
        if (editor == null) { //there is no editor with this file
            editor = editorManager.openEditor(); //creates a new empty editor
            editor.setFile(fileName); //loading a file...
        }
        int startOffset = Integer.parseInt(element.getProperty(RwiProperty.START_POSITION));
        TextPosition textPos = editor.getPositionConverter().offsetToCell(startOffset);
        IdeEditorVisibleArea viewArea = editor.getVisibleArea();
        //viewArea.setFirstColumn(textPos.getColumn());
        viewArea.setFirstColumn(1);
        viewArea.setFirstLine(textPos.getLine());
        editorManager.setActiveEditor(editor); //comment out this line if you don't need editor to become active
        editorManager.setPaneVisible(true); //making the editor pane visible
        return editor;
    
public voidrun(com.togethersoft.openapi.ide.IdeContext context)

        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson11 script: started");
        //checking if project is opened.
        if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null) {
            IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No open project");
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson11 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, "Lesson11 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, "Lesson11 script: finished");
            return;
        }
        if (selectedRwiElements.length > 1) {
            IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "Please select only one element");
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson11 script: finished");
            return;
        }
        RwiElement rwiElement = selectedRwiElements[0];
        String shapeType = rwiElement.getProperty(RwiProperty.SHAPE_TYPE);
        if (!(RwiShapeType.CLASS.equals(shapeType) || RwiShapeType.OPERATION.equals(shapeType) || RwiShapeType.ATTRIBUTE.equals(shapeType))) {
            IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL,
                "Please select either a class/interface or an operation/attribute");
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson11 script: finished");
            return;
        }
        IdeEditorManager editorManager = IdeEditorManagerAccess.getEditorManager();
        editorManager.setPaneVisible(true);
        String fileName = rwiElement.getProperty(RwiProperty.FILE); //name of a file with the selected element
        IdeEditor editor = editorManager.findEditor(fileName);
        if (editor != null) { //file is opened
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION_MODAL,
                "You have selected an element \"" + rwiElement.getProperty(RwiProperty.FULL_NAME) +
                "\". Now we will close the editor");
            editorManager.closeEditor(editor);
        }
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION_MODAL,
            "Now we will open a file containing the selected element (" + rwiElement.getProperty(RwiProperty.FULL_NAME) +
            "), and scroll to that element");
        openAndDisplayRwiElement(rwiElement);
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson11 script: finished");