001: /*
002: * WbWorkspace.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.io.File;
015: import java.io.FileOutputStream;
016: import java.io.IOException;
017: import java.io.InputStream;
018: import java.io.OutputStream;
019: import java.util.ArrayList;
020: import java.util.Enumeration;
021: import java.util.zip.ZipEntry;
022: import java.util.zip.ZipFile;
023: import java.util.zip.ZipOutputStream;
024:
025: import workbench.gui.sql.SqlHistory;
026: import workbench.log.LogMgr;
027:
028: /**
029: *
030: * @author support@sql-workbench.net
031: */
032: public class WbWorkspace {
033: private ZipOutputStream zout;
034: private ZipFile archive;
035: private ZipEntry[] entries;
036:
037: private boolean isReadOnly;
038: private WbProperties tabInfo = new WbProperties(1);
039:
040: public WbWorkspace(String archiveName, boolean createNew)
041: throws IOException {
042: if (createNew) {
043: this .isReadOnly = false;
044: File f = new File(archiveName);
045: OutputStream out = new FileOutputStream(f);
046: this .zout = new ZipOutputStream(out);
047: this .zout.setLevel(9);
048: this .zout.setComment("SQL Workbench/J Workspace file");
049: } else {
050: this .isReadOnly = true;
051: this .zout = null;
052: this .archive = new ZipFile(archiveName);
053: Enumeration e = this .archive.entries();
054: ArrayList tempEntries = new ArrayList(10);
055: while (e.hasMoreElements()) {
056: ZipEntry entry = (ZipEntry) e.nextElement();
057: String filename = entry.getName().toLowerCase();
058:
059: if (filename.endsWith("txt")) {
060: tempEntries.add(entry);
061: } else if (filename.endsWith("properties")) {
062: this .readTabInfo(entry);
063: }
064: }
065:
066: int count = tempEntries.size();
067: this .entries = new ZipEntry[count];
068:
069: for (int i = 0; i < count; i++) {
070: int ind = -1;
071: int realIndex = -1;
072: try {
073: ZipEntry entry = (ZipEntry) tempEntries.get(i);
074: String filename = entry.getName().toLowerCase();
075: int pos = filename.indexOf('.');
076: ind = StringUtil.getIntValue(filename.substring(12,
077: pos), -1);
078: realIndex = ind - 1;
079: if (realIndex >= 0 && realIndex < count) {
080: entries[realIndex] = entry;
081: } else {
082: LogMgr
083: .logError(
084: "WbWorkspace.<init>",
085: "Wrong index "
086: + realIndex
087: + " retrieved from workspace file ("
088: + count + " entries)",
089: null);
090: }
091: } catch (Throwable ex) {
092: LogMgr.logError("WbWorkspace.<init>",
093: "Error reading history data for index "
094: + realIndex + " (of " + count
095: + " entries)", ex);
096: }
097: }
098: }
099: }
100:
101: /**
102: * Increase the counter for visible DbExplorer panels
103: */
104: public void dDbExplorerVisible() {
105: int count = this .tabInfo
106: .getIntProperty("dbexplorer.visible", 0);
107: count++;
108: this .tabInfo.setProperty("dbexplorer.visible", count);
109: }
110:
111: public int getDbExplorerVisibleCount() {
112: return this .tabInfo.getIntProperty("dbexplorer.visible", 0);
113: }
114:
115: public void addHistoryEntry(String aFilename, SqlHistory history)
116: throws IOException {
117: if (this .isReadOnly)
118: throw new IllegalStateException(
119: "Workspace is opened for reading. addHistoryEntry() may not be called");
120:
121: File f = new File(aFilename);
122: String filename = f.getName();
123: ZipEntry entry = new ZipEntry(filename);
124: this .zout.putNextEntry(entry);
125: history.writeToStream(zout);
126: zout.closeEntry();
127: }
128:
129: public int getEntryCount() {
130: if (!this .isReadOnly)
131: throw new IllegalStateException(
132: "Workspace is opened for writing. Entry count is not available");
133: if (this .entries == null)
134: return 0;
135: return this .entries.length;
136: }
137:
138: public void readHistoryData(int anIndex, SqlHistory history)
139: throws IOException {
140: if (!this .isReadOnly)
141: throw new IllegalStateException(
142: "Workspace is opened for writing. Entry count is not available");
143: if (anIndex > this .entries.length - 1)
144: throw new IndexOutOfBoundsException("Index " + anIndex
145: + " is great then " + (this .entries.length - 1));
146: ZipEntry e = this .entries[anIndex];
147: if (e != null) {
148: InputStream in = this .archive.getInputStream(e);
149: history.readFromStream(in);
150: } else {
151: LogMgr.logError("WbWorkspace.readHistoryData()",
152: "Requested ZipEntry for index " + anIndex
153: + " was null!", null);
154: }
155: return;
156: }
157:
158: public WbProperties getSettings() {
159: return this .tabInfo;
160: }
161:
162: public void close() throws IOException {
163: if (this .zout != null) {
164: if (this .tabInfo != null && this .tabInfo.size() > 0) {
165: try {
166: ZipEntry entry = new ZipEntry("tabinfo.properties");
167: this .zout.putNextEntry(entry);
168: this .tabInfo.save(this .zout);
169: zout.closeEntry();
170: } catch (Throwable e) {
171: LogMgr.logError("WbWorkspace.close()",
172: "Could not write tab info!", e);
173: } finally {
174: this .zout.close();
175: }
176: }
177: } else if (this .archive != null) {
178: this .archive.close();
179: }
180: }
181:
182: private void readTabInfo(ZipEntry entry) {
183: try {
184: InputStream in = this .archive.getInputStream(entry);
185: this .tabInfo = new WbProperties(1);
186: this .tabInfo.load(in);
187: } catch (Exception e) {
188: LogMgr.logError("WbWorkspace", "Could not read tab info!",
189: e);
190: this .tabInfo = new WbProperties(this );
191: }
192: }
193:
194: public void setSelectedTab(int anIndex) {
195: this .tabInfo.setProperty("tab.selected", Integer
196: .toString(anIndex));
197: }
198:
199: public int getSelectedTab() {
200: return StringUtil.getIntValue(this .tabInfo.getProperty(
201: "tab.selected", "0"));
202: }
203:
204: public boolean isSelectedTabExplorer() {
205: int index = getSelectedTab();
206: String key = "dbexplorer" + index + ".currentschema";
207: return this .tabInfo.containsKey(key);
208: }
209:
210: public void setTabTitle(int index, String name) {
211: String key = "tab" + index + ".title";
212: String encoded = StringUtil.escapeUnicode(name,
213: CharacterRange.RANGE_7BIT);
214: this .tabInfo.setProperty(key, encoded);
215: }
216:
217: public String getTabTitle(int index) {
218: if (this .tabInfo == null)
219: return null;
220: String key = "tab" + index + ".title";
221: String value = (String) this .tabInfo.get(key);
222: return StringUtil.decodeUnicode(value);
223: }
224:
225: public int getExternalFileCursorPos(int tabIndex) {
226: if (this .tabInfo == null)
227: return -1;
228: String key = "tab" + tabIndex + ".file.cursorpos";
229: String value = (String) this .tabInfo.get(key);
230: if (value == null)
231: return -1;
232: int result = -1;
233: try {
234: result = Integer.parseInt(value);
235: } catch (Exception e) {
236: result = -1;
237: }
238:
239: return result;
240: }
241:
242: public void setQueryTimeout(int index, int timeout) {
243: String key = "tab" + index + ".timeout";
244: this .tabInfo.setProperty(key, Integer.toString(timeout));
245: }
246:
247: public int getQueryTimeout(int index) {
248: if (this .tabInfo == null)
249: return 0;
250: String key = "tab" + index + ".timeout";
251: String value = (String) this .tabInfo.get(key);
252: if (value == null)
253: return 0;
254: int result = 0;
255: try {
256: result = Integer.parseInt(value);
257: } catch (Exception e) {
258: result = 0;
259: }
260: return result;
261: }
262:
263: public void setMaxRows(int index, int numRows) {
264: String key = "tab" + index + ".maxrows";
265: this .tabInfo.setProperty(key, Integer.toString(numRows));
266: }
267:
268: public int getMaxRows(int tabIndex) {
269: if (this .tabInfo == null)
270: return 0;
271: String key = "tab" + tabIndex + ".maxrows";
272: String value = (String) this .tabInfo.get(key);
273: if (value == null)
274: return 0;
275: int result = 0;
276: try {
277: result = Integer.parseInt(value);
278: } catch (Exception e) {
279: result = 0;
280: }
281: return result;
282: }
283:
284: public String getExternalFileName(int tabIndex) {
285: if (this .tabInfo == null)
286: return null;
287: String key = "tab" + tabIndex + ".filename";
288: String value = (String) this .tabInfo.get(key);
289: return StringUtil.decodeUnicode(value);
290: }
291:
292: public String getExternalFileEncoding(int tabIndex) {
293: if (this .tabInfo == null)
294: return null;
295: String key = "tab" + tabIndex + ".encoding";
296: String value = (String) this .tabInfo.get(key);
297: if (StringUtil.isEmptyString(value))
298: return EncodingUtil.getDefaultEncoding();
299: return value;
300: }
301:
302: public void setExternalFileCursorPos(int tabIndex, int cursor) {
303: String key = "tab" + tabIndex + ".file.cursorpos";
304: this .tabInfo.setProperty(key, Integer.toString(cursor));
305: }
306:
307: public void setExternalFileName(int tabIndex, String filename) {
308: String key = "tab" + tabIndex + ".filename";
309: String encoded = StringUtil.escapeUnicode(filename,
310: CharacterRange.RANGE_7BIT);
311: this .tabInfo.setProperty(key, encoded);
312: }
313:
314: public void setExternalFileEncoding(int tabIndex, String encoding) {
315: if (encoding == null)
316: return;
317: String key = "tab" + tabIndex + ".encoding";
318: this.tabInfo.setProperty(key, encoding);
319: }
320: }
|