/*-------------------------------------------------------------------------------
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.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiElement;

/**
 * This module shows the easiest way to include your modules in popup menus
 * for certain elements.
 *
 * This can be done by specifying a string "PopupMenuItem = true" in the
 * tutorial modules's manifest file. By default, this module is disabled in
 * the popup menus. Using the "PopupMenuItem = true" is a great solution for
 * those modules that must appear in a popup menu for classes, interfaces,
 * operations, and attributes, since it doesn't require to add anything to the
 * implementation of the module - all the work is done by Together!
 * Moreover, you can enable the appearance of the module only for elements
 * having the specified shapetype.
 * To do this use in manifest file next strings:
 * PopupMenuItem = true
 * PopupConstraints = "shapeType=Operation"
 * This will make the module appear only for operations, for example.
 * The drawback of this manifest file-based registration approach is that you
 * can't specify a different name of a menu (for example, "My menu" instead
 * of "Modules") or put any conditions on the appearance of the command
 * (for example, you won't be able to make it disabled for public or void operations,
 * or make it visible only for classes, or interfaces with no declared methods etc).
 * All these features can be done using the functionality of
 * com.togethersoft.openapi.ide.command package. Next lesson contains a sample
 * of such module.
 * This module will write the name of the selected element in the message pane.
 * When it is invoked via the "Module" pane, it can be applied to any element.
 *
 * @author TogetherSoft
 */
public class Lesson05 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, "Lesson05 module: started");

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

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

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

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

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

        // making message pane visible
        IdeMessageManagerAccess.getMessageManager().setPaneVisible(true);

        // printing info about element in messsage pane
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION,
            "Lesson05: the name of the element is " +
            rwiElement.getProperty(RwiProperty.NAME) +
            ". See the text of this module, it contains a description.");

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


