1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 IBM Corporation and others.
|
3
|
* All rights reserved. This program and the accompanying materials
|
4
|
* are made available under the terms of the Eclipse Public License v1.0
|
5
|
* which accompanies this distribution, and is available at
|
6
|
* http://www.eclipse.org/legal/epl-v10.html
|
7
|
*
|
8
|
* Contributors:
|
9
|
* IBM Corporation - initial API and implementation
|
10
|
*******************************************************************************/
|
11
|
package org.eclipse.draw2d;
|
12
|
|
13
|
import java.util.ListIterator;
|
14
|
|
15
|
import org.eclipse.draw2d.geometry.Point;
|
16
|
import org.eclipse.draw2d.geometry.Rectangle;
|
17
|
|
18
|
/**
|
19
|
* A layout for {@link org.eclipse.draw2d.FreeformFigure FreeformFigures}.
|
20
|
* Supports option to set only positive (x,y) coordinates for children figures.
|
21
|
*/
|
22
|
public class FreeformLayout extends XYLayout {
|
23
|
|
24
|
/**
|
25
|
* Option that forces only positive coordinates to children figures by
|
26
|
* setting the layout origin appropriately
|
27
|
*/
|
28
|
private static final int FLAG__POSITIVE_COORDINATES = 1;
|
29
|
|
30
|
/**
|
31
|
* Option flags
|
32
|
*/
|
33
|
private int flags;
|
34
|
|
35
|
/**
|
36
|
* Layout origin point
|
37
|
*/
|
38
|
private Point origin = null;
|
39
|
|
40
|
/**
|
41
|
* Returns the point (0,0) as the origin.
|
42
|
*
|
43
|
* @see XYLayout#getOrigin(IFigure)
|
44
|
*/
|
45
|
public Point getOrigin(IFigure figure) {
|
46
|
if (origin == null) {
|
47
|
origin = new Point();
|
48
|
if (isPositiveCoordinates()) {
|
49
|
ListIterator children = figure.getChildren().listIterator();
|
50
|
while (children.hasNext()) {
|
51
|
IFigure f = (IFigure) children.next();
|
52
|
Rectangle constraint = (Rectangle) getConstraint(f);
|
53
|
if (constraint != null) {
|
54
|
origin.x = Math.min(origin.x, constraint.x);
|
55
|
origin.y = Math.min(origin.y, constraint.y);
|
56
|
}
|
57
|
}
|
58
|
origin.negate();
|
59
|
}
|
60
|
}
|
61
|
return origin;
|
62
|
}
|
63
|
|
64
|
/**
|
65
|
* Checks whether the positive coordinates flag is on, e.g positive
|
66
|
* coordinates for children are inforced by the layout
|
67
|
*
|
68
|
* @return <code>boolean</code>
|
69
|
* @since 3.6
|
70
|
*/
|
71
|
public boolean isPositiveCoordinates() {
|
72
|
return (flags & FLAG__POSITIVE_COORDINATES) != 0;
|
73
|
}
|
74
|
|
75
|
/**
|
76
|
* Sets/unsets the positive coordinates flag for true/false parameters
|
77
|
* respectively. If option is set to on then layout calculates positive
|
78
|
* coordinates for children figures by adjusting the layout origin
|
79
|
* accordingly.
|
80
|
*
|
81
|
* @param positiveCoordinates
|
82
|
* @since 3.6
|
83
|
*/
|
84
|
public void setPositiveCoordinates(boolean positiveCoordinates) {
|
85
|
if (positiveCoordinates != isPositiveCoordinates()) {
|
86
|
if (positiveCoordinates) {
|
87
|
flags |= FLAG__POSITIVE_COORDINATES;
|
88
|
} else {
|
89
|
flags &= ~FLAG__POSITIVE_COORDINATES;
|
90
|
}
|
91
|
invalidate();
|
92
|
}
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* @see org.eclipse.draw2d.AbstractLayout#invalidate()
|
97
|
*/
|
98
|
public void invalidate() {
|
99
|
origin = null;
|
100
|
super.invalidate();
|
101
|
}
|
102
|
|
103
|
}
|