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.
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.
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.
18
* AttributeListPanel.java
19
* Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
25
import weka.core.Instances;
27
import java.awt.BorderLayout;
28
import java.awt.Dimension;
30
import javax.swing.JPanel;
31
import javax.swing.JScrollPane;
32
import javax.swing.JTable;
33
import javax.swing.ListSelectionModel;
34
import javax.swing.table.AbstractTableModel;
35
import javax.swing.table.TableColumnModel;
38
* Creates a panel that displays the attributes contained in a set of
39
* instances, letting the user select a single attribute for inspection.
41
* @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
42
* @author Len Trigg (trigg@cs.waikato.ac.nz)
43
* @version $Revision: 1.3 $
45
public class AttributeListPanel
48
/** for serialization */
49
private static final long serialVersionUID = -2030706987910400362L;
52
* A table model that looks at the names of attributes.
54
class AttributeTableModel
55
extends AbstractTableModel {
57
/** for serialization */
58
private static final long serialVersionUID = -7345701953670327707L;
60
/** The instances who's attribute structure we are reporting */
61
protected Instances m_Instances;
64
* Creates the tablemodel with the given set of instances.
66
* @param instances the initial set of Instances
68
public AttributeTableModel(Instances instances) {
70
setInstances(instances);
74
* Sets the tablemodel to look at a new set of instances.
76
* @param instances the new set of Instances.
78
public void setInstances(Instances instances) {
80
m_Instances = instances;
84
* Gets the number of attributes.
86
* @return the number of attributes.
88
public int getRowCount() {
90
return m_Instances.numAttributes();
94
* Gets the number of columns: 2
98
public int getColumnCount() {
106
* @param row the row index
107
* @param column the column index
108
* @return the value at row, column
110
public Object getValueAt(int row, int column) {
114
return new Integer(row + 1);
116
return m_Instances.attribute(row).name();
123
* Gets the name for a column.
125
* @param column the column index.
126
* @return the name of the column.
128
public String getColumnName(int column) {
132
return new String("No.");
134
return new String("Name");
141
* Gets the class of elements in a column.
143
* @param col the column index.
144
* @return the class of elements in the column.
146
public Class getColumnClass(int col) {
147
return getValueAt(0, col).getClass();
157
public boolean isCellEditable(int row, int col) {
163
/** The table displaying attribute names */
164
protected JTable m_Table = new JTable();
166
/** The table model containing attribute names */
167
protected AttributeTableModel m_Model;
170
* Creates the attribute selection panel with no initial instances.
172
public AttributeListPanel() {
174
m_Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
175
m_Table.setColumnSelectionAllowed(false);
176
m_Table.setPreferredScrollableViewportSize(new Dimension(250, 150));
178
setLayout(new BorderLayout());
179
add(new JScrollPane(m_Table), BorderLayout.CENTER);
183
* Sets the instances who's attribute names will be displayed.
185
* @param newInstances the new set of instances
187
public void setInstances(Instances newInstances) {
189
if (m_Model == null) {
190
m_Model = new AttributeTableModel(newInstances);
191
m_Table.setModel(m_Model);
192
TableColumnModel tcm = m_Table.getColumnModel();
193
tcm.getColumn(0).setMaxWidth(60);
194
tcm.getColumn(1).setMinWidth(100);
196
m_Model.setInstances(newInstances);
198
m_Table.sizeColumnsToFit(-1);
199
m_Table.revalidate();
204
* Gets the selection model used by the table.
206
* @return a value of type 'ListSelectionModel'
208
public ListSelectionModel getSelectionModel() {
210
return m_Table.getSelectionModel();
214
* Tests the attribute list panel from the command line.
216
* @param args must contain the name of an arff file to load.
218
public static void main(String[] args) {
221
if (args.length == 0) {
222
throw new Exception("supply the name of an arff file");
224
Instances i = new Instances(new java.io.BufferedReader(
225
new java.io.FileReader(args[0])));
226
AttributeListPanel asp = new AttributeListPanel();
227
final javax.swing.JFrame jf =
228
new javax.swing.JFrame("Attribute List Panel");
229
jf.getContentPane().setLayout(new BorderLayout());
230
jf.getContentPane().add(asp, BorderLayout.CENTER);
231
jf.addWindowListener(new java.awt.event.WindowAdapter() {
232
public void windowClosing(java.awt.event.WindowEvent e) {
240
} catch (Exception ex) {
241
ex.printStackTrace();
242
System.err.println(ex.getMessage());
246
} // AttributeListPanel