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

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

sonar - fb-contrib - minor performance improvements:

  • Method passes constant String of length 1 to character overridden method
  • Method needlessly boxes a boolean constant
  • Method uses iterator().next() on a List to get the first item
  • Method converts String to boxed primitive using excessive boxing
  • Method converts String to primitive using excessive boxing
  • Method creates array using constants
  • Class defines List based fields but uses them like Sets
  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.Locale;
11import java.util.Map;
12
13import javax.swing.AbstractAction;
14import javax.swing.JFileChooser;
15import javax.swing.JOptionPane;
16import javax.swing.filechooser.FileFilter;
17
18import org.openstreetmap.josm.Main;
19import org.openstreetmap.josm.actions.DiskAccessAction;
20import org.openstreetmap.josm.data.CustomConfigurator;
21import org.openstreetmap.josm.data.Preferences;
22import org.openstreetmap.josm.data.Preferences.Setting;
23import org.openstreetmap.josm.gui.widgets.AbstractFileChooser;
24import org.openstreetmap.josm.tools.Utils;
25
26/**
27 * Action that exports some fragment of settings to custom configuration file
28 */
29public 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
72 @Override
73 public String getDescription() {
74 return tr("JOSM custom settings files (*.xml)");
75 }
76 };
77 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter,
78 JFileChooser.FILES_ONLY, "customsettings.lastDirectory");
79 if (fc != null) {
80 File sel = fc.getSelectedFile();
81 if (!sel.getName().endsWith(".xml")) sel = new File(sel.getAbsolutePath()+".xml");
82 if (!sel.getName().startsWith(schemaKey)) {
83 sel = new File(sel.getParentFile().getAbsolutePath()+'/'+schemaKey+'_'+sel.getName());
84 }
85 return sel;
86 }
87 return null;
88 }
89}
Note: See TracBrowser for help on using the repository browser.