Skip to content
Snippets Groups Projects
Commit 8ffee5b0 authored by Florian Hölzl's avatar Florian Hölzl
Browse files

clean up layout utilities

parent 633f1f82
No related branches found
No related tags found
No related merge requests found
......@@ -37,20 +37,33 @@ import org.fortiss.tooling.kernel.base.ui.layout.constants.RecommendedLayoutCons
* @author hummel
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 3964EB0B7D1E388B567D8C31BCBD16F2
* @ConQAT.Rating YELLOW Hash: C35B3098DFF4E3BD3F5B0AB20DC39B30
*/
public class EditPartLayoutUtils {
/** Returns the given value aligned to the grid. */
/** Returns the given value aligned to the given grid size. */
public static int snapToGrid(double d, int gridSize) {
return gridSize * (int) Math.round(d / gridSize);
}
/** Returns the given value aligned to the recommended grid size. */
public static int snapToGrid(double d) {
return RecommendedLayoutConstants.GRID_SIZE
* (int) Math.round(d / RecommendedLayoutConstants.GRID_SIZE);
return snapToGrid(d, RecommendedLayoutConstants.GRID_SIZE);
}
/**
* Modifies the x and y coordinates of the given point to be on the grid of
* the given size.
*/
public static void snapToGrid(Point p, int gridSize) {
p.x = snapToGrid(p.x, gridSize);
p.y = snapToGrid(p.y, gridSize);
}
/** Modifies the x and y coordinates of the given point to be on the grid. */
public static void snapToGrid(Point p) {
p.x = snapToGrid(p.x);
p.y = snapToGrid(p.y);
p.x = snapToGrid(p.x, RecommendedLayoutConstants.GRID_SIZE);
p.y = snapToGrid(p.y, RecommendedLayoutConstants.GRID_SIZE);
}
/**
......@@ -132,61 +145,79 @@ public class EditPartLayoutUtils {
* redeemed.
*/
public static Dimension clampOvalShapeSize(Dimension oldSize, int direction) {
return clampOvalShapeSize(oldSize, direction,
RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_WIDTH,
RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_HEIGHT,
RecommendedLayoutConstants.OVAL_SHAPE_ASPECT_RATIO);
}
/**
* Ensure that the minimum size and aspect ratio of a oval shape are
* redeemed.
*/
public static Dimension clampOvalShapeSize(Dimension oldSize,
int direction, int minimumWidth, int minimumHeight,
double aspectRatio) {
Dimension newSize;
// check for minimum size
newSize = new Dimension(
oldSize.width < RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_WIDTH ? RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_WIDTH
: oldSize.width,
oldSize.height < RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_HEIGHT ? RecommendedLayoutConstants.OVAL_SHAPE_MINIMUM_HEIGHT
: oldSize.height);
newSize = new Dimension(oldSize.width < minimumWidth ? minimumWidth
: oldSize.width, oldSize.height < minimumHeight ? minimumHeight
: oldSize.height);
// ensure that the aspect ratio is redeemed
switch (direction) {
case PositionConstants.SOUTH_EAST:
int aspectHeight = (int) (newSize.width / RecommendedLayoutConstants.OVAL_SHAPE_ASPECT_RATIO);
int aspectWidth = (int) (newSize.height * RecommendedLayoutConstants.OVAL_SHAPE_ASPECT_RATIO);
int aspectHeight = (int) (newSize.width / aspectRatio);
int aspectWidth = (int) (newSize.height * aspectRatio);
newSize.setSize(new Dimension(
newSize.width > aspectWidth ? aspectWidth : newSize.width,
newSize.height > aspectHeight ? aspectHeight
: newSize.height));
break;
case PositionConstants.SOUTH:
newSize.setSize(new Dimension(
(int) (newSize.height * RecommendedLayoutConstants.OVAL_SHAPE_ASPECT_RATIO),
newSize.setSize(new Dimension((int) (newSize.height * aspectRatio),
newSize.height));
break;
case PositionConstants.EAST:
newSize.setSize(new Dimension(
newSize.width,
(int) (newSize.width / RecommendedLayoutConstants.OVAL_SHAPE_ASPECT_RATIO)));
newSize.setSize(new Dimension(newSize.width,
(int) (newSize.width / aspectRatio)));
break;
}
return newSize;
}
/**
* Ensure that the minimum size of a rectangular shape is redeemed.
*/
public static Dimension clampRectangularShapeSize(Dimension oldSize) {
/** Ensure that the minimum size of a rectangular shape is redeemed. */
public static Dimension clampRectangularShapeSize(Dimension oldSize,
int minimumWidth, int minimumHeight) {
// check for minimum size
Dimension newSize = new Dimension(
oldSize.width < RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_WIDTH ? RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_WIDTH
: oldSize.width,
oldSize.height < RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_HEIGHT ? RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_HEIGHT
: oldSize.height);
oldSize.width < minimumWidth ? minimumWidth : oldSize.width,
oldSize.height < minimumHeight ? minimumHeight : oldSize.height);
return newSize;
}
/** Ensure that the minimum size of a rectangular shape is redeemed. */
public static Dimension clampRectangularShapeSize(Dimension oldSize) {
return clampRectangularShapeSize(oldSize,
RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_WIDTH,
RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_HEIGHT);
}
/** Layouts the given connector within the surrounding ellipse. */
public static void layoutConnectorInEllipse(ILayoutedModelElement layouted,
ILayoutedModelElement container, Point location) {
Point position = location.getCopy();
position.translate(LayoutDataUtils.getNodePosition(container)
.getNegated());
position.translate(LayoutDataUtils.getNodeSize(container).scale(-.5));
org.fortiss.tooling.base.model.layout.Point np = LayoutDataUtils
.getNodePosition(container);
position.translate(-np.getX(), -np.getY());
org.fortiss.tooling.base.model.layout.Dimension nd = LayoutDataUtils
.getNodeSize(container);
Dimension d = new Dimension(nd.getWidth(), nd.getHeight());
position.translate(d.scale(-.5));
// store the value in the model
LayoutDataUtils.setAngle(layouted, LayoutKeyConstants.CONNECTOR_ANGLE,
new Polar(position).angle);
......@@ -197,12 +228,15 @@ public class EditPartLayoutUtils {
ILayoutedModelElement layouted, ILayoutedModelElement container,
Point location) {
Point position = location.getCopy();
position.translate(LayoutDataUtils.getNodePosition(container)
.getNegated());
org.fortiss.tooling.base.model.layout.Point np = LayoutDataUtils
.getNodePosition(container);
position.translate(-np.getX(), -np.getY());
// do border clamping
org.fortiss.tooling.base.model.layout.Dimension nd = LayoutDataUtils
.getNodeSize(container);
Dimension d = new Dimension(nd.getWidth(), nd.getHeight());
OffsetOrientation orient = EditPartLayoutUtils
.calculateRectangleBorderOrientation(position,
LayoutDataUtils.getNodeSize(container),
.calculateRectangleBorderOrientation(position, d,
RecommendedLayoutConstants.RECTANGULAR_SHAPE_INSETS);
// snap to grid and store the value in the model
LayoutDataUtils.setConnectorOrientation(layouted,
......@@ -226,7 +260,7 @@ public class EditPartLayoutUtils {
if (snapToGrid) {
EditPartLayoutUtils.snapToGrid(position);
}
LayoutDataUtils.setNodePosition(layouted, position);
LayoutDataUtils.setNodePosition(layouted, position.x, position.y);
}
/**
......
......@@ -19,6 +19,7 @@ package org.fortiss.tooling.kernel.base.ui.layout.util;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.fortiss.tooling.base.model.layout.Angle;
import org.fortiss.tooling.base.model.layout.Dimension;
import org.fortiss.tooling.base.model.layout.EOrientation;
......@@ -40,13 +41,13 @@ import org.fortiss.tooling.kernel.base.ui.layout.constants.RecommendedLayoutCons
* @author hummel
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: 9C4FEB62413E5D5B6355B4A7D2360C09
* @ConQAT.Rating YELLOW Hash: F7FBEB4FD3CE4087069F5CEF38C76851
*/
public class LayoutDataUtils {
/** Returns a {@link Point} layout data object with the given key or null. */
public static Point getPoint(ILayoutedModelElement lobject, String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) && ld instanceof Point) {
return (Point) ld;
}
......@@ -56,21 +57,27 @@ public class LayoutDataUtils {
/** Sets a {@link Point} layout data. */
public static void setPoint(ILayoutedModelElement lobject, String key,
org.eclipse.draw2d.geometry.Point point) {
Point point) {
setPoint(lobject, key, point.getX(), point.getY());
}
/** Sets a {@link Point} layout data. */
public static void setPoint(ILayoutedModelElement lobject, String key,
int x, int y) {
Point p = getPoint(lobject, key);
if (p == null) {
p = LayoutFactory.eINSTANCE.createPoint();
p.setKey(key);
lobject.getLayoutDataList().add(p);
}
p.setX(point.x);
p.setY(point.y);
p.setX(x);
p.setY(y);
}
/** Returns a {@link Dimension} layout data object with the given key. */
public static Dimension getDimension(ILayoutedModelElement lobject,
String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) && ld instanceof Dimension) {
return (Dimension) ld;
}
......@@ -80,21 +87,27 @@ public class LayoutDataUtils {
/** Sets a {@link Dimension} layout data object with the given key. */
public static void setDimension(ILayoutedModelElement lobject, String key,
org.eclipse.draw2d.geometry.Dimension size) {
Dimension size) {
setDimension(lobject, key, size.getWidth(), size.getHeight());
}
/** Sets a {@link Dimension} layout data object with the given key. */
public static void setDimension(ILayoutedModelElement lobject, String key,
int w, int h) {
Dimension d = getDimension(lobject, key);
if (d == null) {
d = LayoutFactory.eINSTANCE.createDimension();
d.setKey(key);
lobject.getLayoutDataList().add(d);
}
d.setWidth(size.width);
d.setHeight(size.height);
d.setWidth(w);
d.setHeight(h);
}
/** Returns a {@link Orientation} layout data object with the given key. */
public static Orientation getOrientation(ILayoutedModelElement lobject,
String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) || ld instanceof Orientation) {
return (Orientation) ld;
}
......@@ -116,7 +129,7 @@ public class LayoutDataUtils {
/** Returns a {@link Offset} layout data object with the given key. */
public static Offset getOffset(ILayoutedModelElement lobject, String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) || ld instanceof Offset) {
return (Offset) ld;
}
......@@ -138,7 +151,7 @@ public class LayoutDataUtils {
/** Returns an angle. */
public static Angle getAngle(ILayoutedModelElement lobject, String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) && ld instanceof Angle) {
return (Angle) ld;
}
......@@ -149,7 +162,7 @@ public class LayoutDataUtils {
/** Returns the angle as double value (0 if not existing). */
public static double getAngleAsDouble(ILayoutedModelElement lobject,
String key) {
final Angle a = getAngle(lobject, key);
Angle a = getAngle(lobject, key);
if (a == null) {
return 0;
}
......@@ -169,26 +182,27 @@ public class LayoutDataUtils {
}
/** Gets the position stored at the given key. */
public static org.eclipse.draw2d.geometry.Point getPosition(
ILayoutedModelElement layouted, String key) {
final Point point = LayoutDataUtils.getPoint(layouted, key);
public static Point getPosition(ILayoutedModelElement layouted, String key) {
Point point = LayoutDataUtils.getPoint(layouted, key);
if (point == null) {
int index = 0;
final EObject container = layouted.eContainer();
EObject container = layouted.eContainer();
if (container != null) {
index = 1 + container.eContents().indexOf(layouted);
}
return new org.eclipse.draw2d.geometry.Point(index * 20, index * 20);
Point p = LayoutFactory.eINSTANCE.createPoint();
p.setKey(key);
p.setX(index * 20);
p.setY(index * 20);
return p;
}
return new org.eclipse.draw2d.geometry.Point(point.getX(), point.getY());
return EcoreUtil.copy(point);
}
/** Returns a {@link Points} layout data object with the given key. */
public static Points getPoints(ILayoutedModelElement lobject, String key) {
for (final ILayoutData ld : lobject.getLayoutDataList()) {
for (ILayoutData ld : lobject.getLayoutDataList()) {
if (ld.getKey().equals(key) || ld instanceof Points) {
return (Points) ld;
}
......@@ -196,79 +210,82 @@ public class LayoutDataUtils {
return null;
}
/**
* Returns the bounds of the given layouted node.
*/
/** Returns the bounds of the given layouted node. */
public static Rectangle getNodeBounds(ILayoutedModelElement layouted) {
return new Rectangle(getNodePosition(layouted), getNodeSize(layouted));
Rectangle result = new Rectangle();
Point p = getNodePosition(layouted);
result.setLocation(p.getX(), p.getY());
Dimension d = getNodeSize(layouted);
result.setSize(d.getWidth(), d.getHeight());
return result;
}
/**
* Sets the bounds of the given layouted node.
*/
/** Sets the bounds of the given layouted node. */
public static void setNodeBounds(ILayoutedModelElement layouted,
Rectangle bounds) {
setNodePosition(layouted, bounds.getLocation());
setNodeSize(layouted, bounds.getSize());
setNodePosition(layouted, bounds.x, bounds.y);
setNodeSize(layouted, bounds.width, bounds.height);
}
/** Returns the position of the given layouted node. */
public static org.eclipse.draw2d.geometry.Point getNodePosition(
ILayoutedModelElement layouted) {
public static Point getNodePosition(ILayoutedModelElement layouted) {
return getPosition(layouted, LayoutKeyConstants.NODE_POSITION);
}
/**
* Sets the layouted node's position.
*/
/** Sets the layouted node's position. */
public static void setNodePosition(ILayoutedModelElement layouted,
org.eclipse.draw2d.geometry.Point position) {
setPoint(layouted, LayoutKeyConstants.NODE_POSITION, position);
Point position) {
setNodePosition(layouted, position.getX(), position.getY());
}
/** Sets the layouted node's position. */
public static void setNodePosition(ILayoutedModelElement layouted, int x,
int y) {
setPoint(layouted, LayoutKeyConstants.NODE_POSITION, x, y);
}
/** Returns the size of the given layouted node. */
public static org.eclipse.draw2d.geometry.Dimension getNodeSize(
ILayoutedModelElement layouted) {
final Dimension size = LayoutDataUtils.getDimension(layouted,
public static Dimension getNodeSize(ILayoutedModelElement layouted) {
Dimension size = LayoutDataUtils.getDimension(layouted,
LayoutKeyConstants.NODE_DIMENSION);
if (size == null) {
return new org.eclipse.draw2d.geometry.Dimension(
RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_WIDTH,
RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_HEIGHT);
Dimension result = LayoutFactory.eINSTANCE.createDimension();
result.setKey(LayoutKeyConstants.NODE_DIMENSION);
result.setWidth(RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_WIDTH);
result.setHeight(RecommendedLayoutConstants.RECTANGULAR_SHAPE_MINIMUM_HEIGHT);
return result;
}
return new org.eclipse.draw2d.geometry.Dimension(size.getWidth(),
size.getHeight());
return EcoreUtil.copy(size);
}
/** Sets the layouted node's size. */
public static void setNodeSize(ILayoutedModelElement layouted,
org.eclipse.draw2d.geometry.Dimension size) {
setDimension(layouted, LayoutKeyConstants.NODE_DIMENSION, size);
Dimension size) {
setDimension(layouted, LayoutKeyConstants.NODE_DIMENSION,
size.getWidth(), size.getHeight());
}
/**
* Returns the layouted connector's position.
*/
public static org.eclipse.draw2d.geometry.Point getConnectorPosition(
ILayoutedModelElement layouted) {
/** Sets the layouted node's size. */
public static void setNodeSize(ILayoutedModelElement layouted, int w, int h) {
setDimension(layouted, LayoutKeyConstants.NODE_DIMENSION, w, h);
}
/** Returns the layouted connector's position. */
public static Point getConnectorPosition(ILayoutedModelElement layouted) {
return getPosition(layouted, LayoutKeyConstants.CONNECTOR_POSITION);
}
/**
* Sets the layouted connector's position.
*/
/** Sets the layouted connector's position. */
public static void setConnectorPosition(ILayoutedModelElement layouted,
org.eclipse.draw2d.geometry.Point position) {
Point position) {
setPoint(layouted, LayoutKeyConstants.CONNECTOR_POSITION, position);
}
/**
* Returns the layouted connector's orientation.
*/
/** Returns the layouted connector's orientation. */
public static EOrientation getConnectorOrientation(
ILayoutedModelElement layouted) {
final Orientation orientation = getOrientation(layouted,
Orientation orientation = getOrientation(layouted,
LayoutKeyConstants.CONNECTOR_ORIENTATION);
if (orientation == null) {
return EOrientation.NORTH;
......@@ -276,88 +293,70 @@ public class LayoutDataUtils {
return orientation.getOrientation();
}
/**
* Sets the layouted connector's orientation.
*/
/** Sets the layouted connector's orientation. */
public static void setConnectorOrientation(ILayoutedModelElement layouted,
EOrientation orientation) {
setOrientation(layouted, LayoutKeyConstants.CONNECTOR_ORIENTATION,
orientation);
}
/**
* Returns the layouted connector's offset.
*/
/** Returns the layouted connector's offset. */
public static int getConnectorOffset(ILayoutedModelElement layouted) {
final Offset offset = getOffset(layouted,
LayoutKeyConstants.CONNECTOR_OFFSET);
Offset offset = getOffset(layouted, LayoutKeyConstants.CONNECTOR_OFFSET);
if (offset == null) {
return 0;
}
return offset.getOffset();
}
/**
* Sets the layouted connector's angle.
*/
/** Sets the layouted connector's angle. */
public static void setConnectorOffset(ILayoutedModelElement layouted,
int offset) {
setOffset(layouted, LayoutKeyConstants.CONNECTOR_OFFSET, offset);
}
/**
* Sets the layouted connector's offset.
*/
/** Sets the layouted connector's offset. */
public static void setConnectorAngle(ILayoutedModelElement layouted,
double angle) {
setAngle(layouted, LayoutKeyConstants.CONNECTOR_OFFSET, angle);
}
/**
* Returns the layouted connector's angle.
*/
/** Returns the layouted connector's angle. */
public static double getConnectorAngle(ILayoutedModelElement layouted) {
final Angle angle = getAngle(layouted,
LayoutKeyConstants.CONNECTOR_ANGLE);
Angle angle = getAngle(layouted, LayoutKeyConstants.CONNECTOR_ANGLE);
if (angle == null) {
return 0;
}
return angle.getAngle();
}
/**
* Creates a new connection layout.
*/
/** Creates a new connection layout. */
public static void createConnectionLayout(ILayoutedModelElement lo) {
final Points points = LayoutFactory.eINSTANCE.createPoints();
Points points = LayoutFactory.eINSTANCE.createPoints();
points.setKey(LayoutKeyConstants.CONNECTION_POINTS);
lo.getLayoutDataList().add(points);
}
/**
* Creates a new connector layout.
*/
/** Creates a new connector layout. */
public static void createConnectorLayout(ILayoutedModelElement lo) {
createNodeLayout(lo);
final Point cpos = LayoutFactory.eINSTANCE.createPoint();
Point cpos = LayoutFactory.eINSTANCE.createPoint();
cpos.setKey(LayoutKeyConstants.CONNECTOR_POSITION);
final Dimension cdim = LayoutFactory.eINSTANCE.createDimension();
Dimension cdim = LayoutFactory.eINSTANCE.createDimension();
cdim.setKey(LayoutKeyConstants.CONNECTOR_DIMENSION);
lo.getLayoutDataList().add(cpos);
lo.getLayoutDataList().add(cdim);
}
/**
* Creates a new node layout.
*/
/** Creates a new node layout. */
public static void createNodeLayout(ILayoutedModelElement lo) {
final Point pos = LayoutFactory.eINSTANCE.createPoint();
Point pos = LayoutFactory.eINSTANCE.createPoint();
pos.setKey(LayoutKeyConstants.NODE_POSITION);
final Dimension dim = LayoutFactory.eINSTANCE.createDimension();
Dimension dim = LayoutFactory.eINSTANCE.createDimension();
dim.setKey(LayoutKeyConstants.NODE_DIMENSION);
lo.getLayoutDataList().add(pos);
......
......@@ -33,13 +33,10 @@ import org.fortiss.tooling.kernel.base.ui.layout.constants.RecommendedLayoutCons
* @author hoelzl
* @author $Author$
* @version $Rev$
* @ConQAT.Rating YELLOW Hash: F959248D524F8DA65AA76E06A092CD9D
* @ConQAT.Rating YELLOW Hash: 0C2C5F21C2F2ECCCF9F45819D868FFA0
*/
public class OrientationUtils {
/** Key which identifies the class. */
public static final String KEY = "ORIENTATION";
/** Return the absolute position for a given orientation. */
public static Point getAbsolute(Orientation orientation, Offset offset,
Dimension parentSize, int parentInsets) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment