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.List;
|
15
|
import java.util.Map;
|
16
|
|
17
|
import org.eclipse.draw2d.geometry.Dimension;
|
18
|
|
19
|
/**
|
20
|
* Figures using a DelegatingLayout as their layout manager give location
|
21
|
* responsibilities to their children. The children of a Figure using a
|
22
|
* DelegatingLayout should have a {@link Locator Locator} as a constraint whose
|
23
|
* {@link Locator#relocate(IFigure target) relocate} method is responsible for
|
24
|
* placing the child.
|
25
|
*/
|
26
|
public class DelegatingLayout extends AbstractLayout {
|
27
|
|
28
|
private Map constraints = new HashMap();
|
29
|
|
30
|
/**
|
31
|
* Calculates the preferred size of the given Figure. For the
|
32
|
* DelegatingLayout, this is the largest width and height values of the
|
33
|
* passed Figure's children.
|
34
|
*
|
35
|
* @param parent
|
36
|
* the figure whose preferred size is being calculated
|
37
|
* @param wHint
|
38
|
* the width hint
|
39
|
* @param hHint
|
40
|
* the height hint
|
41
|
* @return the preferred size
|
42
|
* @since 2.0
|
43
|
*/
|
44
|
protected Dimension calculatePreferredSize(IFigure parent, int wHint,
|
45
|
int hHint) {
|
46
|
List children = parent.getChildren();
|
47
|
Dimension d = new Dimension();
|
48
|
for (int i = 0; i < children.size(); i++) {
|
49
|
IFigure child = (IFigure) children.get(i);
|
50
|
d.union(child.getPreferredSize());
|
51
|
}
|
52
|
return d;
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* @see org.eclipse.draw2d.LayoutManager#getConstraint(org.eclipse.draw2d.IFigure)
|
57
|
*/
|
58
|
public Object getConstraint(IFigure child) {
|
59
|
return constraints.get(child);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Lays out the given figure's children based on their {@link Locator}
|
64
|
* constraint.
|
65
|
*
|
66
|
* @param parent
|
67
|
* the figure whose children should be layed out
|
68
|
*/
|
69
|
public void layout(IFigure parent) {
|
70
|
List children = parent.getChildren();
|
71
|
for (int i = 0; i < children.size(); i++) {
|
72
|
IFigure child = (IFigure) children.get(i);
|
73
|
Locator locator = (Locator) constraints.get(child);
|
74
|
if (locator != null) {
|
75
|
locator.relocate(child);
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
* Removes the locator for the given figure.
|
82
|
*
|
83
|
* @param child
|
84
|
* the child being removed
|
85
|
*/
|
86
|
public void remove(IFigure child) {
|
87
|
constraints.remove(child);
|
88
|
}
|
89
|
|
90
|
/**
|
91
|
* Sets the constraint for the given figure.
|
92
|
*
|
93
|
* @param figure
|
94
|
* the figure whose contraint is being set
|
95
|
* @param constraint
|
96
|
* the new constraint
|
97
|
*/
|
98
|
public void setConstraint(IFigure figure, Object constraint) {
|
99
|
super.setConstraint(figure, constraint);
|
100
|
if (constraint != null)
|
101
|
constraints.put(figure, constraint);
|
102
|
}
|
103
|
|
104
|
}
|