001: package net.sourceforge.squirrel_sql.fw.datasetviewer.cellcomponent;
002:
003: /*
004: * Copyright (C) 2001-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: /**
022: * @author gwg
023: * Object to save, manage and restore DataType-specific properties.
024: * We expect most of these to be settable
025: * by the user, but this can be used to save anything that is class-specific
026: * in the DataType objects.
027: * <P>
028: * IMPORTANT: It is the responsibility of the DataType classes to put the
029: * property/value pair into this class. This class does not proactively
030: * go out to the DataTypes looking for properties to be saved.
031: * <P>
032: * This object is created from a file during application startup by the code in
033: * net.sourceforge.squirrel_sql.client.Application
034: * and saved on application shutdown.
035: * The application deals with one view of the data, while internally the
036: * Data Type objects work with an entirely different view of the same data.
037: * The data is converted from one form to the other during loading and
038: * getting of the data when requesed by the Application.
039: * <P>
040: * The application sees the data as an array of strings.
041: * <P>
042: * The Data Types see the data as individual String properties.
043: * The DataTypes may convert the String to some other form such as
044: * a number or a boolean, but that is not the concern of this class.
045: * Individual properties are accessed based on the DataType class name
046: * and the name of the property.
047: * DataTypes are free to create new properties at any time.
048: * This class just stores what is passed to it and returns those values
049: * when asked.
050: * <P>
051: * This is named DTProperties rather than DataTypeProperties because that
052: * name would indicate that this is another DataType rather than being a
053: * set of data items within the other DataTypes.
054: */
055:
056: import java.util.HashMap;
057: import java.util.ArrayList;
058: import java.util.Iterator;
059:
060: public class DTProperties {
061:
062: /**
063: * The version of the data suitable for loading/unloading from/to files.
064: * The internal representation is a HashMap containing HashMaps containing strings.
065: * The XMLReader/Writer Beans in fw.xml do not handle the general case of HashMaps,
066: * so rather than trying to handle that there, we just convert the data internally into a form
067: * that those classes can handle, i.e. an array of strings.
068: * Each string consists of the name of the class, a space, the name of
069: * a property, an equals, then the contents of the property, which must
070: * also be a string.
071: * This is used in an instance of this class created during load/unload.
072: */
073: private String[] dataArray = new String[0];
074:
075: /**
076: * The mapping from DataType name to object (also a HashMap)
077: * containing the properties for that DataType.
078: * There is only one copy of this table for all instances of this class.
079: */
080: private static HashMap<String, HashMap<String, String>> dataTypes = new HashMap<String, HashMap<String, String>>();
081:
082: /**
083: * ctor
084: */
085: public DTProperties() {
086: }
087:
088: /**
089: * get data in form that can be used to output to file.
090: * This is called from an instance of this class.
091: */
092: public String[] getDataArray() {
093: // first convert internal data into the string array
094: Iterator<String> keys = dataTypes.keySet().iterator();
095:
096: ArrayList<String> propertyList = new ArrayList<String>();
097:
098: // get each DataType's info
099: while (keys.hasNext()) {
100: String tableName = keys.next();
101: HashMap<String, String> h = dataTypes.get(tableName);
102: Iterator<String> propertyNames = h.keySet().iterator();
103: while (propertyNames.hasNext()) {
104: String propertyName = propertyNames.next();
105: StringBuilder tmp = new StringBuilder(tableName);
106: tmp.append(" ");
107: tmp.append(propertyName);
108: tmp.append("=");
109: tmp.append(h.get(propertyName));
110: propertyList.add(tmp.toString());
111: }
112: }
113:
114: dataArray = propertyList.toArray(dataArray);
115: return dataArray;
116: }
117:
118: /**
119: * Data in the external form (array of strings) is passed in and must be converted
120: * to the internal form.
121: * This is called on an instance of this class.
122: * @param inData array of strings in form "DataTypeClass property=value"
123: */
124: public void setDataArray(String[] inData) {
125: dataTypes = new HashMap<String, HashMap<String, String>>(); // make sure we are starting clean
126:
127: // convert each string into Classname, prop, & value and fill it into the data
128: for (int i = 0; i < inData.length; i++) {
129: int endIndex = inData[i].indexOf(" ");
130: String dataTypeName = inData[i].substring(0, endIndex);
131:
132: int startIndex;
133: startIndex = endIndex + 1;
134: endIndex = inData[i].indexOf("=", startIndex);
135: String propertyName = inData[i].substring(startIndex,
136: endIndex);
137: String propertyValue = inData[i].substring(endIndex + 1);
138:
139: // if we have seen a property for this DataType before, then the
140: // hashmap already exists. Otherwise, we need to create it now.
141: HashMap<String, String> h = dataTypes.get(dataTypeName);
142: if (h == null) {
143: h = new HashMap<String, String>();
144: dataTypes.put(dataTypeName, h);
145: }
146:
147: // put the property into the hashmap
148: h.put(propertyName, propertyValue);
149: }
150: }
151:
152: /**
153: * add or replace a table-name/hashmap-of-column-names mapping.
154: * If map is null, remove the entry from the tables.
155: */
156: public static void put(String dataTypeName, String propertyName,
157: String propertyValue) {
158:
159: // get the hashmap for this type, or create it if this is a new property
160: HashMap<String, String> h = dataTypes.get(dataTypeName);
161: if (h == null) {
162: h = new HashMap<String, String>();
163: dataTypes.put(dataTypeName, h);
164: }
165: h.put(propertyName, propertyValue);
166: }
167:
168: /**
169: * get the HashMap of column names for the given table name.
170: * it will be null if the table does not have any limitation on the columns to use.
171: */
172: public static String get(String dataTypeName, String propertyName) {
173: HashMap<String, String> h = dataTypes.get(dataTypeName);
174: if (h == null)
175: return null;
176:
177: return h.get(propertyName);
178: }
179:
180: }
|