/******************************************************************************* | |
* <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.emf.ecore.EAttribute; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.fmc.blockdiagram.editor.BlockDiagramMessages; | |
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.NamedNode; | |
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithmFactory; | |
import org.eclipse.fmc.blockdiagram.editor.model.ShapeStyle; | |
import org.eclipse.fmc.blockdiagram.editor.model.FMCType; | |
import org.eclipse.fmc.blockdiagram.editor.util.StyleUtil; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IAddContext; | |
import org.eclipse.graphiti.features.impl.AbstractAddShapeFeature; | |
import org.eclipse.graphiti.mm.algorithms.MultiText; | |
import org.eclipse.graphiti.mm.algorithms.styles.Orientation; | |
import org.eclipse.graphiti.mm.algorithms.styles.Style; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
import org.eclipse.graphiti.services.Graphiti; | |
import org.eclipse.graphiti.services.IGaService; | |
import org.eclipse.graphiti.services.IPeService; | |
/** | |
* This abstract class is the super class for all add features that add shapes. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public abstract class ShapeAddFeature extends AbstractAddShapeFeature { | |
protected int minimumHeight = 30; | |
protected int minimumWidth = 40; | |
protected int angle = 0; | |
protected int textMargin = NamedNode.DEFAULT_MARGIN; | |
protected boolean linked = true; | |
protected IPeService pe = Graphiti.getPeService(); | |
protected IGaService ga = Graphiti.getGaService(); | |
protected final ShapeStyle shapeType; | |
protected final String defaultShapeText; | |
protected final FMCNodeAlgorithmFactory factory = FMCNodeAlgorithmFactory | |
.getInstance(); | |
/** | |
* | |
* @param fp | |
* The feature provider. | |
*/ | |
public ShapeAddFeature(IFeatureProvider fp) { | |
super(fp); | |
this.shapeType = ShapeStyle.RECT; | |
this.defaultShapeText = BlockDiagramMessages.FMCAddFeature_DefaultShapeTitle; | |
} | |
/** | |
* @param textMargin | |
* the textMargin to set | |
*/ | |
void setTextMargin(int textMargin) { | |
this.textMargin = textMargin; | |
} | |
/** | |
* | |
* @param fp | |
* The feature provider. | |
* @param defaultText | |
* The default text. | |
* @param angle | |
* The angle. | |
*/ | |
public ShapeAddFeature(IFeatureProvider fp, String defaultText, int angle) { | |
super(fp); | |
this.shapeType = ShapeStyle.RECT; | |
this.defaultShapeText = defaultText; | |
this.angle = angle; | |
} | |
/** | |
* | |
* @param fp | |
* The feature provider. | |
* @param type | |
* The shape style. | |
*/ | |
public ShapeAddFeature(IFeatureProvider fp, ShapeStyle type) { | |
super(fp); | |
if (type == null) | |
this.shapeType = ShapeStyle.RECT; | |
else | |
this.shapeType = type; | |
this.defaultShapeText = BlockDiagramMessages.FMCAddFeature_DefaultShapeTitle; | |
} | |
/** | |
* | |
* @return The style used for the Blockdiagram Editor. | |
*/ | |
protected Style getStyle() { | |
Style style = StyleUtil.getStyle(getDiagram(), StyleUtil.SHAPE); | |
style.setAngle(angle); | |
return style; | |
} | |
/** | |
* Adds a MultiText element to the container shape. | |
* | |
* @param container | |
* The container shape this text is embedded in. | |
* @param x | |
* The offset in x direction. | |
* @param y | |
* The offset in y direction. | |
* @param width | |
* The width of the text area. | |
* @param height | |
* The height of the text area. | |
* @param hor | |
* The horizontal Orientation. | |
* @param ver | |
* The vertical Orientation. | |
* @param text | |
* The text to be set. | |
* @param linkedObj | |
* The object this text will be linked to. The object must | |
* provide a name attribute. | |
* @return The Text Algorithm. | |
*/ | |
protected MultiText addText(ContainerShape container, int x, int y, | |
int width, int height, Orientation hor, Orientation ver, | |
String text, Object linkedObj) { | |
if (text != null) { | |
Shape txtShape = pe.createShape(container, false); | |
MultiText txt = ga.createMultiText(txtShape, text); | |
txt.setValue(text); | |
// AdapterFactory factory = new I18NAdapterFactory(); | |
// factory.adapt(txt, I18NMultiTextAdapter.class); | |
// txtShape.eResource().getResourceSet().getAdapterFactories() | |
// .add(factory); | |
// txt.eAdapters().add(new I18NTextAdapter()); | |
txt.setHorizontalAlignment(hor); | |
txt.setVerticalAlignment(ver); | |
ga.setLocationAndSize(txt, x, y, width, height); | |
// txt.setFont(ga.manageFont(getDiagram(), | |
// BlockDiagramMessages.FMCAddFeature_FontType, 10, false, false)); | |
txt.setForeground(manageColor(0, 0, 0)); | |
txt.setStyle(StyleUtil.getStyle(getDiagram(), StyleUtil.SHAPE)); | |
if (linked) | |
linkTextWithModel(txtShape, linkedObj); | |
// txt.setParentGraphicsAlgorithm(container.getGraphicsAlgorithm()); | |
return txt; | |
} | |
return null; | |
} | |
/** | |
* Adds a MultiText element to the container shape. | |
* | |
* @param container | |
* The container shape this text is embedded in. | |
* @param hor | |
* The horizontal Orientation. | |
* @param ver | |
* The vertical Orientation. | |
* @param text | |
* The text to be set. | |
* @param linkedObj | |
* The object this text will be linked to. The object must | |
* provide a name attribute. | |
* @return The Text Algorithm. | |
*/ | |
protected MultiText addText(ContainerShape container, Orientation hor, | |
Orientation ver, String text, Object linkedObj) { | |
return addText(container, textMargin, textMargin, container | |
.getGraphicsAlgorithm().getWidth() - 2 * textMargin, container | |
.getGraphicsAlgorithm().getHeight() - 2 * textMargin, hor, ver, | |
text, linkedObj); | |
} | |
/** | |
* Adds a MultiText element to the container shape with the domain object | |
* name property as text. If the domain object does not provide a name the | |
* default text is given. | |
* | |
* @param container | |
* The container shape this text is embedded in. | |
* @param hor | |
* The horizontal Orientation. | |
* @param ver | |
* The vertical Orientation. | |
* @param linkedObj | |
* The object this text will be linked to. The object must | |
* provide a name attribute. | |
* @return The Text Algorithm. | |
*/ | |
protected MultiText addText(ContainerShape container, Orientation hor, | |
Orientation ver, Object linkedObj) { | |
String txt = getTextFromModel(linkedObj); | |
return addText(container, hor, ver, txt, linkedObj); | |
} | |
/** | |
* Adds a MultiText element to the container shape with default text and | |
* default vertical and horizontal orientation set to CENTER. | |
* | |
* @param container | |
* The container shape this text is embedded in. | |
* @param linkedObj | |
* The object this text will be linked to. The object must | |
* provide a name attribute. | |
* @return The Text Algorithm. | |
*/ | |
protected MultiText addText(ContainerShape container, Object linkedObj) { | |
return addText(container, Orientation.ALIGNMENT_CENTER, | |
Orientation.ALIGNMENT_CENTER, linkedObj); | |
} | |
/** | |
* Adds a MultiText element to the container shape with default text and | |
* default horizontal orientation set to CENTER. | |
* | |
* @param container | |
* The container shape this text is embedded in. | |
* @param orientation | |
* The vertical orientation. | |
* @param linkedObj | |
* The object this text will be linked to. The object must | |
* provide a name attribute. | |
*/ | |
protected void addText(ContainerShape container, Orientation orientation, | |
Object linkedObj) { | |
addText(container, Orientation.ALIGNMENT_CENTER, orientation, linkedObj); | |
} | |
protected void addText(ContainerShape container, int x, int y, int width, | |
int height, Object linkedObj) { | |
String txt = getTextFromModel(linkedObj); | |
addText(container, x, y, width, height, Orientation.ALIGNMENT_CENTER, | |
Orientation.ALIGNMENT_CENTER, txt, linkedObj); | |
} | |
@Override | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.graphiti.func.IAdd#canAdd(org.eclipse.graphiti.features.context | |
* .IAddContext) | |
*/ | |
public boolean canAdd(IAddContext context) { | |
return true; | |
} | |
/** | |
* Returns the width which is always greater or equal to the minimum width | |
* of this shape. | |
* | |
* @param context | |
* The add context providing the height. | |
* @return The width >= minimum height. | |
*/ | |
protected int getHeight(IAddContext context) { | |
if (context.getHeight() < minimumHeight) | |
return minimumHeight; | |
else | |
return context.getHeight(); | |
} | |
/** | |
* Returns the width which is always greater or equal to the minimum height | |
* of this shape. | |
* | |
* @param context | |
* The add context providing the width. | |
* @return The width >= minimum width. | |
*/ | |
protected int getWidth(IAddContext context) { | |
if (context.getWidth() < minimumWidth) | |
return minimumWidth; | |
else | |
return context.getWidth(); | |
} | |
protected void linkTextWithModel(Shape shape, Object obj) { | |
if (obj instanceof FMCType) | |
link(shape, FMCType.Text); | |
else { | |
link(shape, obj); | |
Graphiti.getLinkService().setLinkProperty(shape, "name"); | |
} | |
} | |
protected String getTextFromModel(Object obj) { | |
if (obj instanceof EObject) { | |
String name = null; | |
EObject eobj = ((EObject) obj); | |
for (EAttribute attr : eobj.eClass().getEAllAttributes()) { | |
if ("name".equals(attr.getName())) { | |
name = (String) eobj.eGet(attr); | |
if (name != null) | |
return name; | |
else { | |
eobj.eSet(attr, defaultShapeText); | |
return defaultShapeText; | |
} | |
} | |
} | |
} | |
return defaultShapeText; | |
} | |
} |