1 | /*
|
---|
2 | * $Id: MultiSplitLayout.java,v 1.15 2005/10/26 14:29:54 hansmuller Exp $
|
---|
3 | *
|
---|
4 | * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
|
---|
5 | * Santa Clara, California 95054, U.S.A. All rights reserved.
|
---|
6 | *
|
---|
7 | * This library is free software; you can redistribute it and/or
|
---|
8 | * modify it under the terms of the GNU Lesser General Public
|
---|
9 | * License as published by the Free Software Foundation; either
|
---|
10 | * version 2.1 of the License, or (at your option) any later version.
|
---|
11 | *
|
---|
12 | * This library is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
15 | * Lesser General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU Lesser General Public
|
---|
18 | * License along with this library; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
20 | */
|
---|
21 | package org.openstreetmap.josm.gui.widgets;
|
---|
22 |
|
---|
23 | import java.awt.Component;
|
---|
24 | import java.awt.Container;
|
---|
25 | import java.awt.Dimension;
|
---|
26 | import java.awt.Insets;
|
---|
27 | import java.awt.LayoutManager;
|
---|
28 | import java.awt.Rectangle;
|
---|
29 | import java.beans.PropertyChangeListener;
|
---|
30 | import java.beans.PropertyChangeSupport;
|
---|
31 | import java.util.ArrayList;
|
---|
32 | import java.util.Collections;
|
---|
33 | import java.util.HashMap;
|
---|
34 | import java.util.Iterator;
|
---|
35 | import java.util.List;
|
---|
36 | import java.util.ListIterator;
|
---|
37 | import java.util.Map;
|
---|
38 |
|
---|
39 | import javax.swing.UIManager;
|
---|
40 |
|
---|
41 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * The MultiSplitLayout layout manager recursively arranges its
|
---|
45 | * components in row and column groups called "Splits". Elements of
|
---|
46 | * the layout are separated by gaps called "Dividers". The overall
|
---|
47 | * layout is defined with a simple tree model whose nodes are
|
---|
48 | * instances of MultiSplitLayout.Split, MultiSplitLayout.Divider,
|
---|
49 | * and MultiSplitLayout.Leaf. Named Leaf nodes represent the space
|
---|
50 | * allocated to a component that was added with a constraint that
|
---|
51 | * matches the Leaf's name. Extra space is distributed
|
---|
52 | * among row/column siblings according to their 0.0 to 1.0 weight.
|
---|
53 | * If no weights are specified then the last sibling always gets
|
---|
54 | * all of the extra space, or space reduction.
|
---|
55 | *
|
---|
56 | * <p>
|
---|
57 | * Although MultiSplitLayout can be used with any Container, it's
|
---|
58 | * the default layout manager for MultiSplitPane. MultiSplitPane
|
---|
59 | * supports interactively dragging the Dividers, accessibility,
|
---|
60 | * and other features associated with split panes.
|
---|
61 | *
|
---|
62 | * <p>
|
---|
63 | * All properties in this class are bound: when a properties value
|
---|
64 | * is changed, all PropertyChangeListeners are fired.
|
---|
65 | *
|
---|
66 | * @author Hans Muller - SwingX
|
---|
67 | * @see MultiSplitPane
|
---|
68 | */
|
---|
69 | public class MultiSplitLayout implements LayoutManager {
|
---|
70 | private final Map<String, Component> childMap = new HashMap<>();
|
---|
71 | private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
|
---|
72 | private Node model;
|
---|
73 | private int dividerSize;
|
---|
74 | private boolean floatingDividers = true;
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Create a MultiSplitLayout with a default model with a single
|
---|
78 | * Leaf node named "default".
|
---|
79 | *
|
---|
80 | * #see setModel
|
---|
81 | */
|
---|
82 | public MultiSplitLayout() {
|
---|
83 | this(new Leaf("default"));
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Create a MultiSplitLayout with the specified model.
|
---|
88 | *
|
---|
89 | * #see setModel
|
---|
90 | * @param model model
|
---|
91 | */
|
---|
92 | public MultiSplitLayout(Node model) {
|
---|
93 | this.model = model;
|
---|
94 | this.dividerSize = UIManager.getInt("SplitPane.dividerSize");
|
---|
95 | if (this.dividerSize == 0) {
|
---|
96 | this.dividerSize = 7;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Add property change listener.
|
---|
102 | * @param listener listener to add
|
---|
103 | */
|
---|
104 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
105 | if (listener != null) {
|
---|
106 | pcs.addPropertyChangeListener(listener);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Remove property change listener.
|
---|
112 | * @param listener listener to remove
|
---|
113 | */
|
---|
114 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
115 | if (listener != null) {
|
---|
116 | pcs.removePropertyChangeListener(listener);
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Replies list of property change listeners.
|
---|
122 | * @return list of property change listeners
|
---|
123 | */
|
---|
124 | public PropertyChangeListener[] getPropertyChangeListeners() {
|
---|
125 | return pcs.getPropertyChangeListeners();
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void firePCS(String propertyName, Object oldValue, Object newValue) {
|
---|
129 | if (!(oldValue != null && newValue != null && oldValue.equals(newValue))) {
|
---|
130 | pcs.firePropertyChange(propertyName, oldValue, newValue);
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Return the root of the tree of Split, Leaf, and Divider nodes
|
---|
136 | * that define this layout.
|
---|
137 | *
|
---|
138 | * @return the value of the model property
|
---|
139 | * @see #setModel
|
---|
140 | */
|
---|
141 | public Node getModel() {
|
---|
142 | return model;
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Set the root of the tree of Split, Leaf, and Divider nodes
|
---|
147 | * that define this layout. The model can be a Split node
|
---|
148 | * (the typical case) or a Leaf. The default value of this
|
---|
149 | * property is a Leaf named "default".
|
---|
150 | *
|
---|
151 | * @param model the root of the tree of Split, Leaf, and Divider node
|
---|
152 | * @throws IllegalArgumentException if model is a Divider or null
|
---|
153 | * @see #getModel
|
---|
154 | */
|
---|
155 | public void setModel(Node model) {
|
---|
156 | if ((model == null) || (model instanceof Divider))
|
---|
157 | throw new IllegalArgumentException("invalid model");
|
---|
158 | Node oldModel = model;
|
---|
159 | this.model = model;
|
---|
160 | firePCS("model", oldModel, model);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the width of Dividers in Split rows, and the height of
|
---|
165 | * Dividers in Split columns.
|
---|
166 | *
|
---|
167 | * @return the value of the dividerSize property
|
---|
168 | * @see #setDividerSize
|
---|
169 | */
|
---|
170 | public int getDividerSize() {
|
---|
171 | return dividerSize;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * Sets the width of Dividers in Split rows, and the height of
|
---|
176 | * Dividers in Split columns. The default value of this property
|
---|
177 | * is the same as for JSplitPane Dividers.
|
---|
178 | *
|
---|
179 | * @param dividerSize the size of dividers (pixels)
|
---|
180 | * @throws IllegalArgumentException if dividerSize < 0
|
---|
181 | * @see #getDividerSize
|
---|
182 | */
|
---|
183 | public void setDividerSize(int dividerSize) {
|
---|
184 | if (dividerSize < 0)
|
---|
185 | throw new IllegalArgumentException("invalid dividerSize");
|
---|
186 | int oldDividerSize = this.dividerSize;
|
---|
187 | this.dividerSize = dividerSize;
|
---|
188 | firePCS("dividerSize", oldDividerSize, dividerSize);
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * @return the value of the floatingDividers property
|
---|
193 | * @see #setFloatingDividers
|
---|
194 | */
|
---|
195 | public boolean getFloatingDividers() {
|
---|
196 | return floatingDividers;
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * If true, Leaf node bounds match the corresponding component's
|
---|
201 | * preferred size and Splits/Dividers are resized accordingly.
|
---|
202 | * If false then the Dividers define the bounds of the adjacent
|
---|
203 | * Split and Leaf nodes. Typically this property is set to false
|
---|
204 | * after the (MultiSplitPane) user has dragged a Divider.
|
---|
205 | * @param floatingDividers boolean value
|
---|
206 | *
|
---|
207 | * @see #getFloatingDividers
|
---|
208 | */
|
---|
209 | public void setFloatingDividers(boolean floatingDividers) {
|
---|
210 | boolean oldFloatingDividers = this.floatingDividers;
|
---|
211 | this.floatingDividers = floatingDividers;
|
---|
212 | firePCS("floatingDividers", oldFloatingDividers, floatingDividers);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Add a component to this MultiSplitLayout. The
|
---|
217 | * <code>name</code> should match the name property of the Leaf
|
---|
218 | * node that represents the bounds of <code>child</code>. After
|
---|
219 | * layoutContainer() recomputes the bounds of all of the nodes in
|
---|
220 | * the model, it will set this child's bounds to the bounds of the
|
---|
221 | * Leaf node with <code>name</code>. Note: if a component was already
|
---|
222 | * added with the same name, this method does not remove it from
|
---|
223 | * its parent.
|
---|
224 | *
|
---|
225 | * @param name identifies the Leaf node that defines the child's bounds
|
---|
226 | * @param child the component to be added
|
---|
227 | * @see #removeLayoutComponent
|
---|
228 | */
|
---|
229 | @Override
|
---|
230 | public void addLayoutComponent(String name, Component child) {
|
---|
231 | if (name == null)
|
---|
232 | throw new IllegalArgumentException("name not specified");
|
---|
233 | childMap.put(name, child);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Removes the specified component from the layout.
|
---|
238 | *
|
---|
239 | * @param child the component to be removed
|
---|
240 | * @see #addLayoutComponent
|
---|
241 | */
|
---|
242 | @Override
|
---|
243 | public void removeLayoutComponent(Component child) {
|
---|
244 | String name = child.getName();
|
---|
245 | if (name != null) {
|
---|
246 | childMap.remove(name);
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | private Component childForNode(Node node) {
|
---|
251 | if (node instanceof Leaf) {
|
---|
252 | Leaf leaf = (Leaf) node;
|
---|
253 | String name = leaf.getName();
|
---|
254 | return (name != null) ? childMap.get(name) : null;
|
---|
255 | }
|
---|
256 | return null;
|
---|
257 | }
|
---|
258 |
|
---|
259 | private Dimension preferredComponentSize(Node node) {
|
---|
260 | Component child = childForNode(node);
|
---|
261 | return (child != null) ? child.getPreferredSize() : new Dimension(0, 0);
|
---|
262 |
|
---|
263 | }
|
---|
264 |
|
---|
265 | private Dimension preferredNodeSize(Node root) {
|
---|
266 | if (root instanceof Leaf)
|
---|
267 | return preferredComponentSize(root);
|
---|
268 | else if (root instanceof Divider) {
|
---|
269 | int dividerSize = getDividerSize();
|
---|
270 | return new Dimension(dividerSize, dividerSize);
|
---|
271 | } else {
|
---|
272 | Split split = (Split) root;
|
---|
273 | List<Node> splitChildren = split.getChildren();
|
---|
274 | int width = 0;
|
---|
275 | int height = 0;
|
---|
276 | if (split.isRowLayout()) {
|
---|
277 | for (Node splitChild : splitChildren) {
|
---|
278 | Dimension size = preferredNodeSize(splitChild);
|
---|
279 | width += size.width;
|
---|
280 | height = Math.max(height, size.height);
|
---|
281 | }
|
---|
282 | } else {
|
---|
283 | for (Node splitChild : splitChildren) {
|
---|
284 | Dimension size = preferredNodeSize(splitChild);
|
---|
285 | width = Math.max(width, size.width);
|
---|
286 | height += size.height;
|
---|
287 | }
|
---|
288 | }
|
---|
289 | return new Dimension(width, height);
|
---|
290 | }
|
---|
291 | }
|
---|
292 |
|
---|
293 | private Dimension minimumNodeSize(Node root) {
|
---|
294 | if (root instanceof Leaf) {
|
---|
295 | Component child = childForNode(root);
|
---|
296 | return (child != null) ? child.getMinimumSize() : new Dimension(0, 0);
|
---|
297 | } else if (root instanceof Divider) {
|
---|
298 | int dividerSize = getDividerSize();
|
---|
299 | return new Dimension(dividerSize, dividerSize);
|
---|
300 | } else {
|
---|
301 | Split split = (Split) root;
|
---|
302 | List<Node> splitChildren = split.getChildren();
|
---|
303 | int width = 0;
|
---|
304 | int height = 0;
|
---|
305 | if (split.isRowLayout()) {
|
---|
306 | for (Node splitChild : splitChildren) {
|
---|
307 | Dimension size = minimumNodeSize(splitChild);
|
---|
308 | width += size.width;
|
---|
309 | height = Math.max(height, size.height);
|
---|
310 | }
|
---|
311 | } else {
|
---|
312 | for (Node splitChild : splitChildren) {
|
---|
313 | Dimension size = minimumNodeSize(splitChild);
|
---|
314 | width = Math.max(width, size.width);
|
---|
315 | height += size.height;
|
---|
316 | }
|
---|
317 | }
|
---|
318 | return new Dimension(width, height);
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | private static Dimension sizeWithInsets(Container parent, Dimension size) {
|
---|
323 | Insets insets = parent.getInsets();
|
---|
324 | int width = size.width + insets.left + insets.right;
|
---|
325 | int height = size.height + insets.top + insets.bottom;
|
---|
326 | return new Dimension(width, height);
|
---|
327 | }
|
---|
328 |
|
---|
329 | @Override
|
---|
330 | public Dimension preferredLayoutSize(Container parent) {
|
---|
331 | Dimension size = preferredNodeSize(getModel());
|
---|
332 | return sizeWithInsets(parent, size);
|
---|
333 | }
|
---|
334 |
|
---|
335 | @Override
|
---|
336 | public Dimension minimumLayoutSize(Container parent) {
|
---|
337 | Dimension size = minimumNodeSize(getModel());
|
---|
338 | return sizeWithInsets(parent, size);
|
---|
339 | }
|
---|
340 |
|
---|
341 | private static Rectangle boundsWithYandHeight(Rectangle bounds, double y, double height) {
|
---|
342 | Rectangle r = new Rectangle();
|
---|
343 | r.setBounds((int) (bounds.getX()), (int) y, (int) (bounds.getWidth()), (int) height);
|
---|
344 | return r;
|
---|
345 | }
|
---|
346 |
|
---|
347 | private static Rectangle boundsWithXandWidth(Rectangle bounds, double x, double width) {
|
---|
348 | Rectangle r = new Rectangle();
|
---|
349 | r.setBounds((int) x, (int) (bounds.getY()), (int) width, (int) (bounds.getHeight()));
|
---|
350 | return r;
|
---|
351 | }
|
---|
352 |
|
---|
353 | private static void minimizeSplitBounds(Split split, Rectangle bounds) {
|
---|
354 | Rectangle splitBounds = new Rectangle(bounds.x, bounds.y, 0, 0);
|
---|
355 | List<Node> splitChildren = split.getChildren();
|
---|
356 | Node lastChild = splitChildren.get(splitChildren.size() - 1);
|
---|
357 | Rectangle lastChildBounds = lastChild.getBounds();
|
---|
358 | if (split.isRowLayout()) {
|
---|
359 | int lastChildMaxX = lastChildBounds.x + lastChildBounds.width;
|
---|
360 | splitBounds.add(lastChildMaxX, bounds.y + bounds.height);
|
---|
361 | } else {
|
---|
362 | int lastChildMaxY = lastChildBounds.y + lastChildBounds.height;
|
---|
363 | splitBounds.add(bounds.x + bounds.width, lastChildMaxY);
|
---|
364 | }
|
---|
365 | split.setBounds(splitBounds);
|
---|
366 | }
|
---|
367 |
|
---|
368 | private void layoutShrink(Split split, Rectangle bounds) {
|
---|
369 | Rectangle splitBounds = split.getBounds();
|
---|
370 | ListIterator<Node> splitChildren = split.getChildren().listIterator();
|
---|
371 |
|
---|
372 | if (split.isRowLayout()) {
|
---|
373 | int totalWidth = 0; // sum of the children's widths
|
---|
374 | int minWeightedWidth = 0; // sum of the weighted childrens' min widths
|
---|
375 | int totalWeightedWidth = 0; // sum of the weighted childrens' widths
|
---|
376 | for (Node splitChild : split.getChildren()) {
|
---|
377 | int nodeWidth = splitChild.getBounds().width;
|
---|
378 | int nodeMinWidth = Math.min(nodeWidth, minimumNodeSize(splitChild).width);
|
---|
379 | totalWidth += nodeWidth;
|
---|
380 | if (splitChild.getWeight() > 0.0) {
|
---|
381 | minWeightedWidth += nodeMinWidth;
|
---|
382 | totalWeightedWidth += nodeWidth;
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | double x = bounds.getX();
|
---|
387 | double extraWidth = splitBounds.getWidth() - bounds.getWidth();
|
---|
388 | double availableWidth = extraWidth;
|
---|
389 | boolean onlyShrinkWeightedComponents =
|
---|
390 | (totalWeightedWidth - minWeightedWidth) > extraWidth;
|
---|
391 |
|
---|
392 | while (splitChildren.hasNext()) {
|
---|
393 | Node splitChild = splitChildren.next();
|
---|
394 | Rectangle splitChildBounds = splitChild.getBounds();
|
---|
395 | double minSplitChildWidth = minimumNodeSize(splitChild).getWidth();
|
---|
396 | double splitChildWeight = onlyShrinkWeightedComponents
|
---|
397 | ? splitChild.getWeight()
|
---|
398 | : (splitChildBounds.getWidth() / totalWidth);
|
---|
399 |
|
---|
400 | if (!splitChildren.hasNext()) {
|
---|
401 | double newWidth = Math.max(minSplitChildWidth, bounds.getMaxX() - x);
|
---|
402 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);
|
---|
403 | layout2(splitChild, newSplitChildBounds);
|
---|
404 | } else if ((availableWidth > 0.0) && (splitChildWeight > 0.0)) {
|
---|
405 | double allocatedWidth = Math.rint(splitChildWeight * extraWidth);
|
---|
406 | double oldWidth = splitChildBounds.getWidth();
|
---|
407 | double newWidth = Math.max(minSplitChildWidth, oldWidth - allocatedWidth);
|
---|
408 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);
|
---|
409 | layout2(splitChild, newSplitChildBounds);
|
---|
410 | availableWidth -= (oldWidth - splitChild.getBounds().getWidth());
|
---|
411 | } else {
|
---|
412 | double existingWidth = splitChildBounds.getWidth();
|
---|
413 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, existingWidth);
|
---|
414 | layout2(splitChild, newSplitChildBounds);
|
---|
415 | }
|
---|
416 | x = splitChild.getBounds().getMaxX();
|
---|
417 | }
|
---|
418 | } else {
|
---|
419 | int totalHeight = 0; // sum of the children's heights
|
---|
420 | int minWeightedHeight = 0; // sum of the weighted childrens' min heights
|
---|
421 | int totalWeightedHeight = 0; // sum of the weighted childrens' heights
|
---|
422 | for (Node splitChild : split.getChildren()) {
|
---|
423 | int nodeHeight = splitChild.getBounds().height;
|
---|
424 | int nodeMinHeight = Math.min(nodeHeight, minimumNodeSize(splitChild).height);
|
---|
425 | totalHeight += nodeHeight;
|
---|
426 | if (splitChild.getWeight() > 0.0) {
|
---|
427 | minWeightedHeight += nodeMinHeight;
|
---|
428 | totalWeightedHeight += nodeHeight;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | double y = bounds.getY();
|
---|
433 | double extraHeight = splitBounds.getHeight() - bounds.getHeight();
|
---|
434 | double availableHeight = extraHeight;
|
---|
435 | boolean onlyShrinkWeightedComponents =
|
---|
436 | (totalWeightedHeight - minWeightedHeight) > extraHeight;
|
---|
437 |
|
---|
438 | while (splitChildren.hasNext()) {
|
---|
439 | Node splitChild = splitChildren.next();
|
---|
440 | Rectangle splitChildBounds = splitChild.getBounds();
|
---|
441 | double minSplitChildHeight = minimumNodeSize(splitChild).getHeight();
|
---|
442 | double splitChildWeight = onlyShrinkWeightedComponents
|
---|
443 | ? splitChild.getWeight()
|
---|
444 | : (splitChildBounds.getHeight() / totalHeight);
|
---|
445 |
|
---|
446 | if (!splitChildren.hasNext()) {
|
---|
447 | double oldHeight = splitChildBounds.getHeight();
|
---|
448 | double newHeight = Math.max(minSplitChildHeight, bounds.getMaxY() - y);
|
---|
449 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);
|
---|
450 | layout2(splitChild, newSplitChildBounds);
|
---|
451 | availableHeight -= (oldHeight - splitChild.getBounds().getHeight());
|
---|
452 | } else if ((availableHeight > 0.0) && (splitChildWeight > 0.0)) {
|
---|
453 | double allocatedHeight = Math.rint(splitChildWeight * extraHeight);
|
---|
454 | double oldHeight = splitChildBounds.getHeight();
|
---|
455 | double newHeight = Math.max(minSplitChildHeight, oldHeight - allocatedHeight);
|
---|
456 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);
|
---|
457 | layout2(splitChild, newSplitChildBounds);
|
---|
458 | availableHeight -= (oldHeight - splitChild.getBounds().getHeight());
|
---|
459 | } else {
|
---|
460 | double existingHeight = splitChildBounds.getHeight();
|
---|
461 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, existingHeight);
|
---|
462 | layout2(splitChild, newSplitChildBounds);
|
---|
463 | }
|
---|
464 | y = splitChild.getBounds().getMaxY();
|
---|
465 | }
|
---|
466 | }
|
---|
467 |
|
---|
468 | /* The bounds of the Split node root are set to be
|
---|
469 | * big enough to contain all of its children. Since
|
---|
470 | * Leaf children can't be reduced below their
|
---|
471 | * (corresponding java.awt.Component) minimum sizes,
|
---|
472 | * the size of the Split's bounds maybe be larger than
|
---|
473 | * the bounds we were asked to fit within.
|
---|
474 | */
|
---|
475 | minimizeSplitBounds(split, bounds);
|
---|
476 | }
|
---|
477 |
|
---|
478 | private void layoutGrow(Split split, Rectangle bounds) {
|
---|
479 | Rectangle splitBounds = split.getBounds();
|
---|
480 | ListIterator<Node> splitChildren = split.getChildren().listIterator();
|
---|
481 | Node lastWeightedChild = split.lastWeightedChild();
|
---|
482 |
|
---|
483 | if (split.isRowLayout()) {
|
---|
484 | /* Layout the Split's child Nodes' along the X axis. The bounds
|
---|
485 | * of each child will have the same y coordinate and height as the
|
---|
486 | * layoutGrow() bounds argument. Extra width is allocated to the
|
---|
487 | * to each child with a non-zero weight:
|
---|
488 | * newWidth = currentWidth + (extraWidth * splitChild.getWeight())
|
---|
489 | * Any extraWidth "left over" (that's availableWidth in the loop
|
---|
490 | * below) is given to the last child. Note that Dividers always
|
---|
491 | * have a weight of zero, and they're never the last child.
|
---|
492 | */
|
---|
493 | double x = bounds.getX();
|
---|
494 | double extraWidth = bounds.getWidth() - splitBounds.getWidth();
|
---|
495 | double availableWidth = extraWidth;
|
---|
496 |
|
---|
497 | while (splitChildren.hasNext()) {
|
---|
498 | Node splitChild = splitChildren.next();
|
---|
499 | Rectangle splitChildBounds = splitChild.getBounds();
|
---|
500 | double splitChildWeight = splitChild.getWeight();
|
---|
501 |
|
---|
502 | if (!splitChildren.hasNext()) {
|
---|
503 | double newWidth = bounds.getMaxX() - x;
|
---|
504 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);
|
---|
505 | layout2(splitChild, newSplitChildBounds);
|
---|
506 | } else if ((availableWidth > 0.0) && (splitChildWeight > 0.0)) {
|
---|
507 | double allocatedWidth = splitChild.equals(lastWeightedChild)
|
---|
508 | ? availableWidth
|
---|
509 | : Math.rint(splitChildWeight * extraWidth);
|
---|
510 | double newWidth = splitChildBounds.getWidth() + allocatedWidth;
|
---|
511 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, newWidth);
|
---|
512 | layout2(splitChild, newSplitChildBounds);
|
---|
513 | availableWidth -= allocatedWidth;
|
---|
514 | } else {
|
---|
515 | double existingWidth = splitChildBounds.getWidth();
|
---|
516 | Rectangle newSplitChildBounds = boundsWithXandWidth(bounds, x, existingWidth);
|
---|
517 | layout2(splitChild, newSplitChildBounds);
|
---|
518 | }
|
---|
519 | x = splitChild.getBounds().getMaxX();
|
---|
520 | }
|
---|
521 | } else {
|
---|
522 | /* Layout the Split's child Nodes' along the Y axis. The bounds
|
---|
523 | * of each child will have the same x coordinate and width as the
|
---|
524 | * layoutGrow() bounds argument. Extra height is allocated to the
|
---|
525 | * to each child with a non-zero weight:
|
---|
526 | * newHeight = currentHeight + (extraHeight * splitChild.getWeight())
|
---|
527 | * Any extraHeight "left over" (that's availableHeight in the loop
|
---|
528 | * below) is given to the last child. Note that Dividers always
|
---|
529 | * have a weight of zero, and they're never the last child.
|
---|
530 | */
|
---|
531 | double y = bounds.getY();
|
---|
532 | double extraHeight = bounds.getMaxY() - splitBounds.getHeight();
|
---|
533 | double availableHeight = extraHeight;
|
---|
534 |
|
---|
535 | while (splitChildren.hasNext()) {
|
---|
536 | Node splitChild = splitChildren.next();
|
---|
537 | Rectangle splitChildBounds = splitChild.getBounds();
|
---|
538 | double splitChildWeight = splitChild.getWeight();
|
---|
539 |
|
---|
540 | if (!splitChildren.hasNext()) {
|
---|
541 | double newHeight = bounds.getMaxY() - y;
|
---|
542 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);
|
---|
543 | layout2(splitChild, newSplitChildBounds);
|
---|
544 | } else if ((availableHeight > 0.0) && (splitChildWeight > 0.0)) {
|
---|
545 | double allocatedHeight = splitChild.equals(lastWeightedChild)
|
---|
546 | ? availableHeight
|
---|
547 | : Math.rint(splitChildWeight * extraHeight);
|
---|
548 | double newHeight = splitChildBounds.getHeight() + allocatedHeight;
|
---|
549 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, newHeight);
|
---|
550 | layout2(splitChild, newSplitChildBounds);
|
---|
551 | availableHeight -= allocatedHeight;
|
---|
552 | } else {
|
---|
553 | double existingHeight = splitChildBounds.getHeight();
|
---|
554 | Rectangle newSplitChildBounds = boundsWithYandHeight(bounds, y, existingHeight);
|
---|
555 | layout2(splitChild, newSplitChildBounds);
|
---|
556 | }
|
---|
557 | y = splitChild.getBounds().getMaxY();
|
---|
558 | }
|
---|
559 | }
|
---|
560 | }
|
---|
561 |
|
---|
562 | /* Second pass of the layout algorithm: branch to layoutGrow/Shrink
|
---|
563 | * as needed.
|
---|
564 | */
|
---|
565 | private void layout2(Node root, Rectangle bounds) {
|
---|
566 | if (root instanceof Leaf) {
|
---|
567 | Component child = childForNode(root);
|
---|
568 | if (child != null) {
|
---|
569 | child.setBounds(bounds);
|
---|
570 | }
|
---|
571 | root.setBounds(bounds);
|
---|
572 | } else if (root instanceof Divider) {
|
---|
573 | root.setBounds(bounds);
|
---|
574 | } else if (root instanceof Split) {
|
---|
575 | Split split = (Split) root;
|
---|
576 | boolean grow = split.isRowLayout()
|
---|
577 | ? split.getBounds().width <= bounds.width
|
---|
578 | : (split.getBounds().height <= bounds.height);
|
---|
579 | if (grow) {
|
---|
580 | layoutGrow(split, bounds);
|
---|
581 | root.setBounds(bounds);
|
---|
582 | } else {
|
---|
583 | layoutShrink(split, bounds);
|
---|
584 | // split.setBounds() called in layoutShrink()
|
---|
585 | }
|
---|
586 | }
|
---|
587 | }
|
---|
588 |
|
---|
589 | /* First pass of the layout algorithm.
|
---|
590 | *
|
---|
591 | * If the Dividers are "floating" then set the bounds of each
|
---|
592 | * node to accomodate the preferred size of all of the
|
---|
593 | * Leaf's java.awt.Components. Otherwise, just set the bounds
|
---|
594 | * of each Leaf/Split node so that it's to the left of (for
|
---|
595 | * Split.isRowLayout() Split children) or directly above
|
---|
596 | * the Divider that follows.
|
---|
597 | *
|
---|
598 | * This pass sets the bounds of each Node in the layout model. It
|
---|
599 | * does not resize any of the parent Container's
|
---|
600 | * (java.awt.Component) children. That's done in the second pass,
|
---|
601 | * see layoutGrow() and layoutShrink().
|
---|
602 | */
|
---|
603 | private void layout1(Node root, Rectangle bounds) {
|
---|
604 | if (root instanceof Leaf) {
|
---|
605 | root.setBounds(bounds);
|
---|
606 | } else if (root instanceof Split) {
|
---|
607 | Split split = (Split) root;
|
---|
608 | Iterator<Node> splitChildren = split.getChildren().iterator();
|
---|
609 | Rectangle childBounds;
|
---|
610 | int dividerSize = getDividerSize();
|
---|
611 |
|
---|
612 | /* Layout the Split's child Nodes' along the X axis. The bounds
|
---|
613 | * of each child will have the same y coordinate and height as the
|
---|
614 | * layout1() bounds argument.
|
---|
615 | *
|
---|
616 | * Note: the column layout code - that's the "else" clause below
|
---|
617 | * this if, is identical to the X axis (rowLayout) code below.
|
---|
618 | */
|
---|
619 | if (split.isRowLayout()) {
|
---|
620 | double x = bounds.getX();
|
---|
621 | while (splitChildren.hasNext()) {
|
---|
622 | Node splitChild = splitChildren.next();
|
---|
623 | Divider dividerChild =
|
---|
624 | splitChildren.hasNext() ? (Divider) (splitChildren.next()) : null;
|
---|
625 |
|
---|
626 | double childWidth;
|
---|
627 | if (getFloatingDividers()) {
|
---|
628 | childWidth = preferredNodeSize(splitChild).getWidth();
|
---|
629 | } else {
|
---|
630 | if (dividerChild != null) {
|
---|
631 | childWidth = dividerChild.getBounds().getX() - x;
|
---|
632 | } else {
|
---|
633 | childWidth = split.getBounds().getMaxX() - x;
|
---|
634 | }
|
---|
635 | }
|
---|
636 | childBounds = boundsWithXandWidth(bounds, x, childWidth);
|
---|
637 | layout1(splitChild, childBounds);
|
---|
638 |
|
---|
639 | if (getFloatingDividers() && (dividerChild != null)) {
|
---|
640 | double dividerX = childBounds.getMaxX();
|
---|
641 | Rectangle dividerBounds = boundsWithXandWidth(bounds, dividerX, dividerSize);
|
---|
642 | dividerChild.setBounds(dividerBounds);
|
---|
643 | }
|
---|
644 | if (dividerChild != null) {
|
---|
645 | x = dividerChild.getBounds().getMaxX();
|
---|
646 | }
|
---|
647 | }
|
---|
648 | } else {
|
---|
649 | /* Layout the Split's child Nodes' along the Y axis. The bounds
|
---|
650 | * of each child will have the same x coordinate and width as the
|
---|
651 | * layout1() bounds argument. The algorithm is identical to what's
|
---|
652 | * explained above, for the X axis case.
|
---|
653 | */
|
---|
654 | double y = bounds.getY();
|
---|
655 | while (splitChildren.hasNext()) {
|
---|
656 | Node splitChild = splitChildren.next();
|
---|
657 | Node nodeChild = splitChildren.hasNext() ? splitChildren.next() : null;
|
---|
658 | Divider dividerChild = nodeChild instanceof Divider ? (Divider) nodeChild : null;
|
---|
659 | double childHeight;
|
---|
660 | if (getFloatingDividers()) {
|
---|
661 | childHeight = preferredNodeSize(splitChild).getHeight();
|
---|
662 | } else {
|
---|
663 | if (dividerChild != null) {
|
---|
664 | childHeight = dividerChild.getBounds().getY() - y;
|
---|
665 | } else {
|
---|
666 | childHeight = split.getBounds().getMaxY() - y;
|
---|
667 | }
|
---|
668 | }
|
---|
669 | childBounds = boundsWithYandHeight(bounds, y, childHeight);
|
---|
670 | layout1(splitChild, childBounds);
|
---|
671 |
|
---|
672 | if (getFloatingDividers() && (dividerChild != null)) {
|
---|
673 | double dividerY = childBounds.getMaxY();
|
---|
674 | Rectangle dividerBounds = boundsWithYandHeight(bounds, dividerY, dividerSize);
|
---|
675 | dividerChild.setBounds(dividerBounds);
|
---|
676 | }
|
---|
677 | if (dividerChild != null) {
|
---|
678 | y = dividerChild.getBounds().getMaxY();
|
---|
679 | }
|
---|
680 | }
|
---|
681 | }
|
---|
682 | /* The bounds of the Split node root are set to be just
|
---|
683 | * big enough to contain all of its children, but only
|
---|
684 | * along the axis it's allocating space on. That's
|
---|
685 | * X for rows, Y for columns. The second pass of the
|
---|
686 | * layout algorithm - see layoutShrink()/layoutGrow()
|
---|
687 | * allocates extra space.
|
---|
688 | */
|
---|
689 | minimizeSplitBounds(split, bounds);
|
---|
690 | }
|
---|
691 | }
|
---|
692 |
|
---|
693 | /**
|
---|
694 | * The specified Node is either the wrong type or was configured incorrectly.
|
---|
695 | */
|
---|
696 | public static class InvalidLayoutException extends RuntimeException {
|
---|
697 | private final transient Node node;
|
---|
698 |
|
---|
699 | /**
|
---|
700 | * Constructs a new {@code InvalidLayoutException}.
|
---|
701 | * @param msg the detail message. The detail message is saved for later retrieval by the {@link #getMessage()} method.
|
---|
702 | * @param node node
|
---|
703 | */
|
---|
704 | public InvalidLayoutException(String msg, Node node) {
|
---|
705 | super(msg);
|
---|
706 | this.node = node;
|
---|
707 | }
|
---|
708 |
|
---|
709 | /**
|
---|
710 | * @return the invalid Node.
|
---|
711 | */
|
---|
712 | public Node getNode() {
|
---|
713 | return node;
|
---|
714 | }
|
---|
715 | }
|
---|
716 |
|
---|
717 | private static void throwInvalidLayout(String msg, Node node) {
|
---|
718 | throw new InvalidLayoutException(msg, node);
|
---|
719 | }
|
---|
720 |
|
---|
721 | private static void checkLayout(Node root) {
|
---|
722 | if (root instanceof Split) {
|
---|
723 | Split split = (Split) root;
|
---|
724 | if (split.getChildren().size() <= 2) {
|
---|
725 | throwInvalidLayout("Split must have > 2 children", root);
|
---|
726 | }
|
---|
727 | Iterator<Node> splitChildren = split.getChildren().iterator();
|
---|
728 | double weight = 0.0;
|
---|
729 | while (splitChildren.hasNext()) {
|
---|
730 | Node splitChild = splitChildren.next();
|
---|
731 | if (splitChild instanceof Divider) {
|
---|
732 | throwInvalidLayout("expected a Split or Leaf Node", splitChild);
|
---|
733 | }
|
---|
734 | if (splitChildren.hasNext()) {
|
---|
735 | Node dividerChild = splitChildren.next();
|
---|
736 | if (!(dividerChild instanceof Divider)) {
|
---|
737 | throwInvalidLayout("expected a Divider Node", dividerChild);
|
---|
738 | }
|
---|
739 | }
|
---|
740 | weight += splitChild.getWeight();
|
---|
741 | checkLayout(splitChild);
|
---|
742 | }
|
---|
743 | if (weight > 1.0 + 0.000000001) { /* add some epsilon to a double check */
|
---|
744 | throwInvalidLayout("Split children's total weight > 1.0", root);
|
---|
745 | }
|
---|
746 | }
|
---|
747 | }
|
---|
748 |
|
---|
749 | /**
|
---|
750 | * Compute the bounds of all of the Split/Divider/Leaf Nodes in
|
---|
751 | * the layout model, and then set the bounds of each child component
|
---|
752 | * with a matching Leaf Node.
|
---|
753 | */
|
---|
754 | @Override
|
---|
755 | public void layoutContainer(Container parent) {
|
---|
756 | checkLayout(getModel());
|
---|
757 | Insets insets = parent.getInsets();
|
---|
758 | Dimension size = parent.getSize();
|
---|
759 | int width = size.width - (insets.left + insets.right);
|
---|
760 | int height = size.height - (insets.top + insets.bottom);
|
---|
761 | Rectangle bounds = new Rectangle(insets.left, insets.top, width, height);
|
---|
762 | layout1(getModel(), bounds);
|
---|
763 | layout2(getModel(), bounds);
|
---|
764 | }
|
---|
765 |
|
---|
766 | private static Divider dividerAt(Node root, int x, int y) {
|
---|
767 | if (root instanceof Divider) {
|
---|
768 | Divider divider = (Divider) root;
|
---|
769 | return divider.getBounds().contains(x, y) ? divider : null;
|
---|
770 | } else if (root instanceof Split) {
|
---|
771 | Split split = (Split) root;
|
---|
772 | for (Node child : split.getChildren()) {
|
---|
773 | if (child.getBounds().contains(x, y))
|
---|
774 | return dividerAt(child, x, y);
|
---|
775 | }
|
---|
776 | }
|
---|
777 | return null;
|
---|
778 | }
|
---|
779 |
|
---|
780 | /**
|
---|
781 | * Return the Divider whose bounds contain the specified
|
---|
782 | * point, or null if there isn't one.
|
---|
783 | *
|
---|
784 | * @param x x coordinate
|
---|
785 | * @param y y coordinate
|
---|
786 | * @return the Divider at x,y
|
---|
787 | */
|
---|
788 | public Divider dividerAt(int x, int y) {
|
---|
789 | return dividerAt(getModel(), x, y);
|
---|
790 | }
|
---|
791 |
|
---|
792 | private static boolean nodeOverlapsRectangle(Node node, Rectangle r2) {
|
---|
793 | Rectangle r1 = node.getBounds();
|
---|
794 | return
|
---|
795 | (r1.x <= (r2.x + r2.width)) && ((r1.x + r1.width) >= r2.x) &&
|
---|
796 | (r1.y <= (r2.y + r2.height)) && ((r1.y + r1.height) >= r2.y);
|
---|
797 | }
|
---|
798 |
|
---|
799 | private static List<Divider> dividersThatOverlap(Node root, Rectangle r) {
|
---|
800 | if (nodeOverlapsRectangle(root, r) && (root instanceof Split)) {
|
---|
801 | List<Divider> dividers = new ArrayList<>();
|
---|
802 | for (Node child : ((Split) root).getChildren()) {
|
---|
803 | if (child instanceof Divider) {
|
---|
804 | if (nodeOverlapsRectangle(child, r)) {
|
---|
805 | dividers.add((Divider) child);
|
---|
806 | }
|
---|
807 | } else if (child instanceof Split) {
|
---|
808 | dividers.addAll(dividersThatOverlap(child, r));
|
---|
809 | }
|
---|
810 | }
|
---|
811 | return dividers;
|
---|
812 | } else
|
---|
813 | return Collections.emptyList();
|
---|
814 | }
|
---|
815 |
|
---|
816 | /**
|
---|
817 | * Return the Dividers whose bounds overlap the specified
|
---|
818 | * Rectangle.
|
---|
819 | *
|
---|
820 | * @param r target Rectangle
|
---|
821 | * @return the Dividers that overlap r
|
---|
822 | * @throws IllegalArgumentException if the Rectangle is null
|
---|
823 | */
|
---|
824 | public List<Divider> dividersThatOverlap(Rectangle r) {
|
---|
825 | CheckParameterUtil.ensureParameterNotNull(r, "r");
|
---|
826 | return dividersThatOverlap(getModel(), r);
|
---|
827 | }
|
---|
828 |
|
---|
829 | /**
|
---|
830 | * Base class for the nodes that model a MultiSplitLayout.
|
---|
831 | */
|
---|
832 | public static class Node {
|
---|
833 | private Split parent;
|
---|
834 | private Rectangle bounds = new Rectangle();
|
---|
835 | private double weight;
|
---|
836 |
|
---|
837 | /**
|
---|
838 | * Constructs a new {@code Node}.
|
---|
839 | */
|
---|
840 | protected Node() {
|
---|
841 | // Default constructor for subclasses only
|
---|
842 | }
|
---|
843 |
|
---|
844 | /**
|
---|
845 | * Returns the Split parent of this Node, or null.
|
---|
846 | *
|
---|
847 | * This method isn't called getParent(), in order to avoid problems
|
---|
848 | * with recursive object creation when using XmlDecoder.
|
---|
849 | *
|
---|
850 | * @return the value of the parent property.
|
---|
851 | * @see #setParent
|
---|
852 | */
|
---|
853 | public Split getParent() {
|
---|
854 | return parent;
|
---|
855 | }
|
---|
856 |
|
---|
857 | /**
|
---|
858 | * Set the value of this Node's parent property. The default
|
---|
859 | * value of this property is null.
|
---|
860 | *
|
---|
861 | * This method isn't called setParent(), in order to avoid problems
|
---|
862 | * with recursive object creation when using XmlEncoder.
|
---|
863 | *
|
---|
864 | * @param parent a Split or null
|
---|
865 | * @see #getParent
|
---|
866 | */
|
---|
867 | public void setParent(Split parent) {
|
---|
868 | this.parent = parent;
|
---|
869 | }
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Returns the bounding Rectangle for this Node.
|
---|
873 | *
|
---|
874 | * @return the value of the bounds property.
|
---|
875 | * @see #setBounds
|
---|
876 | */
|
---|
877 | public Rectangle getBounds() {
|
---|
878 | return new Rectangle(this.bounds);
|
---|
879 | }
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * Set the bounding Rectangle for this node. The value of
|
---|
883 | * bounds may not be null. The default value of bounds
|
---|
884 | * is equal to <code>new Rectangle(0,0,0,0)</code>.
|
---|
885 | *
|
---|
886 | * @param bounds the new value of the bounds property
|
---|
887 | * @throws IllegalArgumentException if bounds is null
|
---|
888 | * @see #getBounds
|
---|
889 | */
|
---|
890 | public void setBounds(Rectangle bounds) {
|
---|
891 | CheckParameterUtil.ensureParameterNotNull(bounds, "bounds");
|
---|
892 | this.bounds = new Rectangle(bounds);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /**
|
---|
896 | * Value between 0.0 and 1.0 used to compute how much space
|
---|
897 | * to add to this sibling when the layout grows or how
|
---|
898 | * much to reduce when the layout shrinks.
|
---|
899 | *
|
---|
900 | * @return the value of the weight property
|
---|
901 | * @see #setWeight
|
---|
902 | */
|
---|
903 | public double getWeight() {
|
---|
904 | return weight;
|
---|
905 | }
|
---|
906 |
|
---|
907 | /**
|
---|
908 | * The weight property is a between 0.0 and 1.0 used to
|
---|
909 | * compute how much space to add to this sibling when the
|
---|
910 | * layout grows or how much to reduce when the layout shrinks.
|
---|
911 | * If rowLayout is true then this node's width grows
|
---|
912 | * or shrinks by (extraSpace * weight). If rowLayout is false,
|
---|
913 | * then the node's height is changed. The default value
|
---|
914 | * of weight is 0.0.
|
---|
915 | *
|
---|
916 | * @param weight a double between 0.0 and 1.0
|
---|
917 | * @throws IllegalArgumentException if weight is not between 0.0 and 1.0
|
---|
918 | * @see #getWeight
|
---|
919 | * @see MultiSplitLayout#layoutContainer
|
---|
920 | */
|
---|
921 | public void setWeight(double weight) {
|
---|
922 | if ((weight < 0.0) || (weight > 1.0))
|
---|
923 | throw new IllegalArgumentException("invalid weight");
|
---|
924 | this.weight = weight;
|
---|
925 | }
|
---|
926 |
|
---|
927 | private Node siblingAtOffset(int offset) {
|
---|
928 | Split parent = getParent();
|
---|
929 | if (parent == null)
|
---|
930 | return null;
|
---|
931 | List<Node> siblings = parent.getChildren();
|
---|
932 | int index = siblings.indexOf(this);
|
---|
933 | if (index == -1)
|
---|
934 | return null;
|
---|
935 | index += offset;
|
---|
936 | return ((index > -1) && (index < siblings.size())) ? siblings.get(index) : null;
|
---|
937 | }
|
---|
938 |
|
---|
939 | /**
|
---|
940 | * Return the Node that comes after this one in the parent's
|
---|
941 | * list of children, or null. If this node's parent is null,
|
---|
942 | * or if it's the last child, then return null.
|
---|
943 | *
|
---|
944 | * @return the Node that comes after this one in the parent's list of children.
|
---|
945 | * @see #previousSibling
|
---|
946 | * @see #getParent
|
---|
947 | */
|
---|
948 | public Node nextSibling() {
|
---|
949 | return siblingAtOffset(+1);
|
---|
950 | }
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Return the Node that comes before this one in the parent's
|
---|
954 | * list of children, or null. If this node's parent is null,
|
---|
955 | * or if it's the last child, then return null.
|
---|
956 | *
|
---|
957 | * @return the Node that comes before this one in the parent's list of children.
|
---|
958 | * @see #nextSibling
|
---|
959 | * @see #getParent
|
---|
960 | */
|
---|
961 | public Node previousSibling() {
|
---|
962 | return siblingAtOffset(-1);
|
---|
963 | }
|
---|
964 | }
|
---|
965 |
|
---|
966 | /**
|
---|
967 | * Defines a vertical or horizontal subdivision into two or more
|
---|
968 | * tiles.
|
---|
969 | */
|
---|
970 | public static class Split extends Node {
|
---|
971 | private List<Node> children = Collections.emptyList();
|
---|
972 | private boolean rowLayout = true;
|
---|
973 |
|
---|
974 | /**
|
---|
975 | * Returns true if the this Split's children are to be
|
---|
976 | * laid out in a row: all the same height, left edge
|
---|
977 | * equal to the previous Node's right edge. If false,
|
---|
978 | * children are laid on in a column.
|
---|
979 | *
|
---|
980 | * @return the value of the rowLayout property.
|
---|
981 | * @see #setRowLayout
|
---|
982 | */
|
---|
983 | public boolean isRowLayout() {
|
---|
984 | return rowLayout;
|
---|
985 | }
|
---|
986 |
|
---|
987 | /**
|
---|
988 | * Set the rowLayout property. If true, all of this Split's
|
---|
989 | * children are to be laid out in a row: all the same height,
|
---|
990 | * each node's left edge equal to the previous Node's right
|
---|
991 | * edge. If false, children are laid on in a column. Default value is true.
|
---|
992 | *
|
---|
993 | * @param rowLayout true for horizontal row layout, false for column
|
---|
994 | * @see #isRowLayout
|
---|
995 | */
|
---|
996 | public void setRowLayout(boolean rowLayout) {
|
---|
997 | this.rowLayout = rowLayout;
|
---|
998 | }
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Returns this Split node's children. The returned value
|
---|
1002 | * is not a reference to the Split's internal list of children
|
---|
1003 | *
|
---|
1004 | * @return the value of the children property.
|
---|
1005 | * @see #setChildren
|
---|
1006 | */
|
---|
1007 | public List<Node> getChildren() {
|
---|
1008 | return new ArrayList<>(children);
|
---|
1009 | }
|
---|
1010 |
|
---|
1011 | /**
|
---|
1012 | * Set's the children property of this Split node. The parent
|
---|
1013 | * of each new child is set to this Split node, and the parent
|
---|
1014 | * of each old child (if any) is set to null. This method
|
---|
1015 | * defensively copies the incoming List. Default value is an empty List.
|
---|
1016 | *
|
---|
1017 | * @param children List of children
|
---|
1018 | * @throws IllegalArgumentException if children is null
|
---|
1019 | * @see #getChildren
|
---|
1020 | */
|
---|
1021 | public void setChildren(List<Node> children) {
|
---|
1022 | if (children == null)
|
---|
1023 | throw new IllegalArgumentException("children must be a non-null List");
|
---|
1024 | for (Node child : this.children) {
|
---|
1025 | child.setParent(null);
|
---|
1026 | }
|
---|
1027 | this.children = new ArrayList<>(children);
|
---|
1028 | for (Node child : this.children) {
|
---|
1029 | child.setParent(this);
|
---|
1030 | }
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | /**
|
---|
1034 | * Convenience method that returns the last child whose weight
|
---|
1035 | * is > 0.0.
|
---|
1036 | *
|
---|
1037 | * @return the last child whose weight is > 0.0.
|
---|
1038 | * @see #getChildren
|
---|
1039 | * @see Node#getWeight
|
---|
1040 | */
|
---|
1041 | public final Node lastWeightedChild() {
|
---|
1042 | List<Node> children = getChildren();
|
---|
1043 | Node weightedChild = null;
|
---|
1044 | for (Node child : children) {
|
---|
1045 | if (child.getWeight() > 0.0) {
|
---|
1046 | weightedChild = child;
|
---|
1047 | }
|
---|
1048 | }
|
---|
1049 | return weightedChild;
|
---|
1050 | }
|
---|
1051 |
|
---|
1052 | @Override
|
---|
1053 | public String toString() {
|
---|
1054 | int nChildren = getChildren().size();
|
---|
1055 | StringBuilder sb = new StringBuilder("MultiSplitLayout.Split");
|
---|
1056 | sb.append(isRowLayout() ? " ROW [" : " COLUMN [")
|
---|
1057 | .append(nChildren + ((nChildren == 1) ? " child" : " children"))
|
---|
1058 | .append("] ")
|
---|
1059 | .append(getBounds());
|
---|
1060 | return sb.toString();
|
---|
1061 | }
|
---|
1062 | }
|
---|
1063 |
|
---|
1064 | /**
|
---|
1065 | * Models a java.awt Component child.
|
---|
1066 | */
|
---|
1067 | public static class Leaf extends Node {
|
---|
1068 | private String name = "";
|
---|
1069 |
|
---|
1070 | /**
|
---|
1071 | * Create a Leaf node. The default value of name is "".
|
---|
1072 | */
|
---|
1073 | public Leaf() {
|
---|
1074 | // Name can be set later with setName()
|
---|
1075 | }
|
---|
1076 |
|
---|
1077 | /**
|
---|
1078 | * Create a Leaf node with the specified name. Name can not be null.
|
---|
1079 | *
|
---|
1080 | * @param name value of the Leaf's name property
|
---|
1081 | * @throws IllegalArgumentException if name is null
|
---|
1082 | */
|
---|
1083 | public Leaf(String name) {
|
---|
1084 | CheckParameterUtil.ensureParameterNotNull(name, "name");
|
---|
1085 | this.name = name;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /**
|
---|
1089 | * Return the Leaf's name.
|
---|
1090 | *
|
---|
1091 | * @return the value of the name property.
|
---|
1092 | * @see #setName
|
---|
1093 | */
|
---|
1094 | public String getName() {
|
---|
1095 | return name;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /**
|
---|
1099 | * Set the value of the name property. Name may not be null.
|
---|
1100 | *
|
---|
1101 | * @param name value of the name property
|
---|
1102 | * @throws IllegalArgumentException if name is null
|
---|
1103 | */
|
---|
1104 | public void setName(String name) {
|
---|
1105 | CheckParameterUtil.ensureParameterNotNull(name, "name");
|
---|
1106 | this.name = name;
|
---|
1107 | }
|
---|
1108 |
|
---|
1109 | @Override
|
---|
1110 | public String toString() {
|
---|
1111 | return new StringBuilder("MultiSplitLayout.Leaf \"")
|
---|
1112 | .append(getName())
|
---|
1113 | .append("\" weight=")
|
---|
1114 | .append(getWeight())
|
---|
1115 | .append(' ')
|
---|
1116 | .append(getBounds())
|
---|
1117 | .toString();
|
---|
1118 | }
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | /**
|
---|
1122 | * Models a single vertical/horiztonal divider.
|
---|
1123 | */
|
---|
1124 | public static class Divider extends Node {
|
---|
1125 | /**
|
---|
1126 | * Convenience method, returns true if the Divider's parent
|
---|
1127 | * is a Split row (a Split with isRowLayout() true), false
|
---|
1128 | * otherwise. In other words if this Divider's major axis
|
---|
1129 | * is vertical, return true.
|
---|
1130 | *
|
---|
1131 | * @return true if this Divider is part of a Split row.
|
---|
1132 | */
|
---|
1133 | public final boolean isVertical() {
|
---|
1134 | Split parent = getParent();
|
---|
1135 | return parent != null && parent.isRowLayout();
|
---|
1136 | }
|
---|
1137 |
|
---|
1138 | /**
|
---|
1139 | * Dividers can't have a weight, they don't grow or shrink.
|
---|
1140 | * @throws UnsupportedOperationException always
|
---|
1141 | */
|
---|
1142 | @Override
|
---|
1143 | public void setWeight(double weight) {
|
---|
1144 | throw new UnsupportedOperationException();
|
---|
1145 | }
|
---|
1146 |
|
---|
1147 | @Override
|
---|
1148 | public String toString() {
|
---|
1149 | return "MultiSplitLayout.Divider " + getBounds();
|
---|
1150 | }
|
---|
1151 | }
|
---|
1152 | }
|
---|