001: /*
002: * Javu WingS - Lightweight Java Component Set
003: * Copyright (c) 2005-2007 Krzysztof A. Sadlocha
004: * e-mail: ksadlocha@programics.com
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: package com.javujavu.javux.wings.table;
022:
023: import java.util.Vector;
024:
025: /**
026: * This class uses a <code>Vector</code> of object to store the
027: * rows of a table. A row can be a <code>Vector</code> or an array of
028: * items. Override this class to get custom table model.
029: * <br><br>
030: * <b>This class is not thread safe
031: * synchronization is provided by WingTable</b>
032: */
033: public class DefaultTableModel {
034: protected Vector rows;
035:
036: public DefaultTableModel() {
037: rows = new Vector();
038: }
039:
040: public int getRowCount() {
041: return rows.size();
042: }
043:
044: public void setRowCount(int rowCount) {
045: rows.setSize(rowCount);
046: }
047:
048: public void setRow(Object row, int rowIndex) {
049: if (rowIndex < 0 || rowIndex >= rows.size())
050: return;
051: rows.setElementAt(row, rowIndex);
052: }
053:
054: public Object getRow(int rowIndex) {
055: if (rowIndex < 0 || rowIndex >= rows.size())
056: return null;
057: return rows.elementAt(rowIndex);
058: }
059:
060: public void insertRow(int rowIndex, Object row) {
061: if (rowIndex < 0 || rowIndex > rows.size())
062: return;
063: rows.insertElementAt(row, rowIndex);
064: }
065:
066: public void removeRow(int rowIndex) {
067: if (rowIndex < 0 || rowIndex >= rows.size())
068: return;
069: rows.removeElementAt(rowIndex);
070: }
071:
072: public int indexOfRow(Object row) {
073: return rows.indexOf(row);
074: }
075:
076: public boolean isDarkRow(int row) {
077: return (row % 2 == 1);
078: }
079:
080: public Object getValueAt(int rowIndex, int columnIndex) {
081: Object row = getRow(rowIndex);
082: if (row != null) {
083: if (row instanceof Vector) {
084: Vector vrow = (Vector) row;
085: if (columnIndex < vrow.size())
086: return vrow.elementAt(columnIndex);
087: } else if (row instanceof Object[]) {
088: Object[] oorow = (Object[]) row;
089: if (columnIndex < oorow.length)
090: return oorow[columnIndex];
091: }
092: }
093: return null;
094: }
095:
096: public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
097: if (rowIndex < 0)
098: return;
099: if (rowIndex >= getRowCount())
100: setRowCount(rowIndex + 1);
101: Object row = getRow(rowIndex);
102: if (row != null) {
103: if (row instanceof Vector) {
104: Vector vrow = (Vector) row;
105: if (columnIndex >= vrow.size())
106: vrow.setSize(columnIndex);
107: vrow.setElementAt(aValue, columnIndex);
108: } else if (row instanceof Object[]) {
109: Object[] oorow = (Object[]) row;
110: if (columnIndex < oorow.length)
111: oorow[columnIndex] = aValue;
112: }
113: }
114: }
115: }
|