File Source: KFileChooser.java
/*
P/P * Method: com.dmdirc.addons.dcc.kde.KFileChooser__static_init
*/
1 /*
2 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.dmdirc.addons.dcc.kde;
24
25 import com.dmdirc.addons.dcc.DCCPlugin;
26
27 import com.dmdirc.config.IdentityManager;
28
29 import java.awt.Component;
30 import java.awt.HeadlessException;
31 import java.io.File;
32 import java.util.ArrayList;
33 import java.util.List;
34
35 import javax.swing.JFileChooser;
36 import javax.swing.filechooser.FileSystemView;
37
38 /**
39 * JFileChooser that uses KDialog to show the actual chooser.
40 * This is quite hacky, and not guarenteed to behave identically to JFileChooser,
41 * altho it tries to be as close as possible.
42 * Almost a drop in replacement for JFileChooser, replace:
43 * new JFileChooser();
44 * with:
45 * KFileChooser.getFileChooser();
46 *
47 * There are obviously some differences:
48 * - File filters must be set using setKDEFileFilter() not using FileFilter objects.
49 * - FileSystemView's are ignored
50 * - showOpenDialog and showSaveDialog shell kdialog, so only options available
51 * in kdialog work.
52 * - getFileChooser() will return a JFileChooser object unless the DCC plugin's
53 * config option "general.useKFileChooser" is set to "true" (defaults to false)
54 * and kdialog is in either /usr/bin or /bin
55 * - Selection mode FILES_AND_DIRECTORIES can not be used
56 */
57 public class KFileChooser extends JFileChooser {
58 /**
59 * A version number for this class.
60 * It should be changed whenever the class structure is changed (or anything
61 * else that would prevent serialized objects being unserialized with the new
62 * class).
63 */
64 private static final long serialVersionUID = 200806141;
65
66 /** File Filter */
67 private String fileFilter = null;
68
69 /** The plugin that this file chooser is for. */
70 private final DCCPlugin plugin;
71
72 /**
73 * Should a KFileChooser be used rather than a JFileChooser?
74 *
75 * @param plugin The DCC Plugin that is requesting a chooser
76 * @return return true if getFileChooser() will return a KFileChooser not a
77 * JFileChooser
78 */
79 public static boolean useKFileChooser(final DCCPlugin plugin) {
/*
P/P * Method: bool useKFileChooser(DCCPlugin)
*
* Preconditions:
* (soft) plugin != null
*
* Presumptions:
* com.dmdirc.config.IdentityManager:getGlobalConfig(...)@80 != null
*
* Postconditions:
* init'ed(return_value)
*/
80 return KDialogProcess.hasKDialog() && IdentityManager.getGlobalConfig().getOptionBool(plugin.getDomain(), "general.useKFileChooser");
81 }
82
83 /**
84 * Constructs a FileChooser pointing to the user's default directory.
85 *
86 * @param plugin The DCC Plugin that is requesting a chooser
87 * @return The relevant FileChooser
88 */
89 public static JFileChooser getFileChooser(final DCCPlugin plugin) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
90 return useKFileChooser(plugin) ? new KFileChooser(plugin) : new JFileChooser();
91 }
92
93 /**
94 * Constructs a FileChooser using the given File as the path.
95 *
96 * @param plugin The DCC Plugin that is requesting a chooser
97 * @param currentDirectory Directory to use as the base directory
98 * @return The relevant FileChooser
99 */
100 public static JFileChooser getFileChooser(final DCCPlugin plugin, final File currentDirectory) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin, File)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
101 return useKFileChooser(plugin) ? new KFileChooser(plugin, currentDirectory) : new JFileChooser(currentDirectory);
102 }
103
104 /**
105 * Constructs a FileChooser using the given current directory and FileSystemView.
106 *
107 * @param plugin The DCC Plugin that is requesting a chooser
108 * @param currentDirectory Directory to use as the base directory
109 * @param fsv The FileSystemView to use
110 * @return The relevant FileChooser
111 */
112 public static JFileChooser getFileChooser(final DCCPlugin plugin, final File currentDirectory, final FileSystemView fsv) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin, File, FileSystemView)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
113 return useKFileChooser(plugin) ? new KFileChooser(plugin, currentDirectory, fsv) : new JFileChooser(currentDirectory, fsv);
114 }
115
116 /**
117 * Constructs a FileChooser using the given FileSystemView.
118 *
119 * @param plugin The DCC Plugin that is requesting a chooser
120 * @param fsv The FileSystemView to use
121 * @return The relevant FileChooser
122 */
123 public static JFileChooser getFileChooser(final DCCPlugin plugin, final FileSystemView fsv) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin, FileSystemView)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
124 return useKFileChooser(plugin) ? new KFileChooser(plugin, fsv) : new JFileChooser(fsv);
125 }
126
127 /**
128 * Constructs a FileChooser using the given path.
129 *
130 * @param plugin The DCC Plugin that is requesting a chooser
131 * @param currentDirectoryPath Directory to use as the base directory
132 * @return The relevant FileChooser
133 */
134 public static JFileChooser getFileChooser(final DCCPlugin plugin, final String currentDirectoryPath) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin, String)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
135 return useKFileChooser(plugin) ? new KFileChooser(plugin, currentDirectoryPath) : new JFileChooser(currentDirectoryPath);
136 }
137
138 /**
139 * Constructs a FileChooser using the given current directory path and FileSystemView.
140 *
141 * @param plugin The DCC Plugin that is requesting a chooser
142 * @param currentDirectoryPath Directory to use as the base directory
143 * @param fsv The FileSystemView to use
144 * @return The relevant FileChooser
145 */
146 public static JFileChooser getFileChooser(final DCCPlugin plugin, final String currentDirectoryPath, final FileSystemView fsv) {
/*
P/P * Method: JFileChooser getFileChooser(DCCPlugin, String, FileSystemView)
*
* Preconditions:
* (soft) plugin != null
*
* Postconditions:
* return_value == One-of{&new KFileChooser(getFileChooser#1), &new JFileChooser(getFileChooser#2)}
* return_value in Addr_Set{&new JFileChooser(getFileChooser#2),&new KFileChooser(getFileChooser#1)}
* new JFileChooser(getFileChooser#2) num objects <= 1
* new KFileChooser(getFileChooser#1) num objects <= 1
* new KFileChooser(getFileChooser#1).fileFilter == null
* new KFileChooser(getFileChooser#1).plugin == plugin
* new KFileChooser(getFileChooser#1).plugin != null
*/
147 return useKFileChooser(plugin) ? new KFileChooser(plugin, currentDirectoryPath, fsv) : new JFileChooser(currentDirectoryPath, fsv);
148 }
149
150 /**
151 * Constructs a FileChooser pointing to the user's default directory.
152 *
153 * @param plugin The plugin that owns this KFileChooser
154 */
155 private KFileChooser(final DCCPlugin plugin) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
156 super();
157
158 this.plugin = plugin;
159 }
160
161 /**
162 * Constructs a FileChooser using the given File as the path.
163 *
164 * @param plugin The plugin that owns this KFileChooser
165 * @param currentDirectory Directory to use as the base directory
166 */
167 private KFileChooser(final DCCPlugin plugin, final File currentDirectory) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin, File)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
168 super(currentDirectory);
169
170 this.plugin = plugin;
171 }
172
173 /**
174 * Constructs a FileChooser using the given current directory and FileSystemView.
175 *
176 * @param plugin The plugin that owns this KFileChooser
177 * @param currentDirectory Directory to use as the base directory
178 * @param fsv The FileSystemView to use
179 */
180 private KFileChooser(final DCCPlugin plugin, final File currentDirectory, final FileSystemView fsv) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin, File, FileSystemView)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
181 super(currentDirectory, fsv);
182
183 this.plugin = plugin;
184 }
185
186 /**
187 * Constructs a FileChooser using the given FileSystemView.
188 *
189 * @param plugin The plugin that owns this KFileChooser
190 * @param fsv The FileSystemView to use
191 */
192 private KFileChooser(final DCCPlugin plugin, final FileSystemView fsv) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin, FileSystemView)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
193 super(fsv);
194
195 this.plugin = plugin;
196 }
197
198 /**
199 * Constructs a FileChooser using the given path.
200 *
201 * @param plugin The plugin that owns this KFileChooser
202 * @param currentDirectoryPath Directory to use as the base directory
203 */
204 private KFileChooser(final DCCPlugin plugin, final String currentDirectoryPath) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin, String)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
205 super(currentDirectoryPath);
206
207 this.plugin = plugin;
208 }
209
210 /**
211 * Constructs a FileChooser using the given current directory path and FileSystemView.
212 *
213 * @param plugin The plugin that owns this KFileChooser
214 * @param currentDirectoryPath Directory to use as the base directory
215 * @param fsv The FileSystemView to use
216 */
217 private KFileChooser(final DCCPlugin plugin, final String currentDirectoryPath, final FileSystemView fsv) {
/*
P/P * Method: void com.dmdirc.addons.dcc.kde.KFileChooser(DCCPlugin, String, FileSystemView)
*
* Postconditions:
* this.fileFilter == null
* this.plugin == plugin
* init'ed(this.plugin)
*/
218 super(currentDirectoryPath, fsv);
219
220 this.plugin = plugin;
221 }
222
223 /**
224 * Set the file filter.
225 *
226 * @param fileFilter File filter (eg "*.php *.jpg" or null for no filter)
227 */
228 public void setKDEFileFilter(final String fileFilter) {
/*
P/P * Method: void setKDEFileFilter(String)
*
* Postconditions:
* this.fileFilter == fileFilter
* init'ed(this.fileFilter)
*/
229 this.fileFilter = fileFilter;
230 }
231
232 /**
233 * Get the file filter.
234 *
235 * @return File filter (eg "*.php *.jpg" or null for no filter)
236 */
237 public String getKDEFileFilter() {
/*
P/P * Method: String getKDEFileFilter()
*
* Preconditions:
* init'ed(this.fileFilter)
*
* Postconditions:
* return_value == this.fileFilter
* init'ed(return_value)
*/
238 return fileFilter;
239 }
240
241 /** {@inheritDoc} */
242 @Override
243 public int showOpenDialog(final Component parent) throws HeadlessException {
/*
P/P * Method: int showOpenDialog(Component)
*
* Preconditions:
* (soft) init'ed(this.fileFilter)
* (soft) this.plugin != null
*
* Presumptions:
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@263 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@268 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getDialogTitle(...)@252 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@261 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@262 != null
* ...
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* this.fileFilter: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@267: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:getDialogTitle(...)@252: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:getFileSelectionMode(...)@256: {-231..0, 2..232-1}, {1}
* com.dmdirc.addons.dcc.kde.KFileChooser:getFileSelectionMode(...)@261: {1}, {-231..0, 2..232-1}
* com.dmdirc.addons.dcc.kde.KFileChooser:getFileSelectionMode(...)@270: {1}, {-231..0, 2..232-1}
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@261: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:isMultiSelectionEnabled(...)@248: {0}, {1}
* com.dmdirc.addons.dcc.kde.KFileChooser:isMultiSelectionEnabled(...)@282: {0}, {1}
* java.lang.Process:exitValue(...)@281: {-231..-1, 1..232-1}, {0}
* ...
*/
244 if (!useKFileChooser(plugin)) {
245 return super.showOpenDialog(parent);
246 }
247 final ArrayList<String> params = new ArrayList<String>();
248 if (isMultiSelectionEnabled()) {
249 params.add("--multiple");
250 params.add("--separate-output");
251 }
252 if (getDialogTitle() != null && !getDialogTitle().isEmpty()) {
253 params.add("--caption");
254 params.add(getDialogTitle());
255 }
256 if (getFileSelectionMode() == DIRECTORIES_ONLY) {
257 params.add("--getexistingdirectory");
258 } else {
259 params.add("--getopenfilename");
260 }
261 if (getSelectedFile() != null && getFileSelectionMode() != DIRECTORIES_ONLY && !getSelectedFile().getPath().isEmpty()) {
262 if (getSelectedFile().getPath().charAt(0) != '/') {
263 params.add(getCurrentDirectory().getPath() + File.separator + getSelectedFile().getPath());
264 } else {
265 params.add(getSelectedFile().getPath());
266 }
267 } else if (getCurrentDirectory() != null) {
268 params.add(getCurrentDirectory().getPath());
269 }
270 if (getFileSelectionMode() != DIRECTORIES_ONLY && fileFilter != null && !fileFilter.isEmpty()) {
271 params.add(fileFilter);
272 }
273 KDialogProcess kdp;
274 try {
275 kdp = new KDialogProcess(params.toArray(new String[0]));
276 kdp.waitFor();
277 } catch (Exception e) {
278 return JFileChooser.ERROR_OPTION;
279 }
280
281 if (kdp.getProcess().exitValue() == 0) {
282 if (isMultiSelectionEnabled()) {
283 final List<String> list = kdp.getStdOutStream().getList();
284 final File[] fileList = new File[list.size()];
285 for (int i = 0; i < list.size(); ++i) {
286 fileList[i] = new File(list.get(i));
287 }
288 setSelectedFiles(fileList);
289 } else {
290 setSelectedFile(new File(kdp.getStdOutStream().getList().get(0)));
291 }
292 return JFileChooser.APPROVE_OPTION;
293 } else {
294 return JFileChooser.ERROR_OPTION;
295 }
296 }
297
298 /** {@inheritDoc} */
299 @Override
300 public int showSaveDialog(final Component parent) throws HeadlessException {
/*
P/P * Method: int showSaveDialog(Component)
*
* Preconditions:
* (soft) this.plugin != null
*
* Presumptions:
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@312 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@317 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getDialogTitle(...)@305 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@310 != null
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@311 != null
* ...
*
* Postconditions:
* init'ed(return_value)
*
* Test Vectors:
* com.dmdirc.addons.dcc.kde.KFileChooser:getCurrentDirectory(...)@316: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:getDialogTitle(...)@305: Addr_Set{null}, Inverse{null}
* com.dmdirc.addons.dcc.kde.KFileChooser:getSelectedFile(...)@310: Addr_Set{null}, Inverse{null}
* java.lang.Process:exitValue(...)@327: {-231..-1, 1..232-1}, {0}
* java.lang.String:charAt(...)@311: {47}, {0..46, 48..216-1}
* java.lang.String:isEmpty(...)@305: {1}, {0}
* java.lang.String:isEmpty(...)@310: {1}, {0}
*/
301 if (!useKFileChooser(plugin)) {
302 return super.showSaveDialog(parent);
303 }
304 final ArrayList<String> params = new ArrayList<String>();
305 if (getDialogTitle() != null && !getDialogTitle().isEmpty()) {
306 params.add("--caption");
307 params.add(getDialogTitle());
308 }
309 params.add("--getsavefilename");
310 if (getSelectedFile() != null && !getSelectedFile().getPath().isEmpty()) {
311 if (getSelectedFile().getPath().charAt(0) != '/') {
312 params.add(getCurrentDirectory().getPath() + File.separator + getSelectedFile().getPath());
313 } else {
314 params.add(getSelectedFile().getPath());
315 }
316 } else if (getCurrentDirectory() != null) {
317 params.add(getCurrentDirectory().getPath());
318 }
319 KDialogProcess kdp;
320 try {
321 kdp = new KDialogProcess(params.toArray(new String[0]));
322 kdp.waitFor();
323 } catch (Exception e) {
324 return JFileChooser.ERROR_OPTION;
325 }
326
327 if (kdp.getProcess().exitValue() == 0) {
328 setSelectedFile(new File(kdp.getStdOutStream().getList().get(0)));
329 return JFileChooser.APPROVE_OPTION;
330 } else {
331 return JFileChooser.ERROR_OPTION;
332 }
333 }
334 }
SofCheck Inspector Build Version : 2.17854
| KFileChooser.java |
2009-Jun-25 01:54:24 |
| KFileChooser.class |
2009-Sep-02 17:04:15 |