/*-------------------------------------------------------------------------------
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.IdeMessageManager;
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.diagram.IdeDiagramElement;
import com.togethersoft.openapi.ide.diagram.IdeDiagram;
import com.togethersoft.openapi.ide.window.IdeWindowManager;
import com.togethersoft.openapi.ide.window.IdeWindowManagerAccess;
import com.togethersoft.openapi.rwi.RwiModelAccess;
import com.togethersoft.openapi.rwi.RwiModel;
import com.togethersoft.openapi.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.RwiDiagram;

/**
 * This module shows the usage of Together's IDE. It illustrates how to
 * open a diagram containing a certain element, how to programmatically
 * select this element in a diagram, and how to scroll to this element in a
 * diagram in order to make it visible.
 *
 * Consider the situation: your module somehow has found the desired element
 * in the model and you wish to open a diagram with this element and show
 * the element. In this case you can reuse the method openDiagramWithRwiElement
 * from this module which opens the corresponding diagram given a RwiElement.
 *
 * The module requires that the the user selects an element in a diagram.
 * After that the module determines the selected RwiElement, closes the diagram
 * and after that just calls the openDiagramWithRwiElement method passing to it
 * the selected RwiElement.
 *
 * @author TogetherSoft
 */
public class Lesson07 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 message into message pane
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson07 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 into message pane
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson07 module: finished");
            return;
        }

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

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

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

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

        // checking number of selected elements
        if (selectedRwiElements.length > 1)
        {
            // more than one element were selected
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "Please select only one element");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson07 module: finished");
            return;
        }

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

        // getting window manager
        IdeWindowManager windowManager = IdeWindowManagerAccess.getWindowManager();

        // print the message confirming the selection
        windowManager.showMessageDialog("Information", IdeMessageType.INFORMATION,
            "You have selected an element \"" + rwiElement.getProperty(RwiProperty.NAME) +
            "\". Now we will close the diagram");

        // closing a diagram
        diagramManager.closeDiagram(diagramManager.getActiveDiagram());

        // printing message about new diagram opening
        windowManager.showMessageDialog("Information", IdeMessageType.INFORMATION,
            "Now we will open a diagram containing selected element (" + rwiElement.getProperty(RwiProperty.NAME) +
            "), select this element in the diagram and scroll to it");

        // open diagram
        // use this method in your code. It's very simple.
        openDiagramWithRwiElement(rwiElement);

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

    /**
     * You can use this method in your modules to open a diagram containing
     * the specified RwiElement. It automatically makes the element selected
     * and visible on the screen.
     *
     * @param rwiElement RwiElement to be displayed in a diagram
     */
    public static void openDiagramWithRwiElement(RwiElement rwiElement)
    {
        // getting RWI model
        RwiModel model = RwiModelAccess.getModel();

        // finding RWI diagram in model
        RwiDiagram rwiDiagram = model.findDiagramFor(rwiElement);

        // if RWI diagram was not found
        if (rwiDiagram == null)
        {
            // error, diagram  was not found
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "Error: diagram was not found");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson07 module: finished");
            return;
        }

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

        // opening diagram and making it active
        IdeDiagram ideDiagram = diagramManager.openDiagram(rwiDiagram, true);
        diagramManager.setActiveDiagram(ideDiagram);

        ideDiagram.unselectAll(); // clearing all selection

        // finding element
        IdeDiagramElement ideRepresentative = ideDiagram.findElement(rwiElement);

        // if element was found
        if (ideRepresentative != null)
        {
            // select element and scrolling to it
            ideDiagram.select(ideRepresentative);
            ideDiagram.scrollTo(ideRepresentative);
        }
    }
}


