001: package net.sourceforge.squirrel_sql.fw.datasetviewer;
002:
003: /*
004: * Copyright (C) 2001 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: import java.util.HashMap;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.Collections;
026: import java.util.Arrays;
027:
028: //import net.sourceforge.squirrel_sql.fw.util.beanwrapper.StringWrapper;
029:
030: /**
031: * @author gwg
032: *
033: * Save and return Import/Export file name and command
034: * based on table+column name. Also allow for load and dump of all data
035: * on application startup/shutdown.
036: *
037: * There should be only one mapping from table+column to import/export info
038: * for the entire application, so this is a singleton class.
039: * To access the table, other classes must call
040: * CellImportExportInfoSaver.getInstance() to get the singleton instance to use.
041: *
042: * The loading of this from the file is particularly finicky since it involves
043: * the XMLBean Reader class creating an empty instance of the class and loading
044: * that instance from the file, then passing that instance in to become the
045: * singleton used by the rest of the framework. Other loading done during startup
046: * (e.g. SQLHistory) is simpler because the data is "owned" by the application-level
047: * code, so the singleton is kept there (e.g. _sqlHistory in Application.java).
048: * That does not make sense here because this data is "owned" and used only by
049: * the fw code.
050: */
051: public class CellImportExportInfoSaver {
052:
053: /**
054: * The map holding the data for lookup.
055: */
056: private HashMap<String, CellImportExportInfo> map = new HashMap<String, CellImportExportInfo>();
057:
058: /**
059: * The list of commands that user has previously entered
060: */
061: private ArrayList<String> cmdList = new ArrayList<String>();
062:
063: /**
064: * the singleton instance of this class.
065: */
066: private static CellImportExportInfoSaver instance = null;
067:
068: /**
069: * Null Constructor:
070: * This should be used only by the XMLBean creator when loading from file.
071: */
072: public CellImportExportInfoSaver() {
073: };
074:
075: /**
076: * get the singleton instance of this class.
077: */
078: static public CellImportExportInfoSaver getInstance() {
079: if (instance == null)
080: instance = new CellImportExportInfoSaver();
081: return instance;
082: }
083:
084: /**
085: * During application startup, an instance of this class is created by
086: * the XMLBean reader and loaded by it automatically. After it is done
087: * loading the entries into that copy, this method is called to make
088: * that instance become the singleton used by everyone.
089: */
090: static public void setInstance(CellImportExportInfoSaver newInstance) {
091: if (newInstance == null)
092: instance = new CellImportExportInfoSaver(); // better safe than sorry!
093: else
094: instance = newInstance;
095: }
096:
097: /**
098: * Used by fw to save user input for export/import on column.
099: */
100: public synchronized void save(String tableColumnName,
101: String fileName, String command) {
102:
103: // If the table+column already has a data object in the map,
104: // then remove it.
105: map.remove(tableColumnName);
106:
107: CellImportExportInfo infoObject = new CellImportExportInfo(
108: tableColumnName, fileName, command);
109:
110: map.put(tableColumnName, infoObject);
111:
112: if (command != null && command.length() > 0) {
113: cmdList.add(command);
114: Collections.sort(cmdList);
115: }
116:
117: }
118:
119: /**
120: * Used by fw to find entries user previously entered for this column.
121: */
122: public CellImportExportInfo get(String tableColumnName) {
123: return map.get(tableColumnName);
124: }
125:
126: /**
127: * Used by fw to get the list of all commands this user has associated
128: * with columns.
129: */
130: public synchronized String[] getCmdList() {
131: String[] data = new String[cmdList.size()];
132: return cmdList.toArray(data);
133: }
134:
135: /**
136: * Used by fw to delete an entry or to make sure entry does not
137: * exist in table, e.g. because it has been reset to defaults.
138: */
139: static public void remove(String tableColumnName) {
140: // make sure there is an instance
141: if (instance == null)
142: instance = new CellImportExportInfoSaver(); // better safe than sorry!
143: instance.map.remove(tableColumnName);
144: }
145:
146: /**
147: * method used by XMLBeans to add new item.
148: */
149: public void add(CellImportExportInfo info) {
150: map.put(info.getTableColumnName(), info);
151: }
152:
153: /**
154: * Method called by reflection in the XMLBean loading of
155: * CellImportExportInfo data from
156: * the files during application startup.
157: */
158: public synchronized void setData(CellImportExportInfo[] data) {
159: for (int i = 0; i < data.length; i++) {
160: map.put(data[i].getTableColumnName(), data[i]);
161: }
162: }
163:
164: /**
165: * Method called by reflection in the XMLBean loading of command list
166: * data from the files during application startup.
167: */
168: public synchronized void setCmdList(String[] data) {
169: cmdList = new ArrayList<String>(Arrays.asList(data));
170: Collections.sort(cmdList);
171: }
172:
173: /**
174: * Method used by the application code when unloading this object
175: * into a file for storage during program shutdown. It is called
176: * by reflection in the XMLBean Writer code.
177: */
178: public synchronized CellImportExportInfo[] getData() {
179: if (instance == null)
180: instance = new CellImportExportInfoSaver(); // better safe than sorry!
181:
182: CellImportExportInfo[] array = new CellImportExportInfo[instance.map
183: .size()];
184: Iterator<CellImportExportInfo> iterator = instance.map.values()
185: .iterator();
186: int index = 0;
187: while (iterator.hasNext()) {
188: array[index] = iterator.next();
189: index++;
190: }
191:
192: return array;
193: }
194:
195: }
|