Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnsgar Radermacher2014-05-26 09:02:22 -0400
committerAnsgar Radermacher2014-05-26 09:22:17 -0400
commit756ff1883fadfd58c78406e9c1853c62692bd1b1 (patch)
tree8c5773b908506a78cc978f894cb878dff5d6d7a0 /examples
parentac1b6096b5e3ca02b97053e9df9321494f616b69 (diff)
downloadorg.eclipse.papyrus-756ff1883fadfd58c78406e9c1853c62692bd1b1.tar.gz
org.eclipse.papyrus-756ff1883fadfd58c78406e9c1853c62692bd1b1.tar.xz
org.eclipse.papyrus-756ff1883fadfd58c78406e9c1853c62692bd1b1.zip
Example for programmatic creation of diagram elements, see https://wiki.eclipse.org/Papyrus_Developer_Guide/CreateProgrammatically
Diffstat (limited to 'examples')
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.classpath7
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.project28
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.settings/org.eclipse.jdt.core.prefs7
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/META-INF/MANIFEST.MF17
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/build.properties4
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/plugin.xml63
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/Activator.java50
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CmdHandler.java46
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsCommand.java103
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsHandler.java68
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsCommand.java194
-rw-r--r--examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsHandler.java68
12 files changed, 655 insertions, 0 deletions
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.classpath b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.classpath
new file mode 100644
index 00000000000..098194ca4b7
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.classpath
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
+ <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.project b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.project
new file mode 100644
index 00000000000..fa4c72415fb
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.project
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>org.eclipse.papyrus.uml.diagram.example.programmaticcreation</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.ManifestBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ <buildCommand>
+ <name>org.eclipse.pde.SchemaBuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.pde.PluginNature</nature>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.settings/org.eclipse.jdt.core.prefs b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.settings/org.eclipse.jdt.core.prefs
new file mode 100644
index 00000000000..f42de363afa
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/.settings/org.eclipse.jdt.core.prefs
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
+org.eclipse.jdt.core.compiler.compliance=1.7
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.7
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/META-INF/MANIFEST.MF b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/META-INF/MANIFEST.MF
new file mode 100644
index 00000000000..5a43b70a8e8
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/META-INF/MANIFEST.MF
@@ -0,0 +1,17 @@
+Bundle-ManifestVersion: 2
+Bundle-Name: Programmaticsequence
+Bundle-SymbolicName: org.eclipse.papyrus.uml.diagram.example.programmaticcreation;singleton:=true
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.eclipse.papyrus.uml.diagram.example.programmaticcreation.Activator
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.eclipse.emf.ecore;bundle-version="2.9.0",
+ org.eclipse.papyrus.infra.emf;bundle-version="0.10.0",
+ org.eclipse.papyrus.uml.diagram.composite;bundle-version="0.10.0",
+ org.eclipse.papyrus.uml.diagram.sequence;bundle-version="0.10.0",
+ org.eclipse.uml2.uml;bundle-version="4.1.0",
+ org.eclipse.gmf.runtime.notation;bundle-version="1.7.0",
+ org.eclipse.gmf.runtime.diagram.core;bundle-version="1.7.0",
+ org.eclipse.papyrus.infra.core;bundle-version="0.10.0"
+Bundle-ActivationPolicy: lazy
+Bundle-RequiredExecutionEnvironment: JavaSE-1.7
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/build.properties b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/build.properties
new file mode 100644
index 00000000000..34d2e4d2dad
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/build.properties
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+ .
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/plugin.xml b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/plugin.xml
new file mode 100644
index 00000000000..3aaee74fecf
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/plugin.xml
@@ -0,0 +1,63 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<?eclipse version="3.2"?>
+<plugin>
+ <extension
+ point="org.eclipse.ui.menus">
+ <menuContribution
+ allPopups="true"
+ locationURI="popup:org.eclipse.papyrus.views.modelexplorer.modelexplorer.popup">
+ <menu
+ id="org.eclipse.papyrus.views.modelexplorer.popup.testSequenceDiag"
+ label="Test diagram creation">
+ </menu>
+ </menuContribution>
+ <menuContribution
+ allPopups="true"
+ locationURI="popup:org.eclipse.gmf.runtime.diagram.ui.DiagramEditorContextMenu?after=filtersMenu">
+ <menu
+ id="org.eclipse.papyrus.views.modelexplorer.popup.testSequenceDiag"
+ label="Test diagram creation">
+ </menu>
+ </menuContribution>
+ </extension>
+ <extension
+ point="org.eclipse.ui.commands">
+ <command
+ categoryId="org.eclipse.papyrus.editor.category"
+ defaultHandler="org.eclipse.papyrus.uml.diagram.example.programmaticcreation.CreateCompositeDiagramElementsHandler"
+ id="org.eclipse.papyrus.example.programmaticsequence.createCU"
+ name="dummy">
+ </command>
+ <command
+ categoryId="org.eclipse.papyrus.editor.category"
+ defaultHandler="org.eclipse.papyrus.uml.diagram.example.programmaticcreation.CreateSequenceDiagramElementsHandler"
+ id="org.eclipse.papyrus.example.programmaticsequence.createLifeLine"
+ name="dummy">
+ </command>
+ </extension>
+ <extension
+ point="org.eclipse.ui.menus">
+ <menuContribution
+ allPopups="false"
+ locationURI="popup:org.eclipse.papyrus.views.modelexplorer.popup.testSequenceDiag">
+ <command
+ commandId="org.eclipse.papyrus.example.programmaticsequence.createCU"
+ id="org.eclipse.papyrus.example.programmaticsequence.createCU.ID"
+ label="Create collaboration use"
+ style="push">
+ <visibleWhen
+ checkEnabled="true">
+ </visibleWhen>
+ </command>
+ <command
+ commandId="org.eclipse.papyrus.example.programmaticsequence.createLifeLine"
+ id="org.eclipse.papyrus.example.programmaticsequence.createLifeLine.ID"
+ label="Create lifelines and message"
+ style="push">
+ <visibleWhen
+ checkEnabled="true">
+ </visibleWhen>
+ </command>
+ </menuContribution>
+ </extension>
+</plugin>
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/Activator.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/Activator.java
new file mode 100644
index 00000000000..4bfb5b2b9f9
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/Activator.java
@@ -0,0 +1,50 @@
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends AbstractUIPlugin {
+
+ // The plug-in ID
+ public static final String PLUGIN_ID = "org.eclipse.papyrus.uml.diagram.example.programmaticcreation"; //$NON-NLS-1$
+
+ // The shared instance
+ private static Activator plugin;
+
+ /**
+ * The constructor
+ */
+ public Activator() {
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception {
+ super.start(context);
+ plugin = this;
+ }
+
+ /*
+ * (non-Javadoc)
+ * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(BundleContext context) throws Exception {
+ plugin = null;
+ super.stop(context);
+ }
+
+ /**
+ * Returns the shared instance
+ *
+ * @return the shared instance
+ */
+ public static Activator getDefault() {
+ return plugin;
+ }
+
+}
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CmdHandler.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CmdHandler.java
new file mode 100644
index 00000000000..4c7b467b7cf
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CmdHandler.java
@@ -0,0 +1,46 @@
+/*****************************************************************************
+ * Copyright (c) 2010 CEA LIST.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Ansgar Radermacher (CEA LIST) ansgar.radermacher@cea.fr - Initial API and implementation
+ *****************************************************************************/
+
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+
+
+import org.eclipse.core.commands.AbstractHandler;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
+import org.eclipse.ui.PlatformUI;
+
+
+/**
+ * Superclass for handlers. It converts a selection into an EObject
+ */
+public abstract class CmdHandler extends AbstractHandler {
+
+ /**
+ * Convert selected elements within model explorer or diagram to an eObject
+ */
+ public void updateSelectedEObject() {
+ // Retrieve selected elements
+ IStructuredSelection selection = (IStructuredSelection)PlatformUI
+ .getWorkbench().getActiveWorkbenchWindow()
+ .getSelectionService().getSelection();
+
+ if(selection != null) {
+ selectedEObject = EMFHelper.getEObject(selection.getFirstElement());
+ }
+ }
+
+ /**
+ * Store the selected EObject. Accessible for subclasses
+ */
+ protected EObject selectedEObject;
+}
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsCommand.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsCommand.java
new file mode 100644
index 00000000000..18283ce5f1c
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsCommand.java
@@ -0,0 +1,103 @@
+/*******************************************************************************
+ * All rights reserved. This program and the accompanying materials
+ * are property of the CEA, their use is subject to specific agreement
+ * with the CEA.
+ *
+ * Contributors:
+ * CEA LIST - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.workspace.AbstractEMFOperation;
+import org.eclipse.gmf.runtime.notation.BasicCompartment;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.Location;
+import org.eclipse.gmf.runtime.notation.Node;
+import org.eclipse.gmf.runtime.notation.NotationFactory;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
+import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
+import org.eclipse.papyrus.uml.diagram.composite.providers.UMLViewProvider;
+import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.CollaborationUse;
+import org.eclipse.uml2.uml.StructuredClassifier;
+
+
+
+public class CreateCompositeDiagramElementsCommand extends AbstractEMFOperation {
+
+ public CreateCompositeDiagramElementsCommand(TransactionalEditingDomain domain, ExecutionEvent event, Class context) {
+ super(domain, "add collaboration use"); //$NON-NLS-1$
+ m_context = context;
+ m_event = event;
+ }
+
+ /**
+ * Create a collaboration use element
+ *
+ * @param m_context
+ */
+ @Override
+ protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info)
+ throws ExecutionException {
+
+ try {
+ ISashWindowsContainer windowsContainer =
+ ServiceUtilsForHandlers.getInstance().getISashWindowsContainer(m_event);
+ Object model = windowsContainer.getActiveSashWindowsPage().getRawModel();
+ if(model instanceof PageRef) {
+ EObject diagramEObj = ((PageRef)model).getEmfPageIdentifier();
+ if(diagramEObj instanceof Diagram) {
+ Diagram diagram = (Diagram)diagramEObj;
+ View view = null;
+ // get first view
+ for(Object child : diagram.getChildren()) {
+ if(child instanceof View) {
+ if(((View)child).getElement() == m_context) {
+ view = (View)child;
+ }
+ }
+ }
+ CollaborationUse cu = ((Class)m_context).createCollaborationUse("TestCollaboration"); //$NON-NLS-1$
+ addCuToCompositeDiagram((Class) m_context, (Class) m_context, cu, view, diagram);
+ }
+ }
+ }
+ catch (ServiceException svcE) {
+ System.err.println(svcE.toString());
+ }
+ return Status.OK_STATUS;
+ }
+
+ public View addCuToCompositeDiagram(Class context, StructuredClassifier solutionClass, CollaborationUse cu, View view, Diagram diagram)
+ {
+ // add CU to compartment
+ Object compartment = view.getChildren().get(1);
+ View roleBindingView = compViewProvider.createCollaborationUse_3071(cu, (BasicCompartment)compartment, -1, true,
+ UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
+ Location location = NotationFactory.eINSTANCE.createLocation();
+ location.setX(50);
+ location.setY(40);
+ if(roleBindingView instanceof Node) {
+ ((Node)roleBindingView).setLayoutConstraint(location);
+ }
+ return roleBindingView;
+ }
+
+ public static UMLViewProvider compViewProvider = new UMLViewProvider();
+
+ protected ExecutionEvent m_event;
+
+ protected Class m_context;
+}
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsHandler.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsHandler.java
new file mode 100644
index 00000000000..3a3fb8cdb92
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateCompositeDiagramElementsHandler.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * All rights reserved. This program and the accompanying materials
+ * are property of the CEA, their use is subject to specific agreement
+ * with the CEA.
+ *
+ * Contributors:
+ * CEA LIST - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.operations.IOperationHistory;
+import org.eclipse.core.commands.operations.OperationHistoryFactory;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.emf.workspace.AbstractEMFOperation;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.NamedElement;
+
+/**
+ * Implementation class for ClassAction action
+ */
+public class CreateCompositeDiagramElementsHandler extends CmdHandler {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isEnabled() {
+ updateSelectedEObject();
+ if(selectedEObject instanceof Class) {
+ return true;
+ }
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ if(!(selectedEObject instanceof NamedElement)) {
+ return null;
+ }
+
+ final NamedElement element = (NamedElement)selectedEObject;
+
+ // 1. select possible connectors according to port types
+ // (only show compatible connectors check-box?)
+ // 2. select implementation group according to connector type
+
+ if(element instanceof Class) {
+ // container dialog: either extension, rule or interceptor
+ // how-to select? which? (and how-to add/remove?) - is standard dialog sufficient?
+ IOperationHistory history = OperationHistoryFactory.getOperationHistory();
+ try {
+ AbstractEMFOperation command = new CreateCompositeDiagramElementsCommand(TransactionUtil.getEditingDomain(selectedEObject), event, (Class) element);
+ history.execute(command, new NullProgressMonitor(), null);
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+}
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsCommand.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsCommand.java
new file mode 100644
index 00000000000..c4e86a47783
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsCommand.java
@@ -0,0 +1,194 @@
+/*******************************************************************************
+ * All rights reserved. This program and the accompanying materials
+ * are property of the CEA, their use is subject to specific agreement
+ * with the CEA.
+ *
+ * Contributors:
+ * CEA LIST - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.runtime.IAdaptable;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.transaction.TransactionalEditingDomain;
+import org.eclipse.emf.workspace.AbstractEMFOperation;
+import org.eclipse.gmf.runtime.notation.Bounds;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.Edge;
+import org.eclipse.gmf.runtime.notation.IdentityAnchor;
+import org.eclipse.gmf.runtime.notation.Node;
+import org.eclipse.gmf.runtime.notation.NotationFactory;
+import org.eclipse.gmf.runtime.notation.NotationPackage;
+import org.eclipse.gmf.runtime.notation.View;
+import org.eclipse.papyrus.infra.core.sasheditor.editor.ISashWindowsContainer;
+import org.eclipse.papyrus.infra.core.sashwindows.di.PageRef;
+import org.eclipse.papyrus.infra.core.services.ServiceException;
+import org.eclipse.papyrus.infra.emf.utils.ServiceUtilsForHandlers;
+import org.eclipse.papyrus.uml.diagram.sequence.part.UMLDiagramEditorPlugin;
+import org.eclipse.papyrus.uml.diagram.sequence.providers.UMLViewProvider;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.DurationConstraint;
+import org.eclipse.uml2.uml.DurationInterval;
+import org.eclipse.uml2.uml.Interaction;
+import org.eclipse.uml2.uml.Lifeline;
+import org.eclipse.uml2.uml.Message;
+import org.eclipse.uml2.uml.MessageOccurrenceSpecification;
+import org.eclipse.uml2.uml.UMLPackage;
+
+
+public class CreateSequenceDiagramElementsCommand extends AbstractEMFOperation {
+
+ public CreateSequenceDiagramElementsCommand(TransactionalEditingDomain domain, ExecutionEvent event, Class context) {
+ super(domain, "add lifelines and message"); //$NON-NLS-1$
+ m_context = context;
+ m_event = event;
+ }
+
+ /**
+ * Create a collaboration use element
+ *
+ * @param m_context
+ */
+ @Override
+ protected IStatus doExecute(IProgressMonitor monitor, IAdaptable info)
+ throws ExecutionException {
+
+ try {
+ ISashWindowsContainer windowsContainer =
+ ServiceUtilsForHandlers.getInstance().getISashWindowsContainer(m_event);
+ Object model = windowsContainer.getActiveSashWindowsPage().getRawModel();
+ if(model instanceof PageRef) {
+ EObject diagramEObj = ((PageRef)model).getEmfPageIdentifier();
+ if(diagramEObj instanceof Diagram) {
+ Diagram diagram = (Diagram)diagramEObj;
+ View interactionView = null;
+ // get first view
+ for(Object child : diagram.getChildren()) {
+ if(child instanceof View) {
+ if(((View)child).getElement() == m_context) {
+ interactionView = (View)child;
+ }
+ }
+ }
+ // create a new interaction
+ sequenceDiagViewProvider.createInteraction_2001(m_context, diagram, -1, true, UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
+
+ // create UML elements first
+ Interaction interaction = (Interaction) m_context;
+ Lifeline lifeline1 = interaction.createLifeline("TestLifeline1"); //$NON-NLS-1$
+ Lifeline lifeline2 = interaction.createLifeline("TestLifeline2"); //$NON-NLS-1$
+ MessageOccurrenceSpecification mosSrc = (MessageOccurrenceSpecification)
+ interaction.createFragment("source", //$NON-NLS-1$
+ UMLPackage.eINSTANCE.getMessageOccurrenceSpecification());
+ MessageOccurrenceSpecification mosDst = (MessageOccurrenceSpecification)
+ interaction.createFragment("target", //$NON-NLS-1$
+ UMLPackage.eINSTANCE.getMessageOccurrenceSpecification());
+ Message message = interaction.createMessage("from l1 to l2"); //$NON-NLS-1$
+ mosSrc.setMessage(message);
+ mosSrc.setCovered(lifeline1);
+ mosDst.setMessage(message);
+ mosDst.setCovered(lifeline2);
+ message.setSendEvent(mosSrc);
+ message.setReceiveEvent(mosDst);
+
+ DurationConstraint durationConstraint = (DurationConstraint)
+ interaction.createOwnedRule("constraint", UMLPackage.eINSTANCE.getDurationConstraint()); //$NON-NLS-1$
+ DurationInterval di = (DurationInterval)
+ durationConstraint.createSpecification("", null, UMLPackage.eINSTANCE.getDurationInterval()); //$NON-NLS-1$
+
+ // now add graphical elements
+ View lifelineV1 = addLifeline(lifeline1, interactionView, 40, 40);
+ View lifelineV2 = addLifeline(lifeline2, interactionView, 170, 40);
+ addDurationConstraint(durationConstraint, interactionView, 250, 100);
+ addMessage(message, diagram, lifelineV1, lifelineV2);
+ }
+ }
+ }
+ catch (ServiceException svcE) {
+ System.err.println(svcE.toString());
+ }
+ return Status.OK_STATUS;
+ }
+
+ /**
+ * Add a lifeline view for a given UML lifeline within an interaction view
+ * @param lifeline the UML lifeline
+ * @param interactionView the view associated with the interaction
+ * @param x layout hint
+ * @param y layout hint
+ * @return the created view
+ */
+ public View addLifeline(Lifeline lifeline, View interactionView, int x, int y)
+ {
+ // get first compartment of view
+ Object compartment = interactionView.getChildren().get(1);
+ Node lifelineView = sequenceDiagViewProvider.createLifeline_3001(lifeline, (View) compartment, -1, true,
+ UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
+ Bounds location = NotationFactory.eINSTANCE.createBounds();
+ location.setX(x);
+ location.setY(y);
+ if(lifelineView instanceof Node) {
+ ((Node)lifelineView).setLayoutConstraint(location);
+ }
+ return lifelineView;
+ }
+
+ /**
+ * Add a duration-constraint view for a given UML constraint within an interaction view
+ * @param durationContraint a duration constraint
+ * @param interactionView the view associated with the interaction
+ * @param x layout hint
+ * @param y layout hint
+ * @return the created view
+ */
+ public View addDurationConstraint(DurationConstraint durationContraint, View interactionView, int x, int y)
+ {
+ // get first compartment of view
+ Object compartment = interactionView.getChildren().get(1);
+ Node lifelineView = sequenceDiagViewProvider.createDurationConstraint_3021(durationContraint, (View) compartment, -1, true,
+ UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
+ Bounds location = NotationFactory.eINSTANCE.createBounds();
+ location.setX(x);
+ location.setY(y);
+ if(lifelineView instanceof Node) {
+ ((Node)lifelineView).setLayoutConstraint(location);
+ }
+ return lifelineView;
+ }
+
+ /**
+ * Add a message view for a given UML message within a given diagram
+ * @param message UML message
+ * @param diagram Sequence diagram
+ * @param lifelineSrcV view of source lifeline
+ * @param lifelineDstV view of destination lifeline
+ */
+ public void addMessage(Message message, Diagram diagram, View lifelineSrcV, View lifelineDstV)
+ {
+ View messageView = sequenceDiagViewProvider.createMessage_4004(message, diagram, -1, true,
+ UMLDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);
+ if(messageView instanceof Edge) {
+ Edge messageEdge = (Edge) messageView;
+ messageEdge.setSource(lifelineSrcV);
+ messageEdge.setTarget(lifelineDstV);
+ IdentityAnchor sourceAnchor = (IdentityAnchor)
+ messageEdge.createSourceAnchor(NotationPackage.eINSTANCE.getIdentityAnchor());
+ IdentityAnchor targetAnchor = (IdentityAnchor)
+ messageEdge.createSourceAnchor(NotationPackage.eINSTANCE.getIdentityAnchor());
+ sourceAnchor.setId("(1.0,0.7){R}"); //$NON-NLS-1$
+ targetAnchor.setId("(1.1,0.7){R}"); //$NON-NLS-1$
+ }
+ }
+
+ public static UMLViewProvider sequenceDiagViewProvider = new UMLViewProvider();
+
+ protected ExecutionEvent m_event;
+
+ protected Class m_context;
+}
diff --git a/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsHandler.java b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsHandler.java
new file mode 100644
index 00000000000..46229462f98
--- /dev/null
+++ b/examples/uml/org.eclipse.papyrus.uml.diagram.example.programmaticcreation/src/org/eclipse/papyrus/uml/diagram/example/programmaticcreation/CreateSequenceDiagramElementsHandler.java
@@ -0,0 +1,68 @@
+/*******************************************************************************
+ * All rights reserved. This program and the accompanying materials
+ * are property of the CEA, their use is subject to specific agreement
+ * with the CEA.
+ *
+ * Contributors:
+ * CEA LIST - initial API and implementation
+ *******************************************************************************/
+
+package org.eclipse.papyrus.uml.diagram.example.programmaticcreation;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.commands.operations.IOperationHistory;
+import org.eclipse.core.commands.operations.OperationHistoryFactory;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.transaction.util.TransactionUtil;
+import org.eclipse.emf.workspace.AbstractEMFOperation;
+import org.eclipse.uml2.uml.Class;
+import org.eclipse.uml2.uml.NamedElement;
+
+/**
+ * Implementation class for ClassAction action
+ */
+public class CreateSequenceDiagramElementsHandler extends CmdHandler {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean isEnabled() {
+ updateSelectedEObject();
+ if(selectedEObject instanceof Class) {
+ return true;
+ }
+ return false;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
+ */
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ if(!(selectedEObject instanceof NamedElement)) {
+ return null;
+ }
+
+ final NamedElement element = (NamedElement)selectedEObject;
+
+ // 1. select possible connectors according to port types
+ // (only show compatible connectors check-box?)
+ // 2. select implementation group according to connector type
+
+ if(element instanceof Class) {
+ // container dialog: either extension, rule or interceptor
+ // how-to select? which? (and how-to add/remove?) - is standard dialog sufficient?
+ IOperationHistory history = OperationHistoryFactory.getOperationHistory();
+ try {
+ AbstractEMFOperation command = new CreateSequenceDiagramElementsCommand(TransactionUtil.getEditingDomain(selectedEObject), event, (Class) element);
+ history.execute(command, new NullProgressMonitor(), null);
+ } catch (ExecutionException e) {
+ e.printStackTrace();
+ }
+ }
+ return null;
+ }
+}

Back to the top