/******************************************************************************* | |
* <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.add; | |
import org.eclipse.fmc.blockdiagram.editor.model.ConnectionStyle; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCType; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IAddConnectionContext; | |
import org.eclipse.graphiti.features.context.IAddContext; | |
import org.eclipse.graphiti.mm.algorithms.Ellipse; | |
import org.eclipse.graphiti.mm.algorithms.Polyline; | |
import org.eclipse.graphiti.mm.pictograms.Connection; | |
import org.eclipse.graphiti.mm.pictograms.ConnectionDecorator; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
import org.eclipse.graphiti.services.Graphiti; | |
import org.eclipse.graphiti.services.IGaService; | |
import org.eclipse.graphiti.services.IPeService; | |
/** | |
* This class is responsible for adding dots as a connection to the diagram. | |
* Notice that dots connection can be drawn with no target (only source). | |
* However, in the background, an invisible shape will be added. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public class DotsConnectionAddFeature extends ConnectionAddFeature { | |
/** | |
* The diameter of a dots decorator. | |
*/ | |
public static final int DOTS_SIZE = 6; | |
/** | |
* The number of circles drawn when a dots connection is created. | |
*/ | |
public static final int NO_OF_DOTS = 3; | |
/** | |
* Specifies whether the graphical element is bound to a domain object. | |
*/ | |
protected boolean linked = true; | |
/** | |
* The main cosntructor. | |
* | |
* @param featureProvider | |
* The feature provider. | |
* @param style | |
* The connection style. | |
*/ | |
public DotsConnectionAddFeature(IFeatureProvider featureProvider, ConnectionStyle style) { | |
super(featureProvider, style); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.func.IAdd#canAdd(org.eclipse.graphiti.features.context | |
* .IAddContext) | |
*/ | |
@Override | |
public boolean canAdd(IAddContext context) { | |
return true; | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.func.IAdd#add(org.eclipse.graphiti.features.context | |
* .IAddContext) | |
*/ | |
@Override | |
public PictogramElement add(IAddContext context) { | |
IPeService pe = Graphiti.getPeService(); | |
IGaService ga = Graphiti.getGaService(); | |
IAddConnectionContext cContext = (IAddConnectionContext) context; | |
Connection cShape = super.createConnection(); | |
Polyline line = ga.createPolyline(cShape); | |
line.setLineVisible(false); | |
cShape.setStart(cContext.getSourceAnchor()); | |
cShape.setEnd(cContext.getTargetAnchor()); | |
line.setForeground(manageColor(0, 0, 0)); | |
for (int i = 0; i < NO_OF_DOTS; i++) { | |
ConnectionDecorator deco = pe.createConnectionDecorator(cShape, | |
false, (0.8 / NO_OF_DOTS) * (i + 1), true); | |
Ellipse e = ga.createEllipse(deco); | |
e.setBackground(manageColor(0, 0, 0)); | |
e.setForeground(manageColor(0, 0, 0)); | |
ga.setSize(e, DOTS_SIZE, DOTS_SIZE); | |
} | |
if (linked) | |
link(cShape, ((FMCType) context.getNewObject())); | |
return cShape; | |
} | |
/** | |
* Checks whether the given connection is a dots connection using the number | |
* of decorators and their type and size. | |
* | |
* @param connection | |
* The connection to be checked. | |
* @return True if it is a dots connection. | |
*/ | |
public static boolean isDots(Connection connection) { | |
for (ConnectionDecorator decorator : connection | |
.getConnectionDecorators()) { | |
if (decorator.getGraphicsAlgorithm() instanceof Ellipse) { | |
Ellipse ellipse = (Ellipse) decorator.getGraphicsAlgorithm(); | |
if (ellipse.getWidth() != DOTS_SIZE | |
|| ellipse.getHeight() != DOTS_SIZE) { | |
return false; | |
} | |
} | |
} | |
return connection.getConnectionDecorators().size() == 3; | |
} | |
} |