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
* ArffSortedTableModel.java
19
* Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
23
package weka.gui.arffviewer;
25
import weka.gui.SortedTableModel;
26
import weka.core.Instances;
27
import weka.core.Attribute;
28
import weka.core.Undoable;
29
import javax.swing.table.TableModel;
30
import javax.swing.event.TableModelEvent;
31
import javax.swing.event.TableModelListener;
34
* A sorter for the ARFF-Viewer - necessary because of the custom CellRenderer.
36
* @author FracPete (fracpete at waikato dot ac dot nz)
37
* @version $Revision: 1.4 $
40
public class ArffSortedTableModel
41
extends SortedTableModel
44
/** for serialization */
45
static final long serialVersionUID = -5733148376354254030L;
48
* initializes the sorter w/o a model, but loads the given file and creates
51
* @param filename the file to load
53
public ArffSortedTableModel(String filename) {
54
this(new ArffTableModel(filename));
58
* initializes the sorter w/o a model, but uses the given data to create
61
* @param data the data to use
63
public ArffSortedTableModel(Instances data) {
64
this(new ArffTableModel(data));
68
* initializes the sorter with the given model
70
* @param model the model to use
72
public ArffSortedTableModel(TableModel model) {
77
* returns whether the notification of changes is enabled
79
* @return true if notification of changes is enabled
81
public boolean isNotificationEnabled() {
82
return ((ArffTableModel) getModel()).isNotificationEnabled();
86
* sets whether the notification of changes is enabled
88
* @param enabled enables/disables the notification
90
public void setNotificationEnabled(boolean enabled) {
91
((ArffTableModel) getModel()).setNotificationEnabled(enabled);
95
* returns whether undo support is enabled
97
* @return true if undo support is enabled
99
public boolean isUndoEnabled() {
100
return ((ArffTableModel) getModel()).isUndoEnabled();
104
* sets whether undo support is enabled
106
* @param enabled whether to enable/disable undo support
108
public void setUndoEnabled(boolean enabled) {
109
((ArffTableModel) getModel()).setUndoEnabled(enabled);
113
* returns whether the model is read-only
115
* @return true if model is read-only
117
public boolean isReadOnly() {
118
return ((ArffTableModel) getModel()).isReadOnly();
122
* sets whether the model is read-only
124
* @param value if true the model is set to read-only
126
public void setReadOnly(boolean value) {
127
((ArffTableModel) getModel()).setReadOnly(value);
131
* returns the double value of the underlying Instances object at the
132
* given position, -1 if out of bounds
134
* @param rowIndex the row index
135
* @param columnIndex the column index
136
* @return the underlying value in the Instances object
138
public double getInstancesValueAt(int rowIndex, int columnIndex) {
139
return ((ArffTableModel) getModel()).getInstancesValueAt(mIndices[rowIndex], columnIndex);
143
* returns the value at the given position
145
* @param rowIndex the row index
146
* @param columnIndex the column index
147
* @return the value of the model at the given position
149
public Object getModelValueAt(int rowIndex, int columnIndex) {
152
result = super.getModel().getValueAt(rowIndex, columnIndex);
153
// since this is called in the super-class we have to use the original
155
if (((ArffTableModel) getModel()).isMissingAt(rowIndex, columnIndex))
162
* returns the TYPE of the attribute at the given position
164
* @param columnIndex the index of the column
165
* @return the attribute type
167
public int getType(int columnIndex) {
168
return ((ArffTableModel) getModel()).getType(mIndices[0], columnIndex);
172
* returns the TYPE of the attribute at the given position
174
* @param rowIndex the index of the row
175
* @param columnIndex the index of the column
176
* @return the attribute type
178
public int getType(int rowIndex, int columnIndex) {
179
return ((ArffTableModel) getModel()).getType(mIndices[rowIndex], columnIndex);
183
* deletes the attribute at the given col index
185
* @param columnIndex the index of the attribute to delete
187
public void deleteAttributeAt(int columnIndex) {
188
((ArffTableModel) getModel()).deleteAttributeAt(columnIndex);
192
* deletes the attributes at the given indices
194
* @param columnIndices the column indices
196
public void deleteAttributes(int[] columnIndices) {
197
((ArffTableModel) getModel()).deleteAttributes(columnIndices);
201
* renames the attribute at the given col index
203
* @param columnIndex the index of the column
204
* @param newName the new name of the attribute
206
public void renameAttributeAt(int columnIndex, String newName) {
207
((ArffTableModel) getModel()).renameAttributeAt(columnIndex, newName);
211
* sets the attribute at the given col index as the new class attribute
213
* @param columnIndex the index of the column
215
public void attributeAsClassAt(int columnIndex) {
216
((ArffTableModel) getModel()).attributeAsClassAt(columnIndex);
220
* deletes the instance at the given index
222
* @param rowIndex the index of the row
224
public void deleteInstanceAt(int rowIndex) {
225
((ArffTableModel) getModel()).deleteInstanceAt(mIndices[rowIndex]);
229
* deletes the instances at the given positions
231
* @param rowIndices the indices to delete
233
public void deleteInstances(int[] rowIndices) {
237
realIndices = new int[rowIndices.length];
238
for (i = 0; i < rowIndices.length; i++)
239
realIndices[i] = mIndices[rowIndices[i]];
241
((ArffTableModel) getModel()).deleteInstances(realIndices);
245
* sorts the instances via the given attribute
247
* @param columnIndex the index of the column
249
public void sortInstances(int columnIndex) {
250
((ArffTableModel) getModel()).sortInstances(columnIndex);
254
* returns the column of the given attribute name, -1 if not found
256
* @param name the name of the attribute
257
* @return the column index or -1 if not found
259
public int getAttributeColumn(String name) {
260
return ((ArffTableModel) getModel()).getAttributeColumn(name);
264
* checks whether the value at the given position is missing
266
* @param rowIndex the row index
267
* @param columnIndex the column index
268
* @return true if the value at the position is missing
270
public boolean isMissingAt(int rowIndex, int columnIndex) {
271
return ((ArffTableModel) getModel()).isMissingAt(mIndices[rowIndex], columnIndex);
277
* @param data the data to use
279
public void setInstances(Instances data) {
280
((ArffTableModel) getModel()).setInstances(data);
286
* @return the current data
288
public Instances getInstances() {
289
return ((ArffTableModel) getModel()).getInstances();
293
* returns the attribute at the given index, can be NULL if not an attribute
296
* @param columnIndex the index of the column
297
* @return the attribute at the position
299
public Attribute getAttributeAt(int columnIndex) {
300
return ((ArffTableModel) getModel()).getAttributeAt(columnIndex);
304
* adds a listener to the list that is notified each time a change to data
307
* @param l the listener to add
309
public void addTableModelListener(TableModelListener l) {
310
if (getModel() != null)
311
((ArffTableModel) getModel()).addTableModelListener(l);
315
* removes a listener from the list that is notified each time a change to
316
* the data model occurs
318
* @param l the listener to remove
320
public void removeTableModelListener(TableModelListener l) {
321
if (getModel() != null)
322
((ArffTableModel) getModel()).removeTableModelListener(l);
326
* notfies all listener of the change of the model
328
* @param e the event to send to the listeners
330
public void notifyListener(TableModelEvent e) {
331
((ArffTableModel) getModel()).notifyListener(e);
335
* removes the undo history
337
public void clearUndo() {
338
((ArffTableModel) getModel()).clearUndo();
342
* returns whether an undo is possible, i.e. whether there are any undo points
345
* @return returns TRUE if there is an undo possible
347
public boolean canUndo() {
348
return ((ArffTableModel) getModel()).canUndo();
352
* undoes the last action
355
((ArffTableModel) getModel()).undo();
359
* adds an undo point to the undo history
361
public void addUndoPoint() {
362
((ArffTableModel) getModel()).addUndoPoint();