FileDocCategorySizeDatePackage
Lesson12.javaAPI DocExample6567Thu Feb 17 20:00:56 GMT 2000com.togethersoft.modules.tutorial

Lesson12

public class Lesson12 extends Object implements com.togethersoft.openapi.ide.IdeScript
This script shows how to programmatically manage aselection in the text editor. This script is an extension of a previous lesson. It uses the same checks on the selection and static method openAndDisplay. You must select a class (interface) or an operation/attribute on a class diagram. Then this script will close the editor and open it again with a file containing the selected element. After that it will make the selected element's text visible in the editor's visible area and select that text.
author
TogetherSoft LLC

Fields Summary
Constructors Summary
Methods Summary
public voidrun(com.togethersoft.openapi.ide.IdeContext context)

        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson12 script: started");
        //checking if project is opened.
        if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null) {
            IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "No open project");
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson12 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, "Lesson12 script: finished");
            return;
        }
        if (selectedRwiElements.length > 1) {
            IdeMessageManagerAccess.printMessage(IdeMessageType.ERROR_MODAL, "Please select only one element");
            IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson12 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, "Lesson12 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) +
            "), scroll to that element, and select it");
        editor = Lesson11.openAndDisplayRwiElement(rwiElement);
        selectRwiElement(rwiElement, editor);
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson12 script: finished");
    
public static voidselectRwiElement(com.togethersoft.openapi.rwi.RwiElement element, com.togethersoft.openapi.ide.editor.IdeEditor editor)
You can use this method as a template in your editor-related scripts. It selects the text of a specified RwiElement in the specified editor.

param
element RwiElement representing a class/interface or attribute/operation
param
editor IdeEditor containing the file of element.

        IdeEditorSelection selection = editor.getSelection(IdeEditorSelectionType.PRIMARY);
        int startOffset = Integer.parseInt(element.getProperty(RwiProperty.START_POSITION));
        int endOffset = Integer.parseInt(element.getProperty(RwiProperty.END_POSITION));
        TextPosition startPos = editor.getPositionConverter().offsetToCell(startOffset);
        TextPosition endPos = editor.getPositionConverter().offsetToCell(endOffset);
        selection.setSelectionStart(startPos);
        selection.setSelectionEnd(endPos);
        IdeEditorVisibleArea viewArea = editor.getVisibleArea();
        //   viewArea.setFirstColumn(startPos.getColumn());
        viewArea.setFirstColumn(1);
        viewArea.setFirstLine(startPos.getLine());