diff options
author | Martin Fluegge | 2011-08-12 12:48:22 +0000 |
---|---|---|
committer | Martin Fluegge | 2011-08-12 12:48:22 +0000 |
commit | 89d37428937a39c9201b2b4ec87272d7fddf60a3 (patch) | |
tree | 6e286c1df3f14ab7cac71b79fd379bef373c3a68 /plugins/org.eclipse.emf.cdo.dawn.gmf | |
parent | eba4c1814f498107829abba586b7a309cf598ebf (diff) | |
download | cdo-89d37428937a39c9201b2b4ec87272d7fddf60a3.tar.gz cdo-89d37428937a39c9201b2b4ec87272d7fddf60a3.tar.xz cdo-89d37428937a39c9201b2b4ec87272d7fddf60a3.zip |
[332902] [Dawn] Decouple Dawn Runtime from integrated frameworks
https://bugs.eclipse.org/bugs/show_bug.cgi?id=332902
Diffstat (limited to 'plugins/org.eclipse.emf.cdo.dawn.gmf')
7 files changed, 1389 insertions, 7 deletions
diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/META-INF/MANIFEST.MF b/plugins/org.eclipse.emf.cdo.dawn.gmf/META-INF/MANIFEST.MF index b80aed16bc..424e81b382 100644 --- a/plugins/org.eclipse.emf.cdo.dawn.gmf/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/META-INF/MANIFEST.MF @@ -18,7 +18,7 @@ Require-Bundle: org.eclipse.ui;bundle-version="[3.6.0,4.0.0)", org.eclipse.gmf.runtime.notation.providers;bundle-version="[1.3.0,2.0.0)", org.eclipse.gmf.runtime.diagram.ui.resources.editor;bundle-version="[1.3.0,2.0.0)", org.eclipse.gmf.runtime.diagram.ui.resources.editor.ide;bundle-version="[1.2.0,2.0.0)", - org.eclipse.emf.cdo.dawn.util;bundle-version="[1.0.0,2.0.0)" + org.eclipse.emf.cdo.dawn.util;bundle-version="[2.0.0,3.0.0)" Bundle-ActivationPolicy: lazy Bundle-RequiredExecutionEnvironment: J2SE-1.5 Bundle-Vendor: %providerName @@ -30,5 +30,6 @@ Export-Package: org.eclipse.emf.cdo.dawn.gmf;version="2.0.0", org.eclipse.emf.cdo.dawn.gmf.notifications.impl;version="2.0.0", org.eclipse.emf.cdo.dawn.gmf.resources;version="2.0.0", org.eclipse.emf.cdo.dawn.gmf.synchronize;version="2.0.0", + org.eclipse.emf.cdo.dawn.gmf.util;version="2.0.0", org.eclipse.emf.cdo.dawn.transaction;version="2.0.0" Bundle-Localization: plugin diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/editors/impl/DawnGMFEditorSupport.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/editors/impl/DawnGMFEditorSupport.java index 15530b01b1..f1e8190de8 100644 --- a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/editors/impl/DawnGMFEditorSupport.java +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/editors/impl/DawnGMFEditorSupport.java @@ -14,8 +14,8 @@ import org.eclipse.emf.cdo.dawn.editors.IDawnEditor; import org.eclipse.emf.cdo.dawn.editors.impl.DawnAbstractEditorSupport; import org.eclipse.emf.cdo.dawn.gmf.appearance.DawnAppearancer; import org.eclipse.emf.cdo.dawn.gmf.notifications.impl.DawnGMFHandler; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater; import org.eclipse.emf.cdo.dawn.notifications.BasicDawnListener; -import org.eclipse.emf.cdo.dawn.util.DawnDiagramUpdater; import org.eclipse.emf.cdo.transaction.CDOTransaction; import org.eclipse.emf.cdo.view.CDOAdapterPolicy; import org.eclipse.emf.cdo.view.CDOView; diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/notifications/impl/DawnGMFHandler.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/notifications/impl/DawnGMFHandler.java index e6a32118a3..abf56190af 100644 --- a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/notifications/impl/DawnGMFHandler.java +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/notifications/impl/DawnGMFHandler.java @@ -14,8 +14,8 @@ import org.eclipse.emf.cdo.CDOObject; import org.eclipse.emf.cdo.common.revision.delta.CDOFeatureDelta; import org.eclipse.emf.cdo.dawn.editors.IDawnEditor; import org.eclipse.emf.cdo.dawn.gmf.synchronize.DawnConflictHelper; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater; import org.eclipse.emf.cdo.dawn.notifications.BasicDawnListener; -import org.eclipse.emf.cdo.dawn.util.DawnDiagramUpdater; import org.eclipse.emf.cdo.internal.dawn.bundle.OM; import org.eclipse.emf.cdo.transaction.CDOTransaction; import org.eclipse.emf.cdo.transaction.CDOTransactionConflictEvent; diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnChangeHelper.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnChangeHelper.java index 541f20df87..f6f591e813 100644 --- a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnChangeHelper.java +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnChangeHelper.java @@ -10,8 +10,8 @@ ******************************************************************************/ package org.eclipse.emf.cdo.dawn.gmf.synchronize; -import org.eclipse.emf.cdo.dawn.util.DawnDiagramUpdater; -import org.eclipse.emf.cdo.dawn.util.DawnResourceHelper; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnResourceHelper; import org.eclipse.emf.cdo.dawn.util.exceptions.EClassIncompatibleException; import org.eclipse.emf.cdo.internal.dawn.bundle.OM; @@ -91,7 +91,7 @@ public class DawnChangeHelper } /** - * genereic Method to create an EditPart by the given view + * generic Method to create an EditPart by the given view * * @param node * @param elementType diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnConflictHelper.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnConflictHelper.java index 26279daa3d..4a5f122a15 100644 --- a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnConflictHelper.java +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/synchronize/DawnConflictHelper.java @@ -14,7 +14,7 @@ import org.eclipse.emf.cdo.CDOObject; import org.eclipse.emf.cdo.CDOState; import org.eclipse.emf.cdo.dawn.editors.IDawnEditor; import org.eclipse.emf.cdo.dawn.gmf.appearance.DawnAppearancer; -import org.eclipse.emf.cdo.dawn.util.DawnDiagramUpdater; +import org.eclipse.emf.cdo.dawn.gmf.util.DawnDiagramUpdater; import org.eclipse.emf.cdo.internal.dawn.bundle.OM; import org.eclipse.emf.cdo.transaction.CDOTransaction; import org.eclipse.emf.cdo.util.CDOUtil; diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnDiagramUpdater.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnDiagramUpdater.java new file mode 100644 index 0000000000..471d0c86bc --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnDiagramUpdater.java @@ -0,0 +1,397 @@ +/******************************************************************************* + * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others. + * 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: + * Martin Fluegge - initial API and implementation + ******************************************************************************/ +package org.eclipse.emf.cdo.dawn.gmf.util; + +import org.eclipse.emf.cdo.dawn.internal.util.bundle.OM; + +import org.eclipse.net4j.util.om.trace.ContextTracer; + +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.util.ECrossReferenceAdapter; +import org.eclipse.emf.transaction.RecordingCommand; +import org.eclipse.emf.transaction.TransactionalEditingDomain; + +import org.eclipse.gef.EditPart; +import org.eclipse.gef.EditPartViewer; +import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil; +import org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart; +import org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalEditPolicy; +import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; +import org.eclipse.gmf.runtime.diagram.ui.services.editpart.EditPartService; +import org.eclipse.gmf.runtime.emf.core.util.CrossReferenceAdapter; +import org.eclipse.gmf.runtime.notation.Diagram; +import org.eclipse.gmf.runtime.notation.Edge; +import org.eclipse.gmf.runtime.notation.NotationPackage; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.ui.PlatformUI; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; + +/** + * @author Martin Fluegge + */ +public class DawnDiagramUpdater +{ + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, DawnDiagramUpdater.class); + + public static void refresh(final IGraphicalEditPart editPart) + { + TransactionalEditingDomain editingDomain = editPart.getEditingDomain(); + editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) + { + @Override + public void doExecute() + { + editPart.refresh(); + + if (editPart instanceof ConnectionEditPart) + { + DawnDiagramUpdater.refresh((IGraphicalEditPart)((ConnectionEditPart)editPart).getTarget()); + DawnDiagramUpdater.refresh((IGraphicalEditPart)((ConnectionEditPart)editPart).getSource()); + } + } + }); + } + + public static void refreshEditPart(EditPart editPart) + { + refeshEditpartInternal(editPart); + } + + public static void refreshEditPart(final EditPart editPart, DiagramDocumentEditor editor) + { + try + { + editor.getEditingDomain().runExclusive(new Runnable() + { + public void run() + { + DawnDiagramUpdater.refreshEditPart(editPart); + } + }); + } + catch (InterruptedException ex) + { + throw new RuntimeException(ex); + } + } + + public static void refreshEditCurrentSelected(TransactionalEditingDomain editingDomain) + { + editingDomain.getCommandStack().execute(new RecordingCommand(editingDomain) + { + + @Override + protected void doExecute() + { + // ((ExamplediagramDocumentProvider)getDocumentProvider()).changed(getEditorInput()); + ISelection selection = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService() + .getSelection(); + if (selection instanceof IStructuredSelection) + { + IStructuredSelection structuredSelection = (IStructuredSelection)selection; + if (structuredSelection.size() != 1) + { + return; + } + if (structuredSelection.getFirstElement() instanceof EditPart + && ((EditPart)structuredSelection.getFirstElement()).getModel() instanceof View) + { + EObject modelElement = ((View)((EditPart)structuredSelection.getFirstElement()).getModel()).getElement(); + List<?> editPolicies = CanonicalEditPolicy.getRegisteredEditPolicies(modelElement); + for (Iterator<?> it = editPolicies.iterator(); it.hasNext();) + { + CanonicalEditPolicy nextEditPolicy = (CanonicalEditPolicy)it.next(); + nextEditPolicy.refresh(); + } + } + } + } + }); + } + + private static void refeshEditpartInternal(EditPart editPart) + { + if (editPart != null) + { + try + { + + // EObject modelElement = ((View)(editPart).getModel()).getElement(); + // List editPolicies = CanonicalEditPolicy.getRegisteredEditPolicies(modelElement); + // for (Iterator it = editPolicies.iterator(); it.hasNext();) + // { + // CanonicalEditPolicy nextEditPolicy = (CanonicalEditPolicy)it.next(); + // nextEditPolicy.refresh(); + // } + + editPart.refresh(); + } + catch (Exception e) + { + e.printStackTrace(); + } + + if (editPart instanceof DiagramEditPart) + { + for (Object connectionEditPart : ((DiagramEditPart)editPart).getConnections()) + { + if (connectionEditPart instanceof EditPart) + { + if (((Edge)((EditPart)connectionEditPart).getModel()).getBendpoints() != null) + { + refeshEditpartInternal((EditPart)connectionEditPart); + } + } + } + } + + for (Object childEditPart : editPart.getChildren()) + { + if (childEditPart instanceof EditPart) + { + refeshEditpartInternal((EditPart)childEditPart); + } + } + } + } + + public static View findViewByContainer(EObject element) + { + if (element instanceof View) + { + return (View)element; + } + + if (element.eContainer() == null) + { + return null; + } + + if (element.eContainer() instanceof View) + { + return (View)element.eContainer(); + } + + return findViewByContainer(element.eContainer()); + } + + public static View findViewForModel(EObject object, DiagramDocumentEditor editor) + { + if (object == null) + { + return null; + } + for (EObject e : editor.getDiagram().eContents()) + { + if (e instanceof View) + { + View view = (View)e; + if (object.equals(view.getElement())) + { + if (TRACER.isEnabled()) + { + TRACER.format("FOUND View: {0} for view obj: {1} ", e, object); //$NON-NLS-1$ + } + } + return (View)e; + } + } + return null; + } + + public static View getViewFromObject(EObject element, DiagramDocumentEditor editor) + { + View view; + + if (element instanceof Diagram) + { + view = ViewUtil.getViewContainer(element); + return view; + } + + if (element instanceof View) + { + view = (View)element; + } + else + { + view = DawnDiagramUpdater.findViewByContainer(element); // something which is not view (Edge or Node) + if (view == null) + { + view = DawnDiagramUpdater.findViewForModel(element, editor); + } + } + return view; + } + + public static EditPart createOrFindEditPartIfViewExists(View view, DiagramDocumentEditor editor) + { + EditPart editPart = findEditPart(view, editor.getDiagramEditPart()); + + if (view != null) + { + if (editPart == null) // does not exist? + { + if (TRACER.isEnabled()) + { + TRACER.format("EditPart does not exist for view {0} ", view); //$NON-NLS-1$ + } + editPart = EditPartService.getInstance().createGraphicEditPart(view); + setParentEditPart(editor, view, editPart); + } + } + if (TRACER.isEnabled()) + { + TRACER.format("Found EditPart: {0} ", editPart); //$NON-NLS-1$ + } + return editPart; + } + + public static void setParentEditPart(final DiagramDocumentEditor editor, View view, EditPart editPart) + { + View viewParent = (View)view.eContainer(); + EditPart parentEditPart = findEditPart(viewParent, editor); + if (parentEditPart == null) + { + parentEditPart = editor.getDiagramEditPart(); + } + editPart.setParent(parentEditPart); + } + + public static EditPart findEditPart(View view, DiagramDocumentEditor dawnDiagramEditor) + { + DiagramEditPart diagramEditPart = dawnDiagramEditor.getDiagramEditPart(); + + for (Object e : diagramEditPart.getChildren()) + { + EditPart ep = (EditPart)e; + if (ep.getModel().equals(view)) + { + return ep; + } + } + + for (Object e : diagramEditPart.getConnections()) + { + EditPart ep = (EditPart)e; + if (ep.getModel().equals(view)) + { + return ep; + } + } + return null; + } + + public static EditPart findEditPart(View view, EditPart editPart) + { + EditPart ret; + + if (editPart.getModel().equals(view)) + { + return editPart; + } + + for (Object o : editPart.getChildren()) + { + EditPart child = (EditPart)o; + ret = findEditPart(view, child); + if (ret != null) + { + return ret; + } + } + + if (editPart instanceof DiagramEditPart) + { + for (Object o : ((DiagramEditPart)editPart).getConnections()) + { + EditPart child = (EditPart)o; + ret = findEditPart(view, child); + if (ret != null) + { + return ret; + } + } + } + return null; + } + + public static EditPart findEditPart(View view, EditPartViewer viewer) + { + return (EditPart)viewer.getEditPartRegistry().get(view); + } + + /** + * In a normal GMF environment the diagram is loaded from a xml resource. In this scenario the XMLHelper sets "null" + * to the element of edge. Thus the value is 'set' with a null value. We do not have this in our case because the + * element is carefully loaded from the CDO repository. But if the value is not set a getElement() call fills the + * element with the eContainer. See <b>org.eclipse.gmf.runtime.notation.impl.ViewImpl.isSetElement()</b>. This happens + * when the ConnectionEditPart is activated and the model registered. See + * <b>org.eclipse.gmf.runtime.diagram.ui.editparts.ConnectionEditPart.registerModel()</b> + * <p> + * In our scenario the Edges will be wrongly connected to the diagram itself, which makes the CanonicalEditingPolicy + * to remove and restore the edge. Long, short story, we must 'touch' the elements here to have the element set. + * + * @param diagram + */ + public static void initializeElement(Diagram diagram) + { + for (Object obj : diagram.getEdges()) + { + Edge edge = (Edge)obj; + if (!edge.isSetElement()) + { + boolean eDeliver = edge.eDeliver(); + edge.eSetDeliver(false); + edge.setElement(null); + edge.eSetDeliver(eDeliver); + } + } + } + + public static View findViewFromCrossReferences(EObject element) + { + CrossReferenceAdapter crossreferenceAdapter = (CrossReferenceAdapter)ECrossReferenceAdapter + .getCrossReferenceAdapter(element);// getCrossReferenceAdapter(element); + if (crossreferenceAdapter != null) + { + Collection<?> iinverseReferences = crossreferenceAdapter.getInverseReferencers(element, + NotationPackage.eINSTANCE.getView_Element(), null); + + for (Object f : iinverseReferences) + { + if (f instanceof View) + { + return (View)f; + } + } + } + return null; + } + + public static View findView(EObject element) + { + View view = DawnDiagramUpdater.findViewByContainer(element); + + if (view == null) + { + view = DawnDiagramUpdater.findViewFromCrossReferences(element); + } + return view; + } +} diff --git a/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnResourceHelper.java b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnResourceHelper.java new file mode 100644 index 0000000000..4bff247a5e --- /dev/null +++ b/plugins/org.eclipse.emf.cdo.dawn.gmf/src/org/eclipse/emf/cdo/dawn/gmf/util/DawnResourceHelper.java @@ -0,0 +1,984 @@ +/******************************************************************************* + * Copyright (c) 2004 - 2011 Eike Stepper (Berlin, Germany) and others. + * 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: + * Martin Fluegge - initial API and implementation + ******************************************************************************/ +package org.eclipse.emf.cdo.dawn.gmf.util; + +/** + * + * @author Martin Fluegge + */ + +//import org.eclipse.emf.cdo.internal.dawn.bundle.OM; + +import org.eclipse.emf.cdo.dawn.internal.util.bundle.OM; +import org.eclipse.emf.cdo.dawn.util.exceptions.EClassIncompatibleException; + +import org.eclipse.net4j.util.om.trace.ContextTracer; + +import org.eclipse.emf.common.notify.Adapter; +import org.eclipse.emf.common.util.URI; +import org.eclipse.emf.ecore.EAnnotation; +import org.eclipse.emf.ecore.EAttribute; +import org.eclipse.emf.ecore.EClass; +import org.eclipse.emf.ecore.EGenericType; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.emf.ecore.EOperation; +import org.eclipse.emf.ecore.EReference; +import org.eclipse.emf.ecore.EStructuralFeature; +import org.eclipse.emf.ecore.resource.Resource; +import org.eclipse.emf.ecore.resource.ResourceSet; +import org.eclipse.emf.ecore.util.EcoreUtil; +import org.eclipse.emf.ecore.xmi.XMIResource; +import org.eclipse.emf.ecore.xmi.XMLHelper; +import org.eclipse.emf.ecore.xmi.XMLResource; +import org.eclipse.emf.ecore.xmi.impl.XMIHelperImpl; +import org.eclipse.emf.ecore.xmi.impl.XMILoadImpl; +import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; +import org.eclipse.emf.ecore.xmi.impl.XMISaveImpl; +import org.eclipse.emf.ecore.xmi.impl.XMLHelperImpl; + +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.gef.EditPart; +import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart; +import org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor; +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.Node; +import org.eclipse.gmf.runtime.notation.RelativeBendpoints; +import org.eclipse.gmf.runtime.notation.View; +import org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.StringWriter; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * @since 1.0 + */ +public class DawnResourceHelper +{ + private static final ContextTracer TRACER = new ContextTracer(OM.DEBUG, DawnResourceHelper.class); + + public static String getXmiId(EObject eObject) + { + Resource xmiResource = eObject.eResource(); + if (xmiResource == null) + { + return null; + } + + return ((XMLResource)xmiResource).getID(eObject); + } + + public static String getXmiId(EObject eObject, Resource xmiResource) + { + if (xmiResource == null) + { + return null; + } + + return ((XMLResource)xmiResource).getID(eObject); + } + + public static void setXmiId(EObject eObject, String id) + { + Resource xmiResource = eObject.eResource(); + if (xmiResource != null) + { + ((XMLResource)xmiResource).setID(eObject, id); + } + else + { + if (TRACER.isEnabled()) + { + TRACER.trace("Could set xmi id, because object is not attached to a resource!"); //$NON-NLS-1$ + } + } + } + + // public static String resourceToString(Resource res) + // { + // + // StringBuffer s = new StringBuffer(); + // Diagram dia = null; + // for (Object o : res.getContents()) + // { + // LOG.info(o); + // if (o instanceof Diagram) + // { + // dia = (Diagram)o; + // break; + // } + // } + // + // LOG.info("-----------------------------------------------------"); + // + // LOG.info("Diagram" + dia + ""); + // if (dia != null) + // { + // try + // { + // for (Object o : dia.getChildren()) + // { + // Node n = (Node)o; + // EObject element = n.getElement(); + // + // LOG.info(o + " (" + ResourceHelper.getXmiId((EObject)o) + ")"); + // + // for (EAttribute attribute : element.eClass().getEAllAttributes()) + // { + // LOG.info("\t" + attribute.getName() + ": " + element.eGet(attribute)); + // } + // + // LOG.info("- LayoutContraint: " + n.getLayoutConstraint() + ""); + // } + // + // EObject semanticConainer = dia.getElement(); + // LOG.info("SemanticConatiner" + semanticConainer + ""); + // + // for (EObject k : semanticConainer.eContents()) + // { + // LOG.info("Semantic Container Element: " + k + " (" + ResourceHelper.getXmiId(k) + ")" + ""); + // } + // } + // catch (Exception e) + // { + // e.printStackTrace(); + // } + // + // } + // LOG.info("-----------------------------------------------------"); + // return s.toString(); + // } + + public static Resource loadFromFile(String file, ResourceSet rsSet) + { + URI uri = URI.createURI("file:/" + file); + + Resource res = rsSet.getResource(uri, true); + return res; + } + + public static void writeToFile(String path, String xml) throws IOException + { + File file = new File(path); + FileWriter writer = new FileWriter(file); + writer.write(xml); + writer.close(); + } + + public static String saveToXML(XMLResource resource) throws IOException + { + + resource.setXMLVersion("1.0"); + + Map<Object, Object> saveOptions = new HashMap<Object, Object>(); + // options.put(XMLResource.OPTION_EXTENDED_META_DATA, Boolean.TRUE); + // options.put(XMLResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); + // options.put(XMLResource.OPTION_USE_ENCODED_ATTRIBUTE_STYLE, Boolean.TRUE); + saveOptions.putAll(resource.getDefaultSaveOptions()); + saveOptions.put(XMIResource.OPTION_DECLARE_XML, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_PROCESS_DANGLING_HREF, XMIResource.OPTION_PROCESS_DANGLING_HREF_DISCARD); + saveOptions.put(XMIResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_USE_XMI_TYPE, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_SAVE_TYPE_INFORMATION, Boolean.TRUE); + saveOptions.put(XMIResource.OPTION_SKIP_ESCAPE_URI, Boolean.FALSE); + saveOptions.put(XMIResource.OPTION_ENCODING, "UTF-8"); + + final XMLHelper xmlHelper = new XMIHelperImpl(resource); + + XMISaveImpl x = new XMISaveImpl(xmlHelper); + + StringWriter sw = new StringWriter(); + x.save(resource, sw, saveOptions); + return sw.toString(); + } + + public static Resource loadFromXML(String xml, ResourceSet rsSet) throws IOException + { + final Resource res; + + res = new XMIResourceImpl(); + final XMLHelper xmlHelper = new XMLHelperImpl(); + ByteArrayInputStream stringreader = new ByteArrayInputStream(xml.getBytes()); + XMILoadImpl xmiload = new XMILoadImpl(xmlHelper); + xmiload.load((XMLResource)res, stringreader, Collections.EMPTY_MAP); + + /* setdummy URI */ + if (res.getURI() == null) + { + res.setURI(URI.createURI("")); + } + return res; + } + + /** + * returns the diagram from the resource if no diagram can be found it returns null. + * + * @param res + * @return if it exists the diagram otherwise null + */ + public static Diagram getDiagramFromResource(Resource res) + { + for (Object o : res.getContents()) + { + if (o instanceof Diagram) + { + return (Diagram)o; + } + } + return null; + } + + /** + * compares two eObjects by there xmi-id whether the are equal or not + * + * @param oldNode + * @param newNode + * @return true if xmi-id equal, else otherwise + */ + public static boolean areSameObjects(EObject oldNode, EObject newNode) + { + String newXMI = getXmiId(newNode); + String oldXMI = getXmiId(oldNode); + + if (newXMI.equals(oldXMI)) + { + return true; + } + return false; + } + + /** + * returns the same Object in the other resource. The Object is identified by the xmi ID + * + * @param e + * @param resource + * @return an object in the given resource with the same xmi Id + */ + public static EObject getSameEObjectFromOtherResource(EObject e, XMLResource resource) + { + String xmiId = DawnResourceHelper.getXmiId(e); + // EObject ret = resource.getIDToEObjectMap().get(xmiId); + EObject ret = resource.getEObject(xmiId); + return ret; + } + + /** + * finds changed objects + * + * @param serverO + * @param clientO + * @return true if the obejcts are different + */ + public static boolean objectsHaveChanged(Object serverO, Object clientO) + { + if (serverO instanceof Node && clientO instanceof Node) + { + Node s = (Node)serverO; + Node c = (Node)clientO; + + if (objectsHaveChanged(s.getLayoutConstraint(), c.getLayoutConstraint())) + { + return true; + } + if (objectsHaveChanged(s.getElement(), c.getElement())) + { + return true; + } + + } + else if (serverO instanceof Edge && clientO instanceof Edge) // compare edges + { + Edge s = (Edge)serverO; + Edge c = (Edge)clientO; + RelativeBendpoints sr = (RelativeBendpoints)s.getBendpoints(); + RelativeBendpoints cr = (RelativeBendpoints)c.getBendpoints(); + if (sr.getPoints().size() != cr.getPoints().size()) + { + return true; + } + int i = 0; + for (Object o : sr.getPoints()) + { + RelativeBendpoint sb = (RelativeBendpoint)o; + RelativeBendpoint cb = (RelativeBendpoint)cr.getPoints().get(i); + if (objectsHaveChanged(sb, cb)) + { + return true; + } + i++; + } + } + else if (serverO instanceof RelativeBendpoint && clientO instanceof RelativeBendpoint) + { + RelativeBendpoint sb = (RelativeBendpoint)serverO; + RelativeBendpoint cb = (RelativeBendpoint)clientO; + if (sb.getSourceX() != cb.getSourceX()) + { + return true; + } + if (sb.getSourceY() != cb.getSourceY()) + { + return true; + } + if (sb.getTargetX() != cb.getTargetX()) + { + return true; + } + if (sb.getTargetY() != cb.getTargetY()) + { + return true; + } + } + else if (serverO instanceof Bounds && clientO instanceof Bounds) + { + if (((Bounds)serverO).getX() != ((Bounds)clientO).getX()) + { + return true; + } + if (((Bounds)serverO).getY() != ((Bounds)clientO).getY()) + { + return true; + } + if (((Bounds)serverO).getWidth() != ((Bounds)clientO).getWidth()) + { + return true; + } + if (((Bounds)serverO).getHeight() != ((Bounds)clientO).getHeight()) + { + return true; + } + } + else if (serverO instanceof EObject && clientO instanceof EObject) + { + EObject s = (EObject)serverO; + EObject c = (EObject)clientO; + + for (EAttribute attribute : s.eClass().getEAllAttributes()) + { + Object co = c.eGet(attribute); + Object so = s.eGet(attribute); + if (co == null || so == null) + { + return false; + } + if (!co.equals(so)) + { + return true; + } + } + + // checking size of chidlren + if (s.eContents().size() != c.eContents().size()) + { + return true; + } + int i = 0; + // chekcing every child + for (EObject sChild : s.eContents()) + { + EObject cChild = c.eContents().get(i); + if (objectsHaveChanged(sChild, cChild)) + { + return true; + } + i++; + } + } + else + { + if (TRACER.isEnabled()) + { + TRACER.format("classes are not identical...no match {0} / {1}", serverO, clientO); //$NON-NLS-1$ + } + } + return false; + } + + public static Resource loadResourceFromFileString(String filePath, ResourceSet rsSet) throws IOException + { + try + { + File file = new File(filePath); + FileReader reader = new FileReader(file); + BufferedReader Ein = new BufferedReader(reader); + String s; + StringBuffer buff = new StringBuffer(); + while ((s = Ein.readLine()) != null) // Null-Referenz + { + buff.append(s + System.getProperty("line.separator")); + } + Ein.close(); + Resource resource = DawnResourceHelper.loadFromXML(buff.toString(), rsSet); + return resource; + } + catch (FileNotFoundException e) + { + e.printStackTrace(); + } + return null; + } + + public static String readFromFile(String filePath) throws IOException + { + try + { + File file = new File(filePath); + FileReader reader = new FileReader(file); + BufferedReader Ein = new BufferedReader(reader); + String s; + StringBuffer buff = new StringBuffer(); + while ((s = Ein.readLine()) != null) // Null-Referenz + { + buff.append(s + System.getProperty("line.separator")); + } + Ein.close(); + return buff.toString(); + + } + catch (FileNotFoundException e) + { + e.printStackTrace(); + } + return null; + } + + /** + * copies the XMI-id from oldElement to new Element + * + * @param newElement + * @param oldElement + */ + public static void copyXmiId(EObject oldElement, EObject newElement) + { + DawnResourceHelper.setXmiId(newElement, DawnResourceHelper.getXmiId(oldElement)); + } + + /** + * sets the objects id in the old resource to the new resource + * + * @param v + * @param oldResource + */ + public static void setXmiId(EObject v, XMLResource oldResource) + { + DawnResourceHelper.setXmiId(v, getXmiId(v, oldResource)); + } + + /** + * creates a deep Copy and also copies xmi ids This method can be used if the copy should be insertet into newParent + * + * @param oldObject + * @param newParent + * @return the copy of the oldObject + */ + @SuppressWarnings("unchecked") + public static EObject createCopyAndInsert(EObject oldObject, EObject newParent) + { + EObject ret = EcoreUtil.copy(oldObject); + + EStructuralFeature containingFeature = oldObject.eContainingFeature(); + + Object get = newParent.eGet(containingFeature); + if (get instanceof Collection<?>) + { + Collection<EObject> list = (Collection<EObject>)get; + list.add(ret); + } + else + { + newParent.eSet(containingFeature, ret); + } + + setXmiIdForChildren(oldObject, ret); + + return ret; + } + + public static EObject createCopy(EObject v) + { + EObject ret = EcoreUtil.copy(v); + return ret; + } + + private static int setXmiIdForChildren(EObject oldChild, EObject newChild) + { + DawnResourceHelper.setXmiId(newChild, DawnResourceHelper.getXmiId(oldChild)); + int i = 0; + for (EObject oldC : oldChild.eContents()) + { + EObject newC = newChild.eContents().get(i); + setXmiIdForChildren(oldC, newC); + i++; + } + return i; + } + + /** + * This method provides deep copying of all childrens xmi ids from oldObject to newObject + * + * @param oldObject + * @param newObject + */ + public static void copyXmiIds(EObject oldObject, EObject newObject) + { + setXmiIdForChildren(oldObject, newObject); + } + + /** + * this Method copies the values from one Element to the other curretnly only for attributes and not for references + */ + @Deprecated + public static void updateElement(EObject oldElement, EObject newElement) + { + if (oldElement == null) + { + newElement = oldElement; + return; + } + + for (EAttribute attribute : oldElement.eClass().getEAllAttributes()) + { + newElement.eSet(attribute, oldElement.eGet(attribute)); + } + } + + /** + * @param leftObject + * @param rightObject + * @throws EClassIncompatibleException + */ + // TODO delte removed references + public static void updateEObject(EObject leftObject, EObject rightObject) throws EClassIncompatibleException + { + if (rightObject == null) // remove left Object + { + removeEObjectFromParent(leftObject); + return; + } + + if (!leftObject.eClass().equals(rightObject.eClass())) + { + throw new EClassIncompatibleException(leftObject.eClass().getName() + "(" + + DawnResourceHelper.getXmiId(leftObject) + ")" + "/" + rightObject.eClass().getName() + "(" + + DawnResourceHelper.getXmiId(leftObject) + ")"); + } + + updateEAttributes(leftObject, rightObject); + updateEReferences(leftObject, rightObject); + + for (Object o : rightObject.eContents()) + { + EObject rightChild = (EObject)o; + EObject leftChild = getSameEObjectFromOtherResource(rightChild, (XMLResource)leftObject.eResource()); + if (leftChild != null)// child exists + { + updateEObject(leftChild, rightChild); // removes it + } + else + // child must be created + { + if (o instanceof View) + { + View childView = (View)o; + EObject childViewElement = childView.getElement(); + View childViewCopy = (View)createCopyAndInsert(rightChild, leftObject); + if (childViewElement != null) + { + EObject childElementCopy = getSameEObjectFromOtherResource(childViewElement, + (XMLResource)leftObject.eResource()); + + updateEObject(childElementCopy, childViewElement);// not shure whether this is needed here + childViewCopy.setElement(childElementCopy); + } + + updateEObject(childViewCopy, childView);// not shure whether this is needed here + } + else + { + EObject childCopy = createCopyAndInsert(rightChild, leftObject); + updateEObject(childCopy, rightChild); // not shure whether this is needed here + } + } + } + + ArrayList<EObject> tempDeletionList = new ArrayList<EObject>(); + + for (Object leftChild : leftObject.eContents()) + { + tempDeletionList.add((EObject)leftChild); + } + + // here delete all which are not in the right object + for (EObject leftChild : tempDeletionList) + { + EObject rightChild = getSameEObjectFromOtherResource(leftChild, (XMLResource)rightObject.eResource()); + updateEObject(leftChild, rightChild); + } + } + + private static void removeEObjectFromParent(EObject leftObject) + { + if (leftObject instanceof View) + { + View view = (View)leftObject; + if (view.getElement() != null) + { + removeEObjectFromParent(view.getElement()); + } + } + + EObject parent = leftObject.eContainer(); + EStructuralFeature containingFeature = leftObject.eContainingFeature(); + if (parent != null) + { + Object container = parent.eGet(containingFeature); + + if (container instanceof Collection<?>) + { + Collection<?> collection = (Collection<?>)container; + collection.remove(leftObject); + } + else + { + parent.eSet(containingFeature, null); + } + } + else + { + if (TRACER.isEnabled()) + { + TRACER.format("arent is null, object ist still removed from parent : {0} ", leftObject); //$NON-NLS-1$ + } + } + } + + private static void updateEReferences(EObject leftObject, EObject rightObject) throws EClassIncompatibleException + { + for (EReference reference : rightObject.eClass().getEReferences()) + { + updateReference(leftObject, rightObject, reference); + } + } + + @SuppressWarnings("unchecked") + public static void updateReference(EObject leftParent, EObject rightParent, EReference reference) + throws EClassIncompatibleException + { + + if (reference.getName().equals("element")) + { + return; + } + Object leftReferenceObject = leftParent.eGet(reference); + Object rightReferenceObject = rightParent.eGet(reference); + + if (leftReferenceObject != null) + { + + if (leftReferenceObject instanceof Collection<?>) + { + Collection<EObject> leftCollection = (Collection<EObject>)leftReferenceObject; + Collection<?> rightCollection = (Collection<?>)rightReferenceObject; + for (Object o : rightCollection) + { + EObject rightCollectionChild = (EObject)o; + EObject leftCollectionChild = getSameEObjectFromOtherResource(rightCollectionChild, + (XMLResource)leftParent.eResource()); + + if (leftCollectionChild == null)// create + { + leftCollectionChild = DawnResourceHelper.createCopy(rightCollectionChild); + leftCollection.add(leftCollectionChild); + } + else + { + if (!rightCollectionChild.eResource().equals(leftParent.eResource())) // reference to another resource + { + leftCollection.remove(rightCollectionChild); + leftCollection.add(leftCollectionChild); + } + + updateEObject(leftCollectionChild, rightCollectionChild); + } + } + } + else + { + updateEObject((EObject)leftParent.eGet(reference), (EObject)rightParent.eGet(reference)); + } + } + else + // create + { + if (rightReferenceObject != null) + { + leftParent.eSet(reference, EcoreUtil.copy((EObject)rightReferenceObject)); + } + } + } + + private static void updateEAttributes(EObject leftObject, EObject rightObject) + { + for (EAttribute attribute : rightObject.eClass().getEAllAttributes()) + { + leftObject.eSet(attribute, rightObject.eGet(attribute)); + } + } + + /** + * @since 1.0 + */ + public static void deleteViewInResource(Resource resource, EObject e) + { + Diagram diagram = DawnResourceHelper.getDiagramFromResource(resource); + EObject element = ((View)e).getElement(); + + if (element != null) + { + removeElementFromContainer(element); + } + + if (e instanceof Node) + { + View node = (View)e; + diagram.removeChild(node);// ..getChildren().add(v); + @SuppressWarnings("unchecked") + List<Edge> toBeDeleted = new ArrayList<Edge>(node.getSourceEdges()); + for (Object obj : toBeDeleted) + { + Edge edge = (Edge)obj; + deleteViewInResource(resource, edge); + } + } + else if (e instanceof Edge) + { + Edge edge = (Edge)e; + diagram.removeEdge(edge);// ..getChildren().add(v); + edge.setSource(null); + edge.setTarget(null); + } + } + + private static void removeElementFromContainer(EObject element) + { + EStructuralFeature containingFeature = element.eContainingFeature(); + EObject container = element.eContainer(); + if (container != null) + { + Object get = container.eGet(containingFeature); + if (get instanceof Collection<?>) + { + Collection<?> list = (Collection<?>)get; + list.remove(element); + } + else + { + container.eSet(containingFeature, null); + } + } + } + + @SuppressWarnings("unchecked") + public static void addElementToContainer(EObject element, EObject container, EStructuralFeature containingFeature) + { + // EStructuralFeature containingFeature = element.eContainingFeature(); + Object get = container.eGet(containingFeature); + if (get instanceof Collection<?>) + { + Collection<EObject> list = (Collection<EObject>)get; + list.add(element); + } + else + { + container.eSet(containingFeature, element); + } + } + + /** + * checks if an element with element's id exists in the give resource + * + * @param res + * @param element + * @return true is the object with the same xmi id as element is contained in the given resource + */ + public static boolean isObjectInResource(Resource res, EObject element) + { + EObject object = res.getEObject(DawnResourceHelper.getXmiId(element)); + if (object == null) + { + return false; + } + + return true; + } + + public static void printResource(Resource r) + { + for (EObject o : r.getContents()) + { + printEObjectFull(o, 0); + } + } + + public static void printEObject(EObject o, int level) + { + for (Object child : o.eContents()) + { + printEObject((EObject)child, level + 1); + } + } + + public static void printEObjectFull(EObject o, int level) + { + print(tabs(level) + "--------------------------------------------"); + print(tabs(level) + "Object: " + o); + print(tabs(level) + "o.eContainer: " + o.eContainer()); + print(tabs(level) + "o.eContainingFeature: " + o.eContainingFeature()); + print(tabs(level) + "o.eContainmentFeature: " + o.eContainmentFeature()); + print(tabs(level) + "o.eIsProxy: " + o.eIsProxy()); + print(tabs(level) + "o.eResource: " + o.eResource()); + print(tabs(level) + "o.getClass: " + o.getClass()); + print(tabs(level) + "o.eClass: " + o.eClass()); + print(tabs(level) + "o.eCrossReferences: " + o.eCrossReferences()); + + print(tabs(level) + "o.eClass.getClassifierID: " + o.eClass().getClassifierID()); + print(tabs(level) + "o.eClass.getFeatureCount: " + o.eClass().getFeatureCount()); + print(tabs(level) + "o.eClass.getInstanceClassName: " + o.eClass().getInstanceClassName()); + print(tabs(level) + "o.eClass.getInstanceTypeName: " + o.eClass().getInstanceTypeName()); + print(tabs(level) + "o.eClass.getName: " + o.eClass().getName()); + print(tabs(level) + "o.eClass.getDefaultValue: " + o.eClass().getDefaultValue()); + print(tabs(level) + "o.eClass.getEPackage: " + o.eClass().getEPackage()); + print(tabs(level) + "o.eClass.getEIDAttribute: " + o.eClass().getEIDAttribute()); + print(tabs(level) + "o.eClass.getInstanceClass: " + o.eClass().getInstanceClass()); + + for (EAttribute attribute : o.eClass().getEAllAttributes()) + { + print(tabs(level + 1) + "o.eClass.getEAllAttributes.attribute: '" + attribute.getName() + "': " + + o.eGet(attribute)); + } + for (EAnnotation annotation : o.eClass().getEAnnotations()) + { + print(tabs(level + 1) + "o.eClass().getEAnnotations().annotation: '" + annotation); + } + for (EReference containment : o.eClass().getEAllContainments()) + { + print(tabs(level + 1) + "o.eClass().getEAllContainments().containment: '" + containment); + } + for (EGenericType genericSupertype : o.eClass().getEAllGenericSuperTypes()) + { + print(tabs(level + 1) + "o.eClass().getEAllGenericSuperTypes().genericSupertype: '" + genericSupertype); + } + for (EOperation operation : o.eClass().getEAllOperations()) + { + print(tabs(level + 1) + "o.eClass().getEAllOperations().operation: '" + operation); + } + + for (EReference reference : o.eClass().getEAllReferences()) + { + print(tabs(level + 1) + "o.eClass().getEAllReferences().reference: '" + reference); + } + for (EClass supertype : o.eClass().getEAllSuperTypes()) + { + print(tabs(level + 1) + "o.eClass().getEAllSuperTypes().supertype: '" + supertype); + } + for (EStructuralFeature structuralFeature : o.eClass().getEAllStructuralFeatures()) + { + print(tabs(level + 1) + "o.eClass().getEAllStructuralFeatures().structuralFeature: '" + structuralFeature); + } + for (EAnnotation annotation : o.eClass().getEAnnotations()) + { + print(tabs(level + 1) + "o.eClass().getEAnnotations().annotation: '" + annotation); + } + for (EAttribute attribute : o.eClass().getEAttributes()) + { + print(tabs(level + 1) + "o.eClass().getEAttributes().annotation: '" + attribute); + } + for (Adapter adapter : o.eClass().eAdapters()) + { + print(tabs(level + 1) + "o.eClass().eAdapters().adapter: '" + adapter); + } + + for (Object child : o.eContents()) + { + printEObjectFull((EObject)child, level + 2); + } + } + + private static String tabs(int level) + { + String s = ""; + for (int i = 0; i < level; i++) + { + s += "\t"; + } + return s; + } + + public static String getLocationFromResource(Resource r) throws MalformedURLException, IOException + { + return FileLocator.resolve(new URL(r.getURI().toString())).toString().replace("file:", ""); + } + + /**************************************************************************************************** + * finds EditPart from a View + * + * @param view + * @param dawnDiagramEditor + * @return find an Editpart for a view with the same xmi id + ***************************************************************************************************/ + @Deprecated + public static EditPart findEditPartByXMIId(View view, DiagramDocumentEditor dawnDiagramEditor) + { + DiagramEditPart diagramEditPart = dawnDiagramEditor.getDiagramEditPart(); + + for (Object e : diagramEditPart.getChildren()) + { + EditPart ep = (EditPart)e; + if (DawnResourceHelper.getXmiId((EObject)ep.getModel()).equals(DawnResourceHelper.getXmiId(view))) + { + return ep; + } + } + + for (Object e : diagramEditPart.getConnections()) + { + EditPart ep = (EditPart)e; + if (DawnResourceHelper.getXmiId((EObject)ep.getModel()).equals(DawnResourceHelper.getXmiId(view))) + { + return ep; + } + } + return null; + } + + protected static void print(String s) + { + if (TRACER.isEnabled()) + { + TRACER.trace(s); + } + } +} |