/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft LLC. Patents pending. All rights reserved.
----------------------------------------------------------------------------*/
package com.togethersoft.modules.tutorial;
import com.togethersoft.modules.tutorial.Lesson11;
import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.IdeScript;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.ide.project.IdeProjectManagerAccess;
import com.togethersoft.openapi.ide.diagram.IdeDiagramManagerAccess;
import com.togethersoft.openapi.ide.diagram.IdeDiagramManager;
import com.togethersoft.openapi.ide.editor.IdeEditor;
import com.togethersoft.openapi.ide.editor.IdeEditorManager;
import com.togethersoft.openapi.ide.editor.IdeEditorManagerAccess;
import com.togethersoft.openapi.ide.editor.IdeEditorVisibleArea;
import com.togethersoft.openapi.ide.editor.IdeEditorSelection;
import com.togethersoft.openapi.ide.editor.IdeEditorSelectionType;
import com.togethersoft.util.TextPosition;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiShapeType;
import com.togethersoft.openapi.rwi.RwiProperty;
/**
* 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
*/
public class Lesson12 implements IdeScript {
public void run(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");
}
/**
* 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 <code>element</code>.
*/
public static void selectRwiElement(RwiElement element, IdeEditor editor) {
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());
}
}
|