001// License: GPL. See LICENSE file for details. 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.Map; 011 012import javax.swing.AbstractAction; 013import javax.swing.JFileChooser; 014import javax.swing.JOptionPane; 015import javax.swing.filechooser.FileFilter; 016 017import org.openstreetmap.josm.Main; 018import org.openstreetmap.josm.actions.DiskAccessAction; 019import org.openstreetmap.josm.data.CustomConfigurator; 020import org.openstreetmap.josm.data.Preferences; 021import org.openstreetmap.josm.data.Preferences.Setting; 022 023/** 024 * Action that exports some fragment of settings to custom configuration file 025 */ 026public class ExportProfileAction extends AbstractAction { 027 private final String prefPattern; 028 private final String schemaKey; 029 private final Preferences prefs; 030 031 public ExportProfileAction(Preferences prefs, String schemaKey, String prefPattern) { 032 super(tr("Save {0} profile", tr(schemaKey))); 033 this.prefs = prefs; 034 this.prefPattern = prefPattern; 035 this.schemaKey = schemaKey; 036 } 037 038 @Override 039 public void actionPerformed(ActionEvent ae) { 040 List<String> keys = new ArrayList<String>(); 041 Map<String, Setting> all = prefs.getAllSettings(); 042 for (String key: all.keySet()) { 043 if (key.matches(prefPattern)) { 044 keys.add(key); 045 } 046 } 047 if (keys.isEmpty()) { 048 JOptionPane.showMessageDialog(Main.parent, 049 tr("All the preferences of this group are default, nothing to save"), tr("Warning"), JOptionPane.WARNING_MESSAGE); 050 return; 051 } 052 File f = askUserForCustomSettingsFile(); 053 if (f!=null) 054 CustomConfigurator.exportPreferencesKeysToFile(f.getAbsolutePath(), false, keys); 055 } 056 057 private File askUserForCustomSettingsFile() { 058 String title = tr("Choose profile file"); 059 060 FileFilter filter = new FileFilter() { 061 @Override 062 public boolean accept(File f) { 063 return f.isDirectory() || f.getName().toLowerCase().endsWith(".xml") && f.getName().toLowerCase().startsWith(schemaKey); 064 } 065 @Override 066 public String getDescription() { 067 return tr("JOSM custom settings files (*.xml)"); 068 } 069 }; 070 JFileChooser fc = DiskAccessAction.createAndOpenFileChooser(false, false, title, filter, JFileChooser.FILES_ONLY, "customsettings.lastDirectory"); 071 if (fc != null) { 072 File sel = fc.getSelectedFile(); 073 if (!sel.getName().endsWith(".xml")) sel=new File(sel.getAbsolutePath()+".xml"); 074 if (!sel.getName().startsWith(schemaKey)) { 075 sel = new File(sel.getParentFile().getAbsolutePath()+"/"+schemaKey+"_"+sel.getName()); 076 } 077 return sel; 078 } 079 return null; 080 } 081}