1 | package org.openstreetmap.josm.actions;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 |
|
---|
5 | import java.io.File;
|
---|
6 |
|
---|
7 | import javax.swing.JFileChooser;
|
---|
8 | import javax.swing.JOptionPane;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 |
|
---|
12 | /**
|
---|
13 | * Helper class for all actions, that access the disk
|
---|
14 | */
|
---|
15 | abstract public class DiskAccessAction extends JosmAction {
|
---|
16 |
|
---|
17 | public DiskAccessAction(String name, String iconName, String tooltip, int shortCut, int modifiers) {
|
---|
18 | super(name, iconName, tooltip, shortCut, modifiers, true);
|
---|
19 | }
|
---|
20 |
|
---|
21 | protected static JFileChooser createAndOpenFileChooser(boolean open, boolean multiple) {
|
---|
22 | String curDir = Main.pref.get("lastDirectory");
|
---|
23 | if (curDir.equals(""))
|
---|
24 | curDir = ".";
|
---|
25 | JFileChooser fc = new JFileChooser(new File(curDir));
|
---|
26 | fc.setMultiSelectionEnabled(multiple);
|
---|
27 | for (int i = 0; i < ExtensionFileFilter.filters.length; ++i)
|
---|
28 | fc.addChoosableFileFilter(ExtensionFileFilter.filters[i]);
|
---|
29 | fc.setAcceptAllFileFilterUsed(true);
|
---|
30 |
|
---|
31 | int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent);
|
---|
32 | if (answer != JFileChooser.APPROVE_OPTION)
|
---|
33 | return null;
|
---|
34 |
|
---|
35 | if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
|
---|
36 | Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
|
---|
37 |
|
---|
38 | if (!open) {
|
---|
39 | File file = fc.getSelectedFile();
|
---|
40 | if (file == null || (file.exists() && JOptionPane.YES_OPTION !=
|
---|
41 | JOptionPane.showConfirmDialog(Main.parent, tr("File exists. Overwrite?"), tr("Overwrite"), JOptionPane.YES_NO_OPTION)))
|
---|
42 | return null;
|
---|
43 | }
|
---|
44 |
|
---|
45 | return fc;
|
---|
46 | }
|
---|
47 | }
|
---|