Coverage Report - org.jfree.chart.plot.StandardHCClusteringInfo
 
Classes in this File Line Coverage Branch Coverage Complexity
StandardHCClusteringInfo
100%
23/23
100%
5/5
2.778
 
 1  
 /* =======================================================================
 2  
  * A visualisation library extension for JFreeChart. Please see JFreeChart
 3  
  * for further information.
 4  
  * =======================================================================
 5  
  * Copyright (C) 2006  University of Helsinki, Department of Computer Science
 6  
  *
 7  
  * This library is free software; you can redistribute it and/or
 8  
  * modify it under the terms of the GNU Lesser General Public
 9  
  * License as published by the Free Software Foundation; either
 10  
  * version 2.1 of the License, or (at your option) any later version.
 11  
  *
 12  
  * This library is distributed in the hope that it will be useful,
 13  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 15  
  * Lesser General Public License for more details.
 16  
  *
 17  
  * You should have received a copy of the GNU Lesser General Public
 18  
  * License along with this library; if not, write to the Free Software
 19  
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 20  
  * -----------------------------
 21  
  * Contact:  ohtu@cs.helsinki.fi
 22  
  * -----------------------------
 23  
  *
 24  
  */
 25  
 
 26  
 package org.jfree.chart.plot;
 27  
 
 28  
 import java.util.Iterator;
 29  
 import java.util.LinkedList;
 30  
 import java.util.List;
 31  
 
 32  
 import javax.swing.event.ChangeEvent;
 33  
 import javax.swing.event.ChangeListener;
 34  
 
 35  
 import org.jfree.data.hc.HCTreeNode;
 36  
 import org.jfree.data.hc.DataRange;
 37  
 import org.jfree.chart.plot.AbstractHCClusteringInfo;
 38  
 
 39  
 /**
 40  
  * A class that stores information about the state of a clustering tree
 41  
  * used in {@link HCPlot}. This is organized as a tree with the same topology
 42  
  * as the original clustering tree.
 43  
  *
 44  
  * @author  viski project
 45  
  */
 46  
 public class StandardHCClusteringInfo extends AbstractHCClusteringInfo {
 47  
 
 48  
     private HCTreeNodeInfo root;
 49  
 
 50  
     /**
 51  
      * Returns the visible data range. i.e. number of rows/columns
 52  
      * of a heatmap currently seen on the screen.
 53  
      *
 54  
      * @return  The datarange specifying the visible indices.
 55  
      */
 56  
     public int getNumberOfVisibleItems() {
 57  
 
 58  27
         return this.root.getVisibleDataRange().getWidth();
 59  
 
 60  
     }
 61  
 
 62  
     /**
 63  
      * Creates a new HCTreeNodeInfo reflecting data in HCTreeNode.
 64  
      * The constructor also creates objects for all the childs
 65  
      * of HCTreeNode.
 66  
      *
 67  
      * @param rootNode the root node of a clustering tree for which
 68  
      * HCTreeNodeInfo-objects are created for.
 69  
      * @param names  names of rows or columns.
 70  
      * @param location  column or leaf tree?
 71  
      */
 72  40
     public StandardHCClusteringInfo(HCTreeNode rootNode, String[] names, int location) {
 73  
 
 74  40
         if (rootNode == null) throw new NullPointerException(
 75  
                 "You must create a DummyHCClusteringInfo if you do not have " +
 76  
                 "a root node."
 77  
         );
 78  39
         if ((location != HCPlot.LEFT) && (location != HCPlot.TOP))
 79  2
             throw new IllegalArgumentException(
 80  
                     "location must be LEFT or RIGHT.");
 81  
 
 82  37
         this.location=location;
 83  37
         this.names = names;
 84  37
         this.listeners = new LinkedList();
 85  
 
 86  37
         this.root = new HCTreeNodeInfo(this, rootNode);
 87  
 
 88  37
         this.root.updateVisibleDataRange();
 89  
 
 90  37
     }
 91  
 
 92  
     /**
 93  
      * Return the HCTreeNodeInfo corresponding to the root node
 94  
      * of the clustering tree.
 95  
      *
 96  
      * @return  the root node.
 97  
      */
 98  
     public HCTreeNodeInfo getRootNode() {
 99  
 
 100  71
         return this.root;
 101  
 
 102  
     }
 103  
 
 104  
     /**
 105  
      * Return the names of visible rows at subtree rooted at a specified node.
 106  
      *
 107  
      * @param node  the root of a subtree to generate names for.
 108  
      *
 109  
      * @return  the names as a List object.
 110  
      */
 111  
     private List generateNames(HCTreeNodeInfo node) {
 112  
 
 113  74
         LinkedList list = new LinkedList();
 114  74
         if ((!node.isNodeOpen()) || ((node.getLeftChild() == null) && (node.getRightChild() == null))) {
 115  45
             list.add(new NameObject(node));
 116  45
         } else {
 117  29
             if (node.getLeftChild() != null)
 118  29
                 list.addAll(generateNames(node.getLeftChild()));
 119  29
             if (node.getRightChild() != null)
 120  29
                 list.addAll(generateNames(node.getRightChild()));
 121  
         }
 122  74
         return list;
 123  
 
 124  
     }
 125  
 
 126  
     /**
 127  
      * Return the names of visible rows/columns corresponding to
 128  
      * this clustering info object.
 129  
      *
 130  
      * @return  a list object containing the names.
 131  
      */
 132  
     public List getNames() {
 133  
 
 134  16
         return generateNames(this.root);
 135  
 
 136  
     }
 137  
 
 138  
     /**
 139  
      * Returns the indices of the dataset that
 140  
      * correspond to specified visible index
 141  
      *
 142  
      * @param row  the row.
 143  
      * @return  The datarange specifying the rows.
 144  
      */
 145  
     public DataRange getDataRangeForVisibleIndex(int row) {
 146  
 
 147  130
         return this.root.getNodeByVisibleIndex(row).getNode().getDataRange();
 148  
         
 149  
     }
 150  
 
 151  
     /**
 152  
      * A class used to generate name lists.
 153  
      */
 154  
     public class NameObject implements Comparable {
 155  
 
 156  
         private HCTreeNodeInfo node;
 157  
 
 158  
         /**
 159  
          * Creates a new name object.
 160  
          *
 161  
          * @param node  the node to create the name object for.
 162  
          */
 163  
         public NameObject (HCTreeNodeInfo node) {
 164  
 
 165  
             this.node = node;
 166  
 
 167  
         }
 168  
 
 169  
         /**
 170  
          * Return the names of visible rows/columns corresponding to
 171  
          * this clustering info object.
 172  
          *
 173  
          * @return  a list object containing the names.
 174  
          */
 175  
         public int compareTo(Object o) throws ClassCastException {
 176  
 
 177  
             if (o instanceof NameObject) {
 178  
 
 179  
                 NameObject n = (NameObject)o;
 180  
                 try{
 181  
                 if (n.node.getNode().getDataRange().getLeftBound() >
 182  
                     this.node.getNode().getDataRange().getLeftBound()
 183  
                 ) return -1;
 184  
                 else if (n.node.getNode().getDataRange().getLeftBound() >
 185  
                     this.node.getNode().getDataRange().getLeftBound()
 186  
                 ) return 1;
 187  
                 else return 0;
 188  
                 } catch (Exception e) { throw new ClassCastException(); }
 189  
 
 190  
             }
 191  
             else throw new ClassCastException();
 192  
 
 193  
         }
 194  
 
 195  
         /**
 196  
          * Return the name as a string.
 197  
          *
 198  
          * @return  the name as a string.
 199  
          */
 200  
         public String toString() {
 201  
 
 202  
             return this.node.toString();
 203  
 
 204  
         }
 205  
 
 206  
     }
 207  
 
 208  
 }