1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.advanced;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.io.File;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.List;
|
---|
10 | import java.util.Locale;
|
---|
11 | import java.util.Map;
|
---|
12 |
|
---|
13 | import javax.swing.AbstractAction;
|
---|
14 | import javax.swing.JFileChooser;
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 | import javax.swing.filechooser.FileFilter;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.actions.DiskAccessAction;
|
---|
20 | import org.openstreetmap.josm.data.CustomConfigurator;
|
---|
21 | import org.openstreetmap.josm.data.Preferences;
|
---|
22 | import org.openstreetmap.josm.data.Preferences.Setting;
|
---|
23 | import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
|
---|
24 | import org.openstreetmap.josm.tools.Utils;
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Action that exports some fragment of settings to custom configuration file
|
---|
28 | */
|
---|
29 | public class ExportProfileAction extends AbstractAction {
|
---|
30 | private final String prefPattern;
|
---|
31 | private final String schemaKey;
|
---|
32 | private final transient Preferences prefs;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Constructs a new {@code ExportProfileAction}.
|
---|
36 | */
|
---|
37 | public ExportProfileAction(Preferences prefs, String schemaKey, String prefPattern) {
|
---|
38 | super(tr("Save {0} profile", tr(schemaKey)));
|
---|
39 | this.prefs = prefs;
|
---|
40 | this.prefPattern = prefPattern;
|
---|
41 | this.schemaKey = schemaKey;
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void actionPerformed(ActionEvent ae) {
|
---|
46 | List<String> keys = new ArrayList<>();
|
---|
47 | Map<String, Setting<?>> all = prefs.getAllSettings();
|
---|
48 | for (String key: all.keySet()) {
|
---|
49 | if (key.matches(prefPattern)) {
|
---|
50 | keys.add(key);
|
---|
51 | }
|
---|
52 | }
|
---|
53 | if (keys.isEmpty()) {
|
---|
54 | JOptionPane.showMessageDialog(Main.parent,
|
---|
55 | tr("All the preferences of this group are default, nothing to save"), tr("Warning"), JOptionPane.WARNING_MESSAGE);
|
---|
56 | return;
|
---|
57 | }
|
---|
58 | File f = askUserForCustomSettingsFile();
|
---|
59 | if (f!=null)
|
---|
60 | CustomConfigurator.exportPreferencesKeysToFile(f.getAbsolutePath(), false, keys);
|
---|
61 | }
|
---|
62 |
|
---|
63 | private File askUserForCustomSettingsFile() {
|
---|
64 | String title = tr("Choose profile file");
|
---|
65 |
|
---|
66 | FileFilter filter = new FileFilter() {
|
---|
67 | @Override
|
---|
68 | public boolean accept(File f) {
|
---|
69 | return f.isDirectory() || Utils.hasExtension(f, "xml") && f.getName().toLowerCase(Locale.ENGLISH).startsWith(schemaKey);
|
---|
70 | }
|
---|
71 | @Override
|
---|
72 | public String getDescription() {
|
---|
73 | return tr("JOSM custom settings files (*.xml)");
|
---|
74 | }
|
---|
75 | };
|
---|
76 | AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter,
|
---|
77 | JFileChooser.FILES_ONLY, "customsettings.lastDirectory");
|
---|
78 | if (fc != null) {
|
---|
79 | File sel = fc.getSelectedFile();
|
---|
80 | if (!sel.getName().endsWith(".xml")) sel=new File(sel.getAbsolutePath()+".xml");
|
---|
81 | if (!sel.getName().startsWith(schemaKey)) {
|
---|
82 | sel = new File(sel.getParentFile().getAbsolutePath()+"/"+schemaKey+"_"+sel.getName());
|
---|
83 | }
|
---|
84 | return sel;
|
---|
85 | }
|
---|
86 | return null;
|
---|
87 | }
|
---|
88 | }
|
---|