diff options
author | aradermache | 2010-12-16 09:41:13 +0000 |
---|---|---|
committer | aradermache | 2010-12-16 09:41:13 +0000 |
commit | 38cc6c00fdc6530bd5a27c4cbf1f28e3ea97b906 (patch) | |
tree | da9f09e193291717711c3f2f9c6d4fcd0243179a /extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src | |
parent | 3a4be288156825aa623931524622e692a2ed8a62 (diff) | |
download | org.eclipse.papyrus-38cc6c00fdc6530bd5a27c4cbf1f28e3ea97b906.tar.gz org.eclipse.papyrus-38cc6c00fdc6530bd5a27c4cbf1f28e3ea97b906.tar.xz org.eclipse.papyrus-38cc6c00fdc6530bd5a27c4cbf1f28e3ea97b906.zip |
copied to trunk
Diffstat (limited to 'extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src')
6 files changed, 585 insertions, 0 deletions
diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/Activator.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/Activator.java new file mode 100644 index 00000000000..c660b3cea70 --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/Activator.java @@ -0,0 +1,61 @@ +package org.eclipse.papyrus.conversion.di2todi;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+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.conversion.di2todi"; //$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;
+ }
+
+ /**
+ * Returns an image descriptor for the image file at the given
+ * plug-in relative path
+ *
+ * @param path the path
+ * @return the image descriptor
+ */
+ public static ImageDescriptor getImageDescriptor(String path) {
+ return imageDescriptorFromPlugin(PLUGIN_ID, path);
+ }
+}
diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/ElementType.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/ElementType.java new file mode 100644 index 00000000000..99aff38b68e --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/ElementType.java @@ -0,0 +1,26 @@ +/** + * @author: Manel Fredj - CEA + * This class in meant to centralize all the values of global variables that are used in the QVTO transformation + * At the current stage of implementation it is duplicated in ElementType.qvto + * As a future evolution, this class should be generated automatically from di plugin + */ + +package org.eclipse.papyrus.conversion.di2todi.blackboxes; + + +public class ElementType { + //diagram types in notation (Papyrus MDT-- P2) + public static String ClassDiagram_P2 = "PapyrusUMLClassDiagram"; + public static String CompositeStructure_P2 = "CompositeStructure"; + public static String ActivityDiagram_P2 = "PapyrusUMLActivityDiagram"; + public static String SequenceDiagram_P2 = ""; //todo: check for papyrus 2 the actual name + public static String ComponentDiagram_P2 = ""; //todo: check for papyrus 1 the actual name + + //diagram types in di2(Papyrus 1-- P1) + public static String ClassDiagram_P1 = "ClassDiagram"; + public static String CompositeStructure_P1 = "CompositeStructure"; + public static String ActivityDiagram_P1 = "ActivityDiagram"; + public static String SequenceDiagram_P1 = "SequenceDiagram"; + public static String ComponentDiagram_P1 = "ComponentDiagram"; + +} diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/UtilitiesLibrary.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/UtilitiesLibrary.java new file mode 100644 index 00000000000..f4c4188f02e --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/UtilitiesLibrary.java @@ -0,0 +1,154 @@ +/** + * @author: Manel Fredj - CEA + * This class includes all the operations that are called in the transformation QVTO from java. + * These operations are wrapped into a black-box + */ + +package org.eclipse.papyrus.conversion.di2todi.blackboxes; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.draw2d.geometry.Dimension; +import org.eclipse.draw2d.geometry.Point; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.NotationFactory; +import org.eclipse.gmf.runtime.notation.RelativeBendpoints; +import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint; +import org.eclipse.m2m.qvt.oml.blackbox.java.Operation; +import org.eclipse.swt.graphics.RGB; +import org.eclipse.uml2.uml.Element; +import org.eclipse.uml2.uml.Association; +import org.eclipse.uml2.uml.Generalization; +import org.eclipse.uml2.uml.Dependency; +import org.eclipse.uml2.uml.Realization; +public class UtilitiesLibrary { + + @Operation(contextual=true) + public static String getName(Diagram self) + { + return self.getName(); + } + + @Operation(contextual=true) + public static EObject convertElement(Element self) + { + return self; + } + + @Operation(contextual=true) + public static String getElementType(Element self) + { + if (self instanceof Association ) + return "Association"; + if (self instanceof Generalization ) + return "Generalization"; + else return null; + } + + @Operation(contextual=true) + public static boolean isGeneralization(Element self) + { + if (self instanceof Generalization ) + return true; + else return false; + } + + + @Operation(contextual=true) + public static boolean isAssociation(Element self) + { + if (self instanceof Association ) + return true; + else + return false; + } + @Operation(contextual=true) + public static boolean isRealization(Element self) + { + if (self instanceof Realization ) + return true; + else + return false; + } + @Operation(contextual=true) + public static boolean isDependency(Element self) + { + if (self instanceof Dependency ) + return true; + else + return false; + } + + @Operation(contextual=true) + public static int dimensionGetWidth(Dimension self) + { + if (self!=null) + return self.width; + else + return 0; + } + + @Operation(contextual=true) + public static int dimensionGetHeight(Dimension self) + { + if (self!=null) + return self.height; + else + return 0; + } + + @Operation(contextual=true) + public static int pointGetX(Point self) + { + if (self!=null) + { + + return self.x; + }else + return 0; + } + + @Operation(contextual=true) + public static int pointGetY(Point self) + { + if (self!=null) + { + return self.y; + } + else + return 0; + } + + @Operation(contextual=true) + public static String showcoordinates(Point self) + { + if (self!=null) + { + return "the x is "+ self.x+ "and the y is "+ self.y; + } + else + return "nothing to display"; + } + + @Operation(contextual=true) + public static int rgb2int(RGB self) + { + if (self!=null) + return self.green; + else + return 0; + } + + + public Object createBendpoints() + { + RelativeBendpoints bendpoints = NotationFactory.eINSTANCE.createRelativeBendpoints(); + List<RelativeBendpoint> points = new ArrayList<RelativeBendpoint>(2); + points.add(new RelativeBendpoint(0,0,0,0)); + points.add(new RelativeBendpoint(0,0,0,0)); + bendpoints.setPoints(points); + return bendpoints; + } +}
\ No newline at end of file diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/VisualIDs.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/VisualIDs.java new file mode 100644 index 00000000000..3739c93b95c --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/blackboxes/VisualIDs.java @@ -0,0 +1,34 @@ + +/** + * @author: Manel Fredj - CEA + * This class in meant to centralize all the values of the visualIds that are used in the QVTO transformation + * At the current stage of implementation it is duplicated in VisualIDs.qvto + * As a future evolution, this class should be generated automatically from di plugin + */ + +package org.eclipse.papyrus.conversion.di2todi.blackboxes; + + +public class VisualIDs { + + //for a class + public static String VisualId_Class="2008"; + + public static String VisualId_Class_label="5029"; + public static String VisualId_Class_Attrbutes="7017"; + public static String VisualId_Class_Operations="7018"; + public static String VisualId_Class_UseCase="7019"; + + public static String VisualId_Operation="3013"; + public static String VisualId_Attribute="3012"; + + //for an association + public static String VisualId_Edge="4001"; + + public static String VisualId_firstDecoNode="6001"; + public static String VisualId_secondDecoNode="6002"; + public static String VisualId_thirdDecoNode="6003"; + public static String VisualId_fourthDecoNode="6005"; + public static String VisualId_fifthDecoNode="6033"; + public static String VisualId_sixthDecoNode="6034"; +} diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/Di2toDiAction.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/Di2toDiAction.java new file mode 100644 index 00000000000..6126fc75e50 --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/Di2toDiAction.java @@ -0,0 +1,230 @@ +/** + * @author: Manel Fredj - CEA + * This class enables to invoke QVTO transformation in order to convert a diagram from di2 into di+notation + */ + +package org.eclipse.papyrus.conversion.di2todi.popupactions; + +import java.io.File; +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.IPath; +import org.eclipse.emf.common.util.EList; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.resource.URIConverter; +import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.dialogs.MessageDialog; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.m2m.internal.qvt.oml.ast.env.ModelExtentContents; +import org.eclipse.m2m.internal.qvt.oml.common.MdaException; +import org.eclipse.m2m.internal.qvt.oml.emf.util.ModelContent; +import org.eclipse.m2m.internal.qvt.oml.library.Context; +import org.eclipse.m2m.internal.qvt.oml.runtime.generator.TransformationRunner; +import org.eclipse.m2m.internal.qvt.oml.runtime.generator.TransformationRunner.In; +import org.eclipse.m2m.internal.qvt.oml.runtime.generator.TransformationRunner.Out; +import org.eclipse.m2m.internal.qvt.oml.runtime.project.QvtInterpretedTransformation; +import org.eclipse.m2m.internal.qvt.oml.runtime.project.TransformationUtil; +import org.eclipse.m2m.internal.qvt.oml.trace.Trace; +import org.eclipse.m2m.internal.qvt.oml.trace.TraceRecord; +import org.eclipse.m2m.qvt.oml.util.IContext; +import org.eclipse.papyrus.conversion.di2.util.Di2AdapterFactory; +import org.eclipse.papyrus.conversion.di2todi.Activator; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IActionDelegate; +import org.eclipse.ui.IObjectActionDelegate; +import org.eclipse.ui.IWorkbenchPart; + + + +@SuppressWarnings("restriction") +public class Di2toDiAction implements IObjectActionDelegate { + + private static final Map<?, ?> options = null; + private Shell shell; + private IPath inModelPath; //reference to selected Model + private String inAbsolutepath; + + /** + * Constructor for Action1. + */ + public Di2toDiAction() { + super(); + inModelPath=null; + inAbsolutepath=null; + } + + /** + * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart) + */ + public void setActivePart(IAction action, IWorkbenchPart targetPart) { + shell = targetPart.getSite().getShell(); + } + + /** + * @see IActionDelegate#run(IAction) + */ + public void run(IAction action) { + if (adapterFactory == null) { + // dummy operation to force loading/initialization of di2 plugin + adapterFactory = new Di2AdapterFactory(); + } + // Refer to an existing transformation via URI + URI transformationURI = URI.createURI("platform:/plugin/" + Activator.PLUGIN_ID + "/TransfoQvtoDi2toDi/Transfo.qvto"); + + //get the resources from the input URI + ResourceSet resourceSet = new ResourceSetImpl(); + try { + URI inURI = URI.createPlatformResourceURI(inModelPath.toString(), true); + Resource inResource = resourceSet.getResource(inURI, true); + // create the inputs + List<EObject> inObjects =inResource.getContents(); + ModelContent input_di2 = new ModelContent (inObjects); + ModelContent[] inputs = new ModelContent[1]; + inputs[0]=input_di2; + + // setup the execution environment details -> context + IContext mycontext = new Context(); + Trace trace= null; + + QvtInterpretedTransformation transformation = new QvtInterpretedTransformation (TransformationUtil.getQvtModule(transformationURI)); + In IntransformationRunner=new TransformationRunner.In(inputs, mycontext); + Out OuttransformationRunner=new TransformationRunner.Out(null,null, null); + + // running the transformation + OuttransformationRunner=transformation.run(IntransformationRunner); + + // retrieve the outputs + List<ModelExtentContents> outputs= OuttransformationRunner.getExtents(); + + // retrieve the trace + trace= OuttransformationRunner.getTrace(); + + if (trace !=null && outputs.size()==2) { + // processing the trace + URI Uri_trace = inURI.trimFileExtension().appendFileExtension("trace"); + EList<TraceRecord> outObjects_trace = trace.getTraceRecords(); + Resource outResource_trace = resourceSet.createResource(Uri_trace); + outResource_trace.getContents().addAll(outObjects_trace); + + // processing the outputs + ModelExtentContents output_notation=outputs.get(0); + ModelExtentContents output_di=outputs.get(1); + + URI Uri_notation = inURI.trimFileExtension().appendFileExtension("notation"); + URI Uri_di = inURI.trimFileExtension().appendFileExtension("di"); + + // the output objects got captured in the output extent + List<EObject> outObjects_notation = output_notation.getAllRootElements(); + List<EObject> outObjects_di = output_di.getAllRootElements(); + + //Let's persist them using a resource for notation + Resource outResource_notation = resourceSet.createResource(Uri_notation ); + outResource_notation.getContents().addAll(outObjects_notation); + + // let's persist them using a resource for di + Resource outResource_di = resourceSet.createResource(Uri_di); + outResource_di.getContents().addAll(outObjects_di); + + try{ + outResource_notation.save(Collections.emptyMap()); + outResource_di.save(Collections.emptyMap()); + outResource_trace.save(Collections.emptyMap()); + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + //remove trace file + URIConverter uri= resourceSet.getURIConverter(); + uri.delete(Uri_trace, options); + } + // when the transformation succeeds + //1- restore the content of Di2 file i.e., di2 namespace + PapyrusNamespace.restoreDi2Namespace(this.inAbsolutepath); + //2-show dialog + MessageDialog.openInformation( + shell, + "Paryrus1 => Payrus MDT converter", + "Di2toDi Action was executed."); + } + catch (MdaException e1) { + // TODO Auto-generated catch block + e1.printStackTrace(); + } + catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + catch (RuntimeException e) { + // Shell shell = new Shell (); + MessageDialog.openError( + shell, + "Error during conversion", + e.toString ()); + e.printStackTrace (); + } + + + } + + /** + * @see IActionDelegate#selectionChanged(IAction, ISelection) + */ + public void selectionChanged(IAction action, ISelection selection) + { + this.inModelPath = null; + this.inAbsolutepath=null; + IPath di2Path=null; + IPath location = null; + + + + if (!(selection instanceof IStructuredSelection)) { + // no structured-selection + return; + } + IStructuredSelection structuredSelection = (IStructuredSelection) selection; + if (selection.isEmpty()) { + return; + } + + Object firstElement = structuredSelection.getFirstElement(); + + if (firstElement instanceof IFile) { + + IFile di2Select = (IFile) firstElement; + // retrieve the path of the file + di2Path=di2Select.getFullPath(); + location = ((IFile) firstElement).getLocation(); + if (location != null) { + // Get the file for the location + File file = location.toFile(); + if (file != null) { + // Add the absolute path to the list + this.inAbsolutepath = file.getAbsolutePath(); + } + + // check whether it is a .di2 file + String di2filename = di2Path.toString(); + if (di2filename.endsWith (".di2")) { + // change namespace of di2 + PapyrusNamespace.replaceNamespace(this.inAbsolutepath); + this.inModelPath = di2Path; + } + } + } + } + + Di2AdapterFactory adapterFactory = null; +} + diff --git a/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/PapyrusNamespace.java b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/PapyrusNamespace.java new file mode 100644 index 00000000000..5a17df64ff2 --- /dev/null +++ b/extraplugins/conversion/org.eclipse.papyrus.conversion.di2todi/src/org/eclipse/papyrus/conversion/di2todi/popupactions/PapyrusNamespace.java @@ -0,0 +1,80 @@ +/** + * @author: Manel Fredj - CEA + * This class enables to change the namespace of di2 metamodel from + * replace http://www.papyrusuml.org into http://www.papyrusuml.org/di2 + */ + +package org.eclipse.papyrus.conversion.di2todi.popupactions; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileWriter; +import java.io.IOException; + +public class PapyrusNamespace +{ + ///////////////////////////////////////////////////////////////////////////////////////////// + //replace http://www.papyrusuml.org -- by -- http://www.papyrusuml.org/di2 + + public static void replaceNamespace(String absolutepath) + { + try { + String oldNameSpace = "di2=\"http://www.papyrusuml.org\""; + String newNameSpace="di2=\"http://www.papyrusuml.org/di2\""; + String oldtext =readFileAsString(absolutepath); + // replace the namespace in the di2 file + String newtext = oldtext.replaceAll (oldNameSpace, newNameSpace); + + FileWriter writer = new FileWriter(absolutepath); + writer.write(newtext);writer.close(); + } + catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + public static void restoreDi2Namespace(String absolutepath) + { + try { + String oldNameSpace="di2=\"http://www.papyrusuml.org/di2\""; + String newNameSpace = "di2=\"http://www.papyrusuml.org\""; + + String oldtext =readFileAsString(absolutepath); + // replace the namespace in the di2 file + String newtext = oldtext.replaceAll (oldNameSpace, newNameSpace); + + FileWriter writer = new FileWriter(absolutepath); + writer.write(newtext);writer.close(); + } + catch (IOException ioe) { + ioe.printStackTrace(); + } + } + + /** + * @param filePath the name of the file to open. Not sure if it can accept + * cccURLs or just filenames. Path handling could be better, and buffer + * sizes are hardcoded + */ + private static String readFileAsString (String filePath) + throws java.io.IOException + { + byte[] buffer = new byte[(int) new File(filePath).length()]; + BufferedInputStream f = null; + try { + f = new BufferedInputStream(new FileInputStream(filePath)); + f.read(buffer); + } + finally { + if (f != null) { + try { + f.close(); + } + catch (IOException ignored) { + } + } + } + return new String(buffer); + } +} |