Ignore:
Timestamp:
2013-06-25T14:26:12+02:00 (11 years ago)
Author:
akks
Message:

fix #7449: initial support for shortcut/color/toolbar/imagery profiles (see Advanced preferences - More)

Location:
trunk/src/org/openstreetmap/josm/gui/preferences/advanced
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/AdvancedPreference.java

    r6021 r6022  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
     5import static org.openstreetmap.josm.tools.I18n.marktr;
    56
    67import java.awt.Dimension;
     
    1213import java.util.Collections;
    1314import java.util.Comparator;
     15import java.util.LinkedHashMap;
    1416import java.util.List;
    1517import java.util.Map;
     
    2123import javax.swing.JFileChooser;
    2224import javax.swing.JLabel;
     25import javax.swing.JMenu;
    2326import javax.swing.JOptionPane;
    2427import javax.swing.JPanel;
     
    2730import javax.swing.event.DocumentEvent;
    2831import javax.swing.event.DocumentListener;
     32import javax.swing.event.MenuEvent;
     33import javax.swing.event.MenuListener;
    2934import javax.swing.filechooser.FileFilter;
    3035
     
    4247import org.openstreetmap.josm.gui.widgets.JosmTextField;
    4348import org.openstreetmap.josm.tools.GBC;
    44 import static org.openstreetmap.josm.tools.I18n.tr;
    4549
    4650public class AdvancedPreference extends DefaultTabPreferenceSetting {
     
    255259        readPreferences(tmpPrefs);
    256260        // sorting after modification - first modified, then non-default, then default entries
    257         Collections.sort(allData, new Comparator<PrefEntry>() {
    258             @Override
    259             public int compare(PrefEntry o1, PrefEntry o2) {
    260                 if (o1.isChanged() && !o2.isChanged()) return -1;
    261                 if (o2.isChanged() && !o1.isChanged()) return 1;
    262                 if (!(o1.isDefault()) && o2.isDefault()) return -1;
    263                 if (!(o2.isDefault()) && o1.isDefault()) return 1;
    264                 return o1.compareTo(o2);
    265             }
    266         });
     261        Collections.sort(allData, customComparator);
    267262        applyFilter();
    268263    }
     264   
     265    private Comparator<PrefEntry> customComparator = new Comparator<PrefEntry>() {
     266        @Override
     267        public int compare(PrefEntry o1, PrefEntry o2) {
     268            if (o1.isChanged() && !o2.isChanged()) return -1;
     269            if (o2.isChanged() && !o1.isChanged()) return 1;
     270            if (!(o1.isDefault()) && o2.isDefault()) return -1;
     271            if (!(o2.isDefault()) && o1.isDefault()) return 1;
     272            return o1.compareTo(o2);
     273        }
     274    };
    269275               
    270276    private List<PrefEntry> prepareData(Map<String, Setting> loaded, Map<String, Setting> orig, Map<String, Setting> defaults) {
     
    301307    }
    302308   
     309    Map<String,String> profileTypes = new LinkedHashMap<String, String>();
     310   
    303311    private JPopupMenu buildPopupMenu() {
    304312        JPopupMenu menu = new JPopupMenu();
     313        profileTypes.put(marktr("shortcut"), "shortcut\\..*");
     314        profileTypes.put(marktr("color"), "color\\..*");
     315        profileTypes.put(marktr("toolbar"), "toolbar.*");
     316        profileTypes.put(marktr("imagery"), "imagery.*");
     317       
     318        for (Entry<String,String> e: profileTypes.entrySet()) {
     319            menu.add(new ExportProfileAction(Main.pref, e.getKey(), e.getValue()));
     320        }
     321       
     322        menu.addSeparator();
     323        menu.add(getProfileMenu());
     324        menu.addSeparator();
    305325        menu.add(new AbstractAction(tr("Reset preferences")) {
    306326            @Override public void actionPerformed(ActionEvent ae) {
     
    320340        return menu;
    321341    }
     342   
     343    private JMenu getProfileMenu() {
     344        final JMenu p =new JMenu(tr("Load profile"));
     345        p.addMenuListener(new MenuListener() {
     346            @Override
     347            public void menuSelected(MenuEvent me) {
     348                p.removeAll();
     349                for (File f: new File(".").listFiles()) {
     350                   String s = f.getName();
     351                   int idx = s.indexOf("_");
     352                   if (idx>=0) {
     353                        String t=s.substring(0,idx);
     354                        System.out.println(t);
     355                        if (profileTypes.containsKey(t))
     356                            p.add(new ImportProfileAction(s, f, t));
     357                   }
     358                }
     359                for (File f: Main.pref.getPreferencesDirFile().listFiles()) {
     360                   String s = f.getName();
     361                   int idx = s.indexOf("_");
     362                   if (idx>=0) {
     363                        String t=s.substring(0,idx);
     364                        if (profileTypes.containsKey(t))
     365                            p.add(new ImportProfileAction(s, f, t));
     366                   }
     367                }
     368            }
     369            @Override public void menuDeselected(MenuEvent me) { }
     370            @Override public void menuCanceled(MenuEvent me) { }
     371        });
     372        return p;
     373    }
     374   
     375    private class ImportProfileAction extends AbstractAction {
     376        private final File file;
     377        private final String type;
     378       
     379        public ImportProfileAction(String name, File file, String type) {
     380            super(name);
     381            this.file = file;
     382            this.type = type;
     383        }
     384
     385        @Override
     386        public void actionPerformed(ActionEvent ae) {
     387            Preferences tmpPrefs = CustomConfigurator.clonePreferences(Main.pref);
     388            CustomConfigurator.readXML(file, tmpPrefs);
     389            readPreferences(tmpPrefs);
     390            String prefRegex = profileTypes.get(type);
     391            // clean all the preferences from the chosen group
     392            for (PrefEntry p : allData) {
     393               if (p.getKey().matches(prefRegex) && !p.isDefault()) {
     394                    p.reset();
     395               }
     396            }
     397            // allow user to review the changes in table
     398            Collections.sort(allData, customComparator);
     399            applyFilter();
     400        }
     401    }
    322402
    323403    private void applyFilter() {
     
    345425            }
    346426        }
    347         System.out.println(displayData.size())  ;
    348427        if (table!=null) table.fireDataChanged();
    349428    }
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/PrefEntry.java

    r6021 r6022  
     1// License: GPL. See LICENSE file for details.
    12package org.openstreetmap.josm.gui.preferences.advanced;
    23
  • trunk/src/org/openstreetmap/josm/gui/preferences/advanced/PreferencesTable.java

    r6021 r6022  
     1// License: GPL. See LICENSE file for details.
    12package org.openstreetmap.josm.gui.preferences.advanced;
    23
Note: See TracChangeset for help on using the changeset viewer.