FileDocCategorySizeDatePackage
Lesson6.javaAPI DocExample10079Thu Feb 17 20:00:56 GMT 2000com.togethersoft.modules.tutorial

Lesson6.java

/*----------------------------------------------------------------------------
Copyright (c)2000 TogetherSoft LLC. 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.IdeStartup;
import com.togethersoft.openapi.ide.message.IdeMessageManagerAccess;
import com.togethersoft.openapi.ide.message.IdeMessageType;
import com.togethersoft.openapi.ide.command.IdeCommandManagerAccess;
import com.togethersoft.openapi.ide.command.IdeCommandManager;
import com.togethersoft.openapi.ide.command.IdeCommandAdapter;
import com.togethersoft.openapi.ide.command.IdeCommandCheckListener;
import com.togethersoft.openapi.ide.command.IdeCommandConstraints;
import com.togethersoft.openapi.ide.command.IdeCommandItem;
import com.togethersoft.openapi.ide.command.IdeCommandGroup;
import com.togethersoft.openapi.ide.command.IdeCommandEvent;
import com.togethersoft.openapi.rwi.RwiModelAccess;
import com.togethersoft.openapi.rwi.RwiNode;
import com.togethersoft.openapi.rwi.RwiMember;
import com.togethersoft.openapi.rwi.RwiShapeType;
import com.togethersoft.openapi.rwi.RwiProperty;
import com.togethersoft.openapi.rwi.RwiElement;
import com.togethersoft.openapi.rwi.enum.RwiMemberEnumeration;

/**
 * This script shows how to add custom menus and commands to the element's popup menu.
 * It creates a submenu called "My menu" with commands "Show the qualified name" (outputs the
 * qualified name) and "Simple action" (outputs the "Hello" string).
 * Note the difference with the previous lesson: this script does not rely on the
 * "PopupMenuItem = true" command in the script's manifest file, but builds the submenu and the commands
 * by itself. This requires some effort but allows to control the behavior of the command and submenu more flexibly.
 * To demonstrate this let's make the menu "My menu" appear only for classes and interfaces:
 * 1. for classes its name must be "My menu (for classes)" 2. for interfaces its name must be "My menu (for interfaces)"
 * Moreover, the command "Show the qualified name" must: 1. be disabled for interfaces containing declared methods and its
 * name must be "Show the qualified name (not implemented for interfaces with methods yet)"
 * 2. be hidden for interfaces with no declared methods. 3. be enabled for classes, the name should be "Show the qualified
 * name" for public classes and "Show the qualified name (package local class)" for the classes with the default access level.
 * The command "Simple action" should be located after the "Show the qualified name" command.
 * This script implements both IdeScript and IdeStartup intrefaces, which makes
 * it a regular and a startup spript simultaneously. To run it as a startup script, uncomment
 * the line "Startup = true" in the Lesson6.def file. This will create the described submenu each time you run Together.
 * You can use the contents of the createTheMenu method as a template for creating your own menus.
 * Note: if you use 800x600 resolution you might have some problems seeing the menu since is placed at
 * the very end of a popup menu which already contains too many commands (try to place a class at the
 * top of a diagram or use try to use the 1024x768 for this script)
 * @author TogetherSoft LLC
 */
public class Lesson6 implements IdeScript, IdeStartup {
    public void run(IdeContext context) {
        //in this example we don't need any selection information contained in the IdeContext.
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson6 script: started");
        createTheMenu();
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson6 script: finished");
    }

    public void autorun() { //if the line "Startup=true" is uncommented in the .def file then this method will be called at the startup
        createTheMenu();
    }

    private void createTheMenu() {
        IdeCommandManager cman = IdeCommandManagerAccess.getCommandManager();
        myGroup = cman.createGroup("IDOfTheGroup", new IdeCommandConstraints("context = element, shapeType=Class, location=popupMenu"),
            new IdeCommandCheckListener() {
                public void checkStatus(IdeCommandEvent event) {
                    IdeContext context = event.getElementContext(); //getting the element(s) under the cursor
                    RwiElement[] selectedRwiElements = context.getRwiElements();
                    RwiElement theElement = selectedRwiElements[0];
                    //now, the constraints mechanism works so that it will run checkStatus method only if the shapetype of the element
                    //equals the specified value ("Class") (Otherwise the group/item will be made invisible).
                    //Both classes and interfaces have this shapetype and Together will run the group's listener checkStatus method.
                    //And here we decide what to do with the group
                    if (theElement.hasProperty(RwiProperty.INTERFACE)) { //it is an interface
                        myGroup.setText("My menu (for interfaces)");
                    }
                    else { //it is a class
                        myGroup.setText("My menu (for classes)");
                    }
                }
            });
        //note that in the following line we do not specify the shapetype because it is specified in the group's constraints.
        //(for elements with the shapetype other that RwiShapeType.CLASS the group will be invisible, as well all
        //the items contained in it)
        myItem = cman.createItem("IDOfTheItem", new
            IdeCommandConstraints("context = element, parent=IDOfTheGroup, location=popupMenu"), new IdeCommandAdapter() {
                public void checkStatus(IdeCommandEvent event) {
                    IdeContext context = event.getElementContext(); //getting the element(s) under the cursor
                    RwiElement[] selectedRwiElements = context.getRwiElements();
                    RwiElement theElement = selectedRwiElements[0];
                    //here we decide what to do with the item
                    if (theElement.hasProperty(RwiProperty.INTERFACE)) { //it is an interface
                        RwiMemberEnumeration members = ((RwiNode)theElement).members();
                        int methodsCount = 0;
                        //let's count the declared methods
                        while (members.hasMoreElements()) {
                            RwiMember nextMember = members.nextRwiMember();
                            if (RwiShapeType.OPERATION.equals(nextMember.getProperty(RwiProperty.SHAPE_TYPE)))
                                methodsCount++;
                        }
                        if (methodsCount != 0) {
                            myItem.setVisible(true); //this is required because we might have hidden it (the 4 lines below)
                            myItem.setEnabled(false); //disable it
                            myItem.setText("Show the qualified name (not implemented for interfaces with methods yet)");
                        }
                        else {
                            myItem.setVisible(false);
                        }
                    }
                    else { //it is a class
                        myItem.setEnabled(true); //enable it for classes
                        myItem.setVisible(true);
                        if (theElement.hasProperty(RwiProperty.PUBLIC)) { //class is public
                            myItem.setText("Show the qualified name");
                        }
                        else if (theElement.hasProperty(RwiProperty.PACKAGE_LOCAL)) {
                            myItem.setText("Show the qualified name (package local class)");
                        }
                    }
                }
                public void actionPerformed(IdeCommandEvent event) { //the user managed to invoke the command ;)
                    IdeContext context = event.getElementContext(); //getting the element(s) under the cursor
                    RwiElement[] selectedRwiElements = context.getRwiElements();
                    RwiElement rwiElement = selectedRwiElements[0];
                    IdeMessageManagerAccess.getMessageManager().setPaneVisible(true); //making the message pane visible
                    IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION,
                        "Lesson6: the qualified name is " + rwiElement.getProperty(RwiProperty.FULL_NAME));
                }
            });
        //note the "placeAfter" parameter, there we specify the ID of the myItem
        myItem2 = cman.createItem("IDOfTheItem2", new
            IdeCommandConstraints("context = element, parent=IDOfTheGroup, placeAfter = IDOfTheItem, location=popupMenu"), new IdeCommandAdapter() {
                //we use the empty checkStatus method, it does nothing so that the item will always be visible
                public void actionPerformed(IdeCommandEvent event) { //the user ivoked the command
                    IdeMessageManagerAccess.getMessageManager().setPaneVisible(true); //making the message pane visible
                    IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION, "Lesson6: Hello");
                }
            });
        myItem2.setText("Simple action");
        IdeMessageManagerAccess.getMessageManager().setPaneVisible(true); //making the message pane visible
        IdeMessageManagerAccess.printMessage(IdeMessageType.INFORMATION,
            "Lesson6 script: new popup submenu for classes/interfaces was successfully created.");
    }

    private IdeCommandGroup myGroup;
    private IdeCommandItem myItem;
    private IdeCommandItem myItem2;
}