001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.x.impl.swing;
014:
015: import java.awt.Component;
016: import java.io.File;
017: import java.util.HashMap;
018: import java.util.Map;
019:
020: import javax.swing.JFileChooser;
021:
022: import com.eviware.soapui.support.ExtensionFileFilter;
023: import com.eviware.x.dialogs.XFileDialogs;
024:
025: /**
026: *
027: * @author Lars
028: */
029: public class SwingFileDialogs implements XFileDialogs {
030: private static Component parent;
031: private static Map<Object, JFileChooser> choosers = new HashMap<Object, JFileChooser>();
032:
033: public SwingFileDialogs(Component parent) {
034: SwingFileDialogs.parent = parent;
035: }
036:
037: public static synchronized JFileChooser getChooser(Object action) {
038: action = null;
039: JFileChooser chooser = choosers.get(action);
040: if (chooser == null) {
041: chooser = new JFileChooser();
042: choosers.put(action, chooser);
043: }
044:
045: chooser.resetChoosableFileFilters();
046:
047: return chooser;
048: }
049:
050: public static Component getParent() {
051: return parent;
052: }
053:
054: public File saveAs(Object action, String title) {
055: return saveAs(action, title, null, null, null);
056: }
057:
058: public File saveAs(Object action, String title, String extension,
059: String fileType, File defaultFile) {
060: JFileChooser chooser = getChooser(action);
061: chooser.setDialogTitle(title);
062: chooser.setAcceptAllFileFilterUsed(true);
063:
064: if (extension != null && fileType != null)
065: chooser.setFileFilter(new ExtensionFileFilter(extension,
066: fileType));
067:
068: if (defaultFile != null)
069: chooser.setSelectedFile(defaultFile);
070:
071: if (chooser.showSaveDialog(getParent()) != JFileChooser.APPROVE_OPTION)
072: return null;
073:
074: return chooser.getSelectedFile();
075: }
076:
077: public File open(Object action, String title, String extension,
078: String fileType, String current) {
079: return openFile(action, title, extension, fileType, current);
080: }
081:
082: public static File openFile(Object action, String title,
083: String extension, String fileType, String current) {
084: JFileChooser chooser = getChooser(action);
085: chooser.setDialogTitle(title);
086: chooser.setAcceptAllFileFilterUsed(true);
087: if (current != null)
088: chooser.setSelectedFile(new File(current));
089:
090: if (extension != null && fileType != null)
091: chooser.setFileFilter(new ExtensionFileFilter(extension,
092: fileType));
093:
094: if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
095: return null;
096:
097: return chooser.getSelectedFile();
098: }
099:
100: public File openXML(Object action, String title) {
101: return open(action, title, ".xml", "XML Files (*.xml)", null);
102: }
103:
104: public File openDirectory(Object action, String title,
105: File defaultDirectory) {
106: JFileChooser chooser = getChooser(action);
107: chooser.setDialogTitle(title);
108: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
109:
110: if (defaultDirectory != null)
111: chooser.setCurrentDirectory(defaultDirectory);
112:
113: if (chooser.showOpenDialog(getParent()) != JFileChooser.APPROVE_OPTION)
114: return null;
115:
116: return chooser.getSelectedFile();
117: }
118: }
|