001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jmeter.gui.util;
020:
021: import java.io.File;
022:
023: import javax.swing.JFileChooser;
024: import javax.swing.filechooser.FileFilter;
025:
026: import org.apache.jmeter.gui.GuiPackage;
027: import org.apache.jmeter.gui.JMeterFileFilter;
028: import org.apache.jmeter.util.JMeterUtils;
029:
030: /**
031: * @author Michael Stover
032: * @version $Revision: 571988 $
033: */
034: public final class FileDialoger {
035: /**
036: * The last directory visited by the user while choosing Files.
037: */
038: private static String lastJFCDirectory = null;
039:
040: private static JFileChooser jfc = new JFileChooser();
041:
042: /**
043: * Prevent instantiation of utility class.
044: */
045: private FileDialoger() {
046: }
047:
048: /**
049: * Prompts the user to choose a file from their filesystems for our own
050: * devious uses. This method maintains the last directory the user visited
051: * before dismissing the dialog. This does NOT imply they actually chose a
052: * file from that directory, only that they closed the dialog there. It is
053: * the caller's responsibility to check to see if the selected file is
054: * non-null.
055: *
056: * @return the JFileChooser that interacted with the user, after they are
057: * finished using it (accept or otherwise).
058: */
059: public static JFileChooser promptToOpenFile(String[] exts) {
060: // JFileChooser jfc = null;
061:
062: if (lastJFCDirectory == null) {
063: String start = JMeterUtils.getPropDefault("user.dir", ""); //$NON-NLS-1$//$NON-NLS-2$
064:
065: if (start.length() > 0) {
066: jfc.setCurrentDirectory(new File(start));
067: }
068: }
069: clearFileFilters();
070: if (exts != null && exts.length > 0) {
071: jfc.addChoosableFileFilter(new JMeterFileFilter(exts));
072: }
073: int retVal = jfc.showOpenDialog(GuiPackage.getInstance()
074: .getMainFrame());
075: lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
076:
077: if (retVal == JFileChooser.APPROVE_OPTION) {
078: return jfc;
079: } else {
080: return null;
081: }
082: }
083:
084: private static void clearFileFilters() {
085: FileFilter[] filters = jfc.getChoosableFileFilters();
086: for (int x = 0; x < filters.length; x++) {
087: jfc.removeChoosableFileFilter(filters[x]);
088: }
089: }
090:
091: public static JFileChooser promptToOpenFile() {
092: return promptToOpenFile(new String[0]);
093: }
094:
095: /**
096: * Prompts the user to choose a file from their filesystems for our own
097: * devious uses. This method maintains the last directory the user visited
098: * before dismissing the dialog. This does NOT imply they actually chose a
099: * file from that directory, only that they closed the dialog there. It is
100: * the caller's responsibility to check to see if the selected file is
101: * non-null.
102: *
103: * @return the JFileChooser that interacted with the user, after they are
104: * finished using it (accept or otherwise).
105: * @see #promptToOpenFile()
106: */
107: public static JFileChooser promptToSaveFile(String filename) {
108: return promptToSaveFile(filename, null);
109: }
110:
111: /**
112: * Get a JFileChooser with a new FileFilter.
113: *
114: * @param filename file name
115: * @param extensions list of extensions
116: * @return the FileChooser
117: */
118: public static JFileChooser promptToSaveFile(String filename,
119: String[] extensions) {
120: if (lastJFCDirectory == null) {
121: String start = JMeterUtils.getPropDefault("user.dir", "");//$NON-NLS-1$//$NON-NLS-2$
122: if (start.length() > 0) {
123: jfc = new JFileChooser(new File(start));
124: }
125: lastJFCDirectory = jfc.getCurrentDirectory()
126: .getAbsolutePath();
127: }
128: String ext = ".jmx";//$NON-NLS-1$
129: if (filename != null) {
130: jfc.setSelectedFile(new File(lastJFCDirectory, filename));
131: int i = -1;
132: if ((i = filename.lastIndexOf(".")) > -1) {//$NON-NLS-1$
133: ext = filename.substring(i);
134: }
135: }
136: clearFileFilters();
137: if (extensions != null) {
138: jfc
139: .addChoosableFileFilter(new JMeterFileFilter(
140: extensions));
141: } else {
142: jfc.addChoosableFileFilter(new JMeterFileFilter(
143: new String[] { ext }));
144: }
145:
146: int retVal = jfc.showSaveDialog(GuiPackage.getInstance()
147: .getMainFrame());
148: lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
149: if (retVal == JFileChooser.APPROVE_OPTION) {
150: return jfc;
151: } else {
152: return null;
153: }
154: }
155: }
|