/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. 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.IdeEditorSelection;
import com.togethersoft.openapi.ide.editor.IdeEditorSelectionType;
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.util.text.TextPosition;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiShapeType;
import com.togethersoft.openapi.rwi.RwiProperty;

/**
 * This module 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 module 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
 */
public class Lesson11 implements IdeScript
{
   /**
    * Runs this module.
    *
    * @param context the IdeContext instance containing
    * the selection information at the moment the module was called.
    */
    public void run(IdeContext context)
    {
        // printing information message
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson11 module: started");

        // checking if project is open
        if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null)
        {
            // project was not opened
            // show modal window with error description
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "No open project");
            // printing message into message pane
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson11 module: finished");
            return;
        }

        // getting diagram manager
        IdeDiagramManager diagramManager = IdeDiagramManagerAccess.getDiagramManager();

        // if there is not open diagram
        if (diagramManager.getActiveDiagram() == null)
        {
            // no open diagrams
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "No open diagram");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson11 module: finished");
            return;
        }

        // getting the array of selected RwiElements from context
        RwiElement[] selectedRwiElements = context.getRwiElements();

        // if there are not selected elements
        if (selectedRwiElements == null || selectedRwiElements.length == 0)
        {
            // no selected elements
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "No selection was made.");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson11 module: finished");
            return;
        }

        // checking if too many elements were selected
        if (selectedRwiElements.length > 1)
        {
            // to many elements
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "Please select only one element");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson11 module: finished");
            return;
        }

        // getting selected element
        RwiElement rwiElement = selectedRwiElements[0];

        // getting ShapeType element  property
        String shapeType = rwiElement.getProperty(RwiProperty.SHAPE_TYPE);

        // checking if element is class, interface, operation or attribute
        if (!(RwiShapeType.CLASS.equals(shapeType) ||
            RwiShapeType.OPERATION.equals(shapeType) ||
            RwiShapeType.ATTRIBUTE.equals(shapeType)))
        {
            // another type element
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "Please select either a class/interface or an operation/attribute");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson11 module: finished");
            return;
        }

        // getting editor manager
        IdeEditorManager editorManager = IdeEditorManagerAccess.getEditorManager();

        // making editor pane visible
        editorManager.setPaneVisible(true);

        // getting  name of file that contains selected element
        String fileName = rwiElement.getProperty(RwiProperty.FILE);

        // finding an open editor pane with specified file
        IdeEditor editor = editorManager.findEditor(fileName);

        // if file is opened in editor
        if (editor != null)
        {
            // show information modal window
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION_MODAL,
                "You have selected an element \"" +
                rwiElement.getProperty(RwiProperty.FULL_NAME) +
                "\". Now we will close the editor");
            // close editor pane
            editorManager.closeEditor(editor);
        }

        // show information modal window
        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");

        // open editor pane with specified element
        openAndDisplayRwiElement(rwiElement);

        // printing final information message
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson11 module: finished");
    }

    /**
     * Opens in editor specified element, makes element visible in the editor,
     * makes the editor active.
     *
     * You could use this method as a template in your editor-related modules.
     *
     * @param element RwiElement object
     * @return IdeEditor object containing the file.
     */
    public static IdeEditor openAndDisplayRwiElement(RwiElement element)
    {
        // getting  name of file that contains selected element
        String fileName = element.getProperty(RwiProperty.FILE);

        // getting editor manager
        IdeEditorManager editorManager = IdeEditorManagerAccess.getEditorManager();

        // finding open editor with specified file
        IdeEditor editor = editorManager.findEditor(fileName);

        // if file was not opened in editor
        if (editor == null)
        {
            editor = editorManager.openEditor(); //creates a new empty editor
        }
        editor.setFile(fileName); //loading a file...

        // getting the position of the beginning of an element's
        // defintion in a file
        int startOffset =
            Integer.parseInt(element.getProperty(RwiProperty.START_POSITION));

        // getting TextPosition object
        TextPosition textPos = editor.getPositionConverter().offsetToCell(startOffset);

        /// getting visible editor area
        IdeEditorVisibleArea viewArea = editor.getVisibleArea();

        // setting current position in editor
		viewArea.setFirstColumn(textPos.getColumn());
		viewArea.setFirstLine(textPos.getLine());

        // making editor active
        editorManager.setActiveEditor(editor);
        
        // making editior pane visible
        editorManager.setPaneVisible(true);

        // sellecting element's defintion in a file
 		IdeEditorSelection selection = editor.getSelection(IdeEditorSelectionType.NAVIGATION);
		selection.setSelectionStart(textPos);
		selection.setSelectionEnd(new TextPosition(textPos.getLine()+1,textPos.getColumn()));

        return editor;
    }
}
