PlaceNode1.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 4k
Category:

Windows Develop

Development Platform:

Java

  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.  *    PlaceNode1.java
  18.  *    Copyright (C) 1999 Malcolm Ware
  19.  *
  20.  */
  21. package weka.gui.treevisualizer;
  22. /**
  23.  * This class will place the Nodes of a tree. <p>
  24.  * 
  25.  * It will place these nodes so that they symetrically fill each row. 
  26.  * This is simple to calculate but is not visually nice for most trees.<p>
  27.  *
  28.  * @author Malcolm F Ware (mfw4@cs.waikato.ac.nz)
  29.  * @version $Revision: 1.3 $
  30.  */
  31. public class PlaceNode1 implements NodePlace {
  32.   /** An array containing the spacing value for each level */
  33.   private double[] m_levels; //contains num of nodes one each level
  34.   /** The number of levels in the tree */ 
  35.   private int m_noLevels;//contains num of levels
  36.   /** An array containing the current node place for each level to place 
  37.    * each node accordingly. */
  38.   private int[] m_levelNode; //contains num of node upto on particular level
  39.   /** The distance between each level. */
  40.   private double m_yRatio; //for quicker running y_ratio is a constant after 
  41.                          //being calculated
  42.   /**
  43.    * Call this function to have each node in the tree starting at 'r' placed 
  44.    * in a visual
  45.    * (not logical, they already are) tree position.
  46.    *
  47.    * @param r The top of the tree.
  48.    */
  49.   public void place(Node r) {
  50.     /* this is the first and most basic algorithm to write
  51.        I will use this as a reference to test the classes 
  52.        this works by counting up the nodes on each level and spacing the
  53.        level evenly so that it is all used
  54.     */
  55.     /* this loop will work by starting at the first node
  56.        and systematically going through all their children from left
  57.        to right.but first it will do a quick pass to find out the number
  58.        of levels there are*/
  59.     //+ 1 so that no nodes are on edge of screen
  60.     m_noLevels = r.getHeight(r,0)+1;
  61.     
  62.     m_yRatio = 1 / (double) m_noLevels;
  63.     
  64.     m_levels = new double[m_noLevels];
  65.     m_levelNode = new int[m_noLevels];
  66.     for (int noa = 0;noa < m_noLevels;noa++) {
  67.       m_levels[noa] = 1;
  68.       m_levelNode[noa] = 0;
  69.     }
  70.     
  71.     setNumOfNodes(r,0);
  72.     
  73.     for (int noa = 0;noa < m_noLevels;noa++) {
  74.       m_levels[noa] = 1 / m_levels[noa];
  75.     }
  76.     
  77.     placer(r,0);
  78.   }
  79.   /**
  80.    * This function finds the number of nodes on each level recursively.
  81.    *
  82.    * @param r The current Node upto.
  83.    * @param l The current level upto.
  84.    */
  85.   private void setNumOfNodes(Node r,int l) {
  86.     Edge e;
  87.     l++;
  88.     
  89.     m_levels[l]++;
  90.     for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
  91.       setNumOfNodes(e.getTarget(),l);
  92.     }
  93.   }
  94.   
  95.   /**
  96.    * This function goes through and sets the position of each node
  97.    *
  98.    * @param r The current node upto.
  99.    * @param l the current level upto.
  100.    */
  101.   private void placer(Node r,int l) {
  102.     Edge e;
  103.     l++;
  104.     m_levelNode[l]++;
  105.     r.setCenter(m_levelNode[l] * m_levels[l]);
  106.     r.setTop(l * m_yRatio);
  107.     for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
  108.       placer(e.getTarget(),l);
  109.     }
  110.   }
  111. }