Project

General

Profile

Download (4.24 KB) Statistics
| Branch: | Tag: | Revision:
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.HashMap;
14
import java.util.Iterator;
15
import java.util.ListIterator;
16
import java.util.Map;
17

    
18
import org.eclipse.draw2d.geometry.Dimension;
19
import org.eclipse.draw2d.geometry.Insets;
20
import org.eclipse.draw2d.geometry.Point;
21
import org.eclipse.draw2d.geometry.Rectangle;
22

    
23
/**
24
 * This class implements the {@link org.eclipse.draw2d.LayoutManager} interface
25
 * using the XY Layout algorithm. This lays out the components using the layout
26
 * constraints as defined by each component.
27
 */
28
public class XYLayout extends AbstractLayout {
29

    
30
	/** The layout contraints */
31
	protected Map constraints = new HashMap();
32

    
33
	/**
34
	 * Calculates and returns the preferred size of the input figure. Since in
35
	 * XYLayout the location of the child should be preserved, the preferred
36
	 * size would be a region which would hold all the children of the input
37
	 * figure. If no constraint is set, that child is ignored for calculation.
38
	 * If width and height are not positive, the preferred dimensions of the
39
	 * child are taken.
40
	 * 
41
	 * @see AbstractLayout#calculatePreferredSize(IFigure, int, int)
42
	 * @since 2.0
43
	 */
44
	protected Dimension calculatePreferredSize(IFigure f, int wHint, int hHint) {
45
		Rectangle rect = new Rectangle();
46
		ListIterator children = f.getChildren().listIterator();
47
		while (children.hasNext()) {
48
			IFigure child = (IFigure) children.next();
49
			Rectangle r = (Rectangle) constraints.get(child);
50
			if (r == null)
51
				continue;
52

    
53
			if (r.width == -1 || r.height == -1) {
54
				Dimension preferredSize = child.getPreferredSize(r.width,
55
						r.height);
56
				r = r.getCopy();
57
				if (r.width == -1)
58
					r.width = preferredSize.width;
59
				if (r.height == -1)
60
					r.height = preferredSize.height;
61
			}
62
			rect.union(r);
63
		}
64
		Dimension d = rect.getSize();
65
		Insets insets = f.getInsets();
66
		return new Dimension(d.width + insets.getWidth(), d.height
67
				+ insets.getHeight()).union(getBorderPreferredSize(f));
68
	}
69

    
70
	/**
71
	 * @see LayoutManager#getConstraint(IFigure)
72
	 */
73
	public Object getConstraint(IFigure figure) {
74
		return constraints.get(figure);
75
	}
76

    
77
	/**
78
	 * Returns the origin for the given figure.
79
	 * 
80
	 * @param parent
81
	 *            the figure whose origin is requested
82
	 * @return the origin
83
	 */
84
	public Point getOrigin(IFigure parent) {
85
		return parent.getClientArea().getLocation();
86
	}
87

    
88
	/**
89
	 * Implements the algorithm to layout the components of the given container
90
	 * figure. Each component is laid out using its own layout constraint
91
	 * specifying its size and position.
92
	 * 
93
	 * @see LayoutManager#layout(IFigure)
94
	 */
95
	public void layout(IFigure parent) {
96
		Iterator children = parent.getChildren().iterator();
97
		Point offset = getOrigin(parent);
98
		IFigure f;
99
		while (children.hasNext()) {
100
			f = (IFigure) children.next();
101
			Rectangle bounds = (Rectangle) getConstraint(f);
102
			if (bounds == null)
103
				continue;
104

    
105
			if (bounds.width == -1 || bounds.height == -1) {
106
				Dimension preferredSize = f.getPreferredSize(bounds.width,
107
						bounds.height);
108
				bounds = bounds.getCopy();
109
				if (bounds.width == -1)
110
					bounds.width = preferredSize.width;
111
				if (bounds.height == -1)
112
					bounds.height = preferredSize.height;
113
			}
114
			bounds = bounds.getTranslated(offset);
115
			f.setBounds(bounds);
116
		}
117
	}
118

    
119
	/**
120
	 * @see LayoutManager#remove(IFigure)
121
	 */
122
	public void remove(IFigure figure) {
123
		super.remove(figure);
124
		constraints.remove(figure);
125
	}
126

    
127
	/**
128
	 * Sets the layout constraint of the given figure. The constraints can only
129
	 * be of type {@link Rectangle}.
130
	 * 
131
	 * @see LayoutManager#setConstraint(IFigure, Object)
132
	 * @since 2.0
133
	 */
134
	public void setConstraint(IFigure figure, Object newConstraint) {
135
		super.setConstraint(figure, newConstraint);
136
		if (newConstraint != null)
137
			constraints.put(figure, newConstraint);
138
	}
139

    
140
}
(170-170/171)