~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/gui/AttributeListPanel.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *    AttributeListPanel.java
 
19
 *    Copyright (C) 2002 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.gui;
 
24
 
 
25
import weka.core.Instances;
 
26
 
 
27
import java.awt.BorderLayout;
 
28
import java.awt.Dimension;
 
29
 
 
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;
 
36
 
 
37
/**
 
38
 * Creates a panel that displays the attributes contained in a set of
 
39
 * instances, letting the user select a single attribute for inspection.
 
40
 *
 
41
 * @author Richard Kirkby (rkirkby@cs.waikato.ac.nz)
 
42
 * @author Len Trigg (trigg@cs.waikato.ac.nz)
 
43
 * @version $Revision: 1.3 $
 
44
 */
 
45
public class AttributeListPanel
 
46
  extends JPanel {
 
47
 
 
48
  /** for serialization */
 
49
  private static final long serialVersionUID = -2030706987910400362L;
 
50
 
 
51
  /**
 
52
   * A table model that looks at the names of attributes.
 
53
   */
 
54
  class AttributeTableModel
 
55
    extends AbstractTableModel {
 
56
 
 
57
    /** for serialization */
 
58
    private static final long serialVersionUID = -7345701953670327707L;
 
59
 
 
60
    /** The instances who's attribute structure we are reporting */
 
61
    protected Instances m_Instances;
 
62
    
 
63
    /**
 
64
     * Creates the tablemodel with the given set of instances.
 
65
     *
 
66
     * @param instances the initial set of Instances
 
67
     */
 
68
    public AttributeTableModel(Instances instances) {
 
69
 
 
70
      setInstances(instances);
 
71
    }
 
72
 
 
73
    /**
 
74
     * Sets the tablemodel to look at a new set of instances.
 
75
     *
 
76
     * @param instances the new set of Instances.
 
77
     */
 
78
    public void setInstances(Instances instances) {
 
79
 
 
80
      m_Instances = instances;
 
81
    }
 
82
    
 
83
    /**
 
84
     * Gets the number of attributes.
 
85
     *
 
86
     * @return the number of attributes.
 
87
     */
 
88
    public int getRowCount() {
 
89
      
 
90
      return m_Instances.numAttributes();
 
91
    }
 
92
    
 
93
    /**
 
94
     * Gets the number of columns: 2
 
95
     *
 
96
     * @return 2
 
97
     */
 
98
    public int getColumnCount() {
 
99
      
 
100
      return 2;
 
101
    }
 
102
    
 
103
    /**
 
104
     * Gets a table cell
 
105
     *
 
106
     * @param row the row index
 
107
     * @param column the column index
 
108
     * @return the value at row, column
 
109
     */
 
110
    public Object getValueAt(int row, int column) {
 
111
      
 
112
      switch (column) {
 
113
      case 0:
 
114
        return new Integer(row + 1);
 
115
      case 1:
 
116
        return m_Instances.attribute(row).name();
 
117
      default:
 
118
        return null;
 
119
      }
 
120
    }
 
121
    
 
122
    /**
 
123
     * Gets the name for a column.
 
124
     *
 
125
     * @param column the column index.
 
126
     * @return the name of the column.
 
127
     */
 
128
    public String getColumnName(int column) {
 
129
      
 
130
      switch (column) {
 
131
      case 0:
 
132
        return new String("No.");
 
133
      case 1:
 
134
        return new String("Name");
 
135
      default:
 
136
        return null;
 
137
      }
 
138
    }
 
139
    
 
140
    /**
 
141
     * Gets the class of elements in a column.
 
142
     *
 
143
     * @param col the column index.
 
144
     * @return the class of elements in the column.
 
145
     */
 
146
    public Class getColumnClass(int col) {
 
147
      return getValueAt(0, col).getClass();
 
148
    }
 
149
 
 
150
    /**
 
151
     * Returns false
 
152
     *
 
153
     * @param row ignored
 
154
     * @param col ignored
 
155
     * @return false
 
156
     */
 
157
    public boolean isCellEditable(int row, int col) {
 
158
 
 
159
      return false;
 
160
    }
 
161
  }
 
162
 
 
163
  /** The table displaying attribute names */
 
164
  protected JTable m_Table = new JTable();
 
165
 
 
166
  /** The table model containing attribute names */
 
167
  protected AttributeTableModel m_Model;
 
168
  
 
169
  /**
 
170
   * Creates the attribute selection panel with no initial instances.
 
171
   */
 
172
  public AttributeListPanel() {
 
173
 
 
174
    m_Table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
 
175
    m_Table.setColumnSelectionAllowed(false); 
 
176
    m_Table.setPreferredScrollableViewportSize(new Dimension(250, 150));
 
177
 
 
178
    setLayout(new BorderLayout());
 
179
    add(new JScrollPane(m_Table), BorderLayout.CENTER);
 
180
  }
 
181
 
 
182
  /**
 
183
   * Sets the instances who's attribute names will be displayed.
 
184
   *
 
185
   * @param newInstances the new set of instances
 
186
   */
 
187
  public void setInstances(Instances newInstances) {
 
188
 
 
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);
 
195
    } else {
 
196
      m_Model.setInstances(newInstances);
 
197
    }
 
198
    m_Table.sizeColumnsToFit(-1);
 
199
    m_Table.revalidate();
 
200
    m_Table.repaint();
 
201
  }
 
202
  
 
203
  /**
 
204
   * Gets the selection model used by the table.
 
205
   *
 
206
   * @return a value of type 'ListSelectionModel'
 
207
   */
 
208
  public ListSelectionModel getSelectionModel() {
 
209
 
 
210
    return m_Table.getSelectionModel();
 
211
  }
 
212
  
 
213
  /**
 
214
   * Tests the attribute list panel from the command line.
 
215
   *
 
216
   * @param args must contain the name of an arff file to load.
 
217
   */
 
218
  public static void main(String[] args) {
 
219
 
 
220
    try {
 
221
      if (args.length == 0) {
 
222
        throw new Exception("supply the name of an arff file");
 
223
      }
 
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) {
 
233
          jf.dispose();
 
234
          System.exit(0);
 
235
        }
 
236
      });
 
237
      jf.pack();
 
238
      jf.setVisible(true);
 
239
      asp.setInstances(i);
 
240
    } catch (Exception ex) {
 
241
      ex.printStackTrace();
 
242
      System.err.println(ex.getMessage());
 
243
    }
 
244
  }
 
245
  
 
246
} // AttributeListPanel