source: src/main/java/weka/gui/streams/InstanceTable.java @ 21

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

Import di weka.

File size: 3.7 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 *    InstanceTable.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.BorderLayout;
29
30import javax.swing.JPanel;
31import javax.swing.JScrollPane;
32import javax.swing.JTable;
33import javax.swing.table.AbstractTableModel;
34import javax.swing.table.TableModel;
35
36/**
37 * A bean that takes a stream of instances and displays in a table.
38 *
39 * @author Len Trigg (trigg@cs.waikato.ac.nz)
40 * @version $Revision: 1.5 $
41 */
42public class InstanceTable
43  extends JPanel
44  implements InstanceListener {
45
46  /** for serialization */
47  private static final long serialVersionUID = -2462533698100834803L;
48 
49  private JTable m_InstanceTable;
50  private boolean m_Debug;
51  private boolean m_Clear;
52  private String m_UpdateString;
53  private Instances m_Instances;
54
55 
56  public void inputFormat(Instances instanceInfo) {
57   
58    if (m_Debug) {
59      System.err.println("InstanceTable::inputFormat()\n"
60                         + instanceInfo.toString());
61    }
62    m_Instances = instanceInfo;
63  }
64
65  public void input(Instance instance) throws Exception {
66   
67    if (m_Debug) {
68      System.err.println("InstanceTable::input(" + instance +")");
69    }
70    m_Instances.add(instance);
71  }
72 
73  public void batchFinished() {
74   
75    TableModel newModel = new AbstractTableModel() {
76      private static final long serialVersionUID = 5447106383000555291L;
77     
78      public String getColumnName(int col) {
79        return m_Instances.attribute(col).name();
80      }
81      public Class getColumnClass(int col) {
82        return "".getClass();
83      }
84      public int getColumnCount() {
85        return m_Instances.numAttributes();
86      }
87      public int getRowCount() {
88        return m_Instances.numInstances();
89      }
90      public Object getValueAt(int row, int col) {
91        return new String(m_Instances.instance(row).toString(col));
92      }
93    };
94    m_InstanceTable.setModel(newModel);
95    if (m_Debug) {
96      System.err.println("InstanceTable::batchFinished()");
97    }
98  }
99
100  public InstanceTable() {
101   
102    setLayout(new BorderLayout());
103    m_InstanceTable = new JTable();
104    add("Center", new JScrollPane(m_InstanceTable));
105  }
106
107  public void setDebug(boolean debug) {
108   
109    m_Debug = debug;
110  }
111 
112  public boolean getDebug() {
113   
114    return m_Debug;
115  }
116
117  public void instanceProduced(InstanceEvent e) {
118   
119    Object source = e.getSource();
120    if (source instanceof InstanceProducer) { 
121      try {
122        InstanceProducer a = (InstanceProducer) source;
123        switch (e.getID()) {
124        case InstanceEvent.FORMAT_AVAILABLE:
125          inputFormat(a.outputFormat());
126          break;
127        case InstanceEvent.INSTANCE_AVAILABLE:
128          input(a.outputPeek());
129          break;
130        case InstanceEvent.BATCH_FINISHED:
131          batchFinished();
132          break;
133        default:
134          System.err.println("InstanceTable::instanceProduced()"
135                             + " - unknown event type");
136          break;
137        }
138      } catch (Exception ex) {
139        System.err.println(ex.getMessage());
140      }
141    } else {
142      System.err.println("InstanceTable::instanceProduced()"
143                         + " - Unknown source object type");
144    }
145  }
146}
147
148
149
150
Note: See TracBrowser for help on using the repository browser.