KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > utils > XJFileChooser


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.utils;
33
34 import org.antlr.xjlib.appkit.app.XJApplication;
35 import org.antlr.xjlib.appkit.frame.XJDialog;
36 import org.antlr.xjlib.foundation.XJSystem;
37 import org.antlr.xjlib.foundation.XJUtils;
38
39 import javax.swing.*;
40 import javax.swing.filechooser.FileFilter JavaDoc;
41 import java.awt.*;
42 import java.io.File JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Collections JavaDoc;
45 import java.util.List JavaDoc;
46
47 public class XJFileChooser {
48
49     private static XJFileChooser shared = new XJFileChooser();
50
51     private List JavaDoc<String JavaDoc> selectedFilePaths = null;
52     private String JavaDoc selectedFilePath = null;
53     private String JavaDoc selectedFileExtension = null;
54
55     static {
56         if(XJSystem.isMacOS()) {
57             // Allow traversal of bundle on Mac OS X
58
UIManager.put("JFileChooser.appBundleIsTraversable", "always");
59             UIManager.put("JFileChooser.packageIsTraversable", "always");
60         }
61     }
62
63     public static XJFileChooser shared() {
64         return shared;
65     }
66
67     public boolean displayOpenDialog(Component parent, boolean multiple) {
68         return displayOpenDialog(parent, (List JavaDoc)null, (List JavaDoc<String JavaDoc>)null, false);
69     }
70
71     public boolean displayOpenDialog(Component parent, String JavaDoc extension, String JavaDoc description, boolean multiple) {
72         return displayOpenDialog(parent, Collections.singletonList(extension), Collections.singletonList(description), multiple);
73     }
74
75     public boolean displayOpenDialog(Component parent, List JavaDoc extensions, String JavaDoc description, boolean multiple) {
76         return displayOpenDialog(parent, Collections.singletonList(extensions), Collections.singletonList(description), multiple);
77     }
78
79     public boolean displayOpenDialog(Component parent, List JavaDoc extensions, List JavaDoc<String JavaDoc> descriptions, boolean multiple) {
80         if(parent != null)
81             parent = XJDialog.resolveOwner(parent.getParent());
82
83         JFileChooser chooser = new JFileChooser();
84         chooser.setMultiSelectionEnabled(multiple);
85         if(extensions == null || extensions.size() == 0)
86             chooser.setAcceptAllFileFilterUsed(true);
87         else {
88             for(int i=0; i<extensions.size(); i++) {
89                 XJFileFilter ff = XJFileFilter.createFileFilter(extensions.get(i),
90                         descriptions.get(i));
91                 chooser.addChoosableFileFilter(ff);
92                 if(extensions.size() == 1 && i == 0)
93                     chooser.setFileFilter(ff);
94             }
95             if(extensions.size() > 1)
96                 chooser.setFileFilter(chooser.getAcceptAllFileFilter());
97         }
98
99         loadDefaultDirectory(chooser);
100
101         if(chooser.showOpenDialog(parent==null?null:parent) == JFileChooser.APPROVE_OPTION) {
102             selectedFilePath = chooser.getSelectedFile().getAbsolutePath();
103             selectedFilePaths = filesToList(chooser.getSelectedFiles());
104             if(extensions != null && extensions.size() >= 0) {
105                 FileFilter JavaDoc ff = chooser.getFileFilter();
106                 if(ff instanceof XJFileFilter) {
107                     XJFileFilter filter = (XJFileFilter)ff;
108                     if(selectedFilePath.indexOf(".") == -1)
109                         selectedFilePath += "."+filter.getDefaultExtension();
110                 }
111             }
112             saveCurrentDirectory(chooser);
113             return XJApplication.YES;
114         } else
115             return XJApplication.NO;
116     }
117
118     private static final String JavaDoc DEFAULT_DIR = "xjfilechooser.default.dir";
119
120     private void loadDefaultDirectory(JFileChooser chooser) {
121         String JavaDoc currentDir = XJApplication.shared().getPreferences().getString(DEFAULT_DIR, null);
122         if(currentDir != null) {
123             File JavaDoc dir = new File JavaDoc(currentDir);
124             if(dir.exists()) {
125                 chooser.setCurrentDirectory(dir);
126             }
127         }
128     }
129
130     private void saveCurrentDirectory(JFileChooser chooser) {
131         XJApplication.shared().getPreferences().setString(DEFAULT_DIR, chooser.getSelectedFile().getAbsolutePath());
132     }
133
134     private List JavaDoc<String JavaDoc> filesToList(File JavaDoc[] files) {
135         List JavaDoc<String JavaDoc> array = new ArrayList JavaDoc<String JavaDoc>();
136         for (File JavaDoc file : files) array.add(file.getAbsolutePath());
137         return array;
138     }
139
140     public boolean displaySaveDialog(Component parent, String JavaDoc extension, String JavaDoc extensionDescription, boolean acceptAll) {
141         return displaySaveDialog(parent, Collections.singletonList(extension),
142                 Collections.singletonList(extensionDescription), acceptAll);
143     }
144
145     public boolean displaySaveDialog(Component parent, List JavaDoc extensions, String JavaDoc extensionDescription, boolean acceptAll) {
146         return displaySaveDialog(parent, Collections.singletonList(extensions),
147                 Collections.singletonList(extensionDescription), acceptAll);
148     }
149
150     public boolean displaySaveDialog(Component parent, List JavaDoc extensions, List JavaDoc<String JavaDoc> descriptions, boolean acceptAll) {
151         if(parent != null)
152             parent = XJDialog.resolveOwner(parent.getParent());
153
154         JFileChooser chooser = new JFileChooser();
155         applyExtensions(extensions, chooser, acceptAll, descriptions);
156         loadDefaultDirectory(chooser);
157
158         if(chooser.showSaveDialog(parent) == JFileChooser.APPROVE_OPTION) {
159             selectedFilePath = chooser.getSelectedFile().getAbsolutePath();
160             selectedFilePaths = filesToList(chooser.getSelectedFiles());
161             selectedFilePath += "."+getWithExtension(extensions, chooser);
162             if(new File JavaDoc(selectedFilePath).exists()) {
163                 String JavaDoc name = XJUtils.getLastPathComponent(selectedFilePath);
164                 if(XJAlert.displayAlert(parent, "Warning",
165                         "The file '"+name+"' already exists. Do you want to replace it?",
166                         "Cancel",
167                         "Replace",
168                         1) == 0)
169                     return XJApplication.NO;
170             }
171
172             saveCurrentDirectory(chooser);
173
174             return XJApplication.YES;
175         } else
176             return XJApplication.NO;
177     }
178
179     public boolean displayChooseDirectory(Component parent) {
180         return displayChooseDirectory(parent, null, null, true);
181     }
182
183     public boolean displayChooseDirectory(Component parent, List JavaDoc extensions, List JavaDoc<String JavaDoc> descriptions, boolean acceptAll) {
184         if(parent != null)
185             parent = XJDialog.resolveOwner(parent.getParent());
186
187         JFileChooser chooser = new JFileChooser();
188         applyExtensions(extensions, chooser, acceptAll, descriptions);
189         loadDefaultDirectory(chooser);
190         chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
191         boolean result;
192         if(chooser.showOpenDialog(parent) == JFileChooser.APPROVE_OPTION) {
193             selectedFilePath = chooser.getSelectedFile().getAbsolutePath();
194             selectedFileExtension = getWithExtension(extensions, chooser);
195             saveCurrentDirectory(chooser);
196             result = XJApplication.YES;
197         } else
198             result = XJApplication.NO;
199
200         if(parent instanceof JDialog) {
201             /** Make sure that if the parent is a modal dialog, it is back to
202              * front: by default, Swing doesn't bring back the dialog to front.
203              */

204
205             JDialog dialog = (JDialog)parent;
206             if(dialog.isModal())
207                 dialog.toFront();
208         }
209         return result;
210     }
211
212     private String JavaDoc getWithExtension(List JavaDoc extensions, JFileChooser chooser) {
213         if(extensions != null && extensions.size() >= 0) {
214             FileFilter JavaDoc ff = chooser.getFileFilter();
215             if(ff instanceof XJFileFilter) {
216                 XJFileFilter filter = (XJFileFilter)ff;
217                 if(!filter.accept(selectedFilePath))
218                     return filter.getDefaultExtension();
219             }
220         }
221         return null;
222     }
223
224     private void applyExtensions(List JavaDoc extensions, JFileChooser chooser, boolean acceptAll, List JavaDoc<String JavaDoc> descriptions) {
225         if(extensions == null || extensions.size() == 0)
226             chooser.setAcceptAllFileFilterUsed(acceptAll);
227         else {
228             chooser.setAcceptAllFileFilterUsed(acceptAll);
229             XJFileFilter firstFF = null;
230             for(int i=0; i<extensions.size(); i++) {
231                 XJFileFilter ff = XJFileFilter.createFileFilter(extensions.get(i),
232                         descriptions.get(i));
233                 chooser.addChoosableFileFilter(ff);
234                 if((extensions.size() == 1 || !acceptAll) && i == 0)
235                     firstFF = ff;
236             }
237             if(extensions.size() > 1 && acceptAll)
238                 chooser.setFileFilter(chooser.getAcceptAllFileFilter());
239             else
240                 chooser.setFileFilter(firstFF);
241         }
242     }
243
244     public String JavaDoc getSelectedFilePath() {
245         return selectedFilePath;
246     }
247
248     public List JavaDoc<String JavaDoc> getSelectedFilePaths() {
249         return selectedFilePaths;
250     }
251
252     public String JavaDoc getSelectedFileExtension() {
253         return selectedFileExtension;
254     }
255 }
256
Popular Tags