001: /*
002: * ExportFileDialog.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.gui.dialogs.export;
013:
014: import java.awt.Component;
015: import java.awt.Window;
016: import java.beans.PropertyChangeEvent;
017: import java.beans.PropertyChangeListener;
018: import java.io.File;
019: import java.util.List;
020: import javax.swing.JFileChooser;
021: import javax.swing.SwingUtilities;
022: import javax.swing.filechooser.FileFilter;
023: import workbench.db.ColumnIdentifier;
024: import workbench.db.WbConnection;
025: import workbench.db.exporter.DataExporter;
026: import workbench.db.exporter.PoiHelper;
027: import workbench.gui.components.ExtensionFileFilter;
028: import workbench.log.LogMgr;
029: import workbench.resource.Settings;
030: import workbench.storage.ResultInfo;
031: import workbench.util.StringUtil;
032:
033: /**
034: * @author support@sql-workbench.net
035: */
036: public class ExportFileDialog implements PropertyChangeListener {
037: private int exportType = -1;
038: private String selectedFilename = null;
039: private boolean isCancelled = false;
040: private Settings settings = Settings.getInstance();
041: private ExportOptionsPanel exportOptions;
042: private JFileChooser chooser;
043: private boolean filterChange = false;
044: private boolean selectDirectory = false;
045: private boolean includeSqlUpdate = true;
046: private boolean includeSqlInsert = true;
047: private boolean includeSqlDeleteInsert = true;
048: private String lastDirConfigKey = "workbench.export.lastdir";
049:
050: private Component parentComponent;
051:
052: public ExportFileDialog(Component caller) {
053: this (caller, null);
054: }
055:
056: public ExportFileDialog(Component caller, ResultInfo columns) {
057: this .exportOptions = new ExportOptionsPanel(columns);
058: this .parentComponent = caller;
059: }
060:
061: public void setQuerySql(String sql, WbConnection con) {
062: this .exportOptions.setQuerySql(sql, con);
063: }
064:
065: public List<ColumnIdentifier> getColumnsToExport() {
066: return this .exportOptions.getColumnsToExport();
067: }
068:
069: public void saveSettings() {
070: exportOptions.saveSettings();
071: }
072:
073: public void restoreSettings() {
074: exportOptions.restoreSettings();
075: }
076:
077: public SpreadSheetOptions getOdsOptions() {
078: return exportOptions.getOdsOptions();
079: }
080:
081: public SpreadSheetOptions getXlsOptions() {
082: return exportOptions.getXlsOptions();
083: }
084:
085: public SpreadSheetOptions getXlsXOptions() {
086: return exportOptions.getXlsXOptions();
087: }
088:
089: public SqlOptions getSqlOptions() {
090: return exportOptions.getSqlOptions();
091: }
092:
093: public HtmlOptions getHtmlOptions() {
094: return exportOptions.getHtmlOptions();
095: }
096:
097: public TextOptions getTextOptions() {
098: return exportOptions.getTextOptions();
099: }
100:
101: public XmlOptions getXmlOptions() {
102: return exportOptions.getXmlOptions();
103: }
104:
105: public ExportOptions getBasicExportOptions() {
106: return exportOptions.getExportOptions();
107: }
108:
109: public String getSelectedFilename() {
110: return this .selectedFilename;
111: }
112:
113: public int getExportType() {
114: return this .exportType;
115: }
116:
117: public boolean isCancelled() {
118: return this .isCancelled;
119: }
120:
121: public void setIncludeSqlUpdate(boolean flag) {
122: this .includeSqlUpdate = flag;
123: }
124:
125: public void setIncludeSqlInsert(boolean flag) {
126: this .includeSqlInsert = flag;
127: }
128:
129: public void setIncludeSqlDeleteInsert(boolean flag) {
130: this .includeSqlDeleteInsert = flag;
131: }
132:
133: /**
134: * Set the config key for the Settings object
135: * where the selected directory should be stored
136: */
137: public void setConfigKey(String key) {
138: this .lastDirConfigKey = key;
139: }
140:
141: private void setupFileFilters(JFileChooser fc) {
142: fc.addChoosableFileFilter(ExtensionFileFilter
143: .getTextFileFilter());
144: fc.addChoosableFileFilter(ExtensionFileFilter
145: .getHtmlFileFilter());
146: if (includeSqlInsert) {
147: fc.addChoosableFileFilter(ExtensionFileFilter
148: .getSqlFileFilter());
149: }
150: if (includeSqlUpdate) {
151: fc.addChoosableFileFilter(ExtensionFileFilter
152: .getSqlUpdateFileFilter());
153: }
154: fc.addChoosableFileFilter(ExtensionFileFilter
155: .getXmlFileFilter());
156: fc.addChoosableFileFilter(ExtensionFileFilter
157: .getOdsFileFilter());
158: if (PoiHelper.isPoiAvailable()) {
159: fc.addChoosableFileFilter(ExtensionFileFilter
160: .getXlsFileFilter());
161: }
162: }
163:
164: public void setSelectDirectoryOnly(boolean flag) {
165: this .selectDirectory = flag;
166: }
167:
168: public boolean selectOutput() {
169: return this .selectOutput(null);
170: }
171:
172: public boolean selectOutput(String title) {
173: this .exportType = -1;
174: this .selectedFilename = null;
175: boolean result = false;
176:
177: String lastDir = settings.getProperty(lastDirConfigKey, null);
178: this .chooser = new JFileChooser(lastDir);
179: if (title != null)
180: this .chooser.setDialogTitle(title);
181:
182: if (this .selectDirectory) {
183: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
184: } else {
185: setupFileFilters(chooser);
186: chooser
187: .addPropertyChangeListener("fileFilterChanged",
188: this );
189: chooser.setFileFilter(ExtensionFileFilter
190: .getTextFileFilter());
191: }
192: this .exportOptions
193: .addPropertyChangeListener("exportType", this );
194: this .restoreSettings();
195:
196: chooser.setAccessory(this .exportOptions);
197:
198: Window parentWindow = SwingUtilities
199: .getWindowAncestor(this .parentComponent);
200:
201: int answer = chooser.showSaveDialog(parentWindow);
202: if (answer == JFileChooser.APPROVE_OPTION) {
203: String filename = null;
204: this .isCancelled = false;
205: File fl = chooser.getSelectedFile();
206:
207: if (this .selectDirectory) {
208: filename = fl.getAbsolutePath();
209: this .exportType = this .exportOptions.getExportType();
210: lastDir = filename;
211: } else {
212: FileFilter ff = chooser.getFileFilter();
213: if (ff instanceof ExtensionFileFilter) {
214: ExtensionFileFilter eff = (ExtensionFileFilter) ff;
215: filename = fl.getAbsolutePath();
216:
217: String ext = ExtensionFileFilter.getExtension(fl);
218: if (StringUtil.isEmptyString(ext)) {
219: if (!filename.endsWith("."))
220: filename = filename + ".";
221: filename = filename + eff.getDefaultExtension();
222: }
223: this .exportType = this .getExportType(eff);
224: } else {
225: filename = fl.getAbsolutePath();
226: this .exportType = this .exportOptions
227: .getExportType();
228: }
229: lastDir = chooser.getCurrentDirectory()
230: .getAbsolutePath();
231: }
232: settings.setProperty(this .lastDirConfigKey, lastDir);
233: this .saveSettings();
234: this .selectedFilename = filename;
235: result = true;
236: } else {
237: this .isCancelled = true;
238: result = false;
239: }
240: return result;
241: }
242:
243: public void setExporterOptions(DataExporter exporter) {
244: exporter.setOptions(this .getBasicExportOptions());
245: exporter.setOutputFilename(this .getSelectedFilename());
246:
247: switch (this .exportType) {
248: case DataExporter.EXPORT_SQL:
249: exporter.setSqlOptions(this .getSqlOptions());
250: break;
251: case DataExporter.EXPORT_TXT:
252: exporter.setTextOptions(this .getTextOptions());
253: break;
254: case DataExporter.EXPORT_HTML:
255: exporter.setHtmlOptions(this .getHtmlOptions());
256: break;
257: case DataExporter.EXPORT_XML:
258: exporter.setXmlOptions(this .getXmlOptions());
259: break;
260: case DataExporter.EXPORT_ODS:
261: exporter.setOdsOptions(getOdsOptions());
262: break;
263: case DataExporter.EXPORT_XLSX:
264: exporter.setXlsXOptions(getXlsXOptions());
265: break;
266: case DataExporter.EXPORT_XLS:
267: exporter.setXlsOptions(getXlsOptions());
268: break;
269: default:
270: exporter.setTextOptions(this .getTextOptions());
271: LogMgr.logWarning("ExportFileDialog.setExporterOptions()",
272: "Unknown file type selected", null);
273: break;
274: }
275: }
276:
277: private int getExportType(ExtensionFileFilter ff) {
278: if (ff.hasFilter(ExtensionFileFilter.SQL_EXT)) {
279: return DataExporter.EXPORT_SQL;
280: } else if (ff.hasFilter(ExtensionFileFilter.XML_EXT)) {
281: return DataExporter.EXPORT_XML;
282: } else if (ff.hasFilter(ExtensionFileFilter.TXT_EXT)) {
283: return DataExporter.EXPORT_TXT;
284: } else if (ff.hasFilter(ExtensionFileFilter.HTML_EXT)) {
285: return DataExporter.EXPORT_HTML;
286: } else if (ff.hasFilter(ExtensionFileFilter.XLS_EXT)) {
287: return DataExporter.EXPORT_XLS;
288: } else if (ff.hasFilter(ExtensionFileFilter.XLSX_EXT)) {
289: return DataExporter.EXPORT_XLSX;
290: } else if (ff.hasFilter(ExtensionFileFilter.ODS_EXT)) {
291: return DataExporter.EXPORT_ODS;
292: }
293: return -1;
294: }
295:
296: public void propertyChange(PropertyChangeEvent evt) {
297: if (this .exportOptions == null)
298: return;
299:
300: if (evt.getSource() instanceof JFileChooser && !filterChange
301: && !this .selectDirectory) {
302: JFileChooser fc = (JFileChooser) evt.getSource();
303: FileFilter ff = fc.getFileFilter();
304: if (ff instanceof ExtensionFileFilter) {
305: ExtensionFileFilter eff = (ExtensionFileFilter) ff;
306: int type = this .getExportType(eff);
307: this .exportOptions.setExportType(type);
308: }
309: } else if (evt.getSource() == this .exportOptions
310: && this .chooser != null && !this .selectDirectory) {
311: try {
312: FileFilter ff = this .chooser.getFileFilter();
313: // check for All file (*.*) filter. In that
314: // case we do not change the current filter.
315: if (!(ff instanceof ExtensionFileFilter))
316: return;
317:
318: Integer newvalue = (Integer) evt.getNewValue();
319: int type = (newvalue == null ? -1 : newvalue.intValue());
320: this .filterChange = true;
321:
322: switch (type) {
323: case DataExporter.EXPORT_SQL:
324: this .chooser.setFileFilter(ExtensionFileFilter
325: .getSqlFileFilter());
326: break;
327: case DataExporter.EXPORT_HTML:
328: this .chooser.setFileFilter(ExtensionFileFilter
329: .getHtmlFileFilter());
330: break;
331: case DataExporter.EXPORT_XML:
332: this .chooser.setFileFilter(ExtensionFileFilter
333: .getXmlFileFilter());
334: break;
335: case DataExporter.EXPORT_TXT:
336: this .chooser.setFileFilter(ExtensionFileFilter
337: .getTextFileFilter());
338: break;
339: case DataExporter.EXPORT_XLS:
340: this .chooser.setFileFilter(ExtensionFileFilter
341: .getXlsFileFilter());
342: break;
343: case DataExporter.EXPORT_XLSX:
344: this .chooser.setFileFilter(ExtensionFileFilter
345: .getXlsXFileFilter());
346: break;
347: case DataExporter.EXPORT_ODS:
348: this .chooser.setFileFilter(ExtensionFileFilter
349: .getOdsFileFilter());
350: break;
351: }
352: } catch (Throwable th) {
353: th.printStackTrace();
354: } finally {
355: this .filterChange = false;
356: }
357: }
358: }
359:
360: }
|