/******************************************************************************* | |
* <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.resize; | |
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithm; | |
import org.eclipse.fmc.blockdiagram.editor.algorithm.node.FMCNodeAlgorithmFactory; | |
import org.eclipse.graphiti.features.IFeatureProvider; | |
import org.eclipse.graphiti.features.context.IResizeShapeContext; | |
import org.eclipse.graphiti.mm.algorithms.GraphicsAlgorithm; | |
import org.eclipse.graphiti.mm.algorithms.MultiText; | |
import org.eclipse.graphiti.mm.pictograms.ContainerShape; | |
import org.eclipse.graphiti.mm.pictograms.Shape; | |
/** | |
* This class is responsible for the resizing of shapes that are container for | |
* other shapes that should also be resized. | |
* | |
* @author Benjamin Schmeling | |
* @author Heiko Witteborg | |
* | |
*/ | |
public class ContainerResizeFeature extends ResizeFeature { | |
// private static final int GAP = 10; | |
private FMCNodeAlgorithm fmcNode; | |
public ContainerResizeFeature(IFeatureProvider fp) { | |
super(fp); | |
} | |
@Override | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* org.eclipse.fmc.blockdiagram.editor.features.resize.ResizeFeature#resizeShape | |
* (org.eclipse.graphiti.features.context.IResizeShapeContext) | |
*/ | |
public final void resizeShape(IResizeShapeContext context) { | |
GraphicsAlgorithm resizeGa = context.getPictogramElement() | |
.getGraphicsAlgorithm(); | |
fmcNode = FMCNodeAlgorithmFactory.getInstance().getShape( | |
context.getPictogramElement()); | |
if (fmcNode != null) { | |
int oldContainerWidth = resizeGa.getWidth(); | |
int oldContainerHeight = resizeGa.getHeight(); | |
int oldContainerX = resizeGa.getX(); | |
int oldContainerY = resizeGa.getY(); | |
int newContainerWidth = context.getWidth(); | |
int newContainerHeight = context.getHeight(); | |
int newContainerX = context.getX(); | |
int newContainerY = context.getY(); | |
// ensure minimum size of fmc nodes | |
if (context.getWidth() < fmcNode.getMinimumWidth()) { | |
if (oldContainerX != context.getX()) | |
newContainerX = oldContainerX + oldContainerWidth | |
- fmcNode.getMinimumWidth(); | |
newContainerWidth = fmcNode.getMinimumWidth(); | |
} | |
if (context.getHeight() < fmcNode.getMinimumHeight()) { | |
if (oldContainerY != context.getY()) | |
newContainerY = oldContainerY + oldContainerHeight | |
- fmcNode.getMinimumHeight(); | |
newContainerHeight = fmcNode.getMinimumHeight(); | |
} | |
// Resize (invisible) first level ga of the fmc node | |
fmcNode.resize(resizeGa, newContainerX, newContainerY, | |
newContainerWidth, newContainerHeight); | |
// Resize (visible) ga children | |
if (context.getPictogramElement() instanceof ContainerShape) { | |
for (GraphicsAlgorithm childGa : resizeGa | |
.getGraphicsAlgorithmChildren()) { | |
resizeGaChild(childGa, newContainerWidth | |
- oldContainerWidth, newContainerHeight | |
- oldContainerHeight); | |
} | |
} | |
} else { | |
super.resizeShape(context); | |
} | |
} | |
/** | |
* | |
* @param ga | |
* @param widthDelta | |
* @param heightDelta | |
*/ | |
private void resizeGaChild(GraphicsAlgorithm ga, int widthDelta, | |
int heightDelta) { | |
int oldWidth, oldHeight; | |
oldWidth = ga.getWidth(); | |
oldHeight = ga.getHeight(); | |
fmcNode.resize(ga, ga.getX(), ga.getY(), oldWidth + widthDelta, | |
oldHeight + heightDelta); | |
// Graphiti.getGaService().setLocationAndSize(ga, ga.getX(), ga.getY(), | |
// oldWidth + widthDelta, oldHeight + heightDelta); | |
for (GraphicsAlgorithm childGa : ga.getGraphicsAlgorithmChildren()) | |
resizeGaChild(childGa, widthDelta, heightDelta); | |
} | |
private boolean isDirectGraphicsContainer(Shape shape) { | |
return !shape.isActive() | |
&& !(shape.getGraphicsAlgorithm() instanceof MultiText); | |
} | |
/** | |
* Resizes the child shape. | |
* | |
* @param container | |
* The container of the child that shall be resized. | |
* @param context | |
* The context referencing the child shape. | |
*/ | |
protected void resizeChild(ContainerShape container, | |
IResizeShapeContext context) { | |
super.resizeShape(context); | |
} | |
protected int getMaximumChildWidth(ContainerShape shape) { | |
int max = 0; | |
for (Shape child : shape.getChildren()) { | |
if (!isDirectGraphicsContainer(child)) { | |
if (max < child.getGraphicsAlgorithm().getWidth()) | |
max = child.getGraphicsAlgorithm().getWidth(); | |
} | |
} | |
return max; | |
} | |
protected int getMaximumChildHeight(ContainerShape shape) { | |
int max = 0; | |
for (Shape child : shape.getChildren()) { | |
if (!isDirectGraphicsContainer(child)) { | |
if (max < child.getGraphicsAlgorithm().getHeight()) | |
max = child.getGraphicsAlgorithm().getHeight(); | |
} | |
} | |
return max; | |
} | |
} |