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