001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui;
020:
021: import java.util.ArrayList;
022: import java.util.Arrays;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.swing.event.TableModelEvent;
027: import javax.swing.table.DefaultTableModel;
028:
029: import org.apache.jorphan.logging.LoggingManager;
030: import org.apache.jorphan.reflect.Functor;
031: import org.apache.log.Logger;
032:
033: /**
034: * @version $Revision: 571988 $
035: */
036: public class ObjectTableModel extends DefaultTableModel {
037: private static Logger log = LoggingManager.getLoggerForClass();
038:
039: private transient ArrayList objects = new ArrayList();
040:
041: private transient List headers = new ArrayList();
042:
043: private transient ArrayList classes = new ArrayList();
044:
045: private transient ArrayList readFunctors = new ArrayList();
046:
047: private transient ArrayList writeFunctors = new ArrayList();
048:
049: private transient Class objectClass = null; // if provided
050:
051: /**
052: * The ObjectTableModel is a TableModel whose rows are objects;
053: * columns are defined as Functors on the object.
054: *
055: * @param headers - Column names
056: * @param _objClass - Object class that will be used
057: * @param readFunctors - used to get the values
058: * @param writeFunctors - used to set the values
059: * @param editorClasses - class for each column
060: */
061: public ObjectTableModel(String[] headers, Class _objClass,
062: Functor[] readFunctors, Functor[] writeFunctors,
063: Class[] editorClasses) {
064: this (headers, readFunctors, writeFunctors, editorClasses);
065: this .objectClass = _objClass;
066: }
067:
068: /**
069: * The ObjectTableModel is a TableModel whose rows are objects;
070: * columns are defined as Functors on the object.
071: *
072: * @param headers - Column names
073: * @param readFunctors - used to get the values
074: * @param writeFunctors - used to set the values
075: * @param editorClasses - class for each column
076: */
077: public ObjectTableModel(String[] headers, Functor[] readFunctors,
078: Functor[] writeFunctors, Class[] editorClasses) {
079: this .headers.addAll(Arrays.asList(headers));
080: this .classes.addAll(Arrays.asList(editorClasses));
081: this .readFunctors = new ArrayList(Arrays.asList(readFunctors));
082: this .writeFunctors = new ArrayList(Arrays.asList(writeFunctors));
083:
084: int numHeaders = headers.length;
085:
086: int numClasses = classes.size();
087: if (numClasses != numHeaders) {
088: log.warn("Header count=" + numHeaders
089: + " but classes count=" + numClasses);
090: }
091:
092: // Functor count = 0 is handled specially
093: int numWrite = writeFunctors.length;
094: if (numWrite > 0 && numWrite != numHeaders) {
095: log.warn("Header count=" + numHeaders
096: + " but writeFunctor count=" + numWrite);
097: }
098:
099: int numRead = readFunctors.length;
100: if (numRead > 0 && numRead != numHeaders) {
101: log.warn("Header count=" + numHeaders
102: + " but readFunctor count=" + numRead);
103: }
104: }
105:
106: public Iterator iterator() {
107: return objects.iterator();
108: }
109:
110: public void clearData() {
111: int size = getRowCount();
112: objects.clear();
113: super .fireTableRowsDeleted(0, size);
114: }
115:
116: public void addRow(Object value) {
117: log.debug("Adding row value: " + value);
118: if (objectClass != null) {
119: final Class valueClass = value.getClass();
120: if (!objectClass.isAssignableFrom(valueClass)) {
121: throw new IllegalArgumentException(
122: "Trying to add class: " + valueClass.getName()
123: + "; expecting class: "
124: + objectClass.getName());
125: }
126: }
127: objects.add(value);
128: super .fireTableRowsInserted(objects.size() - 1, objects.size());
129: }
130:
131: public void insertRow(Object value, int index) {
132: objects.add(index, value);
133: super .fireTableRowsInserted(index, index + 1);
134: }
135:
136: /**
137: * @see javax.swing.table.TableModel#getColumnCount()
138: */
139: public int getColumnCount() {
140: return headers.size();
141: }
142:
143: /**
144: * @see javax.swing.table.TableModel#getColumnName(int)
145: */
146: public String getColumnName(int col) {
147: return (String) headers.get(col);
148: }
149:
150: /**
151: * @see javax.swing.table.TableModel#getRowCount()
152: */
153: public int getRowCount() {
154: if (objects == null) {
155: return 0;
156: }
157: return objects.size();
158: }
159:
160: /**
161: * @see javax.swing.table.TableModel#getValueAt(int, int)
162: */
163: public Object getValueAt(int row, int col) {
164: log.debug("Getting row value");
165: Object value = objects.get(row);
166: if (headers.size() == 1 && col >= readFunctors.size())
167: return value;
168: Functor getMethod = (Functor) readFunctors.get(col);
169: if (getMethod != null && value != null) {
170: return getMethod.invoke(value);
171: }
172: return null;
173: }
174:
175: /**
176: * @see javax.swing.table.TableModel#isCellEditable(int, int)
177: */
178: public boolean isCellEditable(int arg0, int arg1) {
179: return true;
180: }
181:
182: /**
183: * @see javax.swing.table.DefaultTableModel#moveRow(int, int, int)
184: */
185: public void moveRow(int start, int end, int to) {
186: List subList = objects.subList(start, end);
187: for (int x = end - 1; x >= start; x--) {
188: objects.remove(x);
189: }
190: objects.addAll(to, subList);
191: super .fireTableChanged(new TableModelEvent(this ));
192: }
193:
194: /**
195: * @see javax.swing.table.DefaultTableModel#removeRow(int)
196: */
197: public void removeRow(int row) {
198: objects.remove(row);
199: super .fireTableRowsDeleted(row, row);
200: }
201:
202: /**
203: * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
204: */
205: public void setValueAt(Object cellValue, int row, int col) {
206: if (row < objects.size()) {
207: Object value = objects.get(row);
208: if (col < writeFunctors.size()) {
209: Functor setMethod = (Functor) writeFunctors.get(col);
210: if (setMethod != null) {
211: setMethod.invoke(value, new Object[] { cellValue });
212: super .fireTableDataChanged();
213: }
214: } else if (headers.size() == 1) {
215: objects.set(row, cellValue);
216: }
217: }
218: }
219:
220: /**
221: * @see javax.swing.table.TableModel#getColumnClass(int)
222: */
223: public Class getColumnClass(int arg0) {
224: return (Class) classes.get(arg0);
225: }
226:
227: /**
228: * Check all registered functors.
229: * <p>
230: * <b>** only for use in unit test code **</b>
231: * </p>
232: *
233: * @param _value - an instance of the table model row data item
234: * (if null, use the class passed to the constructor).
235: *
236: * @param caller - class of caller.
237: *
238: * @return false if at least one Functor cannot be found.
239: */
240: public boolean checkFunctors(Object _value, Class caller) {
241: Object value;
242: if (_value == null && objectClass != null) {
243: try {
244: value = objectClass.newInstance();
245: } catch (InstantiationException e) {
246: log.error("Cannot create instance of class "
247: + objectClass.getName(), e);
248: return false;
249: } catch (IllegalAccessException e) {
250: log.error("Cannot create instance of class "
251: + objectClass.getName(), e);
252: return false;
253: }
254: } else {
255: value = _value;
256: }
257: boolean status = true;
258: for (int i = 0; i < getColumnCount(); i++) {
259: Functor setMethod = (Functor) writeFunctors.get(i);
260: if (setMethod != null) {
261: if (!setMethod.checkMethod(value, getColumnClass(i))) {
262: status = false;
263: log.warn(caller.getName()
264: + " is attempting to use nonexistent "
265: + setMethod.toString());
266: }
267: }
268: Functor getMethod = (Functor) readFunctors.get(i);
269: if (getMethod != null) {
270: if (!getMethod.checkMethod(value)) {
271: status = false;
272: log.warn(caller.getName()
273: + " is attempting to use nonexistent "
274: + getMethod.toString());
275: }
276: }
277:
278: }
279: return status;
280: }
281: }
|