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 | * InstanceViewer.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.gui.streams; |
---|
24 | |
---|
25 | import weka.core.Instance; |
---|
26 | import weka.core.Instances; |
---|
27 | |
---|
28 | import java.awt.BorderLayout; |
---|
29 | |
---|
30 | import javax.swing.JPanel; |
---|
31 | import javax.swing.JScrollPane; |
---|
32 | import javax.swing.JTextArea; |
---|
33 | |
---|
34 | /** |
---|
35 | * This is a very simple instance viewer - just displays the dataset as |
---|
36 | * text output as it would be written to a file. A more complex viewer |
---|
37 | * might be more spreadsheet-like |
---|
38 | * |
---|
39 | * @author Len Trigg (trigg@cs.waikato.ac.nz) |
---|
40 | * @version $Revision: 1.5 $ |
---|
41 | */ |
---|
42 | public class InstanceViewer |
---|
43 | extends JPanel |
---|
44 | implements InstanceListener { |
---|
45 | |
---|
46 | /** for serialization */ |
---|
47 | private static final long serialVersionUID = -4925729441294121772L; |
---|
48 | |
---|
49 | private JTextArea m_OutputTex; |
---|
50 | private boolean m_Debug; |
---|
51 | private boolean m_Clear; |
---|
52 | private String m_UpdateString; |
---|
53 | |
---|
54 | private void updateOutput() { |
---|
55 | |
---|
56 | m_OutputTex.append(m_UpdateString); |
---|
57 | m_UpdateString = ""; |
---|
58 | } |
---|
59 | |
---|
60 | private void clearOutput() { |
---|
61 | |
---|
62 | m_UpdateString = ""; |
---|
63 | m_OutputTex.setText(""); |
---|
64 | } |
---|
65 | |
---|
66 | public void inputFormat(Instances instanceInfo) { |
---|
67 | |
---|
68 | if (m_Debug) { |
---|
69 | System.err.println("InstanceViewer::inputFormat()\n" |
---|
70 | + instanceInfo.toString()); |
---|
71 | } |
---|
72 | if (m_Clear) { |
---|
73 | clearOutput(); |
---|
74 | } |
---|
75 | m_UpdateString += instanceInfo.toString(); |
---|
76 | updateOutput(); |
---|
77 | } |
---|
78 | |
---|
79 | public void input(Instance instance) throws Exception { |
---|
80 | |
---|
81 | if (m_Debug) { |
---|
82 | System.err.println("InstanceViewer::input(" + instance +")"); |
---|
83 | } |
---|
84 | m_UpdateString += instance.toString() + "\n"; |
---|
85 | updateOutput(); |
---|
86 | } |
---|
87 | |
---|
88 | public void batchFinished() { |
---|
89 | |
---|
90 | updateOutput(); |
---|
91 | if (m_Debug) { |
---|
92 | System.err.println("InstanceViewer::batchFinished()"); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | public InstanceViewer() { |
---|
97 | |
---|
98 | setLayout(new BorderLayout()); |
---|
99 | m_UpdateString = ""; |
---|
100 | setClearEachDataset(true); |
---|
101 | m_OutputTex = new JTextArea(10,20); |
---|
102 | m_OutputTex.setEditable(false); |
---|
103 | add("Center", new JScrollPane(m_OutputTex)); |
---|
104 | } |
---|
105 | |
---|
106 | public void setClearEachDataset(boolean clear) { |
---|
107 | |
---|
108 | m_Clear = clear; |
---|
109 | } |
---|
110 | |
---|
111 | public boolean getClearEachDataset() { |
---|
112 | |
---|
113 | return m_Clear; |
---|
114 | } |
---|
115 | |
---|
116 | public void setDebug(boolean debug) { |
---|
117 | |
---|
118 | m_Debug = debug; |
---|
119 | } |
---|
120 | |
---|
121 | public boolean getDebug() { |
---|
122 | |
---|
123 | return m_Debug; |
---|
124 | } |
---|
125 | |
---|
126 | public void instanceProduced(InstanceEvent e) { |
---|
127 | |
---|
128 | Object source = e.getSource(); |
---|
129 | if (source instanceof InstanceProducer) { |
---|
130 | try { |
---|
131 | InstanceProducer a = (InstanceProducer) source; |
---|
132 | switch (e.getID()) { |
---|
133 | case InstanceEvent.FORMAT_AVAILABLE: |
---|
134 | inputFormat(a.outputFormat()); |
---|
135 | break; |
---|
136 | case InstanceEvent.INSTANCE_AVAILABLE: |
---|
137 | input(a.outputPeek()); |
---|
138 | break; |
---|
139 | case InstanceEvent.BATCH_FINISHED: |
---|
140 | batchFinished(); |
---|
141 | break; |
---|
142 | default: |
---|
143 | System.err.println("InstanceViewer::instanceProduced()" |
---|
144 | + " - unknown event type"); |
---|
145 | break; |
---|
146 | } |
---|
147 | } catch (Exception ex) { |
---|
148 | System.err.println(ex.getMessage()); |
---|
149 | } |
---|
150 | } else { |
---|
151 | System.err.println("InstanceViewer::instanceProduced()" |
---|
152 | + " - Unknown source object type"); |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|