/******************************************************************************* | |
* <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.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.pictograms.PictogramElement; | |
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 feature adds independent texts (free text) to the diagram. | |
* | |
* @author Benjamin Schmeling | |
* | |
*/ | |
public class TextAddFeature extends AbstractAddShapeFeature { | |
/** | |
* The text value. | |
*/ | |
private final String text; | |
/** | |
* Determines if the text should be linked to a model object. | |
*/ | |
private final boolean linked; | |
/** | |
* Default constructor. | |
* | |
* @param featureProvider | |
* The feature provider. | |
*/ | |
public TextAddFeature(IFeatureProvider featureProvider) { | |
super(featureProvider); | |
this.linked = true; | |
this.text = "A Text"; | |
} | |
/** | |
* Constructor to define whether the shape should be linked to a model | |
* object. | |
* | |
* @param featureProvider | |
* The feature provider. | |
* @param linked | |
* True if the pictogram element should be linked to a model | |
* object. | |
*/ | |
public TextAddFeature(IFeatureProvider featureProvider, boolean linked) { | |
super(featureProvider); | |
this.linked = linked; | |
this.text = "A Text"; | |
} | |
/** | |
* Constructor to set the initial text. | |
* | |
* @param featureProvider | |
* The feature provider. | |
* @param text | |
* The default text. | |
*/ | |
public TextAddFeature(IFeatureProvider featureProvider, String text) { | |
super(featureProvider); | |
this.linked = true; | |
this.text = text; | |
} | |
/* | |
* (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(); | |
Shape s = pe.createShape(context.getTargetContainer(), true); | |
MultiText txt = ga.createMultiText(s, text); | |
txt.setForeground(manageColor(0, 0, 0)); | |
txt.setHorizontalAlignment(Orientation.ALIGNMENT_CENTER); | |
txt.setFont(ga.manageFont(getDiagram(), "Arial", 10, false, false)); | |
ga.setLocationAndSize(txt, context.getX(), context.getY(), 50, 20); | |
if (linked) | |
link(s, context.getNewObject()); | |
return s; | |
} | |
} |