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