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.ReportGuiPackage;
027: import org.apache.jmeter.gui.JMeterFileFilter;
028: import org.apache.jmeter.util.JMeterUtils;
029:
030: /**
031: * @author Peter Lin
032: * @version $Revision: 571988 $
033: */
034: public final class ReportFileDialoger {
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 ReportFileDialoger() {
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", "");
064:
065: if (!start.equals("")) {
066: jfc.setCurrentDirectory(new File(start));
067: }
068: }
069: clearFileFilters();
070: jfc.addChoosableFileFilter(new JMeterFileFilter(exts));
071: int retVal = jfc.showOpenDialog(ReportGuiPackage.getInstance()
072: .getMainFrame());
073: lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
074:
075: if (retVal == JFileChooser.APPROVE_OPTION) {
076: return jfc;
077: } else {
078: return null;
079: }
080: }
081:
082: private static void clearFileFilters() {
083: FileFilter[] filters = jfc.getChoosableFileFilters();
084: for (int x = 0; x < filters.length; x++) {
085: jfc.removeChoosableFileFilter(filters[x]);
086: }
087: }
088:
089: public static JFileChooser promptToOpenFile() {
090: return promptToOpenFile(new String[0]);
091: }
092:
093: /**
094: * Prompts the user to choose a file from their filesystems for our own
095: * devious uses. This method maintains the last directory the user visited
096: * before dismissing the dialog. This does NOT imply they actually chose a
097: * file from that directory, only that they closed the dialog there. It is
098: * the caller's responsibility to check to see if the selected file is
099: * non-null.
100: *
101: * @return the JFileChooser that interacted with the user, after they are
102: * finished using it (accept or otherwise).
103: * @see #promptToOpenFile()
104: */
105: public static JFileChooser promptToSaveFile(String filename) {
106: return promptToSaveFile(filename, null);
107: }
108:
109: /**
110: * Get a JFileChooser with a new FileFilter.
111: *
112: * @param filename
113: * @param extensions
114: * @return JFileChooser
115: */
116: public static JFileChooser promptToSaveFile(String filename,
117: String[] extensions) {
118: if (lastJFCDirectory == null) {
119: String start = JMeterUtils.getPropDefault("user.dir", "");
120: if (!start.equals("")) {
121: jfc = new JFileChooser(new File(start));
122: }
123: lastJFCDirectory = jfc.getCurrentDirectory()
124: .getAbsolutePath();
125: }
126: String ext = ".jmx";
127: if (filename != null) {
128: jfc.setSelectedFile(new File(lastJFCDirectory, filename));
129: int i = -1;
130: if ((i = filename.lastIndexOf(".")) > -1) {
131: ext = filename.substring(i);
132: }
133: }
134: clearFileFilters();
135: if (extensions != null) {
136: jfc
137: .addChoosableFileFilter(new JMeterFileFilter(
138: extensions));
139: } else {
140: jfc.addChoosableFileFilter(new JMeterFileFilter(
141: new String[] { ext }));
142: }
143:
144: int retVal = jfc.showSaveDialog(ReportGuiPackage.getInstance()
145: .getMainFrame());
146: lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
147: if (retVal == JFileChooser.APPROVE_OPTION) {
148: return jfc;
149: } else {
150: return null;
151: }
152: }
153: }
|