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
27
28
29
30
31
32
33 package net.sf.jtreemap.ktreemap;
34
35 import java.util.List;
36
37 import org.eclipse.swt.graphics.Rectangle;
38
39 /**
40 * Split the treemap by slice
41 *
42 * @author Laurent DUTHEIL
43 */
44 public class SplitBySlice extends SplitStrategy {
45
46 /**
47 * Calculate the dimension of the elements of the Vector.
48 * @param bounds bounds of the KTreeMap
49 * @param children elements to split in the dimensions before
50 * @param sumWeight sum of the weights
51 */
52 public static void splitInSlice(Rectangle bounds, List<TreeMapNode> children,
53 double sumWeight) {
54 int offset = 0;
55 boolean vertical = bounds.height > bounds.width;
56
57 for (TreeMapNode node : children) {
58 if (vertical) {
59 node.setX(bounds.x);
60 node.setWidth(bounds.width);
61 node.setY(bounds.y + offset);
62 node.setHeight((int)Math.round(bounds.height * node.getWeight()
63 / sumWeight));
64 offset = offset + node.getHeight();
65 } else {
66 node.setX(bounds.x + offset);
67 node.setWidth((int)Math.round(bounds.width * node.getWeight()
68 / sumWeight));
69 node.setY(bounds.y);
70 node.setHeight(bounds.height);
71 offset = offset + node.getWidth();
72 }
73 }
74
75
76
77 if (!children.isEmpty()) {
78 TreeMapNode node = children.get(children.size() - 1);
79 if (vertical && bounds.height != offset) {
80 node.setHeight(node.getHeight() - offset + bounds.height);
81 } else if (!vertical && bounds.width != offset) {
82 node.setWidth(node.getWidth() - offset + bounds.width);
83 }
84 }
85 }
86
87
88
89
90
91
92
93 @Override
94 public void splitElements(List<TreeMapNode> v, List<TreeMapNode> v1,
95 List<TreeMapNode> v2) {
96
97
98 }
99
100
101
102
103
104
105
106 @Override
107 protected void calculatePositionsRec(Rectangle bounds, double weight0,
108 List<TreeMapNode> v) {
109
110 SplitBySlice.splitInSlice(bounds, v, weight0);
111
112 for (TreeMapNode node : v) {
113 if (node.isLeaf()) {
114 node.setX(node.getX() + TreeMapNode.getBorder());
115 node.setY(node.getY() + TreeMapNode.getBorder());
116 node.setHeight(node.getHeight() - TreeMapNode.getBorder());
117 node.setWidth(node.getWidth() - TreeMapNode.getBorder());
118 } else {
119
120 if (TreeMapNode.getBorder() > 1) {
121 TreeMapNode.setBorder(TreeMapNode.getBorder() - 2);
122 Rectangle newBounds = new Rectangle(node.getX() + 2, node.getY() + 2,
123 node.getWidth() - 2, node.getHeight() - 2);
124 calculatePositionsRec(newBounds, node.getWeight(), node.getChildren());
125 TreeMapNode.setBorder(TreeMapNode.getBorder() + 2);
126 } else if (TreeMapNode.getBorder() == 1) {
127 TreeMapNode.setBorder(0);
128 Rectangle newBounds = new Rectangle(node.getX() + 1, node.getY() + 1,
129 node.getWidth() - 1, node.getHeight() - 1);
130 calculatePositionsRec(newBounds, node.getWeight(), node.getChildren());
131 TreeMapNode.setBorder(1);
132 } else {
133 calculatePositionsRec(node.getBounds(), node.getWeight(), node
134 .getChildren());
135 }
136 }
137 }
138 }
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155