2
|
1 |
/*
|
|
2 |
* Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
|
15 |
* - Neither the name of Sun Microsystems nor the names of its
|
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
import javax.swing.*;
|
|
36 |
import javax.swing.filechooser.*;
|
|
37 |
import javax.swing.plaf.FileChooserUI;
|
|
38 |
import javax.swing.plaf.basic.BasicFileChooserUI;
|
|
39 |
|
|
40 |
import java.awt.*;
|
|
41 |
import java.io.File;
|
|
42 |
import java.awt.event.*;
|
|
43 |
import java.beans.*;
|
|
44 |
import java.util.Vector;
|
|
45 |
|
|
46 |
import static javax.swing.JFileChooser.*;
|
|
47 |
|
|
48 |
/**
|
|
49 |
*
|
|
50 |
* A demo which makes extensive use of the file chooser.
|
|
51 |
*
|
|
52 |
* @author Jeff Dinkins
|
|
53 |
*/
|
|
54 |
public class FileChooserDemo extends JPanel implements ActionListener {
|
|
55 |
private static JFrame frame;
|
|
56 |
|
|
57 |
private final Vector<SupportedLaF> supportedLaFs = new Vector();
|
|
58 |
|
|
59 |
private static class SupportedLaF {
|
|
60 |
private final String name;
|
|
61 |
private final LookAndFeel laf;
|
|
62 |
|
|
63 |
SupportedLaF(String name, LookAndFeel laf) {
|
|
64 |
this.name = name;
|
|
65 |
this.laf = laf;
|
|
66 |
}
|
|
67 |
|
|
68 |
public String toString() {
|
|
69 |
return name;
|
|
70 |
}
|
|
71 |
}
|
|
72 |
|
|
73 |
|
|
74 |
private JButton showButton;
|
|
75 |
|
|
76 |
private JCheckBox showAllFilesFilterCheckBox;
|
|
77 |
private JCheckBox showImageFilesFilterCheckBox;
|
|
78 |
private JCheckBox showFullDescriptionCheckBox;
|
|
79 |
|
|
80 |
private JCheckBox useFileViewCheckBox;
|
|
81 |
private JCheckBox useFileSystemViewCheckBox;
|
|
82 |
private JCheckBox accessoryCheckBox;
|
|
83 |
private JCheckBox setHiddenCheckBox;
|
|
84 |
private JCheckBox useEmbedInWizardCheckBox;
|
|
85 |
private JCheckBox useControlsCheckBox;
|
|
86 |
private JCheckBox enableDragCheckBox;
|
|
87 |
|
|
88 |
private JRadioButton singleSelectionRadioButton;
|
|
89 |
private JRadioButton multiSelectionRadioButton;
|
|
90 |
|
|
91 |
private JRadioButton openRadioButton;
|
|
92 |
private JRadioButton saveRadioButton;
|
|
93 |
private JRadioButton customButton;
|
|
94 |
|
|
95 |
private JComboBox lafComboBox;
|
|
96 |
|
|
97 |
private JRadioButton justFilesRadioButton;
|
|
98 |
private JRadioButton justDirectoriesRadioButton;
|
|
99 |
private JRadioButton bothFilesAndDirectoriesRadioButton;
|
|
100 |
|
|
101 |
private JTextField customField;
|
|
102 |
|
|
103 |
private final ExampleFileView fileView;
|
|
104 |
|
|
105 |
private final ExampleFileSystemView fileSystemView;
|
|
106 |
|
|
107 |
private final static Dimension hpad10 = new Dimension(10,1);
|
|
108 |
private final static Dimension vpad20 = new Dimension(1,20);
|
|
109 |
private final static Dimension vpad7 = new Dimension(1, 7);
|
|
110 |
private final static Dimension vpad4 = new Dimension(1, 4);
|
|
111 |
private final static Insets insets = new Insets(5, 10, 0, 10);
|
|
112 |
|
|
113 |
private final FilePreviewer previewer;
|
|
114 |
private final JFileChooser chooser;
|
|
115 |
|
|
116 |
public FileChooserDemo() {
|
|
117 |
UIManager.LookAndFeelInfo[] installedLafs = UIManager.getInstalledLookAndFeels();
|
|
118 |
for (UIManager.LookAndFeelInfo lafInfo : installedLafs) {
|
|
119 |
try {
|
|
120 |
Class lnfClass = Class.forName(lafInfo.getClassName());
|
|
121 |
LookAndFeel laf = (LookAndFeel)(lnfClass.newInstance());
|
|
122 |
if (laf.isSupportedLookAndFeel()) {
|
|
123 |
String name = lafInfo.getName();
|
|
124 |
supportedLaFs.add(new SupportedLaF(name, laf));
|
|
125 |
}
|
|
126 |
} catch (Exception e) { // If ANYTHING weird happens, don't add it
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
|
|
131 |
|
|
132 |
chooser = new JFileChooser();
|
|
133 |
previewer = new FilePreviewer(chooser);
|
|
134 |
|
|
135 |
// Create Custom FileView
|
|
136 |
fileView = new ExampleFileView();
|
|
137 |
fileView.putIcon("jpg", new ImageIcon(getClass().getResource("/resources/images/jpgIcon.jpg")));
|
|
138 |
fileView.putIcon("gif", new ImageIcon(getClass().getResource("/resources/images/gifIcon.gif")));
|
|
139 |
|
|
140 |
// Create Custom FileSystemView
|
|
141 |
fileSystemView = new ExampleFileSystemView();
|
|
142 |
|
|
143 |
// create a radio listener to listen to option changes
|
|
144 |
OptionListener optionListener = new OptionListener();
|
|
145 |
|
|
146 |
// Create options
|
|
147 |
openRadioButton = new JRadioButton("Open");
|
|
148 |
openRadioButton.setSelected(true);
|
|
149 |
openRadioButton.addActionListener(optionListener);
|
|
150 |
|
|
151 |
saveRadioButton = new JRadioButton("Save");
|
|
152 |
saveRadioButton.addActionListener(optionListener);
|
|
153 |
|
|
154 |
customButton = new JRadioButton("Custom");
|
|
155 |
customButton.addActionListener(optionListener);
|
|
156 |
|
|
157 |
customField = new JTextField(8) {
|
|
158 |
public Dimension getMaximumSize() {
|
|
159 |
return new Dimension(getPreferredSize().width, getPreferredSize().height);
|
|
160 |
}
|
|
161 |
};
|
|
162 |
customField.setText("Doit");
|
|
163 |
customField.setAlignmentY(JComponent.TOP_ALIGNMENT);
|
|
164 |
customField.setEnabled(false);
|
|
165 |
customField.addActionListener(optionListener);
|
|
166 |
|
|
167 |
ButtonGroup group1 = new ButtonGroup();
|
|
168 |
group1.add(openRadioButton);
|
|
169 |
group1.add(saveRadioButton);
|
|
170 |
group1.add(customButton);
|
|
171 |
|
|
172 |
// filter buttons
|
|
173 |
showAllFilesFilterCheckBox = new JCheckBox("Show \"All Files\" Filter");
|
|
174 |
showAllFilesFilterCheckBox.addActionListener(optionListener);
|
|
175 |
showAllFilesFilterCheckBox.setSelected(true);
|
|
176 |
|
|
177 |
showImageFilesFilterCheckBox = new JCheckBox("Show JPG and GIF Filters");
|
|
178 |
showImageFilesFilterCheckBox.addActionListener(optionListener);
|
|
179 |
showImageFilesFilterCheckBox.setSelected(false);
|
|
180 |
|
|
181 |
accessoryCheckBox = new JCheckBox("Show Preview");
|
|
182 |
accessoryCheckBox.addActionListener(optionListener);
|
|
183 |
accessoryCheckBox.setSelected(false);
|
|
184 |
|
|
185 |
// more options
|
|
186 |
setHiddenCheckBox = new JCheckBox("Show Hidden Files");
|
|
187 |
setHiddenCheckBox.addActionListener(optionListener);
|
|
188 |
|
|
189 |
showFullDescriptionCheckBox = new JCheckBox("With File Extensions");
|
|
190 |
showFullDescriptionCheckBox.addActionListener(optionListener);
|
|
191 |
showFullDescriptionCheckBox.setSelected(true);
|
|
192 |
showFullDescriptionCheckBox.setEnabled(false);
|
|
193 |
|
|
194 |
useFileViewCheckBox = new JCheckBox("Use FileView");
|
|
195 |
useFileViewCheckBox.addActionListener(optionListener);
|
|
196 |
useFileViewCheckBox.setSelected(false);
|
|
197 |
|
|
198 |
useFileSystemViewCheckBox = new JCheckBox("Use FileSystemView", false);
|
|
199 |
useFileSystemViewCheckBox.addActionListener(optionListener);
|
|
200 |
|
|
201 |
useEmbedInWizardCheckBox = new JCheckBox("Embed in Wizard");
|
|
202 |
useEmbedInWizardCheckBox.addActionListener(optionListener);
|
|
203 |
useEmbedInWizardCheckBox.setSelected(false);
|
|
204 |
|
|
205 |
useControlsCheckBox = new JCheckBox("Show Control Buttons");
|
|
206 |
useControlsCheckBox.addActionListener(optionListener);
|
|
207 |
useControlsCheckBox.setSelected(true);
|
|
208 |
|
|
209 |
enableDragCheckBox = new JCheckBox("Enable Dragging");
|
|
210 |
enableDragCheckBox.addActionListener(optionListener);
|
|
211 |
|
|
212 |
// File or Directory chooser options
|
|
213 |
ButtonGroup group3 = new ButtonGroup();
|
|
214 |
justFilesRadioButton = new JRadioButton("Just Select Files");
|
|
215 |
justFilesRadioButton.setSelected(true);
|
|
216 |
group3.add(justFilesRadioButton);
|
|
217 |
justFilesRadioButton.addActionListener(optionListener);
|
|
218 |
|
|
219 |
justDirectoriesRadioButton = new JRadioButton("Just Select Directories");
|
|
220 |
group3.add(justDirectoriesRadioButton);
|
|
221 |
justDirectoriesRadioButton.addActionListener(optionListener);
|
|
222 |
|
|
223 |
bothFilesAndDirectoriesRadioButton = new JRadioButton("Select Files or Directories");
|
|
224 |
group3.add(bothFilesAndDirectoriesRadioButton);
|
|
225 |
bothFilesAndDirectoriesRadioButton.addActionListener(optionListener);
|
|
226 |
|
|
227 |
singleSelectionRadioButton = new JRadioButton("Single Selection", true);
|
|
228 |
singleSelectionRadioButton.addActionListener(optionListener);
|
|
229 |
|
|
230 |
multiSelectionRadioButton = new JRadioButton("Multi Selection");
|
|
231 |
multiSelectionRadioButton.addActionListener(optionListener);
|
|
232 |
|
|
233 |
ButtonGroup group4 = new ButtonGroup();
|
|
234 |
group4.add(singleSelectionRadioButton);
|
|
235 |
group4.add(multiSelectionRadioButton);
|
|
236 |
|
|
237 |
|
|
238 |
// Create show button
|
|
239 |
showButton = new JButton("Show FileChooser");
|
|
240 |
showButton.addActionListener(this);
|
|
241 |
showButton.setMnemonic('s');
|
|
242 |
|
|
243 |
// Create laf combo box
|
|
244 |
lafComboBox = new JComboBox(supportedLaFs);
|
|
245 |
lafComboBox.setEditable(false);
|
|
246 |
lafComboBox.addActionListener(optionListener);
|
|
247 |
|
|
248 |
// ********************************************************
|
|
249 |
// ******************** Dialog Type ***********************
|
|
250 |
// ********************************************************
|
|
251 |
JPanel control1 = new InsetPanel(insets);
|
|
252 |
control1.setBorder(BorderFactory.createTitledBorder("Dialog Type"));
|
|
253 |
|
|
254 |
control1.setLayout(new BoxLayout(control1, BoxLayout.Y_AXIS));
|
|
255 |
control1.add(Box.createRigidArea(vpad20));
|
|
256 |
control1.add(openRadioButton);
|
|
257 |
control1.add(Box.createRigidArea(vpad7));
|
|
258 |
control1.add(saveRadioButton);
|
|
259 |
control1.add(Box.createRigidArea(vpad7));
|
|
260 |
control1.add(customButton);
|
|
261 |
control1.add(Box.createRigidArea(vpad4));
|
|
262 |
JPanel fieldWrapper = new JPanel();
|
|
263 |
fieldWrapper.setLayout(new BoxLayout(fieldWrapper, BoxLayout.X_AXIS));
|
|
264 |
fieldWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
|
|
265 |
fieldWrapper.add(Box.createRigidArea(hpad10));
|
|
266 |
fieldWrapper.add(Box.createRigidArea(hpad10));
|
|
267 |
fieldWrapper.add(customField);
|
|
268 |
control1.add(fieldWrapper);
|
|
269 |
control1.add(Box.createRigidArea(vpad20));
|
|
270 |
control1.add(Box.createGlue());
|
|
271 |
|
|
272 |
// ********************************************************
|
|
273 |
// ***************** Filter Controls **********************
|
|
274 |
// ********************************************************
|
|
275 |
JPanel control2 = new InsetPanel(insets);
|
|
276 |
control2.setBorder(BorderFactory.createTitledBorder("Filter Controls"));
|
|
277 |
control2.setLayout(new BoxLayout(control2, BoxLayout.Y_AXIS));
|
|
278 |
control2.add(Box.createRigidArea(vpad20));
|
|
279 |
control2.add(showAllFilesFilterCheckBox);
|
|
280 |
control2.add(Box.createRigidArea(vpad7));
|
|
281 |
control2.add(showImageFilesFilterCheckBox);
|
|
282 |
control2.add(Box.createRigidArea(vpad4));
|
|
283 |
JPanel checkWrapper = new JPanel();
|
|
284 |
checkWrapper.setLayout(new BoxLayout(checkWrapper, BoxLayout.X_AXIS));
|
|
285 |
checkWrapper.setAlignmentX(Component.LEFT_ALIGNMENT);
|
|
286 |
checkWrapper.add(Box.createRigidArea(hpad10));
|
|
287 |
checkWrapper.add(Box.createRigidArea(hpad10));
|
|
288 |
checkWrapper.add(showFullDescriptionCheckBox);
|
|
289 |
control2.add(checkWrapper);
|
|
290 |
control2.add(Box.createRigidArea(vpad20));
|
|
291 |
control2.add(Box.createGlue());
|
|
292 |
|
|
293 |
// ********************************************************
|
|
294 |
// ****************** Display Options *********************
|
|
295 |
// ********************************************************
|
|
296 |
JPanel control3 = new InsetPanel(insets);
|
|
297 |
control3.setBorder(BorderFactory.createTitledBorder("Display Options"));
|
|
298 |
control3.setLayout(new BoxLayout(control3, BoxLayout.Y_AXIS));
|
|
299 |
control3.add(Box.createRigidArea(vpad20));
|
|
300 |
control3.add(setHiddenCheckBox);
|
|
301 |
control3.add(Box.createRigidArea(vpad7));
|
|
302 |
control3.add(useFileViewCheckBox);
|
|
303 |
control3.add(Box.createRigidArea(vpad7));
|
|
304 |
control3.add(useFileSystemViewCheckBox);
|
|
305 |
control3.add(Box.createRigidArea(vpad7));
|
|
306 |
control3.add(accessoryCheckBox);
|
|
307 |
control3.add(Box.createRigidArea(vpad7));
|
|
308 |
control3.add(useEmbedInWizardCheckBox);
|
|
309 |
control3.add(Box.createRigidArea(vpad7));
|
|
310 |
control3.add(useControlsCheckBox);
|
|
311 |
control3.add(Box.createRigidArea(vpad7));
|
|
312 |
control3.add(enableDragCheckBox);
|
|
313 |
control3.add(Box.createRigidArea(vpad20));
|
|
314 |
control3.add(Box.createGlue());
|
|
315 |
|
|
316 |
// ********************************************************
|
|
317 |
// ************* File & Directory Options *****************
|
|
318 |
// ********************************************************
|
|
319 |
JPanel control4 = new InsetPanel(insets);
|
|
320 |
control4.setBorder(BorderFactory.createTitledBorder("File and Directory Options"));
|
|
321 |
control4.setLayout(new BoxLayout(control4, BoxLayout.Y_AXIS));
|
|
322 |
control4.add(Box.createRigidArea(vpad20));
|
|
323 |
control4.add(justFilesRadioButton);
|
|
324 |
control4.add(Box.createRigidArea(vpad7));
|
|
325 |
control4.add(justDirectoriesRadioButton);
|
|
326 |
control4.add(Box.createRigidArea(vpad7));
|
|
327 |
control4.add(bothFilesAndDirectoriesRadioButton);
|
|
328 |
control4.add(Box.createRigidArea(vpad20));
|
|
329 |
control4.add(singleSelectionRadioButton);
|
|
330 |
control4.add(Box.createRigidArea(vpad7));
|
|
331 |
control4.add(multiSelectionRadioButton);
|
|
332 |
control4.add(Box.createRigidArea(vpad20));
|
|
333 |
control4.add(Box.createGlue());
|
|
334 |
|
|
335 |
|
|
336 |
// ********************************************************
|
|
337 |
// **************** Look & Feel Switch ********************
|
|
338 |
// ********************************************************
|
|
339 |
JPanel panel = new JPanel();
|
|
340 |
panel.add(new JLabel("Look and Feel: "));
|
|
341 |
panel.add(lafComboBox);
|
|
342 |
panel.add(showButton);
|
|
343 |
|
|
344 |
// ********************************************************
|
|
345 |
// ****************** Wrap 'em all up *********************
|
|
346 |
// ********************************************************
|
|
347 |
JPanel wrapper = new JPanel();
|
|
348 |
wrapper.setLayout(new BoxLayout(wrapper, BoxLayout.X_AXIS));
|
|
349 |
|
|
350 |
add(Box.createRigidArea(vpad20));
|
|
351 |
|
|
352 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
353 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
354 |
wrapper.add(control1);
|
|
355 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
356 |
wrapper.add(control2);
|
|
357 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
358 |
wrapper.add(control3);
|
|
359 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
360 |
wrapper.add(control4);
|
|
361 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
362 |
wrapper.add(Box.createRigidArea(hpad10));
|
|
363 |
|
|
364 |
add(wrapper);
|
|
365 |
add(Box.createRigidArea(vpad20));
|
|
366 |
add(panel);
|
|
367 |
add(Box.createRigidArea(vpad20));
|
|
368 |
}
|
|
369 |
|
|
370 |
public void actionPerformed(ActionEvent e) {
|
|
371 |
if (customButton.isSelected()) {
|
|
372 |
chooser.setApproveButtonText(customField.getText());
|
|
373 |
}
|
|
374 |
if (chooser.isMultiSelectionEnabled()) {
|
|
375 |
chooser.setSelectedFiles(null);
|
|
376 |
} else {
|
|
377 |
chooser.setSelectedFile(null);
|
|
378 |
}
|
|
379 |
// clear the preview from the previous display of the chooser
|
|
380 |
JComponent accessory = chooser.getAccessory();
|
|
381 |
if (accessory != null) {
|
|
382 |
((FilePreviewer)accessory).loadImage(null);
|
|
383 |
}
|
|
384 |
|
|
385 |
if (useEmbedInWizardCheckBox.isSelected()) {
|
|
386 |
WizardDialog wizard = new WizardDialog(frame, true);
|
|
387 |
wizard.setVisible(true);
|
|
388 |
wizard.dispose();
|
|
389 |
return;
|
|
390 |
}
|
|
391 |
|
|
392 |
int retval = chooser.showDialog(frame, null);
|
|
393 |
if (retval == APPROVE_OPTION) {
|
|
394 |
JOptionPane.showMessageDialog(frame, getResultString());
|
|
395 |
} else if (retval == CANCEL_OPTION) {
|
|
396 |
JOptionPane.showMessageDialog(frame, "User cancelled operation. No file was chosen.");
|
|
397 |
} else if (retval == ERROR_OPTION) {
|
|
398 |
JOptionPane.showMessageDialog(frame, "An error occured. No file was chosen.");
|
|
399 |
} else {
|
|
400 |
JOptionPane.showMessageDialog(frame, "Unknown operation occured.");
|
|
401 |
}
|
|
402 |
}
|
|
403 |
|
|
404 |
private void resetFileFilters(boolean enableFilters,
|
|
405 |
boolean showExtensionInDescription) {
|
|
406 |
chooser.resetChoosableFileFilters();
|
|
407 |
if (enableFilters) {
|
|
408 |
FileFilter jpgFilter = createFileFilter("JPEG Compressed Image Files",
|
|
409 |
showExtensionInDescription, "jpg");
|
|
410 |
FileFilter gifFilter = createFileFilter("GIF Image Files",
|
|
411 |
showExtensionInDescription, "gif");
|
|
412 |
FileFilter bothFilter = createFileFilter("JPEG and GIF Image Files",
|
|
413 |
showExtensionInDescription, "jpg",
|
|
414 |
"gif");
|
|
415 |
chooser.addChoosableFileFilter(bothFilter);
|
|
416 |
chooser.addChoosableFileFilter(jpgFilter);
|
|
417 |
chooser.addChoosableFileFilter(gifFilter);
|
|
418 |
}
|
|
419 |
}
|
|
420 |
|
|
421 |
private FileFilter createFileFilter(String description,
|
|
422 |
boolean showExtensionInDescription, String...extensions) {
|
|
423 |
if (showExtensionInDescription) {
|
|
424 |
description = createFileNameFilterDescriptionFromExtensions(
|
|
425 |
description, extensions);
|
|
426 |
}
|
|
427 |
return new FileNameExtensionFilter(description, extensions);
|
|
428 |
}
|
|
429 |
|
|
430 |
private String createFileNameFilterDescriptionFromExtensions(
|
|
431 |
String description, String[] extensions) {
|
|
432 |
String fullDescription = (description == null) ?
|
|
433 |
"(" : description + " (";
|
|
434 |
// build the description from the extension list
|
|
435 |
fullDescription += "." + extensions[0];
|
|
436 |
for (int i = 1; i < extensions.length; i++) {
|
|
437 |
fullDescription += ", .";
|
|
438 |
fullDescription += extensions[i];
|
|
439 |
}
|
|
440 |
fullDescription += ")";
|
|
441 |
return fullDescription;
|
|
442 |
}
|
|
443 |
|
|
444 |
private class WizardDialog extends JDialog implements ActionListener {
|
|
445 |
CardLayout cardLayout;
|
|
446 |
JPanel cardPanel;
|
|
447 |
JLabel messageLabel;
|
|
448 |
JButton backButton, nextButton, closeButton;
|
|
449 |
|
|
450 |
WizardDialog(JFrame frame, boolean modal) {
|
|
451 |
super(frame, "Embedded JFileChooser Demo", modal);
|
|
452 |
|
|
453 |
cardLayout = new CardLayout();
|
|
454 |
cardPanel = new JPanel(cardLayout);
|
|
455 |
getContentPane().add(cardPanel, BorderLayout.CENTER);
|
|
456 |
|
|
457 |
messageLabel = new JLabel("", JLabel.CENTER);
|
|
458 |
cardPanel.add(chooser, "fileChooser");
|
|
459 |
cardPanel.add(messageLabel, "label");
|
|
460 |
cardLayout.show(cardPanel, "fileChooser");
|
|
461 |
chooser.addActionListener(this);
|
|
462 |
|
|
463 |
JPanel buttonPanel = new JPanel();
|
|
464 |
backButton = new JButton("< Back");
|
|
465 |
nextButton = new JButton("Next >");
|
|
466 |
closeButton = new JButton("Close");
|
|
467 |
|
|
468 |
buttonPanel.add(backButton);
|
|
469 |
buttonPanel.add(nextButton);
|
|
470 |
buttonPanel.add(closeButton);
|
|
471 |
|
|
472 |
getContentPane().add(buttonPanel, BorderLayout.SOUTH);
|
|
473 |
|
|
474 |
backButton.setEnabled(false);
|
|
475 |
getRootPane().setDefaultButton(nextButton);
|
|
476 |
|
|
477 |
backButton.addActionListener(this);
|
|
478 |
nextButton.addActionListener(this);
|
|
479 |
closeButton.addActionListener(this);
|
|
480 |
|
|
481 |
pack();
|
|
482 |
setLocationRelativeTo(frame);
|
|
483 |
}
|
|
484 |
|
|
485 |
public void actionPerformed(ActionEvent evt) {
|
|
486 |
Object src = evt.getSource();
|
|
487 |
String cmd = evt.getActionCommand();
|
|
488 |
|
|
489 |
if (src == backButton) {
|
|
490 |
back();
|
|
491 |
} else if (src == nextButton) {
|
|
492 |
FileChooserUI ui = chooser.getUI();
|
|
493 |
if (ui instanceof BasicFileChooserUI) {
|
|
494 |
// Workaround for bug 4528663. This is necessary to
|
|
495 |
// pick up the contents of the file chooser text field.
|
|
496 |
// This will trigger an APPROVE_SELECTION action.
|
|
497 |
((BasicFileChooserUI)ui).getApproveSelectionAction().actionPerformed(null);
|
|
498 |
} else {
|
|
499 |
next();
|
|
500 |
}
|
|
501 |
} else if (src == closeButton) {
|
|
502 |
close();
|
|
503 |
} else if (cmd == APPROVE_SELECTION) {
|
|
504 |
next();
|
|
505 |
} else if (cmd == CANCEL_SELECTION) {
|
|
506 |
close();
|
|
507 |
}
|
|
508 |
}
|
|
509 |
|
|
510 |
private void back() {
|
|
511 |
backButton.setEnabled(false);
|
|
512 |
nextButton.setEnabled(true);
|
|
513 |
cardLayout.show(cardPanel, "fileChooser");
|
|
514 |
getRootPane().setDefaultButton(nextButton);
|
|
515 |
chooser.requestFocus();
|
|
516 |
}
|
|
517 |
|
|
518 |
private void next() {
|
|
519 |
backButton.setEnabled(true);
|
|
520 |
nextButton.setEnabled(false);
|
|
521 |
messageLabel.setText(getResultString());
|
|
522 |
cardLayout.show(cardPanel, "label");
|
|
523 |
getRootPane().setDefaultButton(closeButton);
|
|
524 |
closeButton.requestFocus();
|
|
525 |
}
|
|
526 |
|
|
527 |
private void close() {
|
|
528 |
setVisible(false);
|
|
529 |
}
|
|
530 |
|
|
531 |
public void dispose() {
|
|
532 |
chooser.removeActionListener(this);
|
|
533 |
|
|
534 |
// The chooser is hidden by CardLayout on remove
|
|
535 |
// so fix it here
|
|
536 |
cardPanel.remove(chooser);
|
|
537 |
chooser.setVisible(true);
|
|
538 |
|
|
539 |
super.dispose();
|
|
540 |
}
|
|
541 |
}
|
|
542 |
|
|
543 |
private String getResultString() {
|
|
544 |
String resultString;
|
|
545 |
String filter = chooser.getFileFilter().getDescription();
|
|
546 |
String path = null;
|
|
547 |
boolean isDirMode = (chooser.getFileSelectionMode() == DIRECTORIES_ONLY);
|
|
548 |
boolean isMulti = chooser.isMultiSelectionEnabled();
|
|
549 |
|
|
550 |
if (isMulti) {
|
|
551 |
File [] files = chooser.getSelectedFiles();
|
|
552 |
if (files != null && files.length > 0) {
|
|
553 |
path = "";
|
|
554 |
for (File file : files) {
|
|
555 |
path = path + "<br>" + file.getPath();
|
|
556 |
}
|
|
557 |
}
|
|
558 |
} else {
|
|
559 |
File file = chooser.getSelectedFile();
|
|
560 |
if (file != null) {
|
|
561 |
path = "<br>" + file.getPath();
|
|
562 |
}
|
|
563 |
}
|
|
564 |
if (path != null) {
|
|
565 |
path = path.replace(" ", " ");
|
|
566 |
filter = filter.replace(" ", " ");
|
|
567 |
resultString =
|
|
568 |
"<html>You chose " + (isMulti ? "these" : "this") + " " +
|
|
569 |
(isDirMode ? (isMulti ? "directories" : "directory")
|
|
570 |
: (isMulti ? "files" : "file")) +
|
|
571 |
": <code>" + path +
|
|
572 |
"</code><br><br>with filter: <br><code>" + filter;
|
|
573 |
} else {
|
|
574 |
resultString = "Nothing was chosen";
|
|
575 |
}
|
|
576 |
return resultString;
|
|
577 |
}
|
|
578 |
|
|
579 |
|
|
580 |
|
|
581 |
|
|
582 |
/** An ActionListener that listens to the radio buttons. */
|
|
583 |
private class OptionListener implements ActionListener {
|
|
584 |
public void actionPerformed(ActionEvent e) {
|
|
585 |
JComponent c = (JComponent) e.getSource();
|
|
586 |
boolean selected = false;
|
|
587 |
if (c instanceof JToggleButton) {
|
|
588 |
selected = ((JToggleButton)c).isSelected();
|
|
589 |
}
|
|
590 |
|
|
591 |
if (c == openRadioButton) {
|
|
592 |
chooser.setDialogType(OPEN_DIALOG);
|
|
593 |
customField.setEnabled(false);
|
|
594 |
repaint();
|
|
595 |
} else if (c == useEmbedInWizardCheckBox) {
|
|
596 |
useControlsCheckBox.setEnabled(!selected);
|
|
597 |
useControlsCheckBox.setSelected(!selected);
|
|
598 |
chooser.setControlButtonsAreShown(!selected);
|
|
599 |
} else if (c == useControlsCheckBox) {
|
|
600 |
chooser.setControlButtonsAreShown(selected);
|
|
601 |
} else if (c == enableDragCheckBox) {
|
|
602 |
chooser.setDragEnabled(selected);
|
|
603 |
} else if (c == saveRadioButton) {
|
|
604 |
chooser.setDialogType(SAVE_DIALOG);
|
|
605 |
customField.setEnabled(false);
|
|
606 |
repaint();
|
|
607 |
} else if (c == customButton || c == customField) {
|
|
608 |
customField.setEnabled(true);
|
|
609 |
chooser.setDialogType(CUSTOM_DIALOG);
|
|
610 |
repaint();
|
|
611 |
} else if (c == showAllFilesFilterCheckBox) {
|
|
612 |
chooser.setAcceptAllFileFilterUsed(selected);
|
|
613 |
} else if (c == showImageFilesFilterCheckBox) {
|
|
614 |
resetFileFilters(selected,
|
|
615 |
showFullDescriptionCheckBox.isSelected());
|
|
616 |
showFullDescriptionCheckBox.setEnabled(selected);
|
|
617 |
} else if (c == setHiddenCheckBox) {
|
|
618 |
chooser.setFileHidingEnabled(!selected);
|
|
619 |
} else if (c == accessoryCheckBox) {
|
|
620 |
if (selected) {
|
|
621 |
chooser.setAccessory(previewer);
|
|
622 |
} else {
|
|
623 |
chooser.setAccessory(null);
|
|
624 |
}
|
|
625 |
} else if (c == useFileViewCheckBox) {
|
|
626 |
if (selected) {
|
|
627 |
chooser.setFileView(fileView);
|
|
628 |
} else {
|
|
629 |
chooser.setFileView(null);
|
|
630 |
}
|
|
631 |
} else if (c == useFileSystemViewCheckBox) {
|
|
632 |
if (selected) {
|
|
633 |
chooser.setFileSystemView(fileSystemView);
|
|
634 |
} else {
|
|
635 |
// Restore default behaviour
|
|
636 |
chooser.setFileSystemView(FileSystemView.getFileSystemView());
|
|
637 |
}
|
|
638 |
} else if (c == showFullDescriptionCheckBox) {
|
|
639 |
resetFileFilters(showImageFilesFilterCheckBox.isSelected(),
|
|
640 |
selected);
|
|
641 |
} else if (c == justFilesRadioButton) {
|
|
642 |
chooser.setFileSelectionMode(FILES_ONLY);
|
|
643 |
} else if (c == justDirectoriesRadioButton) {
|
|
644 |
chooser.setFileSelectionMode(DIRECTORIES_ONLY);
|
|
645 |
} else if (c == bothFilesAndDirectoriesRadioButton) {
|
|
646 |
chooser.setFileSelectionMode(FILES_AND_DIRECTORIES);
|
|
647 |
} else if (c == singleSelectionRadioButton) {
|
|
648 |
if (selected) {
|
|
649 |
chooser.setMultiSelectionEnabled(false);
|
|
650 |
}
|
|
651 |
} else if (c == multiSelectionRadioButton) {
|
|
652 |
if (selected) {
|
|
653 |
chooser.setMultiSelectionEnabled(true);
|
|
654 |
}
|
|
655 |
} else if (c == lafComboBox) {
|
|
656 |
SupportedLaF supportedLaF = ((SupportedLaF)lafComboBox.getSelectedItem());
|
|
657 |
LookAndFeel laf = supportedLaF.laf;
|
|
658 |
try {
|
|
659 |
UIManager.setLookAndFeel(laf);
|
|
660 |
SwingUtilities.updateComponentTreeUI(frame);
|
|
661 |
if(chooser != null) {
|
|
662 |
SwingUtilities.updateComponentTreeUI(chooser);
|
|
663 |
}
|
|
664 |
frame.pack();
|
|
665 |
} catch (UnsupportedLookAndFeelException exc) {
|
|
666 |
// This should not happen because we already checked
|
|
667 |
((DefaultComboBoxModel)lafComboBox.getModel()).removeElement(supportedLaF);
|
|
668 |
}
|
|
669 |
}
|
|
670 |
|
|
671 |
}
|
|
672 |
}
|
|
673 |
|
|
674 |
private class FilePreviewer extends JComponent implements PropertyChangeListener {
|
|
675 |
ImageIcon thumbnail = null;
|
|
676 |
|
|
677 |
public FilePreviewer(JFileChooser fc) {
|
|
678 |
setPreferredSize(new Dimension(100, 50));
|
|
679 |
fc.addPropertyChangeListener(this);
|
|
680 |
}
|
|
681 |
|
|
682 |
public void loadImage(File f) {
|
|
683 |
if (f == null) {
|
|
684 |
thumbnail = null;
|
|
685 |
} else {
|
|
686 |
ImageIcon tmpIcon = new ImageIcon(f.getPath());
|
|
687 |
if(tmpIcon.getIconWidth() > 90) {
|
|
688 |
thumbnail = new ImageIcon(
|
|
689 |
tmpIcon.getImage().getScaledInstance(90, -1, Image.SCALE_DEFAULT));
|
|
690 |
} else {
|
|
691 |
thumbnail = tmpIcon;
|
|
692 |
}
|
|
693 |
}
|
|
694 |
}
|
|
695 |
|
|
696 |
public void propertyChange(PropertyChangeEvent e) {
|
|
697 |
String prop = e.getPropertyName();
|
|
698 |
if (prop == SELECTED_FILE_CHANGED_PROPERTY) {
|
|
699 |
if(isShowing()) {
|
|
700 |
loadImage((File) e.getNewValue());
|
|
701 |
repaint();
|
|
702 |
}
|
|
703 |
}
|
|
704 |
}
|
|
705 |
|
|
706 |
public void paint(Graphics g) {
|
|
707 |
if(thumbnail != null) {
|
|
708 |
int x = getWidth()/2 - thumbnail.getIconWidth()/2;
|
|
709 |
int y = getHeight()/2 - thumbnail.getIconHeight()/2;
|
|
710 |
if(y < 0) {
|
|
711 |
y = 0;
|
|
712 |
}
|
|
713 |
|
|
714 |
if(x < 5) {
|
|
715 |
x = 5;
|
|
716 |
}
|
|
717 |
thumbnail.paintIcon(this, g, x, y);
|
|
718 |
}
|
|
719 |
}
|
|
720 |
}
|
|
721 |
|
|
722 |
public static void main(String s[]) {
|
|
723 |
/*
|
|
724 |
NOTE: By default, the look and feel will be set to the
|
|
725 |
Cross Platform Look and Feel (which is currently Metal).
|
|
726 |
The user may someday be able to override the default
|
|
727 |
via a system property. If you as the developer want to
|
|
728 |
be sure that a particular L&F is set, you can do so
|
|
729 |
by calling UIManager.setLookAndFeel(). For example, the
|
|
730 |
first code snippet below forcibly sets the UI to be the
|
|
731 |
System Look and Feel. The second code snippet forcibly
|
|
732 |
sets the look and feel to the Cross Platform L&F.
|
|
733 |
|
|
734 |
Snippet 1:
|
|
735 |
try {
|
|
736 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
737 |
} catch (Exception exc) {
|
|
738 |
System.err.println("Error loading L&F: " + exc);
|
|
739 |
}
|
|
740 |
|
|
741 |
Snippet 2:
|
|
742 |
try {
|
|
743 |
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
|
|
744 |
} catch (Exception exc) {
|
|
745 |
System.err.println("Error loading L&F: " + exc);
|
|
746 |
}
|
|
747 |
*/
|
|
748 |
|
|
749 |
FileChooserDemo panel = new FileChooserDemo();
|
|
750 |
|
|
751 |
frame = new JFrame("FileChooserDemo");
|
|
752 |
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
|
753 |
frame.getContentPane().add("Center", panel);
|
|
754 |
frame.pack();
|
|
755 |
frame.setVisible(true);
|
|
756 |
}
|
|
757 |
|
|
758 |
private static class InsetPanel extends JPanel {
|
|
759 |
Insets i;
|
|
760 |
InsetPanel(Insets i) {
|
|
761 |
this.i = i;
|
|
762 |
}
|
|
763 |
public Insets getInsets() {
|
|
764 |
return i;
|
|
765 |
}
|
|
766 |
}
|
|
767 |
}
|