[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[gef3d-commits] r523 - trunk/org.eclipse.gef3d.examples.ecore/src/java/org/eclipse/gef3d/examples/ecore/diagram/parts

Author: jvonpilgrim
Date: 2010-10-30 18:46:46 -0400 (Sat, 30 Oct 2010)
New Revision: 523

Modified:
   trunk/org.eclipse.gef3d.examples.ecore/src/java/org/eclipse/gef3d/examples/ecore/diagram/parts/DiagramEditPart3D.java
Log:
- new semantic model edges (such as references) are automatically reflected in editor

Modified: trunk/org.eclipse.gef3d.examples.ecore/src/java/org/eclipse/gef3d/examples/ecore/diagram/parts/DiagramEditPart3D.java
===================================================================
--- trunk/org.eclipse.gef3d.examples.ecore/src/java/org/eclipse/gef3d/examples/ecore/diagram/parts/DiagramEditPart3D.java	2010-08-20 14:05:20 UTC (rev 522)
+++ trunk/org.eclipse.gef3d.examples.ecore/src/java/org/eclipse/gef3d/examples/ecore/diagram/parts/DiagramEditPart3D.java	2010-10-30 22:46:46 UTC (rev 523)
@@ -10,7 +10,13 @@
  ******************************************************************************/
 package org.eclipse.gef3d.examples.ecore.diagram.parts;
 
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.runtime.IAdaptable;
 import org.eclipse.draw2d.FigureListener;
@@ -20,18 +26,37 @@
 import org.eclipse.draw3d.IFigure3D;
 import org.eclipse.draw3d.geometry.Vector3fImpl;
 import org.eclipse.emf.ecore.EObject;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EAnnotationEditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EClass2EditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EClassEditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EDataType2EditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EDataTypeEditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EEnum2EditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EEnumEditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EPackage2EditPart;
 import org.eclipse.emf.ecoretools.diagram.edit.parts.EPackageEditPart;
+import org.eclipse.emf.ecoretools.diagram.edit.parts.EReferenceEditPart;
 import org.eclipse.emf.ecoretools.diagram.edit.policies.EPackageCanonicalEditPolicy;
 import org.eclipse.emf.ecoretools.diagram.edit.policies.EcoretoolsEditPolicyRoles;
+import org.eclipse.emf.ecoretools.diagram.part.EcoreDiagramUpdater;
+import org.eclipse.emf.ecoretools.diagram.part.EcoreLinkDescriptor;
 import org.eclipse.emf.ecoretools.diagram.part.EcoreVisualIDRegistry;
 import org.eclipse.gef.DragTracker;
+import org.eclipse.gef.EditPart;
 import org.eclipse.gef.Request;
+import org.eclipse.gef.commands.Command;
 import org.eclipse.gef.requests.SelectionRequest;
 import org.eclipse.gef.tools.DeselectAllTracker;
 import org.eclipse.gef3d.gmf.runtime.diagram.ui.figures.DiagramFigure3D;
+import org.eclipse.gmf.runtime.diagram.core.util.ViewUtil;
 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.internal.tools.RubberbandDragTracker;
+import org.eclipse.gmf.runtime.diagram.ui.requests.CreateConnectionViewRequest;
+import org.eclipse.gmf.runtime.diagram.ui.requests.RequestConstants;
 import org.eclipse.gmf.runtime.diagram.ui.tools.DragEditPartsTrackerEx;
+import org.eclipse.gmf.runtime.notation.Diagram;
+import org.eclipse.gmf.runtime.notation.Edge;
 import org.eclipse.gmf.runtime.notation.View;
 import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.widgets.Display;
@@ -55,6 +80,257 @@
 public class DiagramEditPart3D extends EPackageEditPart { // DiagramEditPart {
 
 	/**
+	 * EPackageCanonicalEditPolicySemanticUpdater
+	 * There should really be more documentation here.
+	 *
+	 * @author 	Jens von Pilgrim
+	 * @version	$Revision$
+	 * @since 	Oct 30, 2010
+	 */	
+	private final class EPackageCanonicalEditPolicySemanticUpdater extends
+			EPackageCanonicalEditPolicy {
+		/**
+		 * {@inheritDoc}
+		 * 
+		 * @see org.eclipse.emf.ecoretools.diagram.edit.policies.EPackageCanonicalEditPolicy#refreshSemantic()
+		 */
+		@Override
+		protected void refreshSemantic() {
+			// delete orphans and update connections
+			deleteOrphanedViews();
+			
+			// create views for newly added children
+			List<IAdaptable> createdViews = refreshSemanticChildren();
+			
+			List createdConnectionViews = new LinkedList();
+			createdConnectionViews.addAll(refreshSemanticConnections());
+			createdConnectionViews.addAll(refreshConnections());
+
+			
+			
+			
+			
+			
+			makeViewsImmutable(createdConnectionViews);
+			makeViewsImmutable(createdViews);
+		}
+		
+		/**
+		 * {@inheritDoc}
+		 * 
+		 * @see <a
+		 *      href="http://www.eclipse.org/forums/index.php?t=msg&&th=162860&goto=515348#msg_515348";>Sven
+		 *      Krause: Re: Synchronize semantic and notation
+		 *      information on editor start</a>
+		 * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalEditPolicy#getFactoryHint(org.eclipse.core.runtime.IAdaptable)
+		 */
+		@Override
+		protected String getFactoryHint(IAdaptable elementAdapter) {
+			CanonicalElementAdapter element =
+				(CanonicalElementAdapter) elementAdapter;
+			int visualID =
+				EcoreVisualIDRegistry.getNodeVisualID((View) getHost()
+					.getModel(), (EObject) element.getRealObject());
+			return EcoreVisualIDRegistry.getType(visualID);
+		}
+		
+		/**
+		 * @generated
+		 */
+		private Collection collectAllLinks(View view, Map domain2NotationMap) {
+			if (!EPackageEditPart.MODEL_ID.equals(EcoreVisualIDRegistry.getModelID(view))) {
+				return Collections.EMPTY_LIST;
+			}
+			Collection result = new LinkedList();
+			switch (EcoreVisualIDRegistry.getVisualID(view)) {
+			case EPackageEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEPackage_79ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EClassEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEClass_1001ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EPackage2EditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEPackage_1002ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EAnnotationEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEAnnotation_1003ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EDataTypeEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEDataType_1004ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EEnumEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEEnum_1005ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EClass2EditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEClass_2003ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EDataType2EditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEDataType_2004ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EEnum2EditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEEnum_2005ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			case EReferenceEditPart.VISUAL_ID: {
+				if (!domain2NotationMap.containsKey(view.getElement())) {
+					result.addAll(EcoreDiagramUpdater.getEReference_3002ContainedLinks(view));
+				}
+				if (!domain2NotationMap.containsKey(view.getElement()) || view.getEAnnotation("Shortcut") == null) { //$NON-NLS-1$
+					domain2NotationMap.put(view.getElement(), view);
+				}
+				break;
+			}
+			}
+			for (Iterator children = view.getChildren().iterator(); children.hasNext();) {
+				result.addAll(collectAllLinks((View) children.next(), domain2NotationMap));
+			}
+			for (Iterator edges = view.getSourceEdges().iterator(); edges.hasNext();) {
+				result.addAll(collectAllLinks((View) edges.next(), domain2NotationMap));
+			}
+			return result;
+		}
+		
+		/**
+		 * @generated
+		 */
+		private Diagram getDiagram() {
+			return ((View) getHost().getModel()).getDiagram();
+		}
+		
+		
+		
+		/**
+		 * @generated NOT
+		 */
+		private Collection refreshConnections() {
+			Map domain2NotationMap = new HashMap();
+			Collection linkDescriptors = collectAllLinks(getDiagram(), domain2NotationMap);
+			Collection existingLinks = new LinkedList(getDiagram().getEdges());
+			for (Iterator linksIterator = existingLinks.iterator(); linksIterator.hasNext();) {
+				Edge nextDiagramLink = (Edge) linksIterator.next();
+				int diagramLinkVisualID = EcoreVisualIDRegistry.getVisualID(nextDiagramLink);
+				if (diagramLinkVisualID == -1) {
+					if (nextDiagramLink.getSource() != null && nextDiagramLink.getTarget() != null) {
+						linksIterator.remove();
+					}
+					continue;
+				}
+				EObject diagramLinkObject = nextDiagramLink.getElement();
+				EObject diagramLinkSrc = nextDiagramLink.getSource().getElement();
+				EObject diagramLinkDst = nextDiagramLink.getTarget().getElement();
+				for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator.hasNext();) {
+					EcoreLinkDescriptor nextLinkDescriptor = (EcoreLinkDescriptor) linkDescriptorsIterator.next();
+					if (diagramLinkObject == nextLinkDescriptor.getModelElement() && diagramLinkSrc == nextLinkDescriptor.getSource() && diagramLinkDst == nextLinkDescriptor.getDestination()
+							&& diagramLinkVisualID == nextLinkDescriptor.getVisualID()) {
+						linksIterator.remove();
+						linkDescriptorsIterator.remove();
+					}
+				}
+			}
+			deleteViews(existingLinks.iterator());
+			return createConnections(linkDescriptors, domain2NotationMap);
+
+//			return Collections.emptyList();
+		}
+		
+		/**
+		 * @generated
+		 */
+		private EditPart getEditPart(EObject domainModelElement, Map domain2NotationMap) {
+			View view = (View) domain2NotationMap.get(domainModelElement);
+			if (view != null) {
+				return (EditPart) getHost().getViewer().getEditPartRegistry().get(view);
+			}
+			return null;
+		}
+		
+		/**
+		 * @generated
+		 */
+		private Collection createConnections(Collection linkDescriptors, Map domain2NotationMap) {
+			List adapters = new LinkedList();
+			for (Iterator linkDescriptorsIterator = linkDescriptors.iterator(); linkDescriptorsIterator.hasNext();) {
+				final EcoreLinkDescriptor nextLinkDescriptor = (EcoreLinkDescriptor) linkDescriptorsIterator.next();
+				EditPart sourceEditPart = getEditPart(nextLinkDescriptor.getSource(), domain2NotationMap);
+				EditPart targetEditPart = getEditPart(nextLinkDescriptor.getDestination(), domain2NotationMap);
+				if (sourceEditPart == null || targetEditPart == null) {
+					continue;
+				}
+				CreateConnectionViewRequest.ConnectionViewDescriptor descriptor = new CreateConnectionViewRequest.ConnectionViewDescriptor(nextLinkDescriptor.getSemanticAdapter(), null, ViewUtil.APPEND,
+						false, ((IGraphicalEditPart) getHost()).getDiagramPreferencesHint());
+				CreateConnectionViewRequest ccr = new CreateConnectionViewRequest(descriptor);
+				ccr.setType(RequestConstants.REQ_CONNECTION_START);
+				ccr.setSourceEditPart(sourceEditPart);
+				sourceEditPart.getCommand(ccr);
+				ccr.setTargetEditPart(targetEditPart);
+				ccr.setType(RequestConstants.REQ_CONNECTION_END);
+				Command cmd = targetEditPart.getCommand(ccr);
+				if (cmd != null && cmd.canExecute()) {
+					executeCommand(cmd);
+					IAdaptable viewAdapter = (IAdaptable) ccr.getNewObject();
+					if (viewAdapter != null) {
+						adapters.add(viewAdapter);
+					}
+				}
+			}
+			return adapters;
+		}
+	}
+
+	/**
 	 * Creates a new edit part for the given view.
 	 * 
 	 * @param i_diagramView the view
@@ -73,43 +349,8 @@
 	protected void createDefaultEditPolicies() {
 		super.createDefaultEditPolicies();
 		installEditPolicy(EcoretoolsEditPolicyRoles.PSEUDO_CANONICAL_ROLE,
-			new EPackageCanonicalEditPolicy() {
+			new EPackageCanonicalEditPolicySemanticUpdater());
 
-				/**
-				 * {@inheritDoc}
-				 * 
-				 * @see org.eclipse.emf.ecoretools.diagram.edit.policies.EPackageCanonicalEditPolicy#refreshSemantic()
-				 */
-				@Override
-				protected void refreshSemantic() {
-					// delete orphans and update connections
-					super.refreshSemantic();
-					// create views for newly added children
-					List<IAdaptable> createdViews = refreshSemanticChildren();
-					makeViewsImmutable(createdViews);
-				}
-
-				/**
-				 * {@inheritDoc}
-				 * 
-				 * @see <a
-				 *      href="http://www.eclipse.org/forums/index.php?t=msg&&th=162860&goto=515348#msg_515348";>Sven
-				 *      Krause: Re: Synchronize semantic and notation
-				 *      information on editor start</a>
-				 * @see org.eclipse.gmf.runtime.diagram.ui.editpolicies.CanonicalEditPolicy#getFactoryHint(org.eclipse.core.runtime.IAdaptable)
-				 */
-				@Override
-				protected String getFactoryHint(IAdaptable elementAdapter) {
-					CanonicalElementAdapter element =
-						(CanonicalElementAdapter) elementAdapter;
-					int visualID =
-						EcoreVisualIDRegistry.getNodeVisualID((View) getHost()
-							.getModel(), (EObject) element.getRealObject());
-					return EcoreVisualIDRegistry.getType(visualID);
-				}
-
-			});
-
 	}
 
 	/**