Edge.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    Edge.java
  18.  *    Copyright (C) 1999 Malcolm Ware
  19.  *
  20.  */
  21. package weka.gui.treevisualizer;
  22. import java.util.*;
  23. import java.awt.*;
  24. /**
  25.  * This class is used in conjunction with the Node class to form a tree 
  26.  * structure.
  27.  * This in particular contains information about an edges in the tree.
  28.  *
  29.  * @author Malcolm Ware (mfw4@cs.waikato.ac.nz)
  30.  * @version $Revision: 1.3 $ 
  31.  */
  32. public class Edge {
  33.   /** The text caption for the edge. */
  34.   private String m_label;
  35.   /** The ID string of the parent Node of this edge (used for consrtuction 
  36.    * purposes). */
  37.   private String m_rsource;
  38.   /** The ID string of the child Node of this edge (used for construction 
  39.    * purposes). */
  40.   private String m_rtarget;
  41.   /** The parent Node of this edge. */
  42.   private Node m_source;
  43.   /** The child Node of this edge. */
  44.   private Node m_target;
  45.   /** The label broken up into lines. */
  46.   private Vector m_lines;
  47.   /** 
  48.    * This constructs an Edge with the specified label 
  49.    * and parent , child serial tags.
  50.    *
  51.    * @param label The text caption for the edge.
  52.    * @param source The ID string for this edges parent.
  53.    * @param target The ID string for this edges child.
  54.    */
  55.   public Edge(String label,String source,String target) {
  56.     m_label = label;
  57.     m_rsource = source;
  58.     m_rtarget = target;
  59.     m_lines = new Vector(3,2);
  60.     breakupLabel();
  61.   }
  62.   
  63.   /**
  64.    * Get the value of label.
  65.    *
  66.    * @return Value of label.
  67.    */
  68.   public String getLabel() {
  69.     
  70.     return m_label;
  71.   }
  72.   
  73.   /**
  74.    * This function is called to break the label of the edge up in to 
  75.    * seperate lines
  76.    */
  77.   private void breakupLabel() {
  78.     int prev = 0,noa;
  79.     for (noa = 0;noa < m_label.length();noa++) {
  80.       if (m_label.charAt(noa) == 'n') {
  81. m_lines.addElement(m_label.substring(prev,noa));
  82. prev = noa+1;
  83.       }
  84.     }
  85.     m_lines.addElement(m_label.substring(prev,noa));
  86.   }
  87.   
  88.   /**
  89.    * This will calculate how large a rectangle using the <i>FontMetrics</i>
  90.    * passed that the lines of the label will take up
  91.    *
  92.    * @param f The size information for a particular Font
  93.    * @return A Dimension containing the size and width of the text
  94.    */
  95.   public Dimension stringSize(FontMetrics f) {
  96.     Dimension d = new Dimension();
  97.     int old = 0;
  98.     String s;
  99.     int noa = 0;
  100.     while ((s = getLine(noa)) != null) {
  101.       noa++;
  102.       old = f.stringWidth(s);
  103.       
  104.       if (old > d.width) {
  105. d.width = old;
  106.       }
  107.     }
  108.     d.height = noa * f.getHeight();
  109.     return d;
  110.   }
  111.  
  112.   /**
  113.    * Returns line number <i>n</i>
  114.    *
  115.    * @param n The number of the line requested
  116.    * @return The string for the line number or NULL if it didn't exist
  117.    */ 
  118.   public String getLine(int n) {
  119.     if (n < m_lines.size()) {
  120.       return (String)m_lines.elementAt(n);
  121.     }
  122.     else {
  123.       return null;
  124.     }
  125.   }
  126.   
  127.   
  128.   /**
  129.    * Get the value of rsource.
  130.    *
  131.    * @return Value of rsource.
  132.    */
  133.   public String getRsource() {
  134.     
  135.     return m_rsource;
  136.   }
  137.   
  138.   /**
  139.    * Set the value of rsource.
  140.    *
  141.    * @param v  Value to assign to rsource.
  142.    */
  143.   public void setRsource(String v) {
  144.     
  145.     m_rsource = v;
  146.   }
  147.   
  148.   
  149.   
  150.   /**
  151.    * Get the value of rtarget.
  152.    *
  153.    * @return Value of rtarget.
  154.    */
  155.   public String getRtarget() {
  156.     
  157.     return m_rtarget;
  158.   }
  159.   
  160.   /**
  161.    * Set the value of rtarget.
  162.    *
  163.    * @param v Value to assign to rtarget.
  164.    */
  165.   public void setRtarget(String v) {
  166.     
  167.     m_rtarget = v;
  168.   }
  169.   
  170.   /**
  171.    * Get the value of source.
  172.    *
  173.    * @return Value of source.
  174.    */
  175.   public Node getSource() {
  176.     
  177.     return m_source;
  178.   }
  179.   
  180.   /**
  181.    * Set the value of source. And then call v.addChild to add the edge to 
  182.    * the Node.
  183.    *
  184.    * @param v  Value to assign to source.
  185.    */
  186.   public void setSource(Node v) {
  187.     
  188.     m_source = v;
  189.     v.addChild(this);
  190.   }
  191.   
  192.   /**
  193.    * Get the value of target.
  194.    *
  195.    * @return Value of target.
  196.    */
  197.   public Node getTarget() {
  198.     
  199.     return m_target;
  200.   }
  201.   
  202.   /**
  203.    * Set the value of target. And then call v.addParent to add the edge to 
  204.    * the Node.
  205.    *
  206.    * @param v Value to assign to target.
  207.    */
  208.   public void setTarget(Node v) {
  209.     
  210.     m_target = v;
  211.     v.setParent(this);
  212.   }
  213. }