001: /*
002: * Copyright (C) 2005 Jeff Tassin
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package com.jeta.swingbuilder.gui.components;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023:
024: import javax.swing.table.AbstractTableModel;
025:
026: import com.jeta.open.support.EmptyCollection;
027:
028: /**
029: * This class is the table model for the common tables.
030: *
031: * @author Jeff Tassin
032: */
033: public class JETATableModel extends AbstractTableModel {
034: /** an array of Trigger object */
035: private ArrayList m_data;
036:
037: /** an array of column names for the table */
038: private String[] m_colnames;
039:
040: /** an array of column types for the table */
041: private Class[] m_coltypes;
042:
043: /**
044: * ctor.
045: *
046: * @param connection
047: * the underlying database connection
048: * @param tableId
049: * the id of the table we are displaying the indices for
050: */
051: public JETATableModel() {
052:
053: }
054:
055: /**
056: * Adds the given trigger object to the table
057: */
058: public void addRow(Object obj) {
059: if (m_data == null)
060: m_data = new ArrayList();
061:
062: m_data.add(obj);
063: fireTableRowsInserted(m_data.size() - 1, m_data.size() - 1);
064: }
065:
066: /**
067: * @return true if the model contains the given object
068: */
069: public boolean contains(Object obj) {
070: if (m_data == null)
071: return false;
072: else
073: return m_data.contains(obj);
074: }
075:
076: /**
077: * @return the number of columns in this model
078: */
079: public int getColumnCount() {
080: return m_colnames.length;
081: }
082:
083: /**
084: * @return the collection of objects in this model
085: */
086: public Collection getData() {
087: if (m_data == null)
088: return EmptyCollection.getInstance();
089: else
090: return m_data;
091: }
092:
093: /**
094: * @return the number of rows objects in this model
095: */
096: public int getRowCount() {
097: if (m_data == null)
098: return 0;
099: else
100: return m_data.size();
101: }
102:
103: /**
104: * @return the name of a column at a given index
105: */
106: public String getColumnName(int column) {
107: return m_colnames[column];
108: }
109:
110: /**
111: * @return the type of column at a given index
112: */
113: public Class getColumnClass(int column) {
114: return m_coltypes[column];
115: }
116:
117: /**
118: * @return the object at the given row in the model
119: */
120: public Object getRow(int row) {
121: if (m_data == null)
122: return null;
123: else {
124: if (row >= 0 && row < m_data.size())
125: return m_data.get(row);
126: else
127: return null;
128: }
129: }
130:
131: /**
132: * @return the Object at the given row column. Note: you generally override
133: * this method
134: */
135: public Object getValueAt(int row, int col) {
136: Object obj = getRow(row);
137: if (obj instanceof Object[]) {
138: Object[] array = (Object[]) obj;
139: return array[col];
140: } else {
141: return obj;
142: }
143: }
144:
145: /**
146: * @return the index of the given object if it is contained in this model.
147: * -1 is returned if this model does not contain the object.
148: */
149: public int indexOf(Object obj) {
150: return m_data.indexOf(obj);
151: }
152:
153: /**
154: * Inserts the object at the given row in the model.
155: */
156: public void insertRow(Object obj, int row) {
157: if (row < 0)
158: row = 0;
159:
160: if (row >= m_data.size())
161: m_data.add(obj);
162: else
163: m_data.add(row, obj);
164: }
165:
166: private static int gcd(int i, int j) {
167: return (j == 0) ? i : gcd(j, i % j);
168: }
169:
170: public void moveRow(int start, int end, int to)
171: throws IndexOutOfBoundsException {
172: int shift = to - start;
173: int first, last;
174: if (shift < 0) {
175: first = to;
176: last = end;
177: } else {
178: first = start;
179: last = to + end - start;
180: }
181:
182: int a = first;
183: int b = last + 1;
184:
185: int size = b - a;
186: int r = size - shift;
187: int g = gcd(size, r);
188: for (int i = 0; i < g; i++) {
189: to = i;
190: Object tmp = m_data.get(a + to);
191: for (int from = (to + r) % size; from != i; from = (to + r)
192: % size) {
193: m_data.set(a + to, m_data.get(a + from));
194: to = from;
195: }
196: m_data.set(a + to, tmp);
197: }
198: fireTableRowsUpdated(first, last);
199: }
200:
201: /**
202: * Remove all data items from this model
203: */
204: public void removeAll() {
205: if (m_data != null) {
206: m_data.clear();
207: fireTableDataChanged();
208: }
209: }
210:
211: /**
212: * Removes the object at the given row
213: */
214: public Object removeRow(int row) {
215: Object obj = getRow(row);
216: m_data.remove(obj);
217: return obj;
218: }
219:
220: /**
221: * Removes the object at the given row
222: */
223: public boolean remove(Object obj) {
224: int index = m_data.indexOf(obj);
225: if (index >= 0) {
226: m_data.remove(index);
227: fireTableRowsDeleted(index, index);
228: return true;
229: } else {
230: return false;
231: }
232: }
233:
234: public Object set(int index, Object element)
235: throws IndexOutOfBoundsException {
236: Object result = m_data.set(index, element);
237: fireTableRowsUpdated(index, index);
238: return result;
239: }
240:
241: /**
242: * Sets the names for the columns in this model
243: */
244: public void setColumnNames(String[] names) {
245: m_colnames = names;
246: }
247:
248: /**
249: * Sets the types for the columns in this model
250: */
251: public void setColumnTypes(Class[] types) {
252: m_coltypes = types;
253: }
254:
255: }
|