/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. 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.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 programmatically manage a selection in the text editor.
 *
 * This module 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 module 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
 */
public class Lesson12 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, "Lesson12 module: started");

        // checking if project is opened
        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
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson12 module: finished");
            return;
        }

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

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

        // getting the array of selected RwiElements
        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, "Lesson12 module: finished");
            return;
        }

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

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

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

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

        // getting edotor 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 open editor with specified file
        IdeEditor editor = editorManager.findEditor(fileName);

        // if file is open 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
            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) +
            "), scroll to that element, and select it");

        // opening editor with specified element
        editor = Lesson11.openAndDisplayRwiElement(rwiElement);

        // selecting element text in editor
        selectRwiElement(rwiElement, editor);

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

    /**
     * Selects the text of a specified RwiElement in the specified editor.
     *
     * You can use this method as a template in your editor-related modules.
     *
     * @param element RwiElement representing a class/interface or attribute/operation
     * @param editor IdeEditor object containing the file of element
     */
    public static void selectRwiElement(RwiElement element, IdeEditor editor)
    {

        // getting selection object
        IdeEditorSelection selection = editor.getSelection(IdeEditorSelectionType.PRIMARY);

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

        // getting TextPosition objects for beginning and end of element text
        TextPosition startPos = editor.getPositionConverter().offsetToCell(startOffset);
        TextPosition endPos = editor.getPositionConverter().offsetToCell(endOffset);

        // setting a selection beginning and end
        selection.setSelectionStart(startPos);
        selection.setSelectionEnd(endPos);

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

        // setting current position
        viewArea.setFirstColumn(1);
        viewArea.setFirstLine(startPos.getLine());
    }
}


