001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.util.Iterator;
022: import java.util.Map;
023:
024: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
025: import net.sourceforge.squirrel_sql.fw.util.StringManager;
026: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
027:
028: public class MapDataSet implements IDataSet {
029:
030: /** Internationalized strings for this class. */
031: private static final StringManager s_stringMgr = StringManagerFactory
032: .getStringManager(MapDataSet.class);
033:
034: private interface i18n {
035: // i18n[mapdataset.unsupported=<Unsupported>]
036: String UNSUPPORTED = s_stringMgr
037: .getString("hashtabledataset.unsupported");
038: // i18n[mapdataset.key=Key]
039: String NAME_COLUMN = s_stringMgr
040: .getString("hashtabledataset.key");
041: // i18n[mapdataset.value=Value]
042: String VALUE_COLUMN = s_stringMgr.getString("mapdataset.value");
043: }
044:
045: /** Number of columns in <TT>DataSet</TT>. */
046: private final static int s_columnCount = 2;
047:
048: private final static String[] s_hdgs = new String[] {
049: i18n.NAME_COLUMN, i18n.VALUE_COLUMN };
050:
051: /** The <TT>Map</TT> over which this <TT>DataSet</TT> is built. */
052: private final Map<?, ?> _src;
053:
054: private DataSetDefinition _dsDef;
055:
056: private final static int[] s_hdgLens = new int[] { 30, 100 };
057:
058: /** Iterator used when iterating through using <TT>IDataSet.next()</TT>. */
059: private Iterator<?> _rowKeys;
060:
061: /** Current row used when iterating through using <TT>IDataSet.next()</TT>. */
062: private Object[] _curRow = new Object[2];
063:
064: public MapDataSet(Map<?, ?> src) throws DataSetException {
065: super ();
066: if (src == null) {
067: throw new IllegalArgumentException("Map == null");
068: }
069:
070: _src = src;
071: _dsDef = new DataSetDefinition(createColumnDefinitions());
072: _rowKeys = _src.keySet().iterator();
073: }
074:
075: public final int getColumnCount() {
076: return s_columnCount;
077: }
078:
079: public DataSetDefinition getDataSetDefinition() {
080: return _dsDef;
081: }
082:
083: public synchronized boolean next(IMessageHandler msgHandler) {
084: _curRow[0] = null;
085: _curRow[1] = null;
086: if (_rowKeys.hasNext()) {
087: _curRow[0] = _rowKeys.next();
088: }
089: if (_curRow[0] != null) {
090: _curRow[1] = _src.get(_curRow[0]);
091: }
092: return _curRow[0] != null;
093: }
094:
095: public Object get(int columnIndex) {
096: return _curRow[columnIndex];
097: }
098:
099: private ColumnDisplayDefinition[] createColumnDefinitions() {
100: final int columnCount = getColumnCount();
101: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[columnCount];
102: for (int i = 0; i < columnCount; ++i) {
103: columnDefs[i] = new ColumnDisplayDefinition(s_hdgLens[i],
104: s_hdgs[i]);
105: }
106: return columnDefs;
107: }
108: }
|