| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.app.gui.chart; |
| 27 |
|
| 28 |
import java.awt.*; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 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 |
|
| 48 |
|
| 49 |
public ChartLayout() { |
| 50 |
this(0, 0); |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public ChartLayout(int hgap, int vgap) { |
| 60 |
this.hgap = hgap; |
| 61 |
this.vgap = vgap; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public int getHgap() { |
| 68 |
return hgap; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public void setHgap(int hgap) { |
| 77 |
this.hgap = hgap; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public int getVgap() { |
| 84 |
return vgap; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public void setVgap(int vgap) { |
| 93 |
this.vgap = vgap; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 113 |
|
| 114 |
public void addLayoutComponent(String name, Component comp) { |
| 115 |
synchronized (comp.getTreeLock()) { |
| 116 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 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 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public Dimension maximumLayoutSize(Container target) { |
| 249 |
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE); |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public float getLayoutAlignmentX(Container parent) { |
| 260 |
return 0.5f; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
public float getLayoutAlignmentY(Container parent) { |
| 271 |
return 0.5f; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
public void invalidateLayout(Container target) { |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 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 |
|
| 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 |
|
| 335 |
|
| 336 |
public String toString() { |
| 337 |
return getClass().getName() + "[hgap=" + hgap + ",vgap=" + vgap + "]"; |
| 338 |
} |
| 339 |
} |