001: /*
002: * Created on 8-Jul-2004
003: *
004: */
005: package com.pk;
006:
007: import java.io.BufferedWriter;
008: import java.io.File;
009: import java.io.FileWriter;
010: import java.io.IOException;
011: import java.util.ArrayList;
012: import java.util.Iterator;
013: import java.util.List;
014: import java.util.TreeSet;
015:
016: import javax.swing.LookAndFeel;
017:
018: import org.apache.commons.collections.CollectionUtils;
019:
020: /**
021: * @author ctaylor
022: *
023: */
024: public class Config {
025: public static final int HISTORY_MODE_UNIQUE = 1;
026: public static final int HISTORY_MODE_DIFF_FROM_LAST = 2;
027: public static final int HISTORY_MODE_ALL = 3;
028:
029: private List connectionConfigs = null;
030:
031: private List lookandFeels = null;
032: private LookAndFeel selectedLookAndFeel = null;
033:
034: private String exportDelimiterChar = null;
035:
036: private String encloseByChar = null;
037: private String scriptLineDelimiter = null;
038:
039: private boolean useCdata = true;
040: private String headerColor = null;
041: private String borderColor = null;
042: private String altRowColor = null;
043:
044: private int historyMode = Config.HISTORY_MODE_ALL;
045:
046: /**
047: *
048: */
049: public Config() {
050: super ();
051: connectionConfigs = new ArrayList();
052: }
053:
054: /**
055: * @return Returns the connections.
056: */
057: public List getConnections() {
058: return connectionConfigs;
059: }
060:
061: /**
062: * @param connections
063: * The connections to set.
064: */
065: public void setConnections(List argConnectionConfigs) {
066: this .connectionConfigs = argConnectionConfigs;
067: }
068:
069: public void addConnectionConfig(ConnectionConfig argConnectionConfig) {
070: if (connectionConfigs == null) {
071: connectionConfigs = new ArrayList();
072: }
073: connectionConfigs.add(argConnectionConfig);
074: }
075:
076: public void orderConnections() {
077: if (connectionConfigs == null) {
078: return;
079: }
080: TreeSet tmp = new TreeSet();
081:
082: ConnectionConfig tmpCC;
083: for (Iterator cc = connectionConfigs.iterator(); cc.hasNext();) {
084: tmpCC = (ConnectionConfig) cc.next();
085: tmp.add(tmpCC);
086:
087: }
088: connectionConfigs = new ArrayList();
089: CollectionUtils.addAll(connectionConfigs, tmp.iterator());
090: }
091:
092: public void writeFile() {
093: ConnectionConfig connectionConfig = null;
094: StringBuffer buffer = new StringBuffer("");
095: buffer.append("<?xml version=\"1.0\"?>\n");
096: buffer.append("<config>\n");
097: buffer.append("\t<datasources>\n");
098: int size = 0;
099: if (connectionConfigs != null) {
100: size = connectionConfigs.size();
101: }
102: for (int x = 0; x < size; x++) {
103:
104: connectionConfig = (ConnectionConfig) connectionConfigs
105: .get(x);
106: buffer.append("\t\t<datasource>\n");
107: buffer.append("\t\t\t<name>");
108: buffer.append(connectionConfig.getName());
109: buffer.append("</name>\n");
110: buffer.append("\t\t\t<driver>");
111: buffer.append(connectionConfig.getDriver());
112: buffer.append("</driver>\n");
113: buffer.append("\t\t\t<url>");
114: buffer.append(connectionConfig.getUrl());
115: buffer.append("</url>\n");
116: buffer.append("\t\t\t<databaseDialectName>");
117: buffer.append(connectionConfig.getDatabaseDialectName());
118: buffer.append("</databaseDialectName>\n");
119: buffer.append("\t\t</datasource>\n");
120: }
121: buffer.append("\t</datasources>\n");
122: buffer.append("\t<export>\n");
123: buffer.append("\t\t<delimiterChar>");
124: buffer.append(getExportDelimiterChar());
125: buffer.append("</delimiterChar>\n");
126:
127: buffer.append("\t\t<encloseByChar>");
128: buffer.append(getEncloseByChar());
129: buffer.append("</encloseByChar>\n");
130:
131: buffer.append("\t\t<useCdata>");
132: buffer.append(String.valueOf(isUseCdata()));
133: buffer.append("</useCdata>\n");
134:
135: buffer.append("\t\t<headerColor>");
136: buffer.append(getHeaderColor());
137: buffer.append("</headerColor>\n");
138:
139: buffer.append("\t\t<borderColor>");
140: buffer.append(getBorderColor());
141: buffer.append("</borderColor>\n");
142:
143: buffer.append("\t\t<altRowColor>");
144: buffer.append(getAltRowColor());
145: buffer.append("</altRowColor>\n");
146:
147: buffer.append("\t</export>\n");
148: buffer.append("\t<general>\n");
149: buffer.append("\t\t<lafs>\n");
150:
151: size = lookandFeels.size();
152: LookAndFeel tmpLookAndFeel = null;
153: for (int x = 0; x < size; x++) {
154: tmpLookAndFeel = (LookAndFeel) lookandFeels.get(x);
155: buffer.append("\t\t\t<laf>\n");
156: buffer.append("\t\t\t\t<class>");
157: buffer.append(tmpLookAndFeel.getClass().getName());
158: buffer.append("</class>\n");
159: if (selectedLookAndFeel.getName().equals(
160: tmpLookAndFeel.getName())) {
161: buffer.append("\t\t\t\t<selected>true</selected>\n");
162: } else {
163: buffer.append("\t\t\t\t<selected>false</selected>\n");
164: }
165: buffer.append("\t\t\t</laf>\n");
166: }
167:
168: buffer.append("\t\t</lafs>\n");
169: buffer.append("\t\t\t<history>\n");
170: buffer.append("\t\t\t\t<mode>");
171: buffer.append(String.valueOf(getHistoryMode()));
172: buffer.append("</mode>\n");
173: buffer.append("\t\t\t</history>\n");
174: buffer.append("</general>\n");
175: buffer.append("</config>\n");
176: BufferedWriter out = null;
177: try {
178: out = new BufferedWriter(new FileWriter(new File(
179: "config.xml")));
180: out.write(buffer.toString());
181: out.close();
182: } catch (IOException ex) {
183: ex.printStackTrace();
184: }
185: }
186:
187: public void removeConnection(ConnectionConfig argConnectionConfig) {
188: ConnectionConfig tmp = null;
189: String name = argConnectionConfig.getName();
190: int size = connectionConfigs.size();
191: for (int x = 0; x < size; x++) {
192: tmp = (ConnectionConfig) connectionConfigs.get(x);
193: if (tmp.getName().equals(name)) {
194: connectionConfigs.remove(x);
195: break;
196: }
197: }
198: }
199:
200: /**
201: * @return Returns the encloseByChar.
202: */
203: public String getEncloseByChar() {
204: return encloseByChar;
205: }
206:
207: /**
208: * @param encloseByChar
209: * The encloseByChar to set.
210: */
211: public void setEncloseByChar(String encloseByChar) {
212: this .encloseByChar = encloseByChar;
213: }
214:
215: /**
216: * @return Returns the exportDelimiterChar.
217: */
218: public String getExportDelimiterChar() {
219: return exportDelimiterChar;
220: }
221:
222: /**
223: * @param exportDelimiterChar
224: * The exportDelimiterChar to set.
225: */
226: public void setExportDelimiterChar(String exportDelimiterChar) {
227: this .exportDelimiterChar = exportDelimiterChar;
228: }
229:
230: /**
231: * @return Returns the scriptLineDelimiter.
232: */
233: public String getScriptLineDelimiter() {
234: if (scriptLineDelimiter == null) {
235: scriptLineDelimiter = "/";
236: }
237: return scriptLineDelimiter;
238: }
239:
240: /**
241: * @param scriptLineDelimiter The scriptLineDelimiter to set.
242: */
243: public void setScriptLineDelimiter(String scriptLineDelimiter) {
244: this .scriptLineDelimiter = scriptLineDelimiter;
245: }
246:
247: /**
248: * @return Returns the useCdata.
249: */
250: public boolean isUseCdata() {
251: return useCdata;
252: }
253:
254: /**
255: * @param useCdata The useCdata to set.
256: */
257: public void setUseCdata(boolean useCdata) {
258: this .useCdata = useCdata;
259: }
260:
261: /**
262: * @return Returns the altRowColor.
263: */
264: public String getAltRowColor() {
265: return altRowColor;
266: }
267:
268: /**
269: * @param altRowColor The altRowColor to set.
270: */
271: public void setAltRowColor(String altRowColor) {
272: this .altRowColor = altRowColor;
273: }
274:
275: /**
276: * @return Returns the borderColor.
277: */
278: public String getBorderColor() {
279: return borderColor;
280: }
281:
282: /**
283: * @param borderColor The borderColor to set.
284: */
285: public void setBorderColor(String borderColor) {
286: this .borderColor = borderColor;
287: }
288:
289: /**
290: * @return Returns the connectionConfigs.
291: */
292: public List getConnectionConfigs() {
293: return connectionConfigs;
294: }
295:
296: /**
297: * @param connectionConfigs The connectionConfigs to set.
298: */
299: public void setConnectionConfigs(List connectionConfigs) {
300: this .connectionConfigs = connectionConfigs;
301: }
302:
303: /**
304: * @return Returns the headerColor.
305: */
306: public String getHeaderColor() {
307: return headerColor;
308: }
309:
310: /**
311: * @param headerColor The headerColor to set.
312: */
313: public void setHeaderColor(String headerColor) {
314: this .headerColor = headerColor;
315: }
316:
317: /**
318: * @return Returns the lookandFeels.
319: */
320: public List getLookandFeels() {
321: return lookandFeels;
322: }
323:
324: public int addLookandFeel(LookAndFeel argLookandFeel) {
325: if (lookandFeels == null) {
326: lookandFeels = new ArrayList();
327: }
328: lookandFeels.add(argLookandFeel);
329: return lookandFeels.size() - 1;
330: }
331:
332: /**
333: * @return Returns the selectedLookAndFeel.
334: */
335: public LookAndFeel getSelectedLookAndFeel() {
336: return selectedLookAndFeel;
337: }
338:
339: /**
340: * @param selectedLookAndFeel The selectedLookAndFeel to set.
341: */
342: public void setSelectedLookAndFeel(LookAndFeel selectedLookAndFeel) {
343: this .selectedLookAndFeel = selectedLookAndFeel;
344: }
345:
346: /**
347: * @return Returns the historyMode.
348: */
349: public int getHistoryMode() {
350: return historyMode;
351: }
352:
353: /**
354: * @param historyMode The historyMode to set.
355: */
356: public void setHistoryMode(int historyMode) {
357: this.historyMode = historyMode;
358: }
359: }
|