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 */ 037 public ExportProfileAction(Preferences prefs, String schemaKey, String prefPattern) { 038 super(tr("Save {0} profile", tr(schemaKey))); 039 this.prefs = prefs; 040 this.prefPattern = prefPattern; 041 this.schemaKey = schemaKey; 042 } 043 044 @Override 045 public void actionPerformed(ActionEvent ae) { 046 List<String> keys = new ArrayList<>(); 047 Map<String, Setting<?>> all = prefs.getAllSettings(); 048 for (String key: all.keySet()) { 049 if (key.matches(prefPattern)) { 050 keys.add(key); 051 } 052 } 053 if (keys.isEmpty()) { 054 JOptionPane.showMessageDialog(Main.parent, 055 tr("All the preferences of this group are default, nothing to save"), tr("Warning"), JOptionPane.WARNING_MESSAGE); 056 return; 057 } 058 File f = askUserForCustomSettingsFile(); 059 if (f != null) 060 CustomConfigurator.exportPreferencesKeysToFile(f.getAbsolutePath(), false, keys); 061 } 062 063 private File askUserForCustomSettingsFile() { 064 String title = tr("Choose profile file"); 065 066 FileFilter filter = new FileFilter() { 067 @Override 068 public boolean accept(File f) { 069 return f.isDirectory() || Utils.hasExtension(f, "xml") && f.getName().toLowerCase(Locale.ENGLISH).startsWith(schemaKey); 070 } 071 072 @Override 073 public String getDescription() { 074 return tr("JOSM custom settings files (*.xml)"); 075 } 076 }; 077 AbstractFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, 078 JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 079 if (fc != null) { 080 File sel = fc.getSelectedFile(); 081 if (!sel.getName().endsWith(".xml")) sel = new File(sel.getAbsolutePath()+".xml"); 082 if (!sel.getName().startsWith(schemaKey)) { 083 sel = new File(sel.getParentFile().getAbsolutePath()+'/'+schemaKey+'_'+sel.getName()); 084 } 085 return sel; 086 } 087 return null; 088 } 089}