001: /*
002: * ImportFileDialog.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.dataimport;
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:
020: import javax.swing.JFileChooser;
021: import javax.swing.SwingUtilities;
022: import javax.swing.filechooser.FileFilter;
023: import workbench.db.importer.ProducerFactory;
024:
025: import workbench.gui.components.ExtensionFileFilter;
026: import workbench.log.LogMgr;
027: import workbench.resource.Settings;
028: import workbench.util.StringUtil;
029:
030: /**
031: * @author support@sql-workbench.net
032: */
033: public class ImportFileDialog implements PropertyChangeListener {
034: private int importType = -1;
035: private File selectedFile = null;
036: private boolean isCancelled = false;
037: private Settings settings = Settings.getInstance();
038: private ImportOptionsPanel importOptions;
039: private JFileChooser chooser;
040: private boolean filterChange = false;
041: private String lastDirConfigKey = "workbench.import.lastdir";
042: private Component parentComponent;
043:
044: public ImportFileDialog(Component caller) {
045: this .importOptions = new ImportOptionsPanel();
046: this .parentComponent = caller;
047: }
048:
049: public void allowImportModeSelection(boolean flag) {
050: this .importOptions.allowImportModeSelection(flag);
051: }
052:
053: public void saveSettings() {
054: importOptions.saveSettings();
055: }
056:
057: public void restoreSettings() {
058: importOptions.restoreSettings();
059: }
060:
061: public XmlImportOptions getXmlOptions() {
062: return importOptions.getXmlOptions();
063: }
064:
065: public TextImportOptions getTextOptions() {
066: return importOptions.getTextOptions();
067: }
068:
069: public ImportOptions getGeneralOptions() {
070: return importOptions.getGeneralOptions();
071: }
072:
073: public File getSelectedFile() {
074: return this .selectedFile;
075: }
076:
077: public int getImportType() {
078: return this .importType;
079: }
080:
081: public boolean isCancelled() {
082: return this .isCancelled;
083: }
084:
085: /**
086: * Set the config key for the Settings object
087: * where the selected directory should be stored
088: */
089: public void setConfigKey(String key) {
090: this .lastDirConfigKey = key;
091: }
092:
093: public boolean selectInput() {
094: return this .selectInput(null);
095: }
096:
097: public boolean selectInput(String title) {
098: this .importType = -1;
099: this .selectedFile = null;
100: boolean result = false;
101:
102: String lastDir = settings.getProperty(lastDirConfigKey, null);
103: this .chooser = new JFileChooser(lastDir);
104: if (title != null)
105: this .chooser.setDialogTitle(title);
106:
107: chooser.addChoosableFileFilter(ExtensionFileFilter
108: .getTextFileFilter());
109: chooser.addChoosableFileFilter(ExtensionFileFilter
110: .getXmlFileFilter());
111: chooser.addPropertyChangeListener("fileFilterChanged", this );
112: chooser.setFileFilter(ExtensionFileFilter.getTextFileFilter());
113: this .importOptions
114: .addPropertyChangeListener("exportType", this );
115: this .restoreSettings();
116: this .importOptions.setTypeText();
117:
118: chooser.setAccessory(this .importOptions);
119:
120: Window parentWindow = SwingUtilities
121: .getWindowAncestor(this .parentComponent);
122:
123: int answer = chooser.showOpenDialog(parentWindow);
124: if (answer == JFileChooser.APPROVE_OPTION) {
125: String filename = null;
126: this .isCancelled = false;
127: File fl = chooser.getSelectedFile();
128:
129: FileFilter ff = chooser.getFileFilter();
130: if (ff instanceof ExtensionFileFilter) {
131: ExtensionFileFilter eff = (ExtensionFileFilter) ff;
132: filename = fl.getAbsolutePath();
133:
134: String ext = ExtensionFileFilter.getExtension(fl);
135: if (StringUtil.isEmptyString(ext)) {
136: if (!filename.endsWith("."))
137: filename = filename + ".";
138: filename = filename + eff.getDefaultExtension();
139: }
140: this .importType = this .getImportType(ff);
141: } else {
142: filename = fl.getAbsolutePath();
143: this .importType = this .importOptions.getImportType();
144: }
145: lastDir = chooser.getCurrentDirectory().getAbsolutePath();
146:
147: settings.setProperty(this .lastDirConfigKey, lastDir);
148: this .saveSettings();
149: this .selectedFile = new File(filename);
150: result = true;
151: } else {
152: this .isCancelled = true;
153: result = false;
154: }
155: return result;
156: }
157:
158: private int getImportType(FileFilter ff) {
159: if (ff == ExtensionFileFilter.getXmlFileFilter()) {
160: return ProducerFactory.IMPORT_XML;
161: } else if (ff == ExtensionFileFilter.getTextFileFilter()) {
162: return ProducerFactory.IMPORT_TEXT;
163: }
164: return -1;
165: }
166:
167: public void propertyChange(PropertyChangeEvent evt) {
168: if (this .importOptions == null)
169: return;
170:
171: if (evt.getSource() instanceof JFileChooser && !filterChange) {
172: JFileChooser fc = (JFileChooser) evt.getSource();
173: FileFilter ff = fc.getFileFilter();
174: if (ff instanceof ExtensionFileFilter) {
175: ExtensionFileFilter eff = (ExtensionFileFilter) ff;
176: int type = this .getImportType(eff);
177: this .importOptions.setImportType(type);
178: }
179: } else if (evt.getSource() == this .importOptions
180: && this .chooser != null) {
181: try {
182: FileFilter ff = this .chooser.getFileFilter();
183: // check for All file (*.*) filter. In that
184: // case we do not change the current filter.
185: if (!(ff instanceof ExtensionFileFilter))
186: return;
187:
188: Integer newvalue = (Integer) evt.getNewValue();
189: int type = (newvalue == null ? -1 : newvalue.intValue());
190: this .filterChange = true;
191:
192: switch (type) {
193: case ProducerFactory.IMPORT_XML:
194: this .chooser.setFileFilter(ExtensionFileFilter
195: .getXmlFileFilter());
196: break;
197: case ProducerFactory.IMPORT_TEXT:
198: this .chooser.setFileFilter(ExtensionFileFilter
199: .getTextFileFilter());
200: break;
201: }
202: } catch (Throwable th) {
203: LogMgr.logError("ImportFileDialog.propertyChange",
204: "Error: ", th);
205: } finally {
206: this .filterChange = false;
207: }
208: }
209: }
210:
211: }
|