001: /*
002: * FileDialogUtil.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.awt.Component;
015: import java.awt.Window;
016: import java.io.File;
017:
018: import javax.swing.JFileChooser;
019: import javax.swing.SwingUtilities;
020: import javax.swing.filechooser.FileFilter;
021: import workbench.gui.components.EncodingPanel;
022:
023: import workbench.gui.components.ExtensionFileFilter;
024: import workbench.interfaces.EncodingSelector;
025: import workbench.resource.Settings;
026: import javax.swing.JComponent;
027: import javax.swing.border.EmptyBorder;
028: import workbench.gui.WbSwingUtilities;
029: import workbench.log.LogMgr;
030: import workbench.resource.ResourceMgr;
031:
032: /**
033: *
034: * @author support@sql-workbench.net
035: */
036: public class FileDialogUtil {
037: public static final int FILE_TYPE_UNKNOWN = -1;
038: public static final int FILE_TYPE_TXT = 0;
039: public static final int FILE_TYPE_SQL = 1;
040: public static final int FILE_TYPE_XML = 2;
041: public static final int FILE_TYPE_HTML = 3;
042: public static final int FILE_TYPE_SQL_UPDATE = 4;
043:
044: private int lastFileType = FILE_TYPE_UNKNOWN;
045: public static final String CONFIG_DIR_KEY = "%ConfigDir%";
046: private String encoding = null;
047:
048: public FileDialogUtil() {
049: }
050:
051: public int getLastSelectedFileType() {
052: return this .lastFileType;
053: }
054:
055: public String getEncoding() {
056: return this .encoding;
057: }
058:
059: public String getXmlReportFilename(Component caller) {
060: return this .getXmlReportFilename(caller, null);
061: }
062:
063: public String getXmlReportFilename(Component caller,
064: JComponent accessory) {
065: String lastDir = Settings.getInstance().getProperty(
066: "workbench.xmlreport.lastdir", null);
067: JFileChooser fc = new JFileChooser(lastDir);
068: fc.addChoosableFileFilter(ExtensionFileFilter
069: .getXmlFileFilter());
070: fc.setFileFilter(ExtensionFileFilter.getXmlFileFilter());
071:
072: if (this .encoding == null) {
073: this .encoding = Settings.getInstance()
074: .getDefaultDataEncoding();
075: }
076:
077: EncodingSelector selector = null;
078: if (accessory != null) {
079: fc.setAccessory(accessory);
080: if (accessory instanceof EncodingSelector) {
081: selector = (EncodingSelector) accessory;
082: }
083: selector.setEncoding(this .encoding);
084: } else {
085: EncodingPanel p = new EncodingPanel(this .encoding);
086: p.setBorder(new EmptyBorder(0, 5, 0, 0));
087: selector = p;
088: fc.setAccessory(p);
089: }
090:
091: String filename = null;
092:
093: Window parent = SwingUtilities.getWindowAncestor(caller);
094:
095: int answer = fc.showSaveDialog(parent);
096: if (answer == JFileChooser.APPROVE_OPTION) {
097: this .encoding = selector.getEncoding();
098:
099: File fl = fc.getSelectedFile();
100: FileFilter ff = fc.getFileFilter();
101: if (ff instanceof ExtensionFileFilter) {
102: ExtensionFileFilter eff = (ExtensionFileFilter) ff;
103: filename = fl.getAbsolutePath();
104:
105: String ext = ExtensionFileFilter.getExtension(fl);
106: if (StringUtil.isEmptyString(ext)) {
107: if (!filename.endsWith("."))
108: filename = filename + ".";
109: filename = filename + eff.getDefaultExtension();
110: }
111: this .lastFileType = this .getFileFilterType(ff);
112: } else {
113: filename = fl.getAbsolutePath();
114: }
115:
116: lastDir = fc.getCurrentDirectory().getAbsolutePath();
117: Settings.getInstance().setProperty(
118: "workbench.xmlreport.lastdir", lastDir);
119: }
120:
121: return filename;
122: }
123:
124: private int getFileFilterType(FileFilter ff) {
125: if (ff == ExtensionFileFilter.getSqlFileFilter()) {
126: return FILE_TYPE_SQL;
127: } else if (ff == ExtensionFileFilter.getSqlUpdateFileFilter()) {
128: return FILE_TYPE_SQL_UPDATE;
129: } else if (ff == ExtensionFileFilter.getXmlFileFilter()) {
130: return FILE_TYPE_XML;
131: } else if (ff == ExtensionFileFilter.getTextFileFilter()) {
132: return FILE_TYPE_TXT;
133: } else if (ff == ExtensionFileFilter.getHtmlFileFilter()) {
134: return FILE_TYPE_HTML;
135: }
136: return FILE_TYPE_UNKNOWN;
137: }
138:
139: public static String getBlobFile(Component caller) {
140: return getBlobFile(caller, true);
141: }
142:
143: public static String getBlobFile(Component caller,
144: boolean showSaveDialog) {
145: try {
146: Window parent = SwingUtilities.getWindowAncestor(caller);
147: String lastDir = Settings.getInstance().getLastBlobDir();
148: JFileChooser fc = new JFileChooser(lastDir);
149: int answer = JFileChooser.CANCEL_OPTION;
150: if (showSaveDialog) {
151: answer = fc.showSaveDialog(parent);
152: } else {
153: answer = fc.showOpenDialog(parent);
154: }
155: String filename = null;
156: if (answer == JFileChooser.APPROVE_OPTION) {
157: File fl = fc.getSelectedFile();
158: filename = fl.getAbsolutePath();
159: Settings.getInstance().setLastBlobDir(
160: fl.getParentFile().getAbsolutePath());
161: }
162: return filename;
163: } catch (Throwable e) {
164: LogMgr.logError("FileDialogUtil.getBlobFile()",
165: "Error selecting file", e);
166: WbSwingUtilities.showErrorMessage(ExceptionUtil
167: .getDisplay(e));
168: return null;
169: }
170: }
171:
172: public String getWorkspaceFilename(Window parent, boolean toSave) {
173: return this .getWorkspaceFilename(parent, toSave, false);
174: }
175:
176: public String getWorkspaceFilename(Window parent, boolean toSave,
177: boolean replaceConfigDir) {
178: try {
179: String lastDir = Settings.getInstance()
180: .getLastWorkspaceDir();
181: JFileChooser fc = new JFileChooser(lastDir);
182: FileFilter wksp = ExtensionFileFilter
183: .getWorkspaceFileFilter();
184: fc.addChoosableFileFilter(wksp);
185: String filename = null;
186:
187: int answer = JFileChooser.CANCEL_OPTION;
188: if (toSave) {
189: answer = fc.showSaveDialog(parent);
190: } else {
191: answer = fc.showOpenDialog(parent);
192: }
193: if (answer == JFileChooser.APPROVE_OPTION) {
194: File fl = fc.getSelectedFile();
195: FileFilter ff = fc.getFileFilter();
196: if (ff == wksp) {
197: filename = fl.getAbsolutePath();
198:
199: String ext = ExtensionFileFilter.getExtension(fl);
200: if (StringUtil.isEmptyString(ext)) {
201: if (!filename.endsWith("."))
202: filename = filename + ".";
203: filename = filename
204: + ExtensionFileFilter.WORKSPACE_EXT;
205: }
206: } else {
207: filename = fl.getAbsolutePath();
208: }
209:
210: lastDir = fc.getCurrentDirectory().getAbsolutePath();
211: Settings.getInstance().setLastWorkspaceDir(lastDir);
212: }
213: if (replaceConfigDir && filename != null) {
214: filename = this .putConfigDirKey(filename);
215: }
216: return filename;
217: } catch (Throwable e) {
218: LogMgr.logError("FileDialogUtil.getWorkspaceFilename()",
219: "Error selecting file", e);
220: WbSwingUtilities.showErrorMessage(ExceptionUtil
221: .getDisplay(e));
222: return null;
223: }
224: }
225:
226: public String putConfigDirKey(String aPathname) {
227: File f = new File(aPathname);
228: String fname = f.getName();
229: File dir = f.getParentFile();
230: File config = Settings.getInstance().getConfigDir();
231: if (dir.equals(config)) {
232: return CONFIG_DIR_KEY + "/" + fname;
233: } else {
234: return aPathname;
235: }
236: }
237:
238: public static String replaceConfigDir(String aPathname) {
239: if (aPathname == null)
240: return null;
241: WbFile dir = new WbFile(Settings.getInstance().getConfigDir());
242: return StringUtil.replace(aPathname, CONFIG_DIR_KEY, dir
243: .getFullPath());
244: }
245:
246: public static void selectPkMapFileIfNecessary(Component parent) {
247: String file = Settings.getInstance().getPKMappingFilename();
248: if (file != null) {
249: File f = new File(file);
250: if (f.exists())
251: return;
252: }
253: boolean doSelectFile = WbSwingUtilities.getYesNo(parent,
254: ResourceMgr.getString("MsgSelectPkMapFile"));
255: if (!doSelectFile)
256: return;
257: file = selectPkMapFile(parent);
258: if (file != null) {
259: Settings.getInstance().setPKMappingFilename(file);
260: }
261: }
262:
263: public static String selectPkMapFile(Component parent) {
264: String fileName = Settings.getInstance().getPKMappingFilename();
265: File f = null;
266: if (fileName == null) {
267: f = Settings.getInstance().getConfigDir();
268: } else {
269: f = new File(fileName).getParentFile();
270: }
271:
272: try {
273: JFileChooser dialog = new JFileChooser(f);
274: dialog.setApproveButtonText(ResourceMgr.getString("LblOK"));
275: if (fileName != null) {
276: dialog.setSelectedFile(new File(fileName));
277: }
278: String selectedFile = null;
279: int choice = dialog.showSaveDialog(parent);
280: if (choice == JFileChooser.APPROVE_OPTION) {
281: File target = dialog.getSelectedFile();
282: selectedFile = target.getAbsolutePath();
283: }
284: return selectedFile;
285: } catch (Throwable e) {
286: LogMgr.logError("FileDialogUtil.selectPkMapFile()",
287: "Error selecting file", e);
288: WbSwingUtilities.showErrorMessage(ExceptionUtil
289: .getDisplay(e));
290: return null;
291: }
292: }
293: }
|