Changeset 3869 in josm


Ignore:
Timestamp:
Feb 8, 2011 11:54:02 AM (2 years ago)
Author:
bastiK
Message:

mappaint styles configuration: If the selection of default styles changes in future releases, add the new entries to the user-configured list. Remember the known URLs, so an item that was deleted explicitly is not added again.

Location:
trunk/src/org/openstreetmap/josm
Files:
2 edited

Legend:

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

    r3864 r3869  
    1414import javax.swing.BorderFactory; 
    1515import javax.swing.JCheckBox; 
    16 import javax.swing.JComboBox; 
    17 import javax.swing.JLabel; 
    1816import javax.swing.JPanel; 
    1917import javax.swing.event.ChangeEvent; 
     
    2422import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry; 
    2523import org.openstreetmap.josm.tools.GBC; 
     24import org.openstreetmap.josm.tools.Predicate; 
     25import org.openstreetmap.josm.tools.Utils; 
    2626 
    2727public class MapPaintPreference implements PreferenceSetting { 
     
    178178                put(ls); 
    179179            } 
     180            if (insertNewDefaults(ls)) { 
     181                put(ls); 
     182            } 
    180183            return ls; 
    181184        } 
     
    196199        } 
    197200 
     201        /** 
     202         * If the selection of default styles changes in future releases, add 
     203         * the new entries to the user-configured list. Remember the known URLs, 
     204         * so an item that was deleted explicitly is not added again. 
     205         */ 
     206        private boolean insertNewDefaults(List<SourceEntry> list) { 
     207            boolean changed = false; 
     208             
     209            Collection<String> knownDefaults = new TreeSet<String>(Main.pref.getCollection("mappaint.style.known-defaults")); 
     210 
     211            Collection<ExtendedSourceEntry> defaults = getDefault(); 
     212            int insertionIdx = 0; 
     213            for (final SourceEntry def : defaults) { 
     214                int i = Utils.indexOf(list, 
     215                        new Predicate<SourceEntry>() { 
     216                            @Override 
     217                            public boolean evaluate(SourceEntry se) { 
     218                                return Utils.equal(def.url, se.url); 
     219                            } 
     220                        }); 
     221                if (i == -1 && !knownDefaults.contains(def.url)) { 
     222                    list.add(insertionIdx, def); 
     223                    insertionIdx++; 
     224                    changed = true; 
     225                } else { 
     226                    if (i > insertionIdx) { 
     227                        insertionIdx = i + 1; 
     228                    } 
     229                } 
     230            } 
     231 
     232            for (SourceEntry def : defaults) { 
     233                knownDefaults.add(def.url); 
     234            } 
     235            if (Main.pref.putCollection("mappaint.style.known-defaults", knownDefaults)) { 
     236                changed = true; 
     237            } 
     238 
     239            return changed; 
     240        } 
     241 
    198242        @Override 
    199243        public Collection<ExtendedSourceEntry> getDefault() { 
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r3865 r3869  
    3939        } 
    4040        return null; 
     41    } 
     42 
     43    public static <T> int indexOf(Iterable<? extends T> collection, Predicate<? super T> predicate) { 
     44        int i = 0; 
     45        for (T item : collection) { 
     46            if (predicate.evaluate(item)) 
     47                return i; 
     48            i++; 
     49        } 
     50        return -1; 
    4151    } 
    4252 
Note: See TracChangeset for help on using the changeset viewer.