1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.io.File;
|
---|
7 |
|
---|
8 | import javax.swing.JFileChooser;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
12 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Helper class for all actions that access the disk
|
---|
16 | */
|
---|
17 | abstract public class DiskAccessAction extends JosmAction {
|
---|
18 |
|
---|
19 | public DiskAccessAction(String name, String iconName, String tooltip, Shortcut shortcut) {
|
---|
20 | super(name, iconName, tooltip, shortcut, true);
|
---|
21 | }
|
---|
22 |
|
---|
23 | public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title) {
|
---|
24 | return createAndOpenFileChooser(open, multiple, title, null);
|
---|
25 | }
|
---|
26 |
|
---|
27 | public static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple, String title, String extension) {
|
---|
28 | String curDir = Main.pref.get("lastDirectory");
|
---|
29 | if (curDir.equals("")) {
|
---|
30 | curDir = ".";
|
---|
31 | }
|
---|
32 | JFileChooser fc = new JFileChooser(new File(curDir));
|
---|
33 | if (title != null) {
|
---|
34 | fc.setDialogTitle(title);
|
---|
35 | }
|
---|
36 |
|
---|
37 | fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
|
---|
38 | fc.setMultiSelectionEnabled(multiple);
|
---|
39 | fc.setAcceptAllFileFilterUsed(false);
|
---|
40 | //System.out.println("opening fc for extension " + extension);
|
---|
41 | ExtensionFileFilter.applyChoosableImportFileFilters(fc, extension);
|
---|
42 |
|
---|
43 | int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent);
|
---|
44 | if (answer != JFileChooser.APPROVE_OPTION)
|
---|
45 | return null;
|
---|
46 |
|
---|
47 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir)) {
|
---|
48 | Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (!open) {
|
---|
52 | File file = fc.getSelectedFile();
|
---|
53 | if (file != null && file.exists()) {
|
---|
54 | ExtendedDialog dialog = new ExtendedDialog(
|
---|
55 | Main.parent,
|
---|
56 | tr("Overwrite"),
|
---|
57 | new String[] {tr("Overwrite"), tr("Cancel")}
|
---|
58 | );
|
---|
59 | dialog.setContent(tr("File exists. Overwrite?"));
|
---|
60 | dialog.setButtonIcons(new String[] {"save_as.png", "cancel.png"});
|
---|
61 | if (dialog.getValue() != 1)
|
---|
62 | return null;
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | return fc;
|
---|
67 | }
|
---|
68 | }
|
---|