1 /*
2 * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3 *
4 * Based in London, we are world leaders in the design and development
5 * of bespoke applications for the securities financing markets.
6 *
7 * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8 * ___ _ _ _ _ _
9 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
10 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
11 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
12 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13 * |__/
14 *
15 * www.ObjectLab.co.uk
16 *
17 * $Id: SplitBySlice.java 74 2006-10-24 22:19:05Z benoitx $
18 *
19 * Copyright 2006 the original author or authors.
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22 * use this file except in compliance with the License. You may obtain a copy of
23 * the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30 * License for the specific language governing permissions and limitations under
31 * the License.
32 */
33 package net.sf.jtreemap.swing;
34
35 import java.util.Vector;
36
37 /**
38 * Split the treemap by slice
39 *
40 * @author Laurent DUTHEIL
41 */
42 public class SplitBySlice extends SplitStrategy {
43
44 /**
45 *
46 */
47 private static final long serialVersionUID = 8484486418097321160L;
48
49 /**
50 * Calculate the dimension of the elements of the Vector.
51 *
52 * @param x0
53 * x-coordinate
54 * @param y0
55 * y-coordinate
56 * @param w0
57 * width
58 * @param h0
59 * height
60 * @param v
61 * elements to split in the dimensions before
62 * @param sumWeight
63 * sum of the weights
64 */
65 public static void splitInSlice(final int x0, final int y0, final int w0, final int h0, final Vector<TreeMapNode> v,
66 final double sumWeight) {
67 int offset = 0;
68 boolean vertical = h0 > w0;
69
70 for (final TreeMapNode node : v) {
71 if (vertical) {
72 node.setX(x0);
73 node.setWidth(w0);
74 node.setY(y0 + offset);
75 node.setHeight((int) Math.round(h0 * node.getWeight() / sumWeight));
76 offset = offset + node.getHeight();
77 } else {
78 node.setX(x0 + offset);
79 node.setWidth((int) Math.round(w0 * node.getWeight() / sumWeight));
80 node.setY(y0);
81 node.setHeight(h0);
82 offset = offset + node.getWidth();
83 }
84 }
85
86 // Because of the Math.round(), we adjust the last element to fit the
87 // correctly the JTreeMap
88 if (!v.isEmpty()) {
89 final TreeMapNode node = v.lastElement();
90 if (vertical && h0 != offset) {
91 node.setHeight(node.getHeight() - offset + h0);
92 } else if (!vertical && w0 != offset) {
93 node.setWidth(node.getWidth() - offset + w0);
94 }
95 }
96 }
97
98 /*
99 * (non-Javadoc)
100 *
101 * @see net.sf.jtreemap.swing.SplitStrategy#splitElements(java.util.Vector,
102 * java.util.Vector, java.util.Vector)
103 */
104 @Override
105 public void splitElements(final Vector<TreeMapNode> v, final Vector<TreeMapNode> v1, final Vector<TreeMapNode> v2) {
106 // ignore
107
108 }
109
110 /*
111 * (non-Javadoc)
112 *
113 * @see net.sf.jtreemap.swing.SplitStrategy#calculatePositionsRec(int, int,
114 * int, int, double, java.util.Vector)
115 */
116 @Override
117 protected void calculatePositionsRec(final int x0, final int y0, final int w0, final int h0, final double weight0,
118 final Vector<TreeMapNode> v) {
119
120 SplitBySlice.splitInSlice(x0, y0, w0, h0, v, weight0);
121
122 for (final TreeMapNode node : v) {
123 if (node.isLeaf()) {
124 node.setX(node.getX() + TreeMapNode.getBorder());
125 node.setY(node.getY() + TreeMapNode.getBorder());
126 node.setHeight(node.getHeight() - TreeMapNode.getBorder());
127 node.setWidth(node.getWidth() - TreeMapNode.getBorder());
128 } else {
129 // if this is not a leaf, calculation for the children
130 if (TreeMapNode.getBorder() > 1) {
131 TreeMapNode.setBorder(TreeMapNode.getBorder() - 2);
132 calculatePositionsRec(node.getX() + 2, node.getY() + 2, node.getWidth() - 2, node.getHeight() - 2, node
133 .getWeight(), node.getChildren());
134 TreeMapNode.setBorder(TreeMapNode.getBorder() + 2);
135 } else if (TreeMapNode.getBorder() == 1) {
136 TreeMapNode.setBorder(0);
137 calculatePositionsRec(node.getX() + 1, node.getY() + 1, node.getWidth() - 1, node.getHeight() - 1, node
138 .getWeight(), node.getChildren());
139 TreeMapNode.setBorder(1);
140 } else {
141 calculatePositionsRec(node.getX(), node.getY(), node.getWidth(), node.getHeight(), node.getWeight(), node
142 .getChildren());
143 }
144 }
145 }
146
147 }
148 }
149 /*
150 * ObjectLab is supporing JTreeMap
151 *
152 * Based in London, we are world leaders in the design and development
153 * of bespoke applications for the securities financing markets.
154 *
155 * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
156 * ___ _ _ _ _ _
157 * / _ \| |__ (_) ___ ___| |_| | __ _| |__
158 * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \
159 * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) |
160 * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
161 * |__/
162 *
163 * www.ObjectLab.co.uk
164 */