Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » How to create UML elements from own popupactions
How to create UML elements from own popupactions [message #632249] Tue, 12 October 2010 05:38 Go to next message
Wladimir Schamai is currently offline Wladimir Schamai
Messages: 47
Registered: May 2010
Member
Dear Papyrus developers,

I am trying to creat a popup action for the Papyrus model browser popup menu that will create a UML Class and apply a stereotype to it. In the old Papyrus version we used the extension point org.eclipse.ui.popupMenus and actions (see below) that enables the menu based on the meta class (e.g. objectState name="isUmlType" value="Class"):
<action
               class="com.eadsiw.modelicaml.popupactions.CreateModelicaClassAction"
               enablesFor="1"
               icon="resources/icons/papyrus/Class.gif"
               id="com.eadsiw.modelicaml.popupaction.CreateModelicaClassAction"
               label="Modelica Class"
               menubarPath="com.eadsiw.modelicaml.popupaction.Menu/ModelicaML"
               tooltip="Add new Modelica Class">
            <enablement>
               <or>
                  <objectState
                        name="isUmlType"
                        value="Class">
                  </objectState>
                  <objectState
                        name="isUmlType"
                        value="Package">
                  </objectState>
               </or>
            </enablement>
         </action>

This is the code for the class that creates the element in the old Papyrus version:
package com.eadsiw.modelicaml.popupactions;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.notify.impl.NotificationImpl;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IViewPart;
import org.eclipse.uml2.uml.Stereotype;

import com.cea.papyrus.core.commands.UMLElementCreateCommand;
import com.cea.papyrus.core.editor.CommandStackUtils;
import org.eclipse.emf.common.notify.Notification;
import com.eadsiw.modelicaml.Activator;

/**
 * Implementation class for ClassAction action
 */
public class CreateModelicaClassAction extends Action {
	private org.eclipse.uml2.uml.Element selectedElement = null;

	private String stereotypPath = Platform.getResourceString(Activator.getDefault().getBundle(), "%p_path_ModelicaClassConstructs");

	private String stereotypName = Platform.getResourceString(Activator.getDefault().getBundle(), "%s_class");
	protected com.cea.papyrus.core.commands.PapyrusCommand papyrusCommand;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
		// get CommandStack
		CommandStack stack = (CommandStack) CommandStackUtils.getCommandStack();

		updateSelectedEObject();
		selectedElement = (org.eclipse.uml2.uml.Element) (getSelectedEObject()
				.get(0));

		// create new class
		UMLElementCreateCommand papyrusCommand = new UMLElementCreateCommand(
				org.eclipse.uml2.uml.Class.class, selectedElement);
		stack.execute(papyrusCommand);
		org.eclipse.uml2.uml.Class clazz = (org.eclipse.uml2.uml.Class) papyrusCommand
				.getElement();
				
		// apply ModelicaML Model Stereotyp to Class
		Stereotype stereotype = clazz
			.getApplicableStereotype(stereotypPath+"::"+stereotypName);
		if (stereotype==null) {
			Shell shell = new Shell();
			MessageDialog.openError(shell, "Error:", "Cannot apply the stereotype. You need to apply the ModelicaML profile to the model/package.");
		}
		else {
		clazz.applyStereotype(stereotype);
		}
		//clazz.eNotify(new NotificationImpl(Notification.SET, null, null)); // this is still buggy. Papyrus don't get the "model is dirty" notification after the element was added to the model.
	}

	public void init(IViewPart view) {
		// TODO Auto-generated method stub

	}
}


What is the best way to do it in the new Papyrus version?

Thanks for your support!!
Wladimir
Re: How to create UML elements from own popupactions [message #634005 is a reply to message #632249] Wed, 20 October 2010 05:09 Go to previous message
Remi Schnekenburger is currently offline Remi Schnekenburger
Messages: 127
Registered: July 2009
Senior Member
Hi Wladimir,

you could look in the plugins org.eclipse.papyrus.sysml.modelexplorer and org.eclipse.papyrus.sysml.service.types to see how we create SysML blocks directly from the model explorer, in the menu "create new child" etc.

Otherwise, there are many actions placed on the model explorer. For example, look into the org.eclipse.papyrus.uml.modelexplorer plugin.

The code you propose as an implementation of an action should take care of the fact that Papyrus as migrate to GMF and EMF Transaction.
Thus, it requires to encapsulate commands in a transaction, otherwise, you will have a read/write exception when modifying the model.
I invite you to read the documentation of EMF Transaction project.

Regards,
RĂ©mi


Previous Topic:Cannot install nightly build
Next Topic:Generalization strange behaviour
Goto Forum:
  


Current Time: Thu Aug 21 08:07:40 EDT 2014

Powered by FUDForum. Page generated in 0.09431 seconds