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

« back to all changes in this revision

Viewing changes to weka/gui/arffviewer/ArffSortedTableModel.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
 * ArffSortedTableModel.java
 
19
 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
package weka.gui.arffviewer;
 
24
 
 
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;
 
32
 
 
33
/**
 
34
 * A sorter for the ARFF-Viewer - necessary because of the custom CellRenderer.
 
35
 *
 
36
 * @author FracPete (fracpete at waikato dot ac dot nz)
 
37
 * @version $Revision: 1.4 $ 
 
38
 */
 
39
 
 
40
public class ArffSortedTableModel 
 
41
  extends SortedTableModel 
 
42
  implements Undoable {
 
43
  
 
44
  /** for serialization */
 
45
  static final long serialVersionUID = -5733148376354254030L;
 
46
  
 
47
  /**
 
48
   * initializes the sorter w/o a model, but loads the given file and creates
 
49
   * from that a model
 
50
   * 
 
51
   * @param filename    the file to load
 
52
   */
 
53
  public ArffSortedTableModel(String filename) {
 
54
    this(new ArffTableModel(filename));
 
55
  }
 
56
  
 
57
  /**
 
58
   * initializes the sorter w/o a model, but uses the given data to create
 
59
   * a model from that
 
60
   * 
 
61
   * @param data        the data to use
 
62
   */
 
63
  public ArffSortedTableModel(Instances data) {
 
64
    this(new ArffTableModel(data));
 
65
  }
 
66
  
 
67
  /**
 
68
   * initializes the sorter with the given model
 
69
   * 
 
70
   * @param model       the model to use
 
71
   */
 
72
  public ArffSortedTableModel(TableModel model) {
 
73
    super(model);
 
74
  }
 
75
  
 
76
  /**
 
77
   * returns whether the notification of changes is enabled
 
78
   * 
 
79
   * @return            true if notification of changes is enabled
 
80
   */
 
81
  public boolean isNotificationEnabled() {
 
82
    return ((ArffTableModel) getModel()).isNotificationEnabled();
 
83
  }
 
84
  
 
85
  /**
 
86
   * sets whether the notification of changes is enabled
 
87
   * 
 
88
   * @param enabled     enables/disables the notification
 
89
   */
 
90
  public void setNotificationEnabled(boolean enabled) {
 
91
    ((ArffTableModel) getModel()).setNotificationEnabled(enabled);
 
92
  }
 
93
  
 
94
  /**
 
95
   * returns whether undo support is enabled
 
96
   * 
 
97
   * @return            true if undo support is enabled
 
98
   */
 
99
  public boolean isUndoEnabled() {
 
100
    return ((ArffTableModel) getModel()).isUndoEnabled();
 
101
  }
 
102
  
 
103
  /**
 
104
   * sets whether undo support is enabled
 
105
   * 
 
106
   * @param enabled     whether to enable/disable undo support
 
107
   */
 
108
  public void setUndoEnabled(boolean enabled) {
 
109
    ((ArffTableModel) getModel()).setUndoEnabled(enabled);
 
110
  }
 
111
 
 
112
  /**
 
113
   * returns whether the model is read-only
 
114
   * 
 
115
   * @return            true if model is read-only
 
116
   */
 
117
  public boolean isReadOnly() {
 
118
    return ((ArffTableModel) getModel()).isReadOnly();
 
119
  }
 
120
  
 
121
  /**
 
122
   * sets whether the model is read-only
 
123
   * 
 
124
   * @param value       if true the model is set to read-only
 
125
   */
 
126
  public void setReadOnly(boolean value) {
 
127
    ((ArffTableModel) getModel()).setReadOnly(value);
 
128
  }
 
129
  
 
130
  /**
 
131
   * returns the double value of the underlying Instances object at the
 
132
   * given position, -1 if out of bounds
 
133
   * 
 
134
   * @param rowIndex            the row index
 
135
   * @param columnIndex         the column index
 
136
   * @return                    the underlying value in the Instances object
 
137
   */
 
138
  public double getInstancesValueAt(int rowIndex, int columnIndex) {
 
139
    return ((ArffTableModel) getModel()).getInstancesValueAt(mIndices[rowIndex], columnIndex);
 
140
  }
 
141
  
 
142
  /**
 
143
   * returns the value at the given position
 
144
   * 
 
145
   * @param rowIndex            the row index
 
146
   * @param columnIndex         the column index
 
147
   * @return                    the value of the model at the given  position
 
148
   */
 
149
  public Object getModelValueAt(int rowIndex, int columnIndex) {
 
150
    Object            result;
 
151
    
 
152
    result = super.getModel().getValueAt(rowIndex, columnIndex);
 
153
    // since this is called in the super-class we have to use the original
 
154
    // index!
 
155
    if (((ArffTableModel) getModel()).isMissingAt(rowIndex, columnIndex))
 
156
      result = null;
 
157
    
 
158
    return result;
 
159
  }
 
160
  
 
161
  /**
 
162
   * returns the TYPE of the attribute at the given position
 
163
   * 
 
164
   * @param columnIndex         the index of the column
 
165
   * @return                    the attribute type
 
166
   */
 
167
  public int getType(int columnIndex) {
 
168
    return ((ArffTableModel) getModel()).getType(mIndices[0], columnIndex);
 
169
  }
 
170
  
 
171
  /**
 
172
   * returns the TYPE of the attribute at the given position
 
173
   * 
 
174
   * @param rowIndex            the index of the row
 
175
   * @param columnIndex         the index of the column
 
176
   * @return                    the attribute type
 
177
   */
 
178
  public int getType(int rowIndex, int columnIndex) {
 
179
    return ((ArffTableModel) getModel()).getType(mIndices[rowIndex], columnIndex);
 
180
  }
 
181
  
 
182
  /**
 
183
   * deletes the attribute at the given col index
 
184
   * 
 
185
   * @param columnIndex     the index of the attribute to delete
 
186
   */
 
187
  public void deleteAttributeAt(int columnIndex) {
 
188
    ((ArffTableModel) getModel()).deleteAttributeAt(columnIndex);
 
189
  }
 
190
  
 
191
  /**
 
192
   * deletes the attributes at the given indices
 
193
   * 
 
194
   * @param columnIndices       the column indices
 
195
   */
 
196
  public void deleteAttributes(int[] columnIndices) {
 
197
    ((ArffTableModel) getModel()).deleteAttributes(columnIndices);
 
198
  }
 
199
  
 
200
  /**
 
201
   * renames the attribute at the given col index
 
202
   * 
 
203
   * @param columnIndex         the index of the column
 
204
   * @param newName             the new name of the attribute
 
205
   */
 
206
  public void renameAttributeAt(int columnIndex, String newName) {
 
207
    ((ArffTableModel) getModel()).renameAttributeAt(columnIndex, newName);
 
208
  }
 
209
  
 
210
  /**
 
211
   * sets the attribute at the given col index as the new class attribute
 
212
   * 
 
213
   * @param columnIndex         the index of the column
 
214
   */
 
215
  public void attributeAsClassAt(int columnIndex) {
 
216
    ((ArffTableModel) getModel()).attributeAsClassAt(columnIndex);
 
217
  }
 
218
  
 
219
  /**
 
220
   * deletes the instance at the given index
 
221
   * 
 
222
   * @param rowIndex            the index of the row
 
223
   */
 
224
  public void deleteInstanceAt(int rowIndex) {
 
225
    ((ArffTableModel) getModel()).deleteInstanceAt(mIndices[rowIndex]);
 
226
  }
 
227
  
 
228
  /**
 
229
   * deletes the instances at the given positions
 
230
   * 
 
231
   * @param rowIndices          the indices to delete
 
232
   */
 
233
  public void deleteInstances(int[] rowIndices) {
 
234
    int[]               realIndices;
 
235
    int                 i;
 
236
    
 
237
    realIndices = new int[rowIndices.length];
 
238
    for (i = 0; i < rowIndices.length; i++)
 
239
      realIndices[i] = mIndices[rowIndices[i]];
 
240
   
 
241
    ((ArffTableModel) getModel()).deleteInstances(realIndices);
 
242
  }
 
243
  
 
244
  /**
 
245
   * sorts the instances via the given attribute
 
246
   * 
 
247
   * @param columnIndex         the index of the column
 
248
   */
 
249
  public void sortInstances(int columnIndex) {
 
250
    ((ArffTableModel) getModel()).sortInstances(columnIndex);
 
251
  }
 
252
  
 
253
  /**
 
254
   * returns the column of the given attribute name, -1 if not found
 
255
   * 
 
256
   * @param name                the name of the attribute
 
257
   * @return                    the column index or -1 if not found
 
258
   */
 
259
  public int getAttributeColumn(String name) {
 
260
    return ((ArffTableModel) getModel()).getAttributeColumn(name);
 
261
  }
 
262
  
 
263
  /**
 
264
   * checks whether the value at the given position is missing
 
265
   * 
 
266
   * @param rowIndex            the row index
 
267
   * @param columnIndex         the column index
 
268
   * @return                    true if the value at the position is missing
 
269
   */
 
270
  public boolean isMissingAt(int rowIndex, int columnIndex) {
 
271
    return ((ArffTableModel) getModel()).isMissingAt(mIndices[rowIndex], columnIndex);
 
272
  }
 
273
  
 
274
  /**
 
275
   * sets the data
 
276
   * 
 
277
   * @param data        the data to use
 
278
   */
 
279
  public void setInstances(Instances data) {
 
280
    ((ArffTableModel) getModel()).setInstances(data);
 
281
  }
 
282
  
 
283
  /**
 
284
   * returns the data
 
285
   * 
 
286
   * @return            the current data
 
287
   */
 
288
  public Instances getInstances() {
 
289
    return ((ArffTableModel) getModel()).getInstances();
 
290
  }
 
291
  
 
292
  /**
 
293
   * returns the attribute at the given index, can be NULL if not an attribute
 
294
   * column
 
295
   * 
 
296
   * @param columnIndex         the index of the column
 
297
   * @return                    the attribute at the position
 
298
   */
 
299
  public Attribute getAttributeAt(int columnIndex) {
 
300
    return ((ArffTableModel) getModel()).getAttributeAt(columnIndex);
 
301
  }
 
302
  
 
303
  /**
 
304
   * adds a listener to the list that is notified each time a change to data 
 
305
   * model occurs
 
306
   * 
 
307
   * @param l           the listener to add
 
308
   */
 
309
  public void addTableModelListener(TableModelListener l) {
 
310
    if (getModel() != null)
 
311
      ((ArffTableModel) getModel()).addTableModelListener(l);
 
312
  }
 
313
  
 
314
  /**
 
315
   * removes a listener from the list that is notified each time a change to
 
316
   * the data model occurs
 
317
   * 
 
318
   * @param l           the listener to remove
 
319
   */
 
320
  public void removeTableModelListener(TableModelListener l) {
 
321
    if (getModel() != null)
 
322
      ((ArffTableModel) getModel()).removeTableModelListener(l);
 
323
  }
 
324
  
 
325
  /**
 
326
   * notfies all listener of the change of the model
 
327
   * 
 
328
   * @param e           the event to send to the listeners
 
329
   */
 
330
  public void notifyListener(TableModelEvent e) {
 
331
    ((ArffTableModel) getModel()).notifyListener(e);
 
332
  }
 
333
 
 
334
  /**
 
335
   * removes the undo history
 
336
   */
 
337
  public void clearUndo() {
 
338
    ((ArffTableModel) getModel()).clearUndo();
 
339
  }
 
340
  
 
341
  /**
 
342
   * returns whether an undo is possible, i.e. whether there are any undo points
 
343
   * saved so far
 
344
   * 
 
345
   * @return returns TRUE if there is an undo possible 
 
346
   */
 
347
  public boolean canUndo() {
 
348
    return ((ArffTableModel) getModel()).canUndo();
 
349
  }
 
350
  
 
351
  /**
 
352
   * undoes the last action
 
353
   */
 
354
  public void undo() {
 
355
    ((ArffTableModel) getModel()).undo();
 
356
  }
 
357
  
 
358
  /**
 
359
   * adds an undo point to the undo history 
 
360
   */
 
361
  public void addUndoPoint() {
 
362
    ((ArffTableModel) getModel()).addUndoPoint();
 
363
  }
 
364
}