source: src/main/java/weka/gui/streams/InstanceCounter.java @ 25

Last change on this file since 25 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 3.0 KB
Line 
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/*
18 *    InstanceCounter.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.gui.streams;
24
25import weka.core.Instance;
26import weka.core.Instances;
27
28import java.awt.Color;
29
30import javax.swing.JLabel;
31import javax.swing.JPanel;
32
33/**
34 * A bean that counts instances streamed to it.
35 *
36 * @author Len Trigg (trigg@cs.waikato.ac.nz)
37 * @version $Revision: 1.5 $
38 */
39public class InstanceCounter
40  extends JPanel
41  implements InstanceListener {
42
43  /** for serialization */
44  private static final long serialVersionUID = -6084967152645935934L;
45 
46  private JLabel m_Count_Lab;
47  private int m_Count;
48  private boolean m_Debug;
49
50  public void input(Instance instance) throws Exception {
51   
52    if (m_Debug) {
53      System.err.println("InstanceCounter::input(" + instance +")");
54    }
55    m_Count++;
56    m_Count_Lab.setText(""+m_Count+" instances");
57    repaint();
58  }
59 
60  public void inputFormat(Instances instanceInfo) {
61   
62    if (m_Debug) {
63      System.err.println("InstanceCounter::inputFormat()");
64    }
65    Instances inputInstances = new Instances(instanceInfo,0);
66    m_Count = 0;
67    m_Count_Lab.setText(""+m_Count+" instances");
68  }
69
70  public void setDebug(boolean debug) {
71   
72    m_Debug = debug;
73  }
74 
75  public boolean getDebug() {
76   
77    return m_Debug;
78  }
79
80  public InstanceCounter() {
81   
82    m_Count = 0;
83    m_Count_Lab = new JLabel("no instances");
84    add(m_Count_Lab);
85    //    setSize(60,40);
86    setBackground(Color.lightGray);
87  }
88
89  public void instanceProduced(InstanceEvent e) {
90   
91    Object source = e.getSource();
92    if (source instanceof InstanceProducer) { 
93      try {
94        InstanceProducer a = (InstanceProducer) source;
95        switch (e.getID()) {
96        case InstanceEvent.FORMAT_AVAILABLE:
97          inputFormat(a.outputFormat());
98          break;
99        case InstanceEvent.INSTANCE_AVAILABLE:
100          input(a.outputPeek());
101          break;
102        case InstanceEvent.BATCH_FINISHED:
103          if (m_Debug)
104            System.err.println("InstanceCounter::instanceProduced() - End of instance batch");
105          break;
106        default:
107          System.err.println("InstanceCounter::instanceProduced() - unknown event type");
108          break;
109        }
110      } catch (Exception ex) {
111        System.err.println(ex.getMessage());
112      }
113    } else {
114      System.err.println("InstanceCounter::instanceProduced() - Unknown source object type");
115    }
116  }
117}
118
119
120
121
122
123
124
125
Note: See TracBrowser for help on using the repository browser.