source: josm/trunk/src/org/openstreetmap/josm/actions/DiskAccessAction.java@ 1728

Last change on this file since 1728 was 1648, checked in by stoecker, 15 years ago

prevent overwriting of GPX files

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.actions;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.io.File;
7import javax.swing.JFileChooser;
8import javax.swing.filechooser.FileFilter;
9import org.openstreetmap.josm.Main;
10import org.openstreetmap.josm.gui.ExtendedDialog;
11import org.openstreetmap.josm.tools.Shortcut;
12import org.openstreetmap.josm.io.FileImporter;
13
14/**
15 * Helper class for all actions that access the disk
16 */
17abstract 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 String curDir = Main.pref.get("lastDirectory");
25 if (curDir.equals(""))
26 curDir = ".";
27 JFileChooser fc = new JFileChooser(new File(curDir));
28 if (title != null)
29 fc.setDialogTitle(title);
30
31 fc.setMultiSelectionEnabled(multiple);
32 for (FileImporter imExporter: ExtensionFileFilter.importers) {
33 fc.addChoosableFileFilter(imExporter.filter);
34 }
35
36 fc.setAcceptAllFileFilterUsed(true);
37
38 int answer = open ? fc.showOpenDialog(Main.parent) : fc.showSaveDialog(Main.parent);
39 if (answer != JFileChooser.APPROVE_OPTION)
40 return null;
41
42 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
43 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
44
45 if (!open) {
46 File file = fc.getSelectedFile();
47 if (file == null || (file.exists() && 1 !=
48 new ExtendedDialog(Main.parent,
49 tr("Overwrite"),
50 tr("File exists. Overwrite?"),
51 new String[] {tr("Overwrite"), tr("Cancel")},
52 new String[] {"save_as.png", "cancel.png"}).getValue()))
53 return null;
54 }
55
56 return fc;
57 }
58
59 public static File createAndOpenSaveFileChooser(String title,
60 String extension)
61 {
62 String curDir = Main.pref.get("lastDirectory");
63 if (curDir.equals(""))
64 curDir = ".";
65 JFileChooser fc = new JFileChooser(new File(curDir));
66 if (title != null)
67 fc.setDialogTitle(title);
68
69 fc.setMultiSelectionEnabled(false);
70 for (FileImporter imExporter: ExtensionFileFilter.importers) {
71 fc.addChoosableFileFilter(imExporter.filter);
72 }
73
74 fc.setAcceptAllFileFilterUsed(true);
75
76 int answer = fc.showSaveDialog(Main.parent);
77 if (answer != JFileChooser.APPROVE_OPTION)
78 return null;
79
80 if (!fc.getCurrentDirectory().getAbsolutePath().equals(curDir))
81 Main.pref.put("lastDirectory", fc.getCurrentDirectory().getAbsolutePath());
82
83 File file = fc.getSelectedFile();
84 if(extension != null)
85 {
86 String fn = file.getPath();
87 if(fn.indexOf('.') == -1)
88 {
89 FileFilter ff = fc.getFileFilter();
90 if (ff instanceof ExtensionFileFilter)
91 fn += "." + ((ExtensionFileFilter)ff).defaultExtension;
92 else
93 fn += extension;
94 file = new File(fn);
95 }
96 }
97 if(file == null || (file.exists() && 1 != new ExtendedDialog(Main.parent,
98 tr("Overwrite"), tr("File exists. Overwrite?"),
99 new String[] {tr("Overwrite"), tr("Cancel")},
100 new String[] {"save_as.png", "cancel.png"}).getValue()))
101 return null;
102 return file;
103 }
104
105}
Note: See TracBrowser for help on using the repository browser.