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