Ignore:
Timestamp:
2011-01-22T19:27:33+01:00 (13 years ago)
Author:
bastiK
Message:

Don't merge all mappaitn style rules into one StyleSet, but keep them as separate StyleSources. This allows switching styles on and off at runtime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java

    r3799 r3803  
    22package org.openstreetmap.josm.gui.mappaint;
    33
     4import java.util.Collection;
    45import java.util.Collections;
    56import java.util.HashMap;
     
    78import java.util.LinkedList;
    89import java.util.List;
     10import java.util.regex.Matcher;
     11import java.util.regex.Pattern;
    912import org.openstreetmap.josm.data.osm.Node;
    1013import org.openstreetmap.josm.data.osm.OsmPrimitive;
    1114import org.openstreetmap.josm.data.osm.OsmUtils;
    1215import org.openstreetmap.josm.data.osm.Way;
    13 
    14 public class StyleSource {
    15 
    16     public HashMap<String, IconElemStyle> icons;
    17     public HashMap<String, LineElemStyle> lines;
    18     public HashMap<String, AreaElemStyle> areas;
    19     public HashMap<String, LineElemStyle> modifiers;
    20     public LinkedList<IconElemStyle> iconsList;
    21     public LinkedList<LineElemStyle> linesList;
    22     public LinkedList<AreaElemStyle> areasList;
    23     public LinkedList<LineElemStyle> modifiersList;
    24 
    25     public StyleSource() {
    26         icons = new HashMap<String, IconElemStyle>();
    27         lines = new HashMap<String, LineElemStyle>();
    28         modifiers = new HashMap<String, LineElemStyle>();
    29         areas = new HashMap<String, AreaElemStyle>();
    30         iconsList = new LinkedList<IconElemStyle>();
    31         linesList = new LinkedList<LineElemStyle>();
    32         modifiersList = new LinkedList<LineElemStyle>();
    33         areasList = new LinkedList<AreaElemStyle>();
    34     }
    35 
    36     private IconElemStyle getNode(OsmPrimitive primitive) {
    37         IconElemStyle ret = null;
     16import org.openstreetmap.josm.gui.preferences.SourceEntry;
     17
     18public class StyleSource extends SourceEntry {
     19
     20    public final HashMap<String, IconElemStyle> icons = new HashMap<String, IconElemStyle>();
     21    public final HashMap<String, LineElemStyle> lines = new HashMap<String, LineElemStyle>();
     22    public final HashMap<String, LineElemStyle> modifiers = new HashMap<String, LineElemStyle>();
     23    public final HashMap<String, AreaElemStyle> areas = new HashMap<String, AreaElemStyle>();
     24    public final LinkedList<IconElemStyle> iconsList = new LinkedList<IconElemStyle>();
     25    public final LinkedList<LineElemStyle> linesList = new LinkedList<LineElemStyle>();
     26    public final LinkedList<LineElemStyle> modifiersList = new LinkedList<LineElemStyle>();
     27    public final LinkedList<AreaElemStyle> areasList = new LinkedList<AreaElemStyle>();
     28
     29    public boolean hasError = false;
     30
     31    public StyleSource(String url, String name, String shortdescription) {
     32        super(url, name, shortdescription, true);
     33    }
     34
     35    public StyleSource(SourceEntry entry) {
     36        super(entry.url, entry.name, entry.shortdescription, entry.active);
     37    }
     38
     39    public IconElemStyle getNode(OsmPrimitive primitive, IconElemStyle icon) {
    3840        for (String key : primitive.keySet()) {
    3941            String val = primitive.get(key);
    4042            IconElemStyle style;
    4143            if ((style = icons.get("n" + key + "=" + val)) != null) {
    42                 if (ret == null || style.priority > ret.priority) {
    43                     ret = style;
     44                if (icon == null || style.priority > icon.priority) {
     45                    icon = style;
    4446                }
    4547            }
    4648            if ((style = icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null) {
    47                 if (ret == null || style.priority > ret.priority) {
    48                     ret = style;
     49                if (icon == null || style.priority > icon.priority) {
     50                    icon = style;
    4951                }
    5052            }
    5153            if ((style = icons.get("x" + key)) != null) {
    52                 if (ret == null || style.priority > ret.priority) {
    53                     ret = style;
     54                if (icon == null || style.priority > icon.priority) {
     55                    icon = style;
    5456                }
    5557            }
    5658        }
    5759        for (IconElemStyle s : iconsList) {
    58             if ((ret == null || s.priority > ret.priority) && s.check(primitive)) {
    59                 ret = s;
    60             }
    61         }
    62         return ret;
    63     }
    64 
    65     private ElemStyle get(OsmPrimitive primitive, boolean noclosed) {
    66         AreaElemStyle retArea = null;
    67         LineElemStyle retLine = null;
    68         String linestring = null;
    69         HashMap<String, LineElemStyle> over = new HashMap<String, LineElemStyle>();
     60            if ((icon == null || s.priority > icon.priority) && s.check(primitive)) {
     61                icon = s;
     62            }
     63        }
     64        return icon;
     65    }
     66
     67    public ElemStyle get(OsmPrimitive primitive, boolean noclosed, AreaElemStyle area, LineElemStyle line) {
     68        String lineIdx = null;
     69        HashMap<String, LineElemStyle> overlayMap = new HashMap<String, LineElemStyle>();
    7070        for (String key : primitive.keySet()) {
    7171            String val = primitive.get(key);
     
    7373            LineElemStyle styleLine;
    7474            String idx = "n" + key + "=" + val;
    75             if ((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority) && (!noclosed || !styleArea.closed)) {
    76                 retArea = styleArea;
    77             }
    78             if ((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) {
    79                 retLine = styleLine;
    80                 linestring = idx;
     75            if ((styleArea = areas.get(idx)) != null && (area == null || styleArea.priority > area.priority) && (!noclosed || !styleArea.closed)) {
     76                area = styleArea;
     77            }
     78            if ((styleLine = lines.get(idx)) != null && (line == null || styleLine.priority > line.priority)) {
     79                line = styleLine;
     80                lineIdx = idx;
    8181            }
    8282            if ((styleLine = modifiers.get(idx)) != null) {
    83                 over.put(idx, styleLine);
     83                overlayMap.put(idx, styleLine);
    8484            }
    8585            idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val);
    86             if ((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority) && (!noclosed || !styleArea.closed)) {
    87                 retArea = styleArea;
    88             }
    89             if ((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) {
    90                 retLine = styleLine;
    91                 linestring = idx;
     86            if ((styleArea = areas.get(idx)) != null && (area == null || styleArea.priority > area.priority) && (!noclosed || !styleArea.closed)) {
     87                area = styleArea;
     88            }
     89            if ((styleLine = lines.get(idx)) != null && (line == null || styleLine.priority > line.priority)) {
     90                line = styleLine;
     91                lineIdx = idx;
    9292            }
    9393            if ((styleLine = modifiers.get(idx)) != null) {
    94                 over.put(idx, styleLine);
     94                overlayMap.put(idx, styleLine);
    9595            }
    9696            idx = "x" + key;
    97             if ((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority) && (!noclosed || !styleArea.closed)) {
    98                 retArea = styleArea;
    99             }
    100             if ((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) {
    101                 retLine = styleLine;
    102                 linestring = idx;
     97            if ((styleArea = areas.get(idx)) != null && (area == null || styleArea.priority > area.priority) && (!noclosed || !styleArea.closed)) {
     98                area = styleArea;
     99            }
     100            if ((styleLine = lines.get(idx)) != null && (line == null || styleLine.priority > line.priority)) {
     101                line = styleLine;
     102                lineIdx = idx;
    103103            }
    104104            if ((styleLine = modifiers.get(idx)) != null) {
    105                 over.put(idx, styleLine);
     105                overlayMap.put(idx, styleLine);
    106106            }
    107107        }
    108108        for (AreaElemStyle s : areasList) {
    109             if ((retArea == null || s.priority > retArea.priority) && (!noclosed || !s.closed) && s.check(primitive)) {
    110                 retArea = s;
     109            if ((area == null || s.priority > area.priority) && (!noclosed || !s.closed) && s.check(primitive)) {
     110                area = s;
    111111            }
    112112        }
    113113        for (LineElemStyle s : linesList) {
    114             if ((retLine == null || s.priority > retLine.priority) && s.check(primitive)) {
    115                 retLine = s;
     114            if ((line == null || s.priority > line.priority) && s.check(primitive)) {
     115                line = s;
    116116            }
    117117        }
    118118        for (LineElemStyle s : modifiersList) {
    119119            if (s.check(primitive)) {
    120                 over.put(s.getCode(), s);
    121             }
    122         }
    123         over.remove(linestring);
    124         if (over.size() != 0 && retLine != null) {
    125             List<LineElemStyle> s = new LinkedList<LineElemStyle>(over.values());
    126             Collections.sort(s);
    127             retLine = new LineElemStyle(retLine, s);
    128         }
    129         if (retArea != null) {
    130             if (retLine != null) {
    131                 return new AreaElemStyle(retArea, retLine);
     120                overlayMap.put(s.getCode(), s);
     121            }
     122        }
     123        overlayMap.remove(lineIdx); // do not use overlay if linestyle is from the same rule (example: railway=tram)
     124        if (!overlayMap.isEmpty() && line != null) {
     125            List<LineElemStyle> tmp = new LinkedList<LineElemStyle>();
     126            if (line.overlays != null) {
     127                tmp.addAll(line.overlays);
     128            }
     129            tmp.addAll(overlayMap.values());
     130            Collections.sort(tmp);
     131            line = new LineElemStyle(line, tmp);
     132        }
     133        if (area != null) {
     134            if (line != null) {
     135                return new AreaElemStyle(area, line);
    132136            } else {
    133                 return retArea;
    134             }
    135         }
    136         return retLine;
    137     }
    138 
    139     public ElemStyle get(OsmPrimitive osm) {
    140         return (!osm.hasKeys()) ? null : ((osm instanceof Node) ? getNode(osm) : get(osm, osm instanceof Way && !((Way) osm).isClosed()));
    141     }
    142 
    143     public ElemStyle getArea(Way osm) {
    144         if (osm.hasKeys()) {
    145             /* force area mode also for unclosed ways */
    146             ElemStyle style = get(osm, false);
    147             if (style != null && style instanceof AreaElemStyle) {
    148                 return style;
    149             }
    150         }
    151         return null;
    152     }
    153 
    154     public IconElemStyle getIcon(OsmPrimitive osm) {
    155         return osm.hasKeys() ? getNode(osm) : null;
     137                return area;
     138            }
     139        }
     140        return line;
    156141    }
    157142
     
    186171        return areas.size() > 0;
    187172    }
     173
     174    public void add(Rule r, Collection<Rule> rules, LineElemStyle style) {
     175        if(rules != null)
     176        {
     177            style.rules = rules;
     178            linesList.add(style);
     179        }
     180        else {
     181            String key = r.getKey();
     182            style.code = key;
     183            lines.put(key, style);
     184        }
     185    }
     186
     187    public void addModifier(Rule r, Collection<Rule> rules, LineElemStyle style) {
     188        if(rules != null)
     189        {
     190            style.rules = rules;
     191            modifiersList.add(style);
     192        }
     193        else
     194        {
     195            String key = r.getKey();
     196            style.code = key;
     197            modifiers.put(key, style);
     198        }
     199    }
     200
     201    public void add(Rule r, Collection<Rule> rules, AreaElemStyle style) {
     202        if(rules != null)
     203        {
     204            style.rules = rules;
     205            areasList.add(style);
     206        }
     207        else
     208        {
     209            String key = r.getKey();
     210            style.code = key;
     211            areas.put(key, style);
     212        }
     213    }
     214
     215    public void add(Rule r, Collection<Rule> rules, IconElemStyle style) {
     216        if(rules != null)
     217        {
     218            style.rules = rules;
     219            iconsList.add(style);
     220        }
     221        else
     222        {
     223            String key = r.getKey();
     224            style.code = key;
     225            icons.put(key, style);
     226        }
     227    }
     228
     229    /**
     230     * the name / identifier that should be used to save custom color values
     231     * and similar stuff to the preference file
     232     * @return the identifier; never null. Usually the result is "standard"
     233     */
     234    public String getPrefName() {
     235        return name == null ? "standard" : name;
     236    }
     237
     238    /**
     239     * String to show in menus and error messages.
     240     * @return Usually the shortdescription, but can be the file name
     241     * if no shortdescription is available.
     242     */
     243    public String getDisplayString() {
     244        if (shortdescription != null)
     245            return shortdescription;
     246        /**
     247         * extract file part from url, e.g.:
     248         * http://www.test.com/file.xml?format=text  --> file.xml
     249         */
     250        Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
     251        Matcher m = p.matcher(url);
     252        if (m.find()) {
     253            return m.group(1);
     254        } else {
     255            System.err.println("Warning: Unexpected URL format: "+url);
     256            return url;
     257        }
     258    }
    188259}
Note: See TracChangeset for help on using the changeset viewer.