[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * PlaceNode1.java |
---|
| 19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.gui.treevisualizer; |
---|
| 24 | |
---|
| 25 | /** |
---|
| 26 | * This class will place the Nodes of a tree. <p> |
---|
| 27 | * |
---|
| 28 | * It will place these nodes so that they symetrically fill each row. |
---|
| 29 | * This is simple to calculate but is not visually nice for most trees.<p> |
---|
| 30 | * |
---|
| 31 | * @author Malcolm F Ware (mfw4@cs.waikato.ac.nz) |
---|
| 32 | * @version $Revision: 1.4 $ |
---|
| 33 | */ |
---|
| 34 | public class PlaceNode1 implements NodePlace { |
---|
| 35 | /** An array containing the spacing value for each level */ |
---|
| 36 | private double[] m_levels; //contains num of nodes one each level |
---|
| 37 | |
---|
| 38 | /** The number of levels in the tree */ |
---|
| 39 | private int m_noLevels;//contains num of levels |
---|
| 40 | |
---|
| 41 | /** An array containing the current node place for each level to place |
---|
| 42 | * each node accordingly. */ |
---|
| 43 | private int[] m_levelNode; //contains num of node upto on particular level |
---|
| 44 | |
---|
| 45 | /** The distance between each level. */ |
---|
| 46 | private double m_yRatio; //for quicker running y_ratio is a constant after |
---|
| 47 | |
---|
| 48 | //being calculated |
---|
| 49 | /** |
---|
| 50 | * Call this function to have each node in the tree starting at 'r' placed |
---|
| 51 | * in a visual |
---|
| 52 | * (not logical, they already are) tree position. |
---|
| 53 | * |
---|
| 54 | * @param r The top of the tree. |
---|
| 55 | */ |
---|
| 56 | public void place(Node r) { |
---|
| 57 | /* this is the first and most basic algorithm to write |
---|
| 58 | I will use this as a reference to test the classes |
---|
| 59 | |
---|
| 60 | this works by counting up the nodes on each level and spacing the |
---|
| 61 | level evenly so that it is all used |
---|
| 62 | */ |
---|
| 63 | |
---|
| 64 | /* this loop will work by starting at the first node |
---|
| 65 | and systematically going through all their children from left |
---|
| 66 | to right.but first it will do a quick pass to find out the number |
---|
| 67 | of levels there are*/ |
---|
| 68 | |
---|
| 69 | //+ 1 so that no nodes are on edge of screen |
---|
| 70 | m_noLevels = r.getHeight(r,0)+1; |
---|
| 71 | |
---|
| 72 | m_yRatio = 1 / (double) m_noLevels; |
---|
| 73 | |
---|
| 74 | m_levels = new double[m_noLevels]; |
---|
| 75 | m_levelNode = new int[m_noLevels]; |
---|
| 76 | for (int noa = 0;noa < m_noLevels;noa++) { |
---|
| 77 | m_levels[noa] = 1; |
---|
| 78 | m_levelNode[noa] = 0; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | setNumOfNodes(r,0); |
---|
| 82 | |
---|
| 83 | for (int noa = 0;noa < m_noLevels;noa++) { |
---|
| 84 | m_levels[noa] = 1 / m_levels[noa]; |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | placer(r,0); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * This function finds the number of nodes on each level recursively. |
---|
| 92 | * |
---|
| 93 | * @param r The current Node upto. |
---|
| 94 | * @param l The current level upto. |
---|
| 95 | */ |
---|
| 96 | private void setNumOfNodes(Node r,int l) { |
---|
| 97 | Edge e; |
---|
| 98 | l++; |
---|
| 99 | |
---|
| 100 | m_levels[l]++; |
---|
| 101 | for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) { |
---|
| 102 | setNumOfNodes(e.getTarget(),l); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * This function goes through and sets the position of each node |
---|
| 108 | * |
---|
| 109 | * @param r The current node upto. |
---|
| 110 | * @param l the current level upto. |
---|
| 111 | */ |
---|
| 112 | private void placer(Node r,int l) { |
---|
| 113 | Edge e; |
---|
| 114 | l++; |
---|
| 115 | m_levelNode[l]++; |
---|
| 116 | r.setCenter(m_levelNode[l] * m_levels[l]); |
---|
| 117 | r.setTop(l * m_yRatio); |
---|
| 118 | for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) { |
---|
| 119 | placer(e.getTarget(),l); |
---|
| 120 | } |
---|
| 121 | } |
---|
| 122 | } |
---|