Debian Codegraph @

src/dr/app/gui/chart/ChartLayout.java

raw symbols SCIP
1 /*
2 * ChartLayout.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.gui.chart;
27
28 import java.awt.*;
29
30 /**
31 * A layout manager similar to BorderLayout but with specific regions for
32 * parts of a chart (plot, axis, titles, etc.).
33 *
34 * @author Andrew Rambaut
35 */
36
37 public class ChartLayout implements LayoutManager2 {
38 int hgap;
39 int vgap;
40
41 Component title = null;
42 Component xLabel = null;
43 Component yLabel = null;
44 Component chart = null;
45
46 /**
47 * Constructs a new ChartLayout.
48 */
49 public ChartLayout() {
50 this(0, 0);
51 }
52
53 /**
54 * Constructs a ChartLayout with the specified gaps.
55 *
56 * @param hgap the horizontal gap
57 * @param vgap the vertical gap
58 */
59 public ChartLayout(int hgap, int vgap) {
60 this.hgap = hgap;
61 this.vgap = vgap;
62 }
63
64 /**
65 * @return the horizontal gap between components.
66 */
67 public int getHgap() {
68 return hgap;
69 }
70
71 /**
72 * Sets the horizontal gap between components.
73 *
74 * @param hgap the horizontal gap between components
75 */
76 public void setHgap(int hgap) {
77 this.hgap = hgap;
78 }
79
80 /**
81 * @return the vertical gap between components.
82 */
83 public int getVgap() {
84 return vgap;
85 }
86
87 /**
88 * Sets the vertical gap between components.
89 *
90 * @param vgap the vertical gap between components
91 */
92 public void setVgap(int vgap) {
93 this.vgap = vgap;
94 }
95
96 /**
97 * Adds the specified named component to the layout.
98 *
99 * @param comp the component to be added
100 */
101 public void addLayoutComponent(Component comp, Object constraints) {
102 synchronized (comp.getTreeLock()) {
103 if ((constraints == null) || (constraints instanceof String)) {
104 addLayoutComponent((String) constraints, comp);
105 } else {
106 throw new IllegalArgumentException("cannot add to layout: constraint must be a string (or null)");
107 }
108 }
109 }
110
111 /**
112 * @deprecated replaced by <code>addLayoutComponent(Component, Object)</code>.
113 */
114 public void addLayoutComponent(String name, Component comp) {
115 synchronized (comp.getTreeLock()) {
116 /* Special case: treat null the same as "Chart". */
117 if (name == null) {
118 name = "Chart";
119 }
120
121 if ("Chart".equals(name) || "Table".equals(name)) {
122 chart = comp;
123 } else if ("Title".equals(name)) {
124 title = comp;
125 } else if ("XLabel".equals(name)) {
126 xLabel = comp;
127 } else if ("YLabel".equals(name)) {
128 yLabel = comp;
129 } else {
130 throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
131 }
132 }
133 }
134
135 /**
136 * Removes the specified component from the layout.
137 *
138 * @param comp the component to be removed
139 */
140 public void removeLayoutComponent(Component comp) {
141 synchronized (comp.getTreeLock()) {
142 if (comp == chart) {
143 chart = null;
144 } else if (comp == title) {
145 title = null;
146 } else if (comp == xLabel) {
147 xLabel = null;
148 } else if (comp == yLabel) {
149 yLabel = null;
150 }
151 }
152 }
153
154 /**
155 * Returns the minimum dimensions needed to layout the components
156 * contained in the specified target container.
157 *
158 * @param target the Container on which to do the layout
159 * @see Container
160 * @see #preferredLayoutSize
161 */
162 public Dimension minimumLayoutSize(Container target) {
163 synchronized (target.getTreeLock()) {
164 Dimension dim = new Dimension(0, 0);
165
166 if ((chart != null) && chart.isVisible()) {
167 Dimension d = chart.getMinimumSize();
168 dim.width = d.width;
169 dim.height = d.height;
170 }
171
172 if ((xLabel != null) && xLabel.isVisible()) {
173 Dimension d = xLabel.getMinimumSize();
174 dim.width = Math.max(d.width, dim.width);
175 dim.height += d.height + vgap;
176 }
177
178 if ((yLabel != null) && yLabel.isVisible()) {
179 Dimension d = yLabel.getMinimumSize();
180 dim.width += d.width + hgap;
181 dim.height = Math.max(d.height, dim.height);
182 }
183
184 if ((title != null) && title.isVisible()) {
185 Dimension d = title.getMinimumSize();
186 dim.width = Math.max(d.width, dim.width);
187 dim.height += d.height + vgap;
188 }
189
190 Insets insets = target.getInsets();
191 dim.width += insets.left + insets.right;
192 dim.height += insets.top + insets.bottom;
193
194 return dim;
195 }
196 }
197
198 /**
199 * Returns the preferred dimensions for this layout given the components
200 * in the specified target container.
201 *
202 * @param target the component which needs to be laid out
203 * @see Container
204 * @see #minimumLayoutSize
205 */
206 public Dimension preferredLayoutSize(Container target) {
207 synchronized (target.getTreeLock()) {
208 Dimension dim = new Dimension(0, 0);
209
210 if ((chart != null) && chart.isVisible()) {
211 Dimension d = chart.getPreferredSize();
212 dim.width = d.width;
213 dim.height = d.height;
214 }
215
216 if ((xLabel != null) && xLabel.isVisible()) {
217 Dimension d = xLabel.getPreferredSize();
218 dim.width = Math.max(d.width, dim.width);
219 dim.height += d.height + vgap;
220 }
221
222 if ((yLabel != null) && yLabel.isVisible()) {
223 Dimension d = yLabel.getPreferredSize();
224 dim.width += d.width + hgap;
225 dim.height = Math.max(d.height, dim.height);
226 }
227
228 if ((title != null) && title.isVisible()) {
229 Dimension d = title.getPreferredSize();
230 dim.width = Math.max(d.width, dim.width);
231 dim.height += d.height + vgap;
232 }
233
234 Insets insets = target.getInsets();
235 dim.width += insets.left + insets.right;
236 dim.height += insets.top + insets.bottom;
237
238 return dim;
239 }
240 }
241
242 /**
243 * Returns the maximum dimensions for this layout given the components
244 * in the specified target container.
245 *
246 * @param target the component which needs to be laid out
247 */
248 public Dimension maximumLayoutSize(Container target) {
249 return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
250 }
251
252 /**
253 * Returns the alignment along the x axis. This specifies how
254 * the component would like to be aligned relative to other
255 * components. The value should be a number between 0 and 1
256 * where 0 represents alignment along the origin, 1 is aligned
257 * the furthest away from the origin, 0.5 is centered, etc.
258 */
259 public float getLayoutAlignmentX(Container parent) {
260 return 0.5f;
261 }
262
263 /**
264 * Returns the alignment along the y axis. This specifies how
265 * the component would like to be aligned relative to other
266 * components. The value should be a number between 0 and 1
267 * where 0 represents alignment along the origin, 1 is aligned
268 * the furthest away from the origin, 0.5 is centered, etc.
269 */
270 public float getLayoutAlignmentY(Container parent) {
271 return 0.5f;
272 }
273
274 /**
275 * Invalidates the layout, indicating that if the layout manager
276 * has cached information it should be discarded.
277 */
278 public void invalidateLayout(Container target) {
279 }
280
281 /**
282 * Lays out the specified container. This method will actually reshape the
283 * components in the specified target container in order to satisfy the
284 * constraints of the CompassLayout object.
285 *
286 * @param target the component being laid out
287 * @see Container
288 */
289 public void layoutContainer(Container target) {
290 synchronized (target.getTreeLock()) {
291 Insets insets = target.getInsets();
292 Dimension d;
293
294 d = target.getSize();
295
296 int top = insets.top;
297 int bottom = d.height - insets.bottom;
298 int left = insets.left;
299 int right = d.width - insets.right;
300
301 // defines the centre box.
302 int top1 = top;
303 int bottom1 = bottom;
304 int left1 = left;
305
306 if ((xLabel != null) && xLabel.isVisible()) {
307 d = xLabel.getPreferredSize();
308 bottom1 -= d.height + vgap;
309 }
310 if ((yLabel != null) && yLabel.isVisible()) {
311 d = yLabel.getPreferredSize();
312 left1 += d.width + hgap;
313 }
314 if ((title != null) && title.isVisible()) {
315 d = title.getPreferredSize();
316 top1 += d.height + vgap;
317 }
318
319 if ((xLabel != null) && xLabel.isVisible())
320 xLabel.setBounds(left1, bottom1, right - left1, bottom - bottom1);
321
322 if ((yLabel != null) && yLabel.isVisible())
323 yLabel.setBounds(left, top1, left1 - left, bottom1 - top1);
324
325 if ((title != null) && title.isVisible())
326 title.setBounds(left, top, right - left, top1 - top);
327
328 if ((chart != null) && chart.isVisible())
329 chart.setBounds(left1, top1, right - left1, bottom1 - top1);
330 }
331 }
332
333 /**
334 * Returns the String representation of this CompassLayout's values.
335 */
336 public String toString() {
337 return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]";
338 }
339 }