blob: d960c0387a2e47841b2ef852978eac85da3ad152 [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.graphiti.features.IFeatureProvider;
import org.eclipse.graphiti.features.context.IAddContext;
import org.eclipse.graphiti.mm.algorithms.RoundedRectangle;
import org.eclipse.graphiti.mm.pictograms.ContainerShape;
import org.eclipse.graphiti.mm.pictograms.PictogramElement;
import org.eclipse.graphiti.mm.pictograms.Shape;
import org.eclipse.graphiti.services.Graphiti;
import org.eclipse.graphiti.util.ColorConstant;
import static org.eclipse.fmc.blockdiagram.editor.features.add.DotsConnectionAddFeature.DOTS_SIZE;
/**
* This class is responsible for adding dots as a shape to the diagram.
*
* @author Benjamin Schmeling
*
*/
public class DotsShapeAddFeature extends ShapeAddFeature {
/**
* Main constructor.
*
* @param featureProvider
* The feature provider.
*/
public DotsShapeAddFeature(IFeatureProvider featureProvider) {
super(featureProvider);
}
/**
*
* @param featureProvider
* The feature provider.
* @param linked
* True if graphical object is linked to domain object.
*/
public DotsShapeAddFeature(IFeatureProvider featureProvider, boolean linked) {
super(featureProvider);
super.linked = linked;
}
/*
* (non-Javadoc)
*
* @see
* org.eclipse.graphiti.func.IAdd#add(org.eclipse.graphiti.features.context
* .IAddContext)
*/
@Override
public PictogramElement add(IAddContext context) {
ContainerShape newShape = pe.createContainerShape(
context.getTargetContainer(), true);
createGraphics(newShape, context);
if (linked)
link(newShape, context.getNewObject());
return newShape;
}
/**
* Creates the graphical representation of the dots shape.
*
* @param container
* The container shape to create the graphics algorithms for.
* @param context
* The add context.
*/
protected void createGraphics(ContainerShape container, IAddContext context) {
RoundedRectangle invRectangle = ga.createRoundedRectangle(container,
getWidth(context), getHeight(context));
invRectangle.setLineVisible(false);
invRectangle.setFilled(false);
ga.setLocationAndSize(invRectangle, context.getX(), context.getY(),
getWidth(context), getHeight(context));
// The layout depends on whether the widht or height is greater
// Horizontal layout
if (context.getWidth() > context.getHeight()) {
int gap = getDotsGap(getWidth(context));
for (int i = 0; i < DotsConnectionAddFeature.NO_OF_DOTS; i++) {
Shape nestedContainer = pe.createShape(container, false);
RoundedRectangle rectangle = ga.createRoundedRectangle(
nestedContainer, DOTS_SIZE, DOTS_SIZE);
ga.setLocationAndSize(rectangle, 0 + (i) * gap,
getHeight(context) / 2, DOTS_SIZE, DOTS_SIZE);
rectangle.setForeground(Graphiti.getGaService().manageColor(
getDiagram(), new ColorConstant(0, 0, 0)));
rectangle.setBackground(Graphiti.getGaService().manageColor(
getDiagram(), new ColorConstant(0, 0, 0)));
rectangle.setFilled(true);
}
// Vertical layout
} else {
int gap = getDotsGap(getHeight(context));
for (int i = 0; i < DotsConnectionAddFeature.NO_OF_DOTS; i++) {
Shape nestedContainer = pe.createShape(container, false);
RoundedRectangle rectangle = ga.createRoundedRectangle(
nestedContainer, DOTS_SIZE, DOTS_SIZE);
ga.setLocationAndSize(rectangle, getWidth(context) / 2, 0 + (i)
* gap, DOTS_SIZE, DOTS_SIZE);
rectangle.setForeground(Graphiti.getGaService().manageColor(
getDiagram(), new ColorConstant(0, 0, 0)));
rectangle.setBackground(Graphiti.getGaService().manageColor(
getDiagram(), new ColorConstant(0, 0, 0)));
rectangle.setFilled(true);
}
}
}
/**
* Calculates the gap between two dots based on the width of the shape.
*
* @param width
* The width of the shape.
* @return The gap between two dots symbols/decorators.
*/
public static int getDotsGap(int width) {
return (width - DOTS_SIZE) / 2;
}
}