blob: 4ca38b86feec7eb50eec0367eb30b472680527b8 [file] [log] [blame]
/*******************************************************************************
* <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.util.StyleUtil;
import org.eclipse.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.mm.algorithms.MultiText;
import org.eclipse.graphiti.mm.algorithms.Polyline;
import org.eclipse.graphiti.mm.algorithms.Rectangle;
import org.eclipse.graphiti.mm.algorithms.styles.LineStyle;
import org.eclipse.graphiti.mm.algorithms.styles.Orientation;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
/**
* This feature adds the graphical representation of an Area Border to the
* diagram.
*
* @author Heiko Witteborg
*
*/
public class AreaBorderAddFeature extends ShapeAddFeature {
private static final int MIN_HEIGHT = 100;
private static final int MIN_WIDTH = 100;
/**
*
* @param fp
* The feature provider.
*/
public AreaBorderAddFeature(IFeatureProvider fp) {
super(fp);
super.minimumWidth = MIN_WIDTH;
super.minimumHeight = MIN_HEIGHT;
}
/**
*
* @param featureProvider
* The feature provider.
* @param linked
* True if the pictogram element should be linked to a model
* object.
*/
public AreaBorderAddFeature(IFeatureProvider featureProvider, boolean linked) {
super(featureProvider);
super.minimumWidth = MIN_WIDTH;
super.minimumHeight = MIN_HEIGHT;
super.linked = linked;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.fmc.blockdiagram.editor.features.add.ShapeAddFeature#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) {
ContainerShape container = context.getTargetContainer();
ContainerShape areaBorderContainer = pe.createContainerShape(container,
true);
Rectangle invRec = ga.createInvisibleRectangle(areaBorderContainer);
invRec.setStyle(StyleUtil.getStyle(getDiagram(), StyleUtil.SHAPE));
if (linked)
link(areaBorderContainer, context.getNewObject());
int w = context.getWidth();
int h = context.getHeight();
int min = 100;
// check that the longer dim is at least min
if (context.getWidth() > context.getHeight()) {
w = context.getWidth() < min ? min : w;
} else {
h = context.getHeight() < min ? min : h;
}
int margin = 2;
int lineWidth = 2;
int textHeight = 20;
int textWidth = 100;
// dashed line
Polyline line = ga.createPolyline(invRec);
line.setLineStyle(LineStyle.DASH);
line.setLineWidth(lineWidth);
line.setParentGraphicsAlgorithm(invRec);
line.setStyle(StyleUtil.getStyle(getDiagram(), StyleUtil.SHAPE));
// description text
MultiText txt = ga.createMultiText(invRec, "Area Border");
txt.setForeground(manageColor(0, 0, 0));
txt.setStyle(StyleUtil.getStyle(getDiagram(), StyleUtil.SHAPE));
// txt.setFont(ga.manageFont(getDiagram(), "Arial", 10, false, false));
txt.setParentGraphicsAlgorithm(invRec);
if (context.getWidth() > context.getHeight()) {
// position of containing rectangle
ga.setLocationAndSize(invRec, context.getX(), context.getY(), w, 3
* margin + lineWidth + textHeight);
// position of text field
txt.setHorizontalAlignment(Orientation.ALIGNMENT_RIGHT);
txt.setVerticalAlignment(Orientation.ALIGNMENT_BOTTOM);
ga.setLocationAndSize(txt, margin, margin, w - (2 * margin),
textHeight);
// position of dashed line
ga.setLocationAndSize(line, 0, 2 * margin + textHeight, w, 2);
line.getPoints().add(ga.createPoint(0, 0));
line.getPoints().add(ga.createPoint(w, 0));
} else {
// position of containing rectangle
ga.setLocationAndSize(invRec, context.getX(), context.getY(), 3
* margin + lineWidth + textWidth, h);
// position of dashed line
ga.setLocationAndSize(line, margin, 0, 2, h);
line.getPoints().add(ga.createPoint(0, 0));
line.getPoints().add(ga.createPoint(0, h));
// position of text field
txt.setHorizontalAlignment(Orientation.ALIGNMENT_LEFT);
txt.setVerticalAlignment(Orientation.ALIGNMENT_BOTTOM);
ga.setLocationAndSize(txt, 2 * margin + lineWidth, margin,
textWidth, h - 2 * margin);
}
return areaBorderContainer;
}
}