/******************************************************************************* | |
* <copyright> | |
* | |
* Copyright (c) 2013, 2013 SAP AG. | |
* 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: | |
* SAP AG - initial API, implementation and documentation | |
* | |
* </copyright> | |
* | |
*******************************************************************************/ | |
package org.eclipse.fmc.blockdiagram.editor.features; | |
import org.eclipse.fmc.blockdiagram.editor.features.create.ConnectionCreateFeature; | |
import org.eclipse.fmc.blockdiagram.editor.model.ConnectionStyle; | |
import org.eclipse.fmc.blockdiagram.editor.util.FMCUtil; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IContext; | |
import org.eclipse.graphiti.features.context.IReconnectionContext; | |
import org.eclipse.graphiti.features.context.impl.CreateConnectionContext; | |
import org.eclipse.graphiti.features.context.impl.ReconnectionContext; | |
import org.eclipse.graphiti.features.impl.DefaultReconnectionFeature; | |
import org.eclipse.graphiti.mm.pictograms.Connection; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
import org.eclipse.graphiti.services.Graphiti; | |
/** | |
* This class extends DefaultReconnectionFeature and adds special behavior for | |
* dots connections and support for connecting the shape behind something | |
* non-connectable. Dots connections are special because it is possible to set the | |
* target to an invisible shape. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public class ReconnectionFeature extends DefaultReconnectionFeature { | |
public ReconnectionFeature(IFeatureProvider fp) { | |
super(fp); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.impl.DefaultReconnectionFeature#canReconnect | |
* (org.eclipse.graphiti.features.context.IReconnectionContext) | |
*/ | |
@Override | |
public boolean canReconnect(IReconnectionContext context) { | |
Connection con = context.getConnection(); | |
Object model = FMCUtil.getIndependentObject(con); | |
if (model == null) { | |
model = FMCUtil.getBO(con); | |
} | |
ConnectionCreateFeature feature = null; | |
feature = new ConnectionCreateFeature(getFeatureProvider(), "", "", | |
model, ConnectionStyle.NORMAL, null); | |
CreateConnectionContext ctx = new CreateConnectionContext(); | |
if (context.getTargetPictogramElement() instanceof Shape) { | |
Shape s = ConnectionCreateFeature | |
.getShapeBehindTextOrPicture((Shape) context | |
.getTargetPictogramElement()); | |
if (s != null && !s.getAnchors().isEmpty()) { | |
context.setTargetPictogramElement(s); | |
((ReconnectionContext) context).setNewAnchor(s.getAnchors() | |
.get(0)); | |
} | |
} | |
if (context.getOldAnchor() != null | |
&& context.getOldAnchor().eContainer() instanceof Shape) { | |
Shape shape = (Shape) context.getOldAnchor().eContainer(); | |
if (!shape.isVisible()) { | |
// It is a Dots Connection | |
return true; | |
} | |
} | |
if (ReconnectionContext.RECONNECT_SOURCE.equals(context | |
.getReconnectType())) { | |
ctx.setSourceAnchor(context.getNewAnchor()); | |
ctx.setTargetAnchor(con.getEnd()); | |
if (con.getEnd() != null) | |
ctx.setTargetPictogramElement(con.getEnd().getParent()); | |
if (context.getNewAnchor() != null) | |
ctx.setSourcePictogramElement(context.getNewAnchor() | |
.getParent()); | |
} else if (ReconnectionContext.RECONNECT_TARGET.equals(context | |
.getReconnectType())) { | |
ctx.setSourceAnchor(con.getStart()); | |
ctx.setTargetAnchor(context.getNewAnchor()); | |
ctx.setSourcePictogramElement(con.getStart().getParent()); | |
if (context.getNewAnchor() != null) | |
ctx.setTargetPictogramElement(context.getNewAnchor() | |
.getParent()); | |
} | |
return feature.canCreate(ctx); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.features.impl.DefaultReconnectionFeature#execute | |
* (org.eclipse.graphiti.features.context.IContext) | |
*/ | |
@Override | |
public void execute(IContext context) { | |
// Handle Dots connection with invisible anchor shape | |
if (context instanceof IReconnectionContext) { | |
IReconnectionContext reCtx = (IReconnectionContext) context; | |
if (reCtx.getNewAnchor() == null) { | |
Shape shape = (Shape) reCtx.getOldAnchor().eContainer(); | |
Graphiti.getGaService().setLocation( | |
shape.getGraphicsAlgorithm(), | |
reCtx.getTargetLocation().getX(), | |
reCtx.getTargetLocation().getY()); | |
} else | |
super.execute(context); | |
} else | |
super.execute(context); | |
} | |
} |