001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.preferences.advanced; 003 004import static org.openstreetmap.josm.tools.I18n.tr; 005 006import java.awt.event.ActionEvent; 007import java.io.File; 008import java.util.ArrayList; 009import java.util.List; 010import java.util.Locale; 011import java.util.Map; 012 013import javax.swing.AbstractAction; 014import javax.swing.JFileChooser; 015import javax.swing.JOptionPane; 016import javax.swing.filechooser.FileFilter; 017 018import org.openstreetmap.josm.Main; 019import org.openstreetmap.josm.actions.DiskAccessAction; 020import org.openstreetmap.josm.data.CustomConfigurator; 021import org.openstreetmap.josm.data.Preferences; 022import org.openstreetmap.josm.data.Preferences.Setting; 023import org.openstreetmap.josm.gui.widgets.AbstractFileChooser; 024import org.openstreetmap.josm.tools.Utils; 025 026/** 027 * Action that exports some fragment of settings to custom configuration file 028 */ 029public class ExportProfileAction extends AbstractAction { 030 private final String prefPattern; 031 private final String schemaKey; 032 private final transient Preferences prefs; 033 034 /** 035 * Constructs a new {@code ExportProfileAction}. 036 * @param prefs preferences 037 * @param schemaKey filename prefix 038 * @param prefPattern preference key pattern used to determine which entries are exported 039 */ 040 public ExportProfileAction(Preferences prefs, String schemaKey, String prefPattern) { 041 super(tr("Save {0} profile", tr(schemaKey))); 042 this.prefs = prefs; 043 this.prefPattern = prefPattern; 044 this.schemaKey = schemaKey; 045 } 046 047 @Override 048 public void actionPerformed(ActionEvent ae) { 049 List<String> keys = new ArrayList<>(); 050 Map<String, Setting<?>> all = prefs.getAllSettings(); 051 for (String key: all.keySet()) { 052 if (key.matches(prefPattern)) { 053 keys.add(key); 054 } 055 } 056 if (keys.isEmpty()) { 057 JOptionPane.showMessageDialog(Main.parent, 058 tr("All the preferences of this group are default, nothing to save"), tr("Warning"), JOptionPane.WARNING_MESSAGE); 059 return; 060 } 061 File f = askUserForCustomSettingsFile(); 062 if (f != null) 063 CustomConfigurator.exportPreferencesKeysToFile(f.getAbsolutePath(), false, keys); 064 } 065 066 private File askUserForCustomSettingsFile() { 067 String title = tr("Choose profile file"); 068 069 FileFilter filter = new FileFilter() { 070 @Override 071 public boolean accept(File f) { 072 return f.isDirectory() || (Utils.hasExtension(f, "xml") && f.getName().toLowerCase(Locale.ENGLISH).startsWith(schemaKey)); 073 } 074 075 @Override 076 public String getDescription() { 077 return tr("JOSM custom settings files (*.xml)"); 078 } 079 }; 080 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, 081 JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 082 if (fc != null) { 083 File sel = fc.getSelectedFile(); 084 if (!sel.getName().endsWith(".xml")) sel = new File(sel.getAbsolutePath()+".xml"); 085 if (!sel.getName().startsWith(schemaKey)) { 086 sel = new File(sel.getParentFile().getAbsolutePath()+'/'+schemaKey+'_'+sel.getName()); 087 } 088 return sel; 089 } 090 return null; 091 } 092}