001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001 - 2002 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.Enumeration;
022: import java.util.Hashtable;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Properties;
026:
027: import net.sourceforge.squirrel_sql.fw.util.EnumerationIterator;
028: import net.sourceforge.squirrel_sql.fw.util.IMessageHandler;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031:
032: public class HashtableDataSet implements IDataSet {
033:
034: private static abstract class HashtableDataSetI18n {
035: /** Internationalized strings for this class. */
036: private static final StringManager s_stringMgr = StringManagerFactory
037: .getStringManager(HashtableDataSet.class);
038:
039: // i18n[hashtabledataset.unsupported=<Unsupported>]
040: public static final String UNSUPPORTED = s_stringMgr
041: .getString("hashtabledataset.unsupported");
042: // i18n[hashtabledataset.key=Key]
043: public static final String NAME_COLUMN = s_stringMgr
044: .getString("hashtabledataset.key");
045: // i18n[hashtabledataset.value=Value]
046: public static final String VALUE_COLUMN = s_stringMgr
047: .getString("hashtabledataset.value");
048: }
049:
050: private Hashtable<String, String> _src;
051: private DataSetDefinition _dsDef;
052: private final static String[] s_hdgs = new String[] {
053: HashtableDataSetI18n.NAME_COLUMN,
054: HashtableDataSetI18n.VALUE_COLUMN };
055: private final static int[] s_hdgLens = new int[] { 30, 100 };
056: private String[] _curRow = new String[2];
057: private Iterator<String> _rowKeys;
058:
059: public HashtableDataSet(final Hashtable<String, String> src)
060: throws DataSetException {
061: _src = new Hashtable<String, String>();
062: for (Map.Entry<String, String> entry : src.entrySet()) {
063: _src.put(entry.getKey(), entry.getValue());
064: }
065: init();
066: }
067:
068: public HashtableDataSet(final Properties props)
069: throws DataSetException {
070: _src = new Hashtable<String, String>();
071: for (Object obj : props.keySet()) {
072: String key = (String) obj;
073: String value = props.getProperty(key);
074: _src.put(key, value);
075: }
076: init();
077: }
078:
079: public final int getColumnCount() {
080: return s_hdgs.length;
081: }
082:
083: public DataSetDefinition getDataSetDefinition() {
084: return _dsDef;
085: }
086:
087: public synchronized boolean next(IMessageHandler msgHandler) {
088: _curRow[0] = null;
089: if (_rowKeys.hasNext()) {
090: _curRow[0] = _rowKeys.next();
091: }
092: if (_curRow[0] != null) {
093: _curRow[1] = _src.get(_curRow[0]);
094: }
095: return _curRow[0] != null;
096: }
097:
098: public Object get(int columnIndex) {
099: return _curRow[columnIndex];
100: }
101:
102: private void init() {
103: _dsDef = new DataSetDefinition(createColumnDefinitions());
104: Enumeration<String> keyEnumeration = _src.keys();
105: _rowKeys = new EnumerationIterator<String>(keyEnumeration);
106: }
107:
108: private ColumnDisplayDefinition[] createColumnDefinitions() {
109: final int columnCount = getColumnCount();
110: ColumnDisplayDefinition[] columnDefs = new ColumnDisplayDefinition[columnCount];
111: for (int i = 0; i < columnCount; ++i) {
112: columnDefs[i] = new ColumnDisplayDefinition(s_hdgLens[i],
113: s_hdgs[i]);
114: }
115: return columnDefs;
116: }
117: }
|