source: trunk/gcc/libjava/javax/swing/table/DefaultTableModel.java

Last change on this file was 1389, checked in by bird, 21 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 10.5 KB
Line 
1/* DefaultTableModel.java --
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4This file is part of GNU Classpath.
5
6GNU Classpath is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Classpath is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Classpath; see the file COPYING. If not, write to the
18Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1902111-1307 USA.
20
21Linking this library statically or dynamically with other modules is
22making a combined work based on this library. Thus, the terms and
23conditions of the GNU General Public License cover the whole
24combination.
25
26As a special exception, the copyright holders of this library give you
27permission to link this library with independent modules to produce an
28executable, regardless of the license terms of these independent
29modules, and to copy and distribute the resulting executable under
30terms of your choice, provided that you also meet, for each linked
31independent module, the terms and conditions of the license of that
32module. An independent module is a module which is not derived from
33or based on this library. If you modify this library, you may extend
34this exception to your version of the library, but you are not
35obligated to do so. If you do not wish to do so, delete this
36exception statement from your version. */
37
38package javax.swing.table;
39
40// Imports
41import java.io.*;
42import java.util.*;
43import javax.swing.event.*;
44
45/**
46 * DefaultTableModel
47 * @author Andrew Selkirk
48 */
49public class DefaultTableModel extends AbstractTableModel implements Serializable {
50
51 //-------------------------------------------------------------
52 // Variables --------------------------------------------------
53 //-------------------------------------------------------------
54
55 /**
56 * dataVector
57 */
58 protected Vector dataVector;
59
60 /**
61 * columnIdentifiers
62 */
63 protected Vector columnIdentifiers;
64
65
66 //-------------------------------------------------------------
67 // Initialization ---------------------------------------------
68 //-------------------------------------------------------------
69
70 /**
71 * Constructor DefaultTableModel
72 */
73 public DefaultTableModel() {
74 this(0, 0);
75 } // DefaultTableModel()
76
77 /**
78 * Constructor DefaultTableModel
79 * @param value0 TODO
80 * @param value1 TODO
81 */
82 public DefaultTableModel(int numRows, int numColumns) {
83
84 // Variables
85 int columnIndex;
86 Vector defaultNames;
87
88 // Create Column Names
89 defaultNames = new Vector();
90 for (columnIndex = 0; columnIndex < numColumns; columnIndex++) {
91 defaultNames.addElement(super.getColumnName(columnIndex));
92 } // for
93
94 // Setup Data
95// setDataVector(defaultNames, numRows);
96
97 } // DefaultTableModel()
98
99 /**
100 * Constructor DefaultTableModel
101 * @param value0 TODO
102 * @param value1 TODO
103 */
104 public DefaultTableModel(Vector columnNames, int numRows) {
105
106 // Variables
107 Vector data;
108 Vector rowData;
109 int rowIndex;
110 int columnIndex;
111 int numColumns;
112
113 // Create Data
114 data = new Vector();
115 if (columnNames == null) {
116 numColumns = 0;
117 } else {
118 numColumns = columnNames.size();
119 } // if
120 for (rowIndex = 0; rowIndex < numRows; rowIndex++) {
121 rowData = new Vector();
122 rowData.setSize(numColumns);
123 data.addElement(rowData);
124 } // for
125
126 // Setup Data
127 setDataVector(data, columnNames);
128
129 } // DefaultTableModel()
130
131 /**
132 * Constructor DefaultTableModel
133 * @param value0 TODO
134 * @param value1 TODO
135 */
136 public DefaultTableModel(Object[] columnNames, int numRows) {
137 this(convertToVector(columnNames), numRows);
138 } // DefaultTableModel()
139
140 /**
141 * Constructor DefaultTableModel
142 * @param value0 TODO
143 * @param value1 TODO
144 */
145 public DefaultTableModel(Vector data, Vector columnNames) {
146 setDataVector(data, columnNames);
147 } // DefaultTableModel()
148
149 /**
150 * Constructor DefaultTableModel
151 * @param value0 TODO
152 * @param value1 TODO
153 */
154 public DefaultTableModel(Object[][] data, Object[] columnNames) {
155 this(convertToVector(data), convertToVector(columnNames));
156 } // DefaultTableModel()
157
158
159 //-------------------------------------------------------------
160 // Methods ----------------------------------------------------
161 //-------------------------------------------------------------
162
163 /**
164 * getDataVector
165 * @returns Vector
166 */
167 public Vector getDataVector() {
168 return dataVector;
169 } // getDataVector()
170
171 /**
172 * setDataVector
173 * @param value0 TODO
174 * @param value1 TODO
175 */
176 public void setDataVector(Vector data, Vector columnNames) {
177
178 // Variables
179 int rowIndex;
180 int columnIndex;
181 int numRows;
182 int numColumns;
183 Vector columnVector;
184
185 // Set Data
186 dataVector = data;
187 columnIdentifiers = columnNames;
188
189 // Check Data
190 numRows = data.size();
191 numColumns = columnNames.size();
192 for (rowIndex = 0; rowIndex < numRows; rowIndex++) {
193 columnVector = (Vector) dataVector.get(rowIndex);
194 columnVector.setSize(numColumns);
195 } // for
196
197 } // setDataVector()
198
199 /**
200 * setDataVector
201 * @param value0 TODO
202 * @param value1 TODO
203 */
204 public void setDataVector(Object[][] data, Object[] columnNames) {
205 setDataVector(convertToVector(data), convertToVector(columnNames));
206 } // setDataVector()
207
208 /**
209 * newDataAvailable
210 * @param value0 TODO
211 */
212 public void newDataAvailable(TableModelEvent event) {
213 fireTableChanged(event);
214 } // newDataAvailable()
215
216 /**
217 * newRowsAdded
218 * @param value0 TODO
219 */
220 public void newRowsAdded(TableModelEvent event) {
221 // TODO
222 } // newRowsAdded()
223
224 /**
225 * rowsRemoved
226 * @param value0 TODO
227 */
228 public void rowsRemoved(TableModelEvent event) {
229 fireTableChanged(event);
230 } // rowsRemoved()
231
232 /**
233 * setColumnIdentifiers
234 * @param value0 TODO
235 */
236 public void setColumnIdentifiers(Vector columnIdentifiers) {
237 this.columnIdentifiers = columnIdentifiers;
238 setColumnCount(columnIdentifiers.size());
239 } // setColumnIdentifiers()
240
241 /**
242 * setColumnIdentifiers
243 * @param value0 TODO
244 */
245 public void setColumnIdentifiers(Object[] columnIdentifiers) {
246 setColumnIdentifiers(convertToVector(columnIdentifiers));
247 } // setColumnIdentifiers()
248
249 /**
250 * setNumRows
251 * @param value0 TODO
252 */
253 public void setNumRows(int numRows) {
254 setRowCount(numRows);
255 } // setNumRows()
256
257 /**
258 * setRowCount
259 * @param value0 TODO
260 */
261 public void setRowCount(int rowCount) {
262 // TODO
263 } // setRowCount()
264
265 /**
266 * setColumnCount
267 * @param value0 TODO
268 */
269 public void setColumnCount(int columnCount) {
270 // TODO
271 } // setColumnCount()
272
273 /**
274 * addColumn
275 * @param value0 TODO
276 */
277 public void addColumn(Object columnName) {
278 addColumn(columnName, new Vector(dataVector.size()));
279 } // addColumn()
280
281 /**
282 * addColumn
283 * @param value0 TODO
284 * @param value1 TODO
285 */
286 public void addColumn(Object columnName, Vector columnData) {
287 // TODO
288 } // addColumn()
289
290 /**
291 * addColumn
292 * @param value0 TODO
293 * @param value1 TODO
294 */
295 public void addColumn(Object columnName, Object[] columnData) {
296 // TODO
297 } // addColumn()
298
299 /**
300 * addRow
301 * @param value0 TODO
302 */
303 public void addRow(Vector rowData) {
304 // TODO
305 } // addRow()
306
307 /**
308 * addRow
309 * @param value0 TODO
310 */
311 public void addRow(Object[] rowData) {
312 addRow(convertToVector(rowData));
313 } // addRow()
314
315 /**
316 * insertRow
317 * @param value0 TODO
318 * @param value1 TODO
319 */
320 public void insertRow(int row, Vector rowData) {
321 dataVector.add(row, rowData);
322 } // insertRow()
323
324 /**
325 * insertRow
326 * @param value0 TODO
327 * @param value1 TODO
328 */
329 public void insertRow(int row, Object[] rowData) {
330 insertRow(row, convertToVector(rowData));
331 } // insertRow()
332
333 /**
334 * moveRow
335 * @param value0 TODO
336 * @param value1 TODO
337 * @param value2 TODO
338 */
339 public void moveRow(int startIndex, int endIndex, int toIndex) {
340
341 // Variables
342 int index;
343 Vector vector;
344
345 // Move Rows
346 for (index = 0; index < (endIndex - startIndex); index++) {
347 vector = (Vector) dataVector.remove(startIndex);
348 dataVector.add(toIndex, vector);
349 } // for
350
351 } // moveRow()
352
353 /**
354 * removeRow
355 * @param value0 TODO
356 */
357 public void removeRow(int row) {
358 dataVector.remove(row);
359 } // removeRow()
360
361 /**
362 * getRowCount
363 * @returns int
364 */
365 public int getRowCount() {
366 return dataVector.size();
367 } // getRowCount()
368
369 /**
370 * getColumnCount
371 * @returns int
372 */
373 public int getColumnCount() {
374 return columnIdentifiers.size();
375 } // getColumnCount()
376
377 /**
378 * getColumnName
379 * @param value0 TODO
380 * @returns String
381 */
382 public String getColumnName(int column) {
383
384 // Check for Column
385 if (columnIdentifiers == null || column >= getColumnCount()) {
386 return super.getColumnName(column);
387 } // if
388
389 // Return Column name
390 return (String) columnIdentifiers.get(column);
391
392 } // getColumnName()
393
394 /**
395 * isCellEditable
396 * @param value0 TODO
397 * @param value1 TODO
398 * @returns boolean
399 */
400 public boolean isCellEditable(int row, int column) {
401 return true;
402 } // isCellEditable()
403
404 /**
405 * getValueAt
406 * @param value0 TODO
407 * @param value1 TODO
408 * @returns Object
409 */
410 public Object getValueAt(int row, int column) {
411
412 // Variables
413 Vector rowVector;
414
415 // Get Row Vector
416 rowVector = (Vector) dataVector.get(row);
417
418 // Get Data
419 return rowVector.get(column);
420
421 } // getValueAt()
422
423 /**
424 * setValueAt
425 * @param value0 TODO
426 * @param value1 TODO
427 * @param value2 TODO
428 */
429 public void setValueAt(Object value, int row, int column) {
430
431 // Variables
432 Vector rowVector;
433
434 // Get Row Vector
435 rowVector = (Vector) dataVector.get(row);
436
437 // Set Data
438 rowVector.remove(column);
439 rowVector.add(column, value);
440
441 } // setValueAt()
442
443 /**
444 * convertToVector
445 * @param value0 TODO
446 * @returns Vector
447 */
448 protected static Vector convertToVector(Object[] data) {
449
450 // Variables
451 int index;
452 Vector vector;
453
454 // Check for null
455 if (data == null) {
456 return null;
457 } // if
458
459 // Process
460 vector = new Vector();
461 for (index = 0; index < data.length; index++) {
462 vector.add(data[index]);
463 } // for: index
464
465 // Return new Vector
466 return vector;
467
468 } // convertToVector()
469
470 /**
471 * convertToVector
472 * @param value0 TODO
473 * @returns Vector
474 */
475 protected static Vector convertToVector(Object[][] data) {
476
477 // Variables
478 int index;
479 Vector vector;
480
481 // Process
482 vector = new Vector();
483 for (index = 0; index < data.length; index++) {
484 vector.add(convertToVector(data[index]));
485 } // for: index
486
487 // Return new Vector
488 return vector;
489
490 } // convertToVector()
491
492
493} // DefaultTableModel
Note: See TracBrowser for help on using the repository browser.