001: /*
002: * The contents of this file are subject to the Mozilla Public License
003: * Version 1.1 (the "License"); you may not use this file except in
004: * compliance with the License. You may obtain a copy of the License at
005: * http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
009: * License for the specific language governing rights and limitations
010: * under the License.
011: *
012: * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
013: *
014: * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
015: * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
016: *
017: * Contributor(s):
018: * Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
019: *
020: * If you didn't download this code from the following link, you should check
021: * if you aren't using an obsolete version: http://www.isqlviewer.com
022: */
023: package org.isqlviewer.swing.table;
024:
025: import java.sql.ResultSet;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: import org.isqlviewer.util.LocalMessages;
032:
033: /**
034: * An simple general purpose table model for Strings from various sources.
035: * <p>
036: * This implementation of the TableModel interface is mainly for general use for Properties and other Map type objects
037: * as well as String only ResultSet objects.
038: *
039: * @author Markus A. Kobold <mkobold at sprintpcs dot com>
040: * @version 1.0
041: */
042: public class PropertyTableModel extends EnhancedTableModel {
043:
044: private static final String RESOURCE_BUNDLE = "org.isqlviewer.swing.ResourceBundle";
045: private static final LocalMessages messages = new LocalMessages(
046: RESOURCE_BUNDLE);
047: private boolean isEditable = false;
048:
049: public PropertyTableModel() {
050:
051: this (null);
052: }
053:
054: public PropertyTableModel(Map<String, Object> table) {
055:
056: setData(table);
057: setColumns(new String[] {
058: messages.format("PropertyKey.ColumnHeader"),
059: messages.format("PropertyValue.ColumnHeader") });
060: }
061:
062: @Override
063: public boolean isCellEditable(int row, int col) {
064:
065: return isEditable;
066: }
067:
068: /**
069: * Applies a Map objects as the TableModel.
070: * <p>
071: * This will create a two column table model with one column being the keys, and the other being the values for each
072: * key.
073: * <p>
074: * This will set the model type to TYPE_MAP, also any prexisting data will be cleared.
075: *
076: * @see #setData(ResultSet)
077: * @param set of data to load this table model with.
078: */
079: public synchronized void setData(Map<String, ? extends Object> table) {
080:
081: clear();
082: Iterator<? extends Map.Entry<String, ? extends Object>> i = table
083: .entrySet().iterator();
084: ArrayList<String> row = new ArrayList<String>(2);
085: while (i.hasNext()) {
086: Map.Entry<String, ? extends Object> entry = i.next();
087: if (entry != null) {
088: row.set(0, entry.getKey());
089: String text = entry.getValue() == null ? "" : entry
090: .getValue().toString();
091: row.set(1, text);
092: addRow(row);
093: row.clear();
094: }
095: }
096: fireTableStructureChanged();
097: }
098:
099: /**
100: * Toggles the ability to edit this table model.
101: * <p>
102: * Since this is mainly for using this table model from a Map of strings, in paticular the properties object.
103: *
104: * @param f toggles the ability to edit the underlying data.
105: */
106: public synchronized void allowEditing(boolean f) {
107:
108: isEditable = f;
109: }
110:
111: /**
112: * Creates a properties object from the first two columns.
113: * <p>
114: * If this TableModel doesn't have two columns an empty properties object will be returned.
115: *
116: * @return properties object from the first two columns of the model.
117: */
118: public Properties toProperties() {
119:
120: Properties props = new Properties();
121: if (getColumnCount() >= 2) {
122: synchronized (dataStore) {
123: Iterator itr = dataStore.iterator();
124: while (itr.hasNext()) {
125: try {
126: ArrayList row = (ArrayList) itr.next();
127: String key = (String) row.get(0);
128: String value = (String) row.get(1);
129: props.setProperty(key, value);
130: } catch (NullPointerException npe) {
131: continue;
132: } catch (ClassCastException cce) {
133: continue;
134: } catch (Throwable t) {
135: throw new RuntimeException(t);
136: }
137: }
138: }
139: }
140: return props;
141: }
142:
143: }
|