Ignore:
Timestamp:
04.12.2011 19:17:56 (6 months ago)
Author:
bastiK
Message:

advanced preference: dialogs to edit complex preference settings (list, list of lists, list of maps) (see #7027)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/Preferences.java

    r4614 r4634  
    6565 * accessed using one of the get...() methods. You can use the same preference 
    6666 * key in different parts of the code, but the default value must be the same 
    67  * everywhere. null is a legitimate default value. 
     67 * everywhere. A default value of null means, the setting has been requested, but 
     68 * no default value was set. This is used in advanced preferences to present a list 
     69 * off all possible settings. 
    6870 * 
    6971 * At the moment, there is no such thing as an empty value. 
     
    99101        T getValue(); 
    100102        void visit(SettingVisitor visitor); 
     103        Setting<T> getNullInstance(); 
    101104    } 
    102105 
     
    118121            visitor.visit(this); 
    119122        } 
     123        public StringSetting getNullInstance() { 
     124            return new StringSetting(null); 
     125        } 
    120126    } 
    121127 
     
    127133            visitor.visit(this); 
    128134        } 
     135        public ListSetting getNullInstance() { 
     136            return new ListSetting(null); 
     137        } 
    129138    } 
    130139 
     
    136145            visitor.visit(this); 
    137146        } 
     147        public ListListSetting getNullInstance() { 
     148            return new ListListSetting(null); 
     149        } 
    138150    } 
    139151 
     
    144156        public void visit(SettingVisitor visitor) { 
    145157            visitor.visit(this); 
     158        } 
     159        public MapListSetting getNullInstance() { 
     160            return new MapListSetting(null); 
    146161        } 
    147162    } 
     
    825840     */ 
    826841    public Collection<String> getCollection(String key, Collection<String> def) { 
    827         if (def != null) { 
    828             putCollectionDefault(key, new ArrayList<String>(def)); 
    829         } 
     842        putCollectionDefault(key, def == null ? null : new ArrayList<String>(def)); 
    830843        Collection<String> prop = getCollectionInternal(key); 
    831844        if (prop != null) 
     
    904917    } 
    905918 
    906     private static boolean equalCollection(Collection<String> a, Collection<String> b) { 
     919    public static boolean equalCollection(Collection<String> a, Collection<String> b) { 
    907920        if (a == null) return b == null; 
    908921        if (b == null) return false; 
     
    947960            } 
    948961            putArrayDefault(key, Collections.unmodifiableList(defCopy)); 
     962        } else { 
     963            putArrayDefault(key, null); 
    949964        } 
    950965        List<List<String>> prop = getArrayInternal(key); 
     
    955970        } else 
    956971            return def; 
     972    } 
     973 
     974    public Collection<Collection<String>> getArray(String key) { 
     975        putArrayDefault(key, null); 
     976        List<List<String>> prop = getArrayInternal(key); 
     977        if (prop != null) { 
     978            @SuppressWarnings("unchecked") 
     979            Collection<Collection<String>> prop_cast = (Collection) prop; 
     980            return prop_cast; 
     981        } else 
     982            return Collections.emptyList(); 
    957983    } 
    958984 
     
    10201046    } 
    10211047 
    1022     private static boolean equalArray(Collection<Collection<String>> a, Collection<List<String>> b) { 
     1048    public static boolean equalArray(Collection<Collection<String>> a, Collection<List<String>> b) { 
    10231049        if (a == null) return b == null; 
    10241050        if (b == null) return false; 
     
    10431069            } 
    10441070            putListOfStructsDefault(key, Collections.unmodifiableList(defCopy)); 
     1071        } else { 
     1072            putListOfStructsDefault(key, null); 
    10451073        } 
    10461074        Collection<Map<String, String>> prop = getListOfStructsInternal(key); 
     
    11171145    } 
    11181146 
    1119     private static boolean equalListOfStructs(Collection<Map<String, String>> a, Collection<Map<String, String>> b) { 
     1147    public static boolean equalListOfStructs(Collection<Map<String, String>> a, Collection<Map<String, String>> b) { 
    11201148        if (a == null) return b == null; 
    11211149        if (b == null) return false; 
     
    12981326        } 
    12991327        return struct; 
     1328    } 
     1329 
     1330    public boolean putSetting(final String key, Setting value) { 
     1331        if (value == null) return false; 
     1332        class PutVisitor implements SettingVisitor { 
     1333            public boolean changed; 
     1334            public void visit(StringSetting setting) { 
     1335                changed = put(key, setting.getValue()); 
     1336            } 
     1337            public void visit(ListSetting setting) { 
     1338                changed = putCollection(key, setting.getValue()); 
     1339            } 
     1340            public void visit(ListListSetting setting) { 
     1341                changed = putArray(key, (Collection) setting.getValue()); 
     1342            } 
     1343            public void visit(MapListSetting setting) { 
     1344                changed = putListOfStructs(key, setting.getValue()); 
     1345            } 
     1346        }; 
     1347        PutVisitor putVisitor = new PutVisitor(); 
     1348        value.visit(putVisitor); 
     1349        return putVisitor.changed; 
     1350    } 
     1351 
     1352    public Map<String, Setting> getAllSettings() { 
     1353        Map<String, Setting> settings = new TreeMap<String, Setting>(); 
     1354 
     1355        for (Entry<String, String> e : properties.entrySet()) { 
     1356            settings.put(e.getKey(), new StringSetting(e.getValue())); 
     1357        } 
     1358        for (Entry<String, List<String>> e : collectionProperties.entrySet()) { 
     1359            settings.put(e.getKey(), new ListSetting(e.getValue())); 
     1360        } 
     1361        for (Entry<String, List<List<String>>> e : arrayProperties.entrySet()) { 
     1362            settings.put(e.getKey(), new ListListSetting(e.getValue())); 
     1363        } 
     1364        for (Entry<String, List<Map<String, String>>> e : listOfStructsProperties.entrySet()) { 
     1365            settings.put(e.getKey(), new MapListSetting(e.getValue())); 
     1366        } 
     1367        return settings; 
     1368    } 
     1369 
     1370    public Map<String, Setting> getAllDefaults() { 
     1371        Map<String, Setting> allDefaults = new TreeMap<String, Setting>(); 
     1372 
     1373        for (Entry<String, String> e : defaults.entrySet()) { 
     1374            allDefaults.put(e.getKey(), new StringSetting(e.getValue())); 
     1375        } 
     1376        for (Entry<String, List<String>> e : collectionDefaults.entrySet()) { 
     1377            allDefaults.put(e.getKey(), new ListSetting(e.getValue())); 
     1378        } 
     1379        for (Entry<String, List<List<String>>> e : arrayDefaults.entrySet()) { 
     1380            allDefaults.put(e.getKey(), new ListListSetting(e.getValue())); 
     1381        } 
     1382        for (Entry<String, List<Map<String, String>>> e : listOfStructsDefaults.entrySet()) { 
     1383            allDefaults.put(e.getKey(), new MapListSetting(e.getValue())); 
     1384        } 
     1385        return allDefaults; 
    13001386    } 
    13011387 
Note: See TracChangeset for help on using the changeset viewer.