/*----------------------------------------------------------------------------
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.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.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 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
*/
public class Lesson11 implements IdeScript {
public void run(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");
}
/**
* 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 <code>element</code> visible in the editor.
* @return IdeEditor containing the file. This editor is made active.
*/
public static IdeEditor openAndDisplayRwiElement(RwiElement element) {
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;
}
}
|