View Javadoc
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$
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.List;
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 List.
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 List<TreeMapNode> v, final double sumWeight) {
66          int offset = 0;
67          final boolean vertical = h0 > w0;
68  
69          for (final TreeMapNode node : v) {
70              if (vertical) {
71                  node.setX(x0);
72                  node.setWidth(w0);
73                  node.setY(y0 + offset);
74                  node.setHeight((int) Math.round(h0 * node.getWeight() / sumWeight));
75                  offset = offset + node.getHeight();
76              } else {
77                  node.setX(x0 + offset);
78                  node.setWidth((int) Math.round(w0 * node.getWeight() / sumWeight));
79                  node.setY(y0);
80                  node.setHeight(h0);
81                  offset = offset + node.getWidth();
82              }
83          }
84  
85          // Because of the Math.round(), we adjust the last element to fit the
86          // correctly the JTreeMap
87          if (!v.isEmpty()) {
88              final TreeMapNode lastElement = v.get(v.size() - 1);
89              if (vertical && h0 != offset) {
90                  lastElement.setHeight(lastElement.getHeight() - offset + h0);
91              } else if (!vertical && w0 != offset) {
92                  lastElement.setWidth(lastElement.getWidth() - offset + w0);
93              }
94          }
95      }
96  
97      /*
98       * (non-Javadoc)
99       *
100      * @see net.sf.jtreemap.swing.SplitStrategy#splitElements(java.util.List,
101      *      java.util.List, java.util.List)
102      */
103     @Override
104     public void splitElements(final List<TreeMapNode> v, final List<TreeMapNode> v1, final List<TreeMapNode> v2) {
105         // ignore
106 
107     }
108 
109     /*
110      * (non-Javadoc)
111      *
112      * @see net.sf.jtreemap.swing.SplitStrategy#calculatePositionsRec(int, int,
113      *      int, int, double, java.util.List)
114      */
115     @Override
116     protected void calculatePositionsRec(final int x0, final int y0, final int w0, final int h0, final double weight0, final List<TreeMapNode> v) {
117 
118         // 1. don't calculate if the area is too small,
119         if (w0 * h0 < 20) {
120             return;
121         }
122 
123         // 2. don't calculate if the candidates are too many to display
124         if (w0 * h0 < v.size()) {
125             return;
126         }
127 
128         SplitBySlice.splitInSlice(x0, y0, w0, h0, v, weight0);
129 
130         for (final TreeMapNode node : v) {
131             if (node.isLeaf()) {
132                 node.setX(node.getX() + TreeMapNode.getBorder());
133                 node.setY(node.getY() + TreeMapNode.getBorder());
134                 node.setHeight(node.getHeight() - TreeMapNode.getBorder());
135                 node.setWidth(node.getWidth() - TreeMapNode.getBorder());
136             } else {
137                 // if this is not a leaf, calculation for the children
138                 if (TreeMapNode.getBorder() > 1) {
139                     TreeMapNode.setBorder(TreeMapNode.getBorder() - 2);
140                     calculatePositionsRec(node.getX() + 2, node.getY() + 2, node.getWidth() - 2, node.getHeight() - 2, node.getWeight(),
141                             node.getChildren());
142                     TreeMapNode.setBorder(TreeMapNode.getBorder() + 2);
143                 } else if (TreeMapNode.getBorder() == 1) {
144                     TreeMapNode.setBorder(0);
145                     calculatePositionsRec(node.getX() + 1, node.getY() + 1, node.getWidth() - 1, node.getHeight() - 1, node.getWeight(),
146                             node.getChildren());
147                     TreeMapNode.setBorder(1);
148                 } else {
149                     calculatePositionsRec(node.getX(), node.getY(), node.getWidth(), node.getHeight(), node.getWeight(), node.getChildren());
150                 }
151             }
152         }
153 
154     }
155 }
156 /*
157  *                 ObjectLab is supporing JTreeMap
158  *
159  * Based in London, we are world leaders in the design and development
160  * of bespoke applications for the securities financing markets.
161  *
162  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
163  *           ___  _     _           _   _          _
164  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
165  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
166  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
167  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
168  *                   |__/
169  *
170  *                     www.ObjectLab.co.uk
171  */