Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2008 Anyware Technologies 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: Anyware Technologies - initial API and implementation $Id: UpdateLinkedEReferenceDeferredCommand.java,v 1.6 2009/01/29 10:02:08 jlescot Exp $
 
 package org.eclipse.emf.ecoretools.diagram.edit.commands;
 
 import java.util.List;
 
 import  org.eclipse.core.runtime.IAdaptable;
 import  org.eclipse.core.runtime.IProgressMonitor;
 import  org.eclipse.core.runtime.IStatus;
 import  org.eclipse.gmf.runtime.common.core.command.CommandResult;
 import  org.eclipse.gmf.runtime.diagram.ui.util.EditPartUtil;
 import  org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand;
 import  org.eclipse.gmf.runtime.notation.Anchor;
 import  org.eclipse.gmf.runtime.notation.ConnectorStyle;
 import  org.eclipse.gmf.runtime.notation.Edge;
 import  org.eclipse.gmf.runtime.notation.IdentityAnchor;
 import  org.eclipse.gmf.runtime.notation.NotationFactory;
 import  org.eclipse.gmf.runtime.notation.NotationPackage;
 import  org.eclipse.gmf.runtime.notation.RelativeBendpoints;
 import  org.eclipse.gmf.runtime.notation.datatype.RelativeBendpoint;

Update the two EReference views so that the two views overlay each other. The target and source anchor are the same on each connection, and bendpoints are the same but on an inverted list.
Creation : 17 janv. 08

Author(s):
Gilles Cannenterre
 
 public class UpdateLinkedEReferenceDeferredCommand extends AbstractTransactionalCommand {
 
 	// The first EReference part
 
 	// The second EReference part (as the EOpposite of the first one)
Command used to synchronize two References parts defined as EOpposite each other. Graphical representation should overlay each other.
 
 		super(domain"LinkEReferenceDeferredCommand"null); //$NON-NLS-1$
 		this. = part1;
 		this. = part2;
 	}

See also:
org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor , org.eclipse.core.runtime.IAdaptable )
 
 	protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable infothrows ExecutionException {
 		if ( == null ||  == null) {
 			return CommandResult.newWarningCommandResult(.null);
 		}
 		RunnableWithResult<ObjectrefreshRunnable = new RunnableWithResult<Object>() {
 
 			private IStatus status;
 
 			private Object result;
 
 			public Object getResult() {
 				return ;
 			}
 
 			public void setStatus(IStatus status) {
 				this. = status;
 			}
 
 			public IStatus getStatus() {
 				return ;
 			}
 
 			public void run() {
 				if (false == .getNotationView() instanceof Edge || false == .getNotationView() instanceof Edge) {
 					return;
 				}
 
				Edge edge1 = (Edge) .getNotationView();
				Edge edge2 = (Edge) .getNotationView();
				// Verify source and target
				if (false == edge1.getTarget().equals(edge2.getSource()) || false == edge1.getSource().equals(edge2.getTarget())) {
					return;
				}
				// Refresh anchors
				if (edge1.getSourceAnchor() == null) {
					IdentityAnchor sourceAnchor = NotationFactory.eINSTANCE.createIdentityAnchor();
					sourceAnchor.setId("(0.5,0.5)"); //$NON-NLS-1$
					edge1.setSourceAnchor(sourceAnchor);
				}
				if (edge1.getTargetAnchor() == null) {
					IdentityAnchor targetAnchor = NotationFactory.eINSTANCE.createIdentityAnchor();
					targetAnchor.setId("(0.5,0.5)"); //$NON-NLS-1$
					edge1.setTargetAnchor(targetAnchor);
				}
				if (false == edge1.getTargetAnchor().equals(edge2.getSourceAnchor())) {
					edge2.setSourceAnchor((Anchor) EcoreUtil.copy(edge1.getTargetAnchor()));
				}
				if (false == edge1.getSourceAnchor().equals(edge2.getTargetAnchor())) {
					edge2.setTargetAnchor((Anchor) EcoreUtil.copy(edge1.getSourceAnchor()));
				}
				// Refresh bendpoints
				if ((false == edge1.getBendpoints() instanceof RelativeBendpoints) || (false == edge2.getBendpoints() instanceof RelativeBendpoints)) {
					return;
				}
				RelativeBendpoints edge1Benpoints = (RelativeBendpoints) edge1.getBendpoints();
				if (edge1Benpoints.getPoints().isEmpty()) {
					return;
				}
				List<RelativeBendpoint> relativePoints = new ArrayList<RelativeBendpoint>();
				for (int index = edge1Benpoints.getPoints().size() - 1; index >= 0; index--) {
					RelativeBendpoint relativePoint = (RelativeBendpoint) edge1Benpoints.getPoints().get(index);
					relativePoints.add(new RelativeBendpoint(relativePoint.getTargetX(), relativePoint.getTargetY(), relativePoint.getSourceX(), relativePoint.getSourceY()));
				}
				RelativeBendpoints edge2Benpoints = (RelativeBendpoints) edge2.getBendpoints();
				@SuppressWarnings("unchecked")
				List<RelativeBendpoint> points = edge2Benpoints.getPoints();
				if (false == isBendpointEqual(pointsrelativePoints)) {
					edge2Benpoints.setPoints(relativePoints);
				}
				// Refresh routing
				ConnectorStyle edge1Style = (ConnectorStyle) edge1.getStyle(NotationPackage.eINSTANCE.getConnectorStyle());
				ConnectorStyle edge2Style = (ConnectorStyle) edge2.getStyle(NotationPackage.eINSTANCE.getConnectorStyle());
				if (edge1Style == null || edge2Style == null) {
					return;
				}
				if (false == edge1Style.getRouting().equals(edge2Style.getRouting())) {
					edge2Style.setRouting(edge1Style.getRouting());
				}
			}
		};
		EditPartUtil.synchronizeRunnableToMainThread(refreshRunnable);
		return CommandResult.newOKCommandResult();
	}
	protected boolean isBendpointEqual(List<RelativeBendpoint> relativePoints1List<RelativeBendpoint> relativePoints2) {
		if (relativePoints1.size() != relativePoints2.size()) {
			return false;
		}
		for (int index = 0; index < relativePoints1.size(); index++) {
			if (false == isRelativePointEqual(relativePoints1.get(index), relativePoints2.get(index))) {
				return false;
			}
		}
		return true;
	}
	private boolean isRelativePointEqual(RelativeBendpoint relativeBendpoint1, RelativeBendpoint relativeBendpoint2) {
		if (relativeBendpoint1.getSourceX() != relativeBendpoint2.getSourceX() || relativeBendpoint1.getTargetX() != relativeBendpoint2.getTargetX()
				|| relativeBendpoint1.getSourceY() != relativeBendpoint2.getSourceY() || relativeBendpoint1.getTargetY() != relativeBendpoint2.getTargetY()) {
			return false;
		}
		return true;
	}
New to GrepCode? Check out our FAQ X