Changeset 4450 in josm


Ignore:
Timestamp:
Sep 19, 2011 11:59:27 AM (20 months ago)
Author:
bastiK
Message:

change preference format for imagery menu entries

Location:
trunk/src/org/openstreetmap/josm/data
Files:
3 edited

Legend:

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

    r4426 r4450  
    901901            structPrototype = klass.newInstance(); 
    902902        } catch (InstantiationException ex) { 
    903             throw new RuntimeException(); 
     903            throw new RuntimeException(ex); 
    904904        } catch (IllegalAccessException ex) { 
    905             throw new RuntimeException(); 
     905            throw new RuntimeException(ex); 
    906906        } 
    907907 
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryInfo.java

    r4437 r4450  
    1414import org.openstreetmap.josm.Main; 
    1515import org.openstreetmap.josm.data.Bounds; 
     16import org.openstreetmap.josm.data.Preferences.pref; 
    1617import org.openstreetmap.josm.io.OsmApi; 
    1718import org.openstreetmap.josm.tools.CheckParameterUtil; 
     
    4243        } 
    4344    } 
    44      
     45 
    4546    public static class ImageryBounds extends Bounds { 
    4647        public ImageryBounds(String asString, String separator) { 
     
    4950 
    5051        private List<Shape> shapes = new ArrayList<Shape>(); 
    51          
     52 
    5253        public void addShape(Shape shape) { 
    5354            this.shapes.add(shape); 
     
    8081    private String countryCode = ""; 
    8182 
     83    /** auxiliary class to save an ImageryInfo object in the preferences */ 
     84    public static class ImageryPreferenceEntry { 
     85        @pref String name; 
     86        @pref String type; 
     87        @pref String url; 
     88        @pref String eula; 
     89        @pref String attribution_text; 
     90        @pref String attribution_url; 
     91        @pref String terms_of_use_url; 
     92        @pref String country_code = ""; 
     93        @pref int max_zoom; 
     94        @pref int min_zoom; 
     95        @pref String cookies; 
     96        @pref String bounds; 
     97        @pref String shapes; 
     98        @pref String projections; 
     99 
     100        public ImageryPreferenceEntry() { 
     101        } 
     102 
     103        public ImageryPreferenceEntry(ImageryInfo i) { 
     104            name = i.name; 
     105            type = i.imageryType.getUrlString(); 
     106            url = i.url; 
     107            eula = i.eulaAcceptanceRequired; 
     108            attribution_text = i.attributionText; 
     109            attribution_url = i.attributionLinkURL; 
     110            terms_of_use_url = i.termsOfUseURL; 
     111            country_code = i.countryCode; 
     112            max_zoom = i.defaultMaxZoom; 
     113            min_zoom = i.defaultMinZoom; 
     114            cookies = i.cookies; 
     115            if (i.bounds != null) { 
     116                bounds = i.bounds.encodeAsString(","); 
     117                String shapesString = ""; 
     118                for (Shape s : i.bounds.getShapes()) { 
     119                    if (!shapesString.isEmpty()) { 
     120                        shapesString += ";"; 
     121                    } 
     122                    shapesString += s.encodeAsString(","); 
     123                } 
     124                if (!shapesString.isEmpty()) { 
     125                    shapes = shapesString; 
     126                } 
     127            } 
     128            if (i.serverProjections != null && !i.serverProjections.isEmpty()) { 
     129                String val = ""; 
     130                for (String p : i.serverProjections) { 
     131                    if (!val.isEmpty()) 
     132                        val += ","; 
     133                    val += p; 
     134                } 
     135                projections = val; 
     136            } 
     137        } 
     138    } 
     139 
    82140    public ImageryInfo() { 
    83141    } 
     
    112170    } 
    113171 
    114     public ArrayList<String> getInfoArray() { 
    115         ArrayList<String> res = new ArrayList<String>(); 
    116         res.add(name); 
    117         res.add((url != null && !url.isEmpty()) ? getExtendedUrl() : null); 
    118         res.add(cookies); 
    119         if(imageryType == ImageryType.WMS || imageryType == ImageryType.HTML) { 
    120             res.add(pixelPerDegree != 0.0 ? String.valueOf(pixelPerDegree) : null); 
    121         } else { 
    122             res.add(null); 
    123         } 
    124         res.add(bounds != null ? bounds.encodeAsString(",") : null); 
    125         res.add(attributionText); 
    126         res.add(attributionLinkURL); 
    127         res.add(attributionImage); 
    128         res.add(termsOfUseURL); 
    129         // Shapes 
    130         String shapesString = ""; 
    131         if (bounds != null) { 
    132             for (Shape s : bounds.getShapes()) { 
    133                 if (!shapesString.isEmpty()) { 
    134                     shapesString += ";"; 
    135                 } 
    136                 shapesString += s.encodeAsString(","); 
    137             } 
    138         } 
    139         res.add(shapesString.isEmpty() ? null : shapesString); 
    140         if(serverProjections != null && serverProjections.size() != 0) { 
    141             String val = ""; 
    142             for(String p : serverProjections) { 
    143                 if(!val.isEmpty()) 
    144                     val += ","; 
    145                 val += p; 
    146             } 
    147             res.add(val); 
    148         } 
    149         return res; 
     172    public ImageryInfo(ImageryPreferenceEntry e) { 
     173        name = e.name; 
     174        url = e.url; 
     175        eulaAcceptanceRequired = e.eula; 
     176        for (ImageryType type : ImageryType.values()) { 
     177            if (type.getUrlString().equals(e.type)) { 
     178                imageryType = type; 
     179                break; 
     180            } 
     181        } 
     182        defaultMaxZoom = e.max_zoom; 
     183        defaultMinZoom = e.min_zoom; 
     184        if (e.bounds != null) { 
     185            bounds = new ImageryBounds(e.bounds, ","); 
     186            if (e.shapes != null) { 
     187                try { 
     188                    for (String s : e.shapes.split(";")) { 
     189                        bounds.addShape(new Shape(s, ",")); 
     190                    } 
     191                } catch (IllegalArgumentException ex) { 
     192                    Main.warn(ex.toString()); 
     193                } 
     194            } 
     195        } 
     196        if (e.projections != null) { 
     197            serverProjections = Arrays.asList(e.projections.split(",")); 
     198        } 
     199        attributionText = e.attribution_text; 
     200        attributionLinkURL = e.attribution_url; 
     201        termsOfUseURL = e.terms_of_use_url; 
     202        countryCode = e.country_code; 
    150203    } 
    151204 
     
    246299        this.defaultMinZoom = defaultMinZoom; 
    247300    } 
    248      
     301 
    249302    public void setBounds(ImageryBounds b) { 
    250303        this.bounds = b; 
     
    273326    public void setExtendedUrl(String url) { 
    274327        CheckParameterUtil.ensureParameterNotNull(url); 
    275          
     328 
    276329        // Default imagery type is WMS 
    277330        this.url = url; 
  • trunk/src/org/openstreetmap/josm/data/imagery/ImageryLayerInfo.java

    r4428 r4450  
    1111 
    1212import org.openstreetmap.josm.Main; 
     13import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryPreferenceEntry; 
    1314import org.openstreetmap.josm.io.imagery.ImageryReader; 
    1415import org.openstreetmap.josm.io.MirroredInputStream; 
     
    1617import org.xml.sax.SAXException; 
    1718 
     19/** 
     20 * Manages the list of imagery entries that are shown in the imagery menu. 
     21 */ 
    1822public class ImageryLayerInfo { 
    1923 
     
    3943    public void load() { 
    4044        boolean addedDefault = layers.size() != 0; 
    41         for(Collection<String> c : Main.pref.getArray("imagery.layers", 
    42                 Collections.<Collection<String>>emptySet())) { 
    43             ImageryInfo i = new ImageryInfo(c); 
    44             String url = i.getUrl(); 
    45             if(url != null) { 
    46                 /* FIXME: Remove the attribution copy stuff end of 2011 */ 
    47                 if(!i.hasAttribution()) { 
    48                     for(ImageryInfo d : defaultLayers) { 
    49                         if(url.equals(d.getUrl())) { 
    50                             i.copyAttribution(d); 
    51                             i.setBounds(d.getBounds()); 
    52                             break; 
     45        List<ImageryPreferenceEntry> entries = Main.pref.getListOfStructs("imagery.entries", null, ImageryPreferenceEntry.class); 
     46        if (entries == null) { 
     47            /* FIXME: Remove old format ~ March 2012 */ 
     48            boolean hasOld = loadOld(); 
     49            if (hasOld) { 
     50                save(); 
     51            } 
     52        } else { 
     53            for (ImageryPreferenceEntry prefEntry : entries) { 
     54                ImageryInfo i = new ImageryInfo(prefEntry); 
     55                add(i); 
     56            } 
     57            Collections.sort(layers); 
     58        } 
     59        if (addedDefault) { 
     60            save(); 
     61        } 
     62    } 
     63 
     64    public boolean loadOld() { 
     65        Collection<Collection<String>> entries = Main.pref.getArray("imagery.layers", null); 
     66        if (entries != null) { 
     67            for (Collection<String> c : Main.pref.getArray("imagery.layers", 
     68                    Collections.<Collection<String>>emptySet())) { 
     69                ImageryInfo i = new ImageryInfo(c); 
     70                String url = i.getUrl(); 
     71                if(url != null) { 
     72                    /* FIXME: Remove the attribution copy stuff end of 2011 */ 
     73                    if(!i.hasAttribution()) { 
     74                        for(ImageryInfo d : defaultLayers) { 
     75                            if(url.equals(d.getUrl())) { 
     76                                i.copyAttribution(d); 
     77                                i.setBounds(d.getBounds()); 
     78                                break; 
     79                            } 
    5380                        } 
    5481                    } 
     82                    add(i); 
    5583                } 
    56                 add(i); 
    5784            } 
     85            Collections.sort(layers); 
     86            return true; 
    5887        } 
    59         Collections.sort(layers); 
    60         if(addedDefault) 
    61             save(); 
     88        return false; 
    6289    } 
    6390 
     
    132159 
    133160    public void save() { 
    134         LinkedList<Collection<String>> coll = new LinkedList<Collection<String>>(); 
     161        List<ImageryPreferenceEntry> entries = new ArrayList<ImageryPreferenceEntry>(); 
    135162        for (ImageryInfo info : layers) { 
    136             coll.add(info.getInfoArray()); 
     163            entries.add(new ImageryPreferenceEntry(info)); 
    137164        } 
    138         Main.pref.putArray("imagery.layers", coll); 
     165        Main.pref.putListOfStructs("imagery.entries", entries, ImageryPreferenceEntry.class); 
    139166    } 
    140167 
Note: See TracChangeset for help on using the changeset viewer.