Node.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 13k
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.  *    Node.java
  18.  *    Copyright (C) 1999 Malcolm Ware
  19.  *
  20.  */
  21. package weka.gui.treevisualizer;
  22. import java.awt.*;
  23. import java.util.*;
  24. import java.io.*;
  25. import weka.core.Instances;
  26. //this is a node structure that to be useful needs the Edge class as well
  27. //note i have done an unintentional naughty thing
  28. //getHeight() returns the pixel height of the node
  29. //getHeight(Node,int) returns how many levels down the tree goes
  30. //setHeight(int) is associated to the prior
  31. /**
  32.  * This class records all the data about a particular node for displaying.
  33.  *
  34.  * @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
  35.  * @version $Revision: 1.3 $
  36.  */
  37. public class Node {
  38.   
  39.   /** The fill mode for the node (not in use). */
  40.   private int m_backstyle;       //how the back color will fill
  41.   /** The shape of the node. */
  42.   private int m_shape;
  43.   /** The color of the node. */
  44.   private Color m_color;
  45.   /** the text for the node. */
  46.   private String m_label;
  47.   /** the text broken up into lines */
  48.   private Vector m_lines;
  49.   //the coord of the left side .note all coords are
  50.   //between 1-0 for scaling per Stuart's suggestion
  51.   /** The center of the node (between 0 and 1). */
  52.   private double m_center;       //coord of the center . main x value used
  53.   /** The top of the node (between 0 and 1). */
  54.   private double m_top;          //main y coord to go by
  55.      
  56.   /** true if this nodes descendants are visible (not in use currently). */
  57.   private boolean m_cVisible;   //whether it's descendants are visible
  58.   /** true if this node is visible (not currently in use). */
  59.   private boolean m_visible;     //whether it's visible
  60.   /** true if this is the top of the tree. ie has no parent */
  61.   private boolean m_root;     //whether it is anscestor to all i.e top of tree 
  62.   /** An array containing references to all the parent edges 
  63.    * (only 1 currently). */
  64.   private Vector m_parent;      //the edge to its parent edges(or itself 
  65.   //if true root)
  66.   /** An array containing references to all the child edges. */
  67.   private Vector m_children;     //a vector list of edges to the nodes children
  68.   /** The ID string for this node (used for construction purposes) */
  69.   private String m_refer;
  70.   /** A String containing extra information about the node. */
  71.   private String m_data;
  72.   /**
  73.    * An Instances variable generated from the data.
  74.    * Note that if this exists then the string shall be NULL to save space.
  75.    */
  76.   private Instances m_theData;
  77.   /**
  78.    * This will setup all the values of the node except for its top and center.
  79.    *
  80.    * @param label The text for the node.
  81.    * @param refer The ID string for this node.
  82.    * @param backstyle The backstyle of this node.
  83.    * @param shape The shape of this node.
  84.    * @param color The color of this node.
  85.    */
  86.   public Node(String label,String refer,int backstyle,int shape,
  87.       Color color,String d) {
  88.     m_label = label;
  89.     m_backstyle = backstyle;
  90.     m_shape = shape;
  91.     m_color = color;
  92.     m_refer = refer;
  93.    
  94.     m_center = 0;
  95.     m_top = 0;
  96.     m_cVisible = true;
  97.     m_visible = true;
  98.     m_root = false;
  99.     m_parent = new Vector(1,1);
  100.     m_children = new Vector(20,10);
  101.     m_lines = new Vector(4,2);
  102.     breakupLabel();
  103.     m_data = d;
  104.     m_theData = null;
  105.   } 
  106.   
  107.   /**
  108.    * This will return the Instances object related to this node.
  109.    * If it has not been allocated then that will be done also.
  110.    *
  111.    * @return The Instances object.
  112.    */
  113.   public Instances getInstances() {
  114.     if (m_theData == null && m_data != null) {
  115.       try {
  116. m_theData = new Instances(new StringReader(m_data));
  117.       } catch(Exception e) {
  118. System.out.println("Error : " + e);
  119.       }
  120.       m_data = null;
  121.     }
  122.     return m_theData;
  123.   }
  124.   /**
  125.    * Get If this node's childs are visible.
  126.    *
  127.    * @return True if the childs are visible.
  128.    */
  129.   public boolean getCVisible() {
  130.     return m_cVisible;
  131.   }
  132.   /** 
  133.    * Recursively goes through the tree and sets all the children and the 
  134.    * parent to visible.
  135.    *
  136.    * @param r The current node to set visible.
  137.    */
  138.   private void childVis(Node r) {
  139.     Edge e;
  140.     r.setVisible(true);
  141.     if (r.getCVisible()) {
  142.       for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  143. childVis(e.getTarget());
  144.       }
  145.     }
  146.   }
  147.   /**
  148.    * Sets all the children of this node either to visible or invisible
  149.    *
  150.    * @param v True if the children are to be visible
  151.    */
  152.   public void setCVisible(boolean v) {
  153.     m_cVisible = v;
  154.     if (v) {
  155.       childVis(this);
  156.     }
  157.     else if (!v) {
  158.       childInv(this);
  159.     }
  160.   }
  161.   
  162.   /**
  163.    * Recursively goes through the tree and sets all the children to invisible,
  164.    * Not the parent though.
  165.    *
  166.    * @param r The current node from whom the children are gonna be set 
  167.    * invisible.
  168.    */
  169.   private void childInv(Node r) {
  170.     Edge e;
  171.     Node s;
  172.     for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  173.       s = e.getTarget();
  174.       s.setVisible(false);
  175.       childInv(s);
  176.     }
  177.   }
  178.   
  179.  
  180.   
  181.  
  182.   
  183.   /**
  184.    * Get the value of refer.
  185.    *
  186.    * @return Value of refer.
  187.    */
  188.   public String getRefer() {
  189.     
  190.     return m_refer;
  191.   }
  192.   
  193.   /**
  194.    * Set the value of refer.
  195.    *
  196.    * @param v  Value to assign to refer.
  197.    */
  198.   public void setRefer(String v) {
  199.     
  200.     m_refer = v;
  201.   }
  202.   
  203.   
  204.   
  205.   /**
  206.    * Get the value of shape.
  207.    *
  208.    * @return Value of shape.
  209.    */
  210.   public int getShape() {
  211.     
  212.     return m_shape;
  213.   }
  214.   
  215.   /**
  216.    * Set the value of shape.
  217.    *
  218.    * @param v  Value to assign to shape.
  219.    */
  220.   public void setShape(int v) {
  221.     
  222.     m_shape = v;
  223.   }
  224.   
  225.   
  226.   /**
  227.    * Get the value of color.
  228.    *
  229.    * @return Value of color.
  230.    */
  231.   public Color getColor() {
  232.     
  233.     return m_color;
  234.   }
  235.   
  236.   /**
  237.    * Set the value of color.
  238.    *
  239.    * @param v  Value to assign to color.
  240.    */
  241.   public void setColor(Color v) {
  242.     
  243.     m_color = v;
  244.   }
  245.   
  246.   
  247.   /**
  248.    * Get the value of label.
  249.    *
  250.    * @return Value of label.
  251.    */
  252.   public String getLabel() {
  253.     
  254.     return m_label;
  255.   }
  256.   
  257.   /**
  258.    * This Will break the node's text up into lines.
  259.    *
  260.    */
  261.   private void breakupLabel() {
  262.     int prev = 0,noa;
  263.     for (noa = 0;noa < m_label.length();noa++) {
  264.       if (m_label.charAt(noa) == 'n') {
  265. m_lines.addElement(m_label.substring(prev,noa));
  266. prev = noa+1;
  267.       }
  268.     }
  269.     m_lines.addElement(m_label.substring(prev,noa));
  270.     
  271.   }
  272.   
  273.   /**
  274.    * This will return the width and height of the rectangle that the text 
  275.    * will fit into.
  276.    *
  277.    * @param f The size info for the Font.
  278.    * @return A Dimension containing the size of the text.
  279.    */
  280.   public Dimension stringSize(FontMetrics f) {
  281.     Dimension d = new Dimension();
  282.     int old = 0;
  283.     String s;
  284.     int noa = 0;
  285.     while ((s = getLine(noa)) != null) {
  286.       noa++;
  287.       old = f.stringWidth(s);
  288.       
  289.       if (old > d.width) {
  290. d.width = old;
  291.       }
  292.     }
  293.     d.height = noa * f.getHeight();
  294.     return d;
  295.     
  296.   }
  297.   /**
  298.    * Returns the text String for the specfied line.
  299.    *
  300.    * @param n The line wanted.
  301.    * @return The String corresponding to that line.
  302.    */
  303.   public String getLine(int n) {
  304.     if (n < m_lines.size()) {
  305.       return (String)m_lines.elementAt(n);
  306.     }
  307.     else {
  308.       return null;
  309.     }
  310.   }
  311.   
  312.   
  313.   
  314.   
  315.  
  316.   
  317.   
  318.   
  319.   /**
  320.    * Get the value of center.
  321.    *
  322.    * @return Value of center.
  323.    */
  324.   public double getCenter() {
  325.     
  326.     return m_center;
  327.   }
  328.   
  329.   /**
  330.    * Set the value of center.
  331.    *
  332.    * @param v  Value to assign to center.
  333.    */
  334.   public void setCenter(double v) {
  335.     
  336.     m_center = v;
  337.   }
  338.   
  339.   /**
  340.    * Will increase or decrease the postion of center.
  341.    *
  342.    * @param v The amount to increase or decrease center by.
  343.    */
  344.   public void adjustCenter(double v) {
  345.     m_center += v;
  346.   }
  347.   
  348.   
  349.   /**
  350.    * Get the value of top.
  351.    *
  352.    * @return Value of top.
  353.    */
  354.   public double getTop() {
  355.     
  356.     return m_top;
  357.   }
  358.   
  359.   /**
  360.    * Set the value of top.
  361.    *
  362.    * @param v  Value to assign to top.
  363.    */
  364.   public void setTop(double v) {
  365.     
  366.     m_top = v;
  367.   }
  368.   
  369.   
  370.   
  371.   
  372.   /**
  373.    * Get the value of visible.
  374.    *
  375.    * @return Value of visible.
  376.    */
  377.   public boolean getVisible() {
  378.     
  379.     return m_visible;
  380.   }
  381.   
  382.   /**
  383.    * Set the value of visible.
  384.    *
  385.    * @param v  Value to assign to visible.
  386.    */
  387.   private void setVisible(boolean v) {
  388.     
  389.     m_visible = v;
  390.   }
  391.   
  392.   
  393.   
  394.   
  395.   /**
  396.    * Get the value of root.
  397.    *
  398.    * @return True if has no parents.
  399.    */
  400.   public boolean getRoot() {
  401.     
  402.     return m_root;
  403.   }
  404.   
  405.   /**
  406.    * Set the value of root.
  407.    *
  408.    * @param v  Value to assign to root.
  409.    */
  410.   public void setRoot(boolean v) {
  411.     
  412.     m_root = v;
  413.   }
  414.   
  415.   
  416.   
  417.   /**
  418.    * Get the parent edge.
  419.    *
  420.    * @param i The parent number to get.
  421.    * @return The parent edge or NULL if it doesn't exist.
  422.    */
  423.   public Edge getParent(int i) {
  424.     
  425.     if (i < m_parent.size()) {
  426.       return (Edge)m_parent.elementAt(i);
  427.     }
  428.     else {
  429.       return null;
  430.     }
  431.   }
  432.   
  433.   /**
  434.    * Set the value of parent.
  435.    *
  436.    * @param v  Value to assign to parent.
  437.    */
  438.   public void setParent(Edge v) {
  439.     
  440.     m_parent.addElement(v);
  441.   }
  442.   
  443.   
  444.   
  445.   /**
  446.    * Get the Edge for the child number 'i'.
  447.    *
  448.    * @param i The child number to get.
  449.    * @return The child Edge or NULL if it doesn't exist.
  450.    */
  451.   public Edge getChild(int i) {
  452.     
  453.     if (i < m_children.size()) {
  454.       return (Edge)m_children.elementAt(i);
  455.     }
  456.     else {
  457.       return null;
  458.     }
  459.   }
  460.   
  461.   /**
  462.    * Set the value of children.
  463.    *
  464.    * @param v  Value to assign to children.
  465.    */
  466.   public void addChild(Edge v) {
  467.     m_children.addElement(v);
  468.   }
  469.   
  470.   /**
  471.    * Recursively finds the number of visible groups of siblings there are.
  472.    *
  473.    * @param r The current Node upto.
  474.    * @param n The current number of groups there are.
  475.    * @return The number of groups found so far.
  476.    */
  477.   public static int getGCount(Node r,int n) {
  478.     Edge e;
  479.     
  480.     if (r.getChild(0) != null && r.getCVisible()) {
  481.       n++;
  482.       for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  483. n = getGCount(e.getTarget(),n);
  484.       }
  485.     }
  486.     return n;
  487.   }
  488.   /**
  489.    * Recursively finds the total number of groups of siblings there are.
  490.    *
  491.    * @param r The current Node upto.
  492.    * @param n The current number of groups there are.
  493.    * @return The number of groups found so far.
  494.    */
  495.   public static int getTotalGCount(Node r,int n) {
  496.     Edge e;
  497.     
  498.     if (r.getChild(0) != null) {
  499.       n++;
  500.       for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  501. n = getTotalGCount(e.getTarget(),n);
  502.       }
  503.     }
  504.     return n;
  505.   }
  506.   
  507.   /**
  508.    * Recursively finds the number of visible nodes there are (this may 
  509.    * accidentally count some of the invis nodes).
  510.    *
  511.    * @param r The current Node upto.
  512.    * @param n The current number nodes there are.
  513.    * @return The number of nodes found so far.
  514.    */
  515.   public static int getCount(Node r,int n) {
  516.     Edge e;
  517.     n++;
  518.     for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
  519.       n = getCount(e.getTarget(),n);
  520.     }
  521.     return n;
  522.     
  523.   }
  524.   /**
  525.    * Recursively finds the total number of nodes there are.
  526.    *
  527.    * @param r The current Node upto.
  528.    * @param n The current number nodes there are.
  529.    * @return The number of nodes found so far.
  530.    */
  531.   public static int getTotalCount(Node r,int n) {
  532.     Edge e;
  533.     n++;
  534.     for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  535.       n = getTotalCount(e.getTarget(),n);
  536.     }
  537.     return n;
  538.   }
  539.   
  540.   
  541.   /**
  542.    * Recursively finds the number of visible levels there are.
  543.    *
  544.    * @param r The current Node upto.
  545.    * @param l The curent level.
  546.    * @return The max number of levels found so far.
  547.    */
  548.   public static int getHeight(Node r,int l) {
  549.     l++;
  550.     int lev = l,temp = 0;
  551.     Edge e;
  552.     
  553.     for (int noa = 0;(e = r.getChild(noa)) != null && r.getCVisible();noa++) {
  554.       temp = getHeight(e.getTarget(),l);
  555.       if (temp > lev) {
  556. lev = temp;
  557.       }
  558.       
  559.     }
  560.     
  561.     return lev;
  562.   }
  563.   /**
  564.    * Recursively finds the total number of levels there are.
  565.    *
  566.    * @param r The current Node upto.
  567.    * @param l The curent level.
  568.    * @return The max number of levels found so far.
  569.    */
  570.   public static int getTotalHeight(Node r,int l) {
  571.     l++;
  572.     int lev = l,temp = 0;
  573.     Edge e;
  574.     
  575.     for (int noa = 0;(e = r.getChild(noa)) != null;noa++) {
  576.       temp = getTotalHeight(e.getTarget(),l);
  577.       if (temp > lev) {
  578. lev = temp;
  579.       }
  580.     }
  581.     return lev;
  582.   }
  583. }