1 package ca.spaz.gui;
2
3 import java.awt.*;
4 import java.util.Hashtable;
5
6 /***
7 */
8
9 public class SpazLayout implements LayoutManager2 {
10 Hashtable compTable = new Hashtable();
11
12 /***
13 */
14 public SpazLayout() { }
15
16 public void setConstraints(Component comp, SpazPosition constraints) {
17 compTable.put(comp, new SpazPosition(constraints));
18 }
19
20 /***
21 * Adds the specified component with the specified name to
22 * the layout. This does nothing in GraphPaperLayout, since constraints
23 * are required.
24 */
25 public void addLayoutComponent(String name, Component comp) {}
26
27 /***
28 * Removes the specified component from the layout.
29 * @param comp the component to be removed
30 */
31 public void removeLayoutComponent(Component comp) {
32 compTable.remove(comp);
33 }
34
35 public SpazPosition getPosition(Component comp) {
36 return (SpazPosition)compTable.get(comp);
37 }
38
39 public void setPosition(Component comp, SpazPosition lp) {
40 compTable.put(comp, lp);
41 }
42
43
44 /***
45 * Calculates the preferred size dimensions for the specified
46 * panel given the components in the specified parent container.
47 * @param parent the component to be laid out
48 *
49 * @see #minimumLayoutSize
50 */
51 public Dimension preferredLayoutSize(Container parent) {
52 int ncomponents = parent.getComponentCount();
53 if (ncomponents == 0) return new Dimension(1,1);
54 Rectangle totalRect = new Rectangle(0,0,1,1);
55 Dimension size = parent.getSize();
56 Insets insets = parent.getInsets();
57 int totalW = size.width - (insets.left + insets.right);
58 int totalH = size.height - (insets.top + insets.bottom);
59 for ( int i = 0; i < ncomponents; i++ ) {
60 Component c = parent.getComponent(i);
61 SpazPosition lp = (SpazPosition)compTable.get(c);
62 Rectangle rect = lp.getRectangle(totalW, totalH);
63 if ( rect != null )
64 totalRect = totalRect.union(rect);
65
66 }
67 return new Dimension(totalRect.width,totalRect.height);
68 }
69
70 /***
71 * Calculates the minimum size dimensions for the specified
72 * panel given the components in the specified parent container.
73 * @param parent the component to be laid out
74 * @see #preferredLayoutSize
75 */
76 public Dimension minimumLayoutSize(Container parent) {
77 int ncomponents = parent.getComponentCount();
78 if (ncomponents == 0) return new Dimension(1,1);
79 Rectangle totalRect = new Rectangle(0,0,1,1);
80 Dimension size = parent.getSize();
81 Insets insets = parent.getInsets();
82 int totalW = size.width - (insets.left + insets.right);
83 int totalH = size.height - (insets.top + insets.bottom);
84 for ( int i = 0; i < ncomponents; i++ ) {
85 Component c = parent.getComponent(i);
86 SpazPosition lp = (SpazPosition)compTable.get(c);
87 Rectangle rect = lp.getMinRectangle();
88 if ( rect != null )
89 totalRect = totalRect.union(rect);
90
91 }
92 return new Dimension(totalRect.width,totalRect.height);
93 }
94
95
96 /***
97 * Lays out the container in the specified container.
98 * @param parent the component which needs to be laid out
99 */
100 public void layoutContainer(Container parent) {
101 synchronized (parent.getTreeLock()) {
102 Insets insets = parent.getInsets();
103 int ncomponents = parent.getComponentCount();
104 if (ncomponents == 0) return;
105
106
107 Dimension size = parent.getSize();
108 int totalW = size.width - (insets.left + insets.right);
109 int totalH = size.height - (insets.top + insets.bottom);
110
111 for ( int i = 0; i < ncomponents; i++ ) {
112 Component c = parent.getComponent(i);
113 SpazPosition lp = getPosition(c);
114 Rectangle rect = lp.getRectangle(totalW, totalH);
115 if ( rect != null ) {
116 int x = insets.left + rect.x;
117 int y = insets.top + rect.y;
118 c.setBounds(x, y, rect.width, rect.height);
119 }
120 }
121 }
122 }
123
124
125
126 /***
127 * Adds the specified component to the layout, using the specified
128 * constraint object.
129 * @param comp the component to be added
130 * @param constraints where/how the component is added to the layout.
131 */
132 public void addLayoutComponent(Component comp, Object constraints) {
133 if (constraints instanceof SpazPosition) {
134 SpazPosition cons = (SpazPosition)constraints;
135 setConstraints(comp, cons);
136 } else if (constraints != null) {
137 throw new IllegalArgumentException(
138 "cannot add to layout: constraint must be a SpazPostion");
139 }
140 }
141
142 /***
143 * Returns the maximum size of this component.
144 * @see java.awt.Component#getMinimumSize()
145 * @see java.awt.Component#getPreferredSize()
146 * @see LayoutManager
147 */
148 public Dimension maximumLayoutSize(Container target) {
149 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
150 }
151
152 /***
153 * Returns the alignment along the x axis. This specifies how
154 * the component would like to be aligned relative to other
155 * components. The value should be a number between 0 and 1
156 * where 0 represents alignment along the origin, 1 is aligned
157 * the furthest away from the origin, 0.5 is centered, etc.
158 */
159 public float getLayoutAlignmentX(Container target) {
160 return 0.5f;
161 }
162
163 /***
164 * Returns the alignment along the y axis. This specifies how
165 * the component would like to be aligned relative to other
166 * components. The value should be a number between 0 and 1
167 * where 0 represents alignment along the origin, 1 is aligned
168 * the furthest away from the origin, 0.5 is centered, etc.
169 */
170 public float getLayoutAlignmentY(Container target) {
171 return 0.5f;
172 }
173
174 /***
175 * Invalidates the layout, indicating that if the layout manager
176 * has cached information it should be discarded.
177 */
178 public void invalidateLayout(Container target) {
179
180 }
181 }