KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > FileDialoger


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/FileDialoger.java,v 1.7 2004/02/13 02:21:38 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.io.File JavaDoc;
22
23 import javax.swing.JFileChooser JavaDoc;
24 import javax.swing.filechooser.FileFilter JavaDoc;
25
26 import org.apache.jmeter.gui.GuiPackage;
27 import org.apache.jmeter.gui.JMeterFileFilter;
28 import org.apache.jmeter.util.JMeterUtils;
29
30 /**
31  * @author Michael Stover
32  * @version $Revision: 1.7 $
33  */

34 public final class FileDialoger
35 {
36     /**
37      * The last directory visited by the user while choosing Files.
38      */

39     public static String JavaDoc lastJFCDirectory = null;//TODO: make private?
40
public static JFileChooser JavaDoc jfc = new JFileChooser JavaDoc();//TODO: make private?
41

42     /**
43      * Prevent instantiation of utility class.
44      */

45     private FileDialoger()
46     {
47     }
48
49     /**
50      * Prompts the user to choose a file from their filesystems for our own
51      * devious uses. This method maintains the last directory the user visited
52      * before dismissing the dialog. This does NOT imply they actually chose a
53      * file from that directory, only that they closed the dialog there. It is
54      * the caller's responsibility to check to see if the selected file is
55      * non-null.
56      *
57      * @return the JFileChooser that interacted with the user, after they are
58      * finished using it (accept or otherwise).
59      */

60     public static JFileChooser JavaDoc promptToOpenFile(String JavaDoc[] exts)
61     {
62         //JFileChooser jfc = null;
63

64         if (lastJFCDirectory == null)
65         {
66             String JavaDoc start = JMeterUtils.getPropDefault("user.dir", "");
67
68             if (!start.equals(""))
69             {
70                 jfc.setCurrentDirectory(new File JavaDoc(start));
71             }
72         }
73         clearFileFilters();
74         jfc.addChoosableFileFilter(new JMeterFileFilter(exts));
75         int retVal =
76             jfc.showOpenDialog(GuiPackage.getInstance().getMainFrame());
77         lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
78
79         if (retVal == JFileChooser.APPROVE_OPTION)
80         {
81             return jfc;
82         }
83         else
84         {
85             return null;
86         }
87     }
88
89     private static void clearFileFilters()
90     {
91         FileFilter JavaDoc[] filters = jfc.getChoosableFileFilters();
92         for (int x = 0; x < filters.length; x++)
93         {
94             jfc.removeChoosableFileFilter(filters[x]);
95         }
96     }
97
98     public static JFileChooser JavaDoc promptToOpenFile()
99     {
100         return promptToOpenFile(new String JavaDoc[0]);
101     }
102
103     /**
104      * Prompts the user to choose a file from their filesystems for our own
105      * devious uses. This method maintains the last directory the user visited
106      * before dismissing the dialog. This does NOT imply they actually chose a
107      * file from that directory, only that they closed the dialog there. It is
108      * the caller's responsibility to check to see if the selected file is
109      * non-null.
110      *
111      * @return the JFileChooser that interacted with the user, after they
112      * are finished using it (accept or otherwise).
113      * @see #promptToOpenFile
114      */

115     public static JFileChooser JavaDoc promptToSaveFile(String JavaDoc filename)
116     {
117         if (lastJFCDirectory == null)
118         {
119             String JavaDoc start = JMeterUtils.getPropDefault("user.dir", "");
120             if (!start.equals(""))
121             {
122                 jfc = new JFileChooser JavaDoc(new File JavaDoc(start));
123             }
124             lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
125         }
126         String JavaDoc ext = ".jmx";
127         if (filename != null)
128         {
129             jfc.setSelectedFile(new File JavaDoc(lastJFCDirectory, filename));
130             int i = -1;
131             if ((i = filename.indexOf(".")) > -1)
132             {
133                 ext = filename.substring(i);
134             }
135         }
136         clearFileFilters();
137         jfc.addChoosableFileFilter(new JMeterFileFilter(new String JavaDoc[] { ext }));
138
139         int retVal =
140             jfc.showSaveDialog(GuiPackage.getInstance().getMainFrame());
141         lastJFCDirectory = jfc.getCurrentDirectory().getAbsolutePath();
142         if (retVal == JFileChooser.APPROVE_OPTION)
143         {
144             return jfc;
145         }
146         else
147         {
148             return null;
149         }
150     }
151 }
152
Popular Tags