Code Example DisplayShape Pluglet

This page has not been liked. Updated 4/11/13, 5:03 PM by MarianaAluTags: None



 

DisplayShape Code Example

You can create pluglets to handle routine tasks and use their functionality to explore and learn the application programming interfaces (API) of the workbench platform and other product extensions.

  • Pluglet source file is no different from any other Java source file. Pluglet writers can specify what Eclipse plug-ins their pluglets require, and this controls the pluglet class path, both at compile and at runtime.

Problem

This Code Example shows how to write a pluglet that adds geometric shapes to a package and displays them on a diagram, using IBM Rational Software Architect (RSA), IBM Rational Software Modeler (RSM) or IBM Rational® Systems Developer (RSD).

Warning



This sample might require some optionally installable components. If you encounter errors or cannot find user interface options when you run the sample, ensure that you installed the appropriate optional components.

Solution

The pluglet provided in this Code Example shows a full example that creates a rectangle on a class diagram. It performs the following actions:

  • It operates on all open models
  • It creates a package
  • It creates a diagram
  • It inserts the rectangle on it

To test the pluglet:

  • Start Rational Software Architect (or RSM, or RSD) on a new Workspace.
  • Create a new UML Modeling project containing a test model that the pluglet will iterate through
  • Select File -> New -> Pluglets Project.
  • Select File -> New -> Pluglet.
  • Choose the type: UML Modeler -> Model Enumeration.
  • Choose the package: com.ibm.rational.support.
  • Choose the name: DisplayShape.
  • Replace the template code with the Code Template listed, or Import the DisplayShape.zip into your pluglet project.
  • Open and select the model
  • Run the Pluglet from: Run -> Internal Tools -> DisplayShape (visible in Modeling perspective).













    Note that the pluglet operates on all open models, so you do not need to select a model before running the pluglet.

Code Template

DisplayShape.java
/*
 * Licensed Materials - Use restricted, please refer to the "Samples Gallery" terms
 * and conditions in the IBM International Program License Agreement.
 *
 * © Copyright IBM Corporation 2003, 2010. All Rights Reserved. 
 */
package com.ibm.rational.support;
 
 import java.lang.reflect.InvocationTargetException;
 import java.util.Collection;
 import java.util.Iterator;
 
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.core.runtime.NullProgressMonitor;
 import org.eclipse.emf.ecore.EClass;
 import org.eclipse.uml2.Model;
 import org.eclipse.uml2.Package;
 import org.eclipse.uml2.UML2Package;
 
 import com.ibm.xtools.emf.msl.ResourceSetModifyOperation;
 import com.ibm.xtools.modeler.UMLModeler;
 import com.ibm.xtools.notation.Bounds;
 import com.ibm.xtools.notation.Diagram;
 import com.ibm.xtools.notation.FillStyle;
 import com.ibm.xtools.notation.LineStyle;
 import com.ibm.xtools.notation.Node;
 import com.ibm.xtools.notation.NotationPackage;
 import com.ibm.xtools.pluglets.Pluglet;
 import com.ibm.xtools.uml.diagram.IUMLDiagramHelper;
 import com.ibm.xtools.umlnotation.UMLDiagramKind;
 
 public class DisplayShape extends Pluglet {
 
  public void plugletmain(String[] args) {
   out.println("Pluglet \"DisplayShape\"."); //$NON-NLS-1$
   try {
    UMLModeler.getEditingDomain().run(
      new ResourceSetModifyOperation("Display Shape") {
 
       protected void execute(IProgressMonitor monitor)
         throws InvocationTargetException, InterruptedException {
 
        try {
         UMLModeler.getEditingDomain().run(
           new ResourceSetModifyOperation("display with default layout") {
 
            protected void execute(IProgressMonitor monitor)
              throws InvocationTargetException, InterruptedException {
 
             Collection models = UMLModeler.getOpenedModels();
             for (Iterator iter = models.iterator(); iter.hasNext();) {
 
              Model model = (Model) iter.next();
              if (model == null)
               out.println("Could not open model");
              else {
               createAndDisplayShape(model);
              }
             }
 
            }
           }, new NullProgressMonitor());
 
        } catch (InvocationTargetException e) {
         out.println(e);
         throw new RuntimeException(e);
        } catch (InterruptedException e) {
         out.println(e);
        }
 
       }
      }, new NullProgressMonitor());
 
   } catch (InvocationTargetException e) {
    out.println(e);
    throw new RuntimeException(e);
   } catch (InterruptedException e) {
    out.println(e);
   }
 
  }
 
  private Package addPackage(Model model, String name) {
 
   EClass newEClass = UML2Package.eINSTANCE.getPackage();
   Package aPackage = model.createOwnedMember(newEClass).getNearestPackage();
   aPackage.setName(name);
   return aPackage;
  }
 
  private Diagram addClassDiagram(Package aPackage, String name) {
   Diagram diagram;
   diagram = UMLModeler.getUMLDiagramHelper().createDiagram(aPackage,
     UMLDiagramKind.CLASS_LITERAL, aPackage);
   diagram.setName(name);
 
   return diagram;
  }
 
  /**
   * @param model
   */
  private void createAndDisplayShape(Model model) {
   Package aPackage = addPackage(model, "package1");
   Diagram diagram = addClassDiagram(aPackage, "diagram1");
 
   IUMLDiagramHelper helper = UMLModeler.getUMLDiagramHelper();
   Node node = helper.createNode(diagram, "rectangle");
   Bounds bounds = (Bounds) node.getLayoutConstraint();
   bounds.setHeight(8000);
   bounds.setWidth(7000);
   bounds.setX(5000);
   bounds.setY(5000);
 
   FillStyle fillStyle = (FillStyle) node.getStyle(NotationPackage.eINSTANCE
     .getFillStyle());
   fillStyle.setFillColor(140000000);
 
   LineStyle lineStyle = (LineStyle) node.getStyle(NotationPackage.eINSTANCE
     .getLineStyle());
   lineStyle.setLineColor(120120050);
 
  }
 }

Disclaimer:

All source code and/or binaries attached to this document are referred to here as "the Program". IBM is not providing program services of any kind for the Program. IBM is providing the Program on an "AS IS" basis without warranty of any kind. IBM WILL NOT BE LIABLE FOR ANY ACTUAL, DIRECT, SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES OR FOR ANY ECONOMIC CONSEQUENTIAL DAMAGES (INCLUDING LOST PROFITS OR SAVINGS), EVEN IF IBM, OR ITS RESELLER, HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

See Also:

Downloads

  • Eclipse: https://www.ibm.com/developerworks/mydeveloperworks/wikis/form/anonymous/api/library/b7da455c-5c51-4706-91c9-dcca9923c303/document/765ac084-bb0f-4626-9e10-cf2ac011ddca/attachment/24203ee9-8be0-4f29-bef6-f00e0555eee7/media/DisplayShape.zip
  • OnLine(DisplayShape.java file)

Author: rsxapi

Mariana Alupului is an Information Developer for IBM Rational Software.

You can reach Mariana at malupulu@us.ibm.com