Lines 46-51
Link Here
|
46 |
import java.awt.HeadlessException; |
46 |
import java.awt.HeadlessException; |
47 |
import java.awt.KeyboardFocusManager; |
47 |
import java.awt.KeyboardFocusManager; |
48 |
import java.io.File; |
48 |
import java.io.File; |
|
|
49 |
import java.util.ArrayList; |
50 |
import java.util.List; |
49 |
import javax.swing.Icon; |
51 |
import javax.swing.Icon; |
50 |
import javax.swing.JFileChooser; |
52 |
import javax.swing.JFileChooser; |
51 |
import javax.swing.filechooser.FileFilter; |
53 |
import javax.swing.filechooser.FileFilter; |
Lines 113-118
Link Here
|
113 |
private boolean filesOnly; |
115 |
private boolean filesOnly; |
114 |
private static final boolean DONT_STORE_DIRECTORIES = |
116 |
private static final boolean DONT_STORE_DIRECTORIES = |
115 |
Boolean.getBoolean("forget.recent.dirs"); |
117 |
Boolean.getBoolean("forget.recent.dirs"); |
|
|
118 |
private SelectionApprover approver; |
119 |
private final List<FileFilter> filters = new ArrayList<FileFilter>(3); |
116 |
/** |
120 |
/** |
117 |
* Create a new FileChooserBuilder using the name of the passed class |
121 |
* Create a new FileChooserBuilder using the name of the passed class |
118 |
* as the metadata for looking up a starting directory from previous |
122 |
* as the metadata for looking up a starting directory from previous |
Lines 248-254
Link Here
|
248 |
* @return A file chooser |
252 |
* @return A file chooser |
249 |
*/ |
253 |
*/ |
250 |
public JFileChooser createFileChooser() { |
254 |
public JFileChooser createFileChooser() { |
251 |
JFileChooser result = new SavedDirFileChooser(dirKey, failoverDir, force); |
255 |
JFileChooser result = new SavedDirFileChooser(dirKey, failoverDir, |
|
|
256 |
force, approver); |
252 |
prepareFileChooser(result); |
257 |
prepareFileChooser(result); |
253 |
return result; |
258 |
return result; |
254 |
} |
259 |
} |
Lines 362-373
Link Here
|
362 |
if (aDescription != null) { |
367 |
if (aDescription != null) { |
363 |
chooser.getAccessibleContext().setAccessibleDescription(aDescription); |
368 |
chooser.getAccessibleContext().setAccessibleDescription(aDescription); |
364 |
} |
369 |
} |
|
|
370 |
if (!filters.isEmpty()) { |
371 |
for (FileFilter f : filters) { |
372 |
chooser.addChoosableFileFilter(f); |
373 |
} |
374 |
} |
375 |
} |
376 |
|
377 |
/** |
378 |
* Equivalent to calling <code>JFileChooser.addChoosableFileFilter(filter)</code>. |
379 |
* Adds another file filter that can be displayed in the file filters combo |
380 |
* box in the file chooser. |
381 |
* |
382 |
* @param filter The file filter to add |
383 |
* @return this |
384 |
* @since 7.26.0 |
385 |
*/ |
386 |
public FileChooserBuilder addFileFilter (FileFilter filter) { |
387 |
filters.add (filter); |
388 |
return this; |
389 |
} |
390 |
|
391 |
/** |
392 |
* Set a selection approver which can display an "Overwrite file?" |
393 |
* or similar dialog if necessary, when the user presses the accept button |
394 |
* in the file chooser dialog. |
395 |
* |
396 |
* @param approver A SelectionApprover which will determine if the selection |
397 |
* is valid |
398 |
* @return this |
399 |
* @since 7.26.0 |
400 |
*/ |
401 |
public FileChooserBuilder setSelectionApprover (SelectionApprover approver) { |
402 |
this.approver = approver; |
403 |
return this; |
404 |
} |
405 |
|
406 |
/** |
407 |
* Object which can approve the selection (enabling the OK button or |
408 |
* equivalent) in a JFileChooser. Equivalent to overriding |
409 |
* <code>JFileChooser.approveSelection()</code> |
410 |
* @since 7.26.0 |
411 |
*/ |
412 |
public interface SelectionApprover { |
413 |
/** |
414 |
* Approve the selection, enabling the dialog to be closed. Called by |
415 |
* the JFileChooser's <code>approveSelection()</code> method. Use this |
416 |
* interface if you want to, for example, show a dialog asking |
417 |
* "Overwrite File X?" or similar. |
418 |
* |
419 |
* @param selection The selected file(s) at the time the user presses |
420 |
* the Open, Save or OK button |
421 |
* @return true if the selection is accepted, false if it is not and |
422 |
* the dialog should not be closed |
423 |
*/ |
424 |
public boolean approve (File[] selection); |
365 |
} |
425 |
} |
366 |
|
426 |
|
367 |
private static final class SavedDirFileChooser extends JFileChooser { |
427 |
private static final class SavedDirFileChooser extends JFileChooser { |
368 |
private final String dirKey; |
428 |
private final String dirKey; |
369 |
SavedDirFileChooser(String dirKey, File failoverDir, boolean force) { |
429 |
private final SelectionApprover approver; |
|
|
430 |
SavedDirFileChooser(String dirKey, File failoverDir, boolean force, SelectionApprover approver) { |
370 |
this.dirKey = dirKey; |
431 |
this.dirKey = dirKey; |
|
|
432 |
this.approver = approver; |
371 |
if (force && failoverDir != null && failoverDir.exists() && failoverDir.isDirectory()) { |
433 |
if (force && failoverDir != null && failoverDir.exists() && failoverDir.isDirectory()) { |
372 |
setCurrentDirectory(failoverDir); |
434 |
setCurrentDirectory(failoverDir); |
373 |
} else { |
435 |
} else { |
Lines 387-392
Link Here
|
387 |
} |
449 |
} |
388 |
|
450 |
|
389 |
@Override |
451 |
@Override |
|
|
452 |
public void approveSelection() { |
453 |
if (approver != null) { |
454 |
boolean approved = approver.approve(getSelectedFiles()); |
455 |
if (approved) { |
456 |
super.approveSelection(); |
457 |
} |
458 |
} else { |
459 |
super.approveSelection(); |
460 |
} |
461 |
} |
462 |
|
463 |
@Override |
390 |
public int showOpenDialog(Component parent) throws HeadlessException { |
464 |
public int showOpenDialog(Component parent) throws HeadlessException { |
391 |
int result = super.showOpenDialog(parent); |
465 |
int result = super.showOpenDialog(parent); |
392 |
if (result == APPROVE_OPTION) { |
466 |
if (result == APPROVE_OPTION) { |