01: package net.sourceforge.squirrel_sql.fw.datasetviewer;
02:
03: /*
04: * Copyright (C) 2001-2002 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
22:
23: public class ObjectArrayDataSet implements IDataSet {
24: private Object[][] _src;
25: private DataSetDefinition _dsDef;
26:
27: private Object[] _curRow;
28: private int _curIndex = -1;
29: private int _columnCount;
30:
31: public ObjectArrayDataSet(Object[] src) throws DataSetException {
32: this (convert(src));
33: }
34:
35: public ObjectArrayDataSet(Object[][] src) throws DataSetException {
36: super ();
37: if (src == null) {
38: throw new IllegalArgumentException("Null Object[][] passed");
39: }
40: _src = src;
41: for (int i = 0; i < src.length; ++i) {
42: if (src[i] != null && src[i].length > _columnCount) {
43: _columnCount = src[i].length;
44: }
45: }
46: _dsDef = new DataSetDefinition(createColumnDefinitions());
47: }
48:
49: public final int getColumnCount() {
50: return _columnCount;
51: }
52:
53: public DataSetDefinition getDataSetDefinition() {
54: return _dsDef;
55: }
56:
57: public synchronized boolean next(IMessageHandler msgHandler) {
58: _curRow = null;
59: if (_src.length > (_curIndex + 1)) {
60: _curRow = _src[++_curIndex];
61: return true;
62: }
63: return false;
64: }
65:
66: public synchronized Object get(int columnIndex) {
67: if (_curRow != null && columnIndex < _curRow.length) {
68: return _curRow[columnIndex];
69: }
70: return null;
71: }
72:
73: private ColumnDisplayDefinition[] createColumnDefinitions() {
74: final int columnCount = getColumnCount();
75: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[columnCount];
76: for (int i = 0; i < columnCount; ++i) {
77: columnDefs[i] = new ColumnDisplayDefinition(100, " ");
78: }
79: return columnDefs;
80: }
81:
82: private static Object[][] convert(Object[] src) {
83: Object[][] dsInfo = new Object[src.length][1];
84: for (int i = 0; i < src.length; ++i) {
85: dsInfo[i][0] = src[i];
86: }
87: return dsInfo;
88: }
89: }
|