/******************************************************************************* | |
* <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.algorithm.node; | |
import org.eclipse.emf.common.util.EList; | |
import org.eclipse.fmc.blockdiagram.editor.model.ShapeStyle; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCTypeChecker; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCTypeCheckerFactory; | |
import org.eclipse.graphiti.mm.algorithms.Polygon; | |
import org.eclipse.graphiti.mm.algorithms.styles.Point; | |
import org.eclipse.graphiti.mm.pictograms.PictogramElement; | |
/** | |
* This factory produces FMC node algorithms depending on the shape style or an | |
* already existing FMC node instance. The factory is responsible for creating | |
* the appropriate algorithm. This class is singleton. | |
* | |
* @author Benjamin Schmeling | |
* @author Heiko Witteborg | |
* | |
*/ | |
public class FMCNodeAlgorithmFactory { | |
private static FMCNodeAlgorithmFactory instance; | |
private FMCTypeChecker helper = FMCTypeCheckerFactory.getInstance(); | |
private RectangleStorageAlgorithm storage = null; | |
private HumanAgentAlgorithm humanAgent = null; | |
private RectangleAgentAlgorithm agent = null; | |
private LAgentAlgorithm lAgent = null; | |
private UAgentAlgorithm uAgent = null; | |
private LStorageAlgorithm lStorage = null; | |
private UStorageAlgorithm uStorage = null; | |
private FMCNodeAlgorithmFactory() { | |
} | |
/** | |
* Lazy initialization method of this factory. | |
* | |
* @return The singleton instance of this factory. | |
*/ | |
public static synchronized FMCNodeAlgorithmFactory getInstance() { | |
if (instance == null) { | |
instance = new FMCNodeAlgorithmFactory(); | |
} | |
return instance; | |
} | |
private RectangleAgentAlgorithm getAgent() { | |
if (agent == null) { | |
agent = new RectangleAgentAlgorithm(); | |
} | |
return agent; | |
} | |
public HumanAgentAlgorithm getHumanAgent() { | |
if (humanAgent == null) { | |
humanAgent = new HumanAgentAlgorithm(); | |
} | |
return humanAgent; | |
} | |
private RectangleStorageAlgorithm getStorage() { | |
if (storage == null) { | |
storage = new RectangleStorageAlgorithm(); | |
} | |
return storage; | |
} | |
private RectangleStorageAlgorithm getStructureVariance() { | |
if (storage == null) { | |
// TODO own algorithm for structure variances? | |
storage = new RectangleStorageAlgorithm(); | |
} | |
return storage; | |
} | |
private LAgentAlgorithm getLAgent() { | |
if (lAgent == null) { | |
lAgent = new LAgentAlgorithm(); | |
} | |
return lAgent; | |
} | |
private UAgentAlgorithm getUAgent() { | |
if (uAgent == null) { | |
uAgent = new UAgentAlgorithm(); | |
} | |
return uAgent; | |
} | |
private LStorageAlgorithm getLStorage() { | |
if (lStorage == null) { | |
lStorage = new LStorageAlgorithm(); | |
} | |
return lStorage; | |
} | |
private UStorageAlgorithm getUStorage() { | |
if (uStorage == null) { | |
uStorage = new UStorageAlgorithm(); | |
} | |
return uStorage; | |
} | |
/** | |
* Returns the singleton instance of the appropriate agent algorithm. | |
* | |
* @param style | |
* The shape style | |
* | |
* @return The AgentAlgorithm appropriate for the given shape style. | |
*/ | |
public AgentAlgorithm getAgentByShapestyle(ShapeStyle style) { | |
switch (style) { | |
case L: | |
return getLAgent(); | |
case U: | |
return getUAgent(); | |
case RECT: | |
default: | |
return getAgent(); | |
} | |
} | |
/** | |
* Returns the singleton instance of the appropriate storage algorithm. | |
* | |
* @param style | |
* The shape style | |
* | |
* @return The StorageAlgorithm appropriate for the given shape style. | |
*/ | |
public StorageAlgorithm getStorageByShapestyle(ShapeStyle style) { | |
switch (style) { | |
case L: | |
return getLStorage(); | |
case U: | |
return getUStorage(); | |
case RECT: | |
default: | |
return getStorage(); | |
} | |
} | |
/** | |
* Returns the appropriate algorithm for the already existing pictogram | |
* element. | |
* | |
* @param picto | |
* The pictogram element to find the algorithm for. | |
* @return The appropriate FMCNodeAlgorithm instance. | |
* | |
*/ | |
public FMCNodeAlgorithm getShape(PictogramElement picto) { | |
if (isAgentWithStyle(picto, ShapeStyle.L)) { | |
return getLAgent(); | |
} else if (isAgentWithStyle(picto, ShapeStyle.U)) { | |
return getUAgent(); | |
} else if (helper.isHumanAgent(picto)) | |
return getHumanAgent(); | |
else if (helper.isAgent(picto)) | |
return getAgent(); | |
else if (isStorageWithStyle(picto, ShapeStyle.L)) { | |
return getLStorage(); | |
} else if (isStorageWithStyle(picto, ShapeStyle.U)) { | |
return getUStorage(); | |
} else if (helper.isStorage(picto)) | |
return getStorage(); | |
else if (helper.isStructureVariance(picto)) | |
return getStructureVariance(); | |
return null; | |
} | |
private boolean isAgentWithStyle(PictogramElement picto, ShapeStyle style) { | |
if (!helper.isAgent(picto)) | |
return false; | |
if (style != ShapeStyle.RECT | |
&& picto.getGraphicsAlgorithm() instanceof Polygon) { | |
Polygon polygon = (Polygon) picto.getGraphicsAlgorithm(); | |
EList<Point> points = polygon.getPoints(); | |
if (style == ShapeStyle.L && points.size() == 6) | |
return true; | |
else if (style == ShapeStyle.U && points.size() == 8) | |
return true; | |
} else if (style == ShapeStyle.RECT) { | |
return (helper.isAgent(picto)); | |
} | |
return false; | |
} | |
private boolean isStorageWithStyle(PictogramElement picto, ShapeStyle style) { | |
if (!helper.isStorage(picto)) | |
return false; | |
if (style != ShapeStyle.RECT | |
&& picto.getGraphicsAlgorithm() instanceof Polygon) { | |
Polygon polygon = (Polygon) picto.getGraphicsAlgorithm(); | |
EList<Point> points = polygon.getPoints(); | |
if (style == ShapeStyle.L && points.size() == 6) | |
return true; | |
else if (style == ShapeStyle.U && points.size() == 8) | |
return true; | |
} else if (style == ShapeStyle.RECT) { | |
return helper.isStorage(picto); | |
} | |
return false; | |
} | |
} |