source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/advanced/ExportProfileAction.java@ 7005

Last change on this file since 7005 was 7005, checked in by Don-vip, 10 years ago

see #8465 - use diamond operator where applicable

File size: 3.1 KB
Line 
1// License: GPL. See LICENSE file for details.
2package org.openstreetmap.josm.gui.preferences.advanced;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.event.ActionEvent;
7import java.io.File;
8import java.util.ArrayList;
9import java.util.List;
10import java.util.Map;
11
12import javax.swing.AbstractAction;
13import javax.swing.JFileChooser;
14import javax.swing.JOptionPane;
15import javax.swing.filechooser.FileFilter;
16
17import org.openstreetmap.josm.Main;
18import org.openstreetmap.josm.actions.DiskAccessAction;
19import org.openstreetmap.josm.data.CustomConfigurator;
20import org.openstreetmap.josm.data.Preferences;
21import org.openstreetmap.josm.data.Preferences.Setting;
22
23/**
24 * Action that exports some fragment of settings to custom configuration file
25 */
26public 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}
Note: See TracBrowser for help on using the repository browser.