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: SplitBySlice.java 75 2006-10-24 23:00:51Z 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.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      // Because of the Math.round(), we adjust the last element to fit the
76      // correctly the JTreeMap
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     * (non-Javadoc)
89     *
90     * @see net.sf.jtreemap.swing.SplitStrategy#splitElements(java.util.Vector,
91     *      java.util.Vector, java.util.Vector)
92     */
93    @Override
94    public void splitElements(List<TreeMapNode> v, List<TreeMapNode> v1,
95        List<TreeMapNode> v2) {
96    // ignore
97  
98    }
99  
100   /*
101    * (non-Javadoc)
102    *
103    * @see net.sf.jtreemap.swing.SplitStrategy#calculatePositionsRec(int, int,
104    *      int, int, double, java.util.Vector)
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         // if this is not a leaf, calculation for the children
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  *                 ObjectLab is supporing JTreeMap
142  * 
143  * Based in London, we are world leaders in the design and development 
144  * of bespoke applications for the securities financing markets.
145  * 
146  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
147  *           ___  _     _           _   _          _
148  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
149  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
150  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
151  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
152  *                   |__/
153  *
154  *                     www.ObjectLab.co.uk
155  */