InstanceCounter.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
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.  *    InstanceCounter.java
  18.  *    Copyright (C) 1999 Len Trigg
  19.  *
  20.  */
  21. package weka.gui.streams;
  22. import javax.swing.JPanel;
  23. import javax.swing.JLabel;
  24. import java.awt.Color;
  25. import java.awt.event.ActionListener;
  26. import java.awt.event.ActionEvent;
  27. import java.io.Serializable;
  28. import weka.core.Instance;
  29. import weka.core.Instances;
  30. /** 
  31.  * A bean that counts instances streamed to it.
  32.  *
  33.  * @author Len Trigg (trigg@cs.waikato.ac.nz)
  34.  * @version $Revision: 1.3 $
  35.  */
  36. public class InstanceCounter extends JPanel
  37.   implements Serializable, InstanceListener {
  38.   
  39.   private JLabel m_Count_Lab;
  40.   private int m_Count;
  41.   private boolean m_Debug;
  42.   public void input(Instance instance) throws Exception {
  43.     
  44.     if (m_Debug) {
  45.       System.err.println("InstanceCounter::input(" + instance +")");
  46.     }
  47.     m_Count++;
  48.     m_Count_Lab.setText(""+m_Count+" instances");
  49.     repaint();
  50.   }
  51.   
  52.   public void inputFormat(Instances instanceInfo) {
  53.     
  54.     if (m_Debug) {
  55.       System.err.println("InstanceCounter::inputFormat()");
  56.     }
  57.     Instances inputInstances = new Instances(instanceInfo,0);
  58.     m_Count = 0;
  59.     m_Count_Lab.setText(""+m_Count+" instances");
  60.   }
  61.   public void setDebug(boolean debug) {
  62.     
  63.     m_Debug = debug;
  64.   }
  65.   
  66.   public boolean getDebug() {
  67.     
  68.     return m_Debug;
  69.   }
  70.   public InstanceCounter() {
  71.     
  72.     m_Count = 0;
  73.     m_Count_Lab = new JLabel("no instances");
  74.     add(m_Count_Lab);
  75.     //    setSize(60,40);
  76.     setBackground(Color.lightGray);
  77.   }
  78.   public void instanceProduced(InstanceEvent e) {
  79.     
  80.     Object source = e.getSource();
  81.     if (source instanceof InstanceProducer) { 
  82.       try {
  83. InstanceProducer a = (InstanceProducer) source;
  84. switch (e.getID()) {
  85. case InstanceEvent.FORMAT_AVAILABLE:
  86.   inputFormat(a.outputFormat());
  87.   break;
  88. case InstanceEvent.INSTANCE_AVAILABLE:
  89.   input(a.outputPeek());
  90.   break;
  91. case InstanceEvent.BATCH_FINISHED:
  92.   if (m_Debug)
  93.     System.err.println("InstanceCounter::instanceProduced() - End of instance batch");
  94.   break;
  95. default:
  96.   System.err.println("InstanceCounter::instanceProduced() - unknown event type");
  97.   break;
  98. }
  99.       } catch (Exception ex) {
  100. System.err.println(ex.getMessage());
  101.       }
  102.     } else {
  103.       System.err.println("InstanceCounter::instanceProduced() - Unknown source object type");
  104.     }
  105.   }
  106. }