/*-------------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft Corporation. Patents pending. All rights reserved.
-------------------------------------------------------------------------------*/

package com.togethersoft.modules.testModule;

import com.togethersoft.openapi.ide.IdeContext;
import com.togethersoft.openapi.ide.IdeScript;
import com.togethersoft.openapi.ide.window.IdeWindowManager;
import com.togethersoft.openapi.ide.window.IdeWindowManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.project.IdeProjectManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiElement;

/**
 *        This is a same module taken from the module's tutorial
 *        (com.togethersoft.modules.tutorial.Lesson02.java)
 *        Here we will show how to debug this module.
 * ==========================================================================
 * This module goes through the selection and outputs the full names and
 * shapetype property of each element in selection.
 *
 * The ShapeType property is one of the most important properties in RWI -
 * you will see how it is used in the next lessons. For now it is useful just
 * to run this module several times to see the shapetypes of the elements
 * in a diagram.
 * See RwiProperty interface description in the API docs for information about
 * RwiProperty.SHAPE_TYPE property.
 *
 * @author TogetherSoft
 */
public class TestModule 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 into message pane
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Lesson02 module: started");

        // checking if project is opened
        if (IdeProjectManagerAccess.getProjectManager().getActiveProject() == null)
        {
            // project was not opened
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.ERROR_MODAL, "No open project");
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION, "Lesson02 module: finished");
            return;
        }

        // start work with the selection

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

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

        // making the message pane visible and printing message
        IdeMessageManagerAccess.getMessageManager().setPaneVisible(true);
        IdeMessageManagerAccess.printMessage
            (IdeMessageType.INFORMATION, "Selection contains the following elements:");

        // iterating the selection
        for (int i = 0; i < selectedRwiElements.length; i++)
        {
            // getting current element from array
            RwiElement rwiElement = selectedRwiElements[i];

            // printing ShapeType and full name info
            IdeMessageManagerAccess.printMessage
                (IdeMessageType.INFORMATION,
                "ShapeType: " + rwiElement.getProperty(RwiProperty.SHAPE_TYPE) +
                ", full name: " + rwiElement.getProperty(RwiProperty.FULL_NAME));
        }

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



