Ignore:
Timestamp:
11.01.2009 11:20:44 (3 years ago)
Author:
stoecker
Message:

cleanup style handling

File:
1 edited

Legend:

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

    r1235 r1240  
    1717public class ElemStyles 
    1818{ 
    19     private class StyleSet { 
    20         HashMap<String, IconElemStyle> icons; 
    21         HashMap<String, LineElemStyle> lines; 
    22         HashMap<String, AreaElemStyle> areas; 
    23         HashMap<String, LineElemStyle> modifiers; 
     19    public class StyleSet { 
     20        private HashMap<String, IconElemStyle> icons; 
     21        private HashMap<String, LineElemStyle> lines; 
     22        private HashMap<String, AreaElemStyle> areas; 
     23        private HashMap<String, LineElemStyle> modifiers; 
    2424        public StyleSet() 
    2525        { 
     
    2929            areas = new HashMap<String, AreaElemStyle>(); 
    3030        } 
    31     } 
     31        private ElemStyle getNode(Map<String, String> keys) 
     32        { 
     33            IconElemStyle ret = null; 
     34            Iterator<String> iterator = keys.keySet().iterator(); 
     35            while(iterator.hasNext()) 
     36            { 
     37                String key = iterator.next(); 
     38                String val = keys.get(key); 
     39                IconElemStyle style; 
     40                if((style = icons.get("n" + key + "=" + val)) != null) 
     41                { 
     42                    if(ret == null || style.priority > ret.priority) 
     43                        ret = style; 
     44                } 
     45                if((style = icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null) 
     46                { 
     47                    if(ret == null || style.priority > ret.priority) 
     48                        ret = style; 
     49                } 
     50                if((style = icons.get("x" + key)) != null) 
     51                { 
     52                    if(ret == null || style.priority > ret.priority) 
     53                        ret = style; 
     54                } 
     55            } 
     56            return ret; 
     57        } 
     58        private ElemStyle get(Map<String, String> keys) 
     59        { 
     60            AreaElemStyle retArea = null; 
     61            LineElemStyle retLine = null; 
     62            String linestring = null; 
     63            HashMap<String, LineElemStyle> over = new HashMap<String, LineElemStyle>(); 
     64            Iterator<String> iterator = keys.keySet().iterator(); 
     65            while(iterator.hasNext()) 
     66            { 
     67                String key = iterator.next(); 
     68                String val = keys.get(key); 
     69                AreaElemStyle styleArea; 
     70                LineElemStyle styleLine; 
     71                String idx = "n" + key + "=" + val; 
     72                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
     73                    retArea = styleArea; 
     74                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
     75                { 
     76                    retLine = styleLine; 
     77                    linestring = idx; 
     78                } 
     79                if((styleLine = modifiers.get(idx)) != null) 
     80                    over.put(idx, styleLine); 
     81                idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val); 
     82                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
     83                    retArea = styleArea; 
     84                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
     85                { 
     86                    retLine = styleLine; 
     87                    linestring = idx; 
     88                } 
     89                if((styleLine = modifiers.get(idx)) != null) 
     90                    over.put(idx, styleLine); 
     91                idx = "x" + key; 
     92                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
     93                    retArea = styleArea; 
     94                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
     95                { 
     96                    retLine = styleLine; 
     97                    linestring = idx; 
     98                } 
     99                if((styleLine = modifiers.get(idx)) != null) 
     100                    over.put(idx, styleLine); 
     101            } 
     102            over.remove(linestring); 
     103            if(over.size() != 0 && retLine != null) 
     104            { 
     105                List<LineElemStyle> s = new LinkedList<LineElemStyle>(over.values()); 
     106                Collections.sort(s); 
     107                retLine = new LineElemStyle(retLine, s); 
     108            } 
     109            if(retArea != null) 
     110            { 
     111                if(retLine != null) 
     112                    return new AreaElemStyle(retArea, retLine); 
     113                else 
     114                    return retArea; 
     115            } 
     116            return retLine; 
     117        } 
     118 
     119        public ElemStyle get(OsmPrimitive osm) 
     120        { 
     121            return (osm.keys == null) ? null : 
     122            ((osm instanceof Node) ? getNode(osm.keys) : get(osm.keys)); 
     123        } 
     124 
     125        public boolean isArea(OsmPrimitive o) 
     126        { 
     127            if(o.keys != null && !(o instanceof Node)) 
     128            { 
     129                Iterator<String> iterator = o.keys.keySet().iterator(); 
     130                while(iterator.hasNext()) 
     131                { 
     132                    String key = iterator.next(); 
     133                    String val = o.keys.get(key); 
     134                    if(areas.containsKey("n" + key + "=" + val) 
     135                    || areas.containsKey("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val)) 
     136                    || areas.containsKey("x" + key)) 
     137                        return true; 
     138                } 
     139            } 
     140            return false; 
     141        } 
     142 
     143        public boolean hasAreas() 
     144        { 
     145            return areas.size() > 0; 
     146        } 
     147    } 
     148 
    32149    HashMap<String, StyleSet> styleSet; 
    33     String styleName; 
    34  
    35150    public ElemStyles() 
    36151    { 
    37152        styleSet = new HashMap<String, StyleSet>(); 
    38         updateStyleName(); 
    39     } 
    40  
    41     public void updateStyleName() { 
    42         // Main.pref.get() is slow when done thousands of times, do it once here and cache it 
    43         styleName = Main.pref.get("mappaint.style", "standard"); 
    44153    } 
    45154 
     
    85194    { 
    86195        if(name == null) 
    87             name = styleName; 
     196            name = Main.pref.get("mappaint.style", "standard"); 
     197 
    88198        StyleSet s = styleSet.get(name); 
    89199        if(create && s == null) 
     
    95205    } 
    96206 
    97     private ElemStyle getNode(Map<String, String> keys, StyleSet ss) 
    98     { 
    99         IconElemStyle ret = null; 
    100         Iterator<String> iterator = keys.keySet().iterator(); 
    101         while(iterator.hasNext()) 
    102         { 
    103             String key = iterator.next(); 
    104             String val = keys.get(key); 
    105             IconElemStyle style; 
    106             if((style = ss.icons.get("n" + key + "=" + val)) != null) 
    107             { 
    108                 if(ret == null || style.priority > ret.priority) 
    109                     ret = style; 
    110             } 
    111             if((style = ss.icons.get("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null) 
    112             { 
    113                 if(ret == null || style.priority > ret.priority) 
    114                     ret = style; 
    115             } 
    116             if((style = ss.icons.get("x" + key)) != null) 
    117             { 
    118                 if(ret == null || style.priority > ret.priority) 
    119                     ret = style; 
    120             } 
    121         } 
    122         return ret; 
    123     } 
    124  
    125     private ElemStyle get(Map<String, String> keys, StyleSet ss) 
    126     { 
    127         AreaElemStyle retArea = null; 
    128         LineElemStyle retLine = null; 
    129         String linestring = null; 
    130         HashMap<String, LineElemStyle> over = new HashMap<String, LineElemStyle>(); 
    131         Iterator<String> iterator = keys.keySet().iterator(); 
    132         while(iterator.hasNext()) 
    133         { 
    134             String key = iterator.next(); 
    135             String val = keys.get(key); 
    136             AreaElemStyle styleArea; 
    137             LineElemStyle styleLine; 
    138             String idx = "n" + key + "=" + val; 
    139             if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
    140                 retArea = styleArea; 
    141             if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
    142             { 
    143                 retLine = styleLine; 
    144                 linestring = idx; 
    145             } 
    146             if((styleLine = ss.modifiers.get(idx)) != null) 
    147                 over.put(idx, styleLine); 
    148             idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val); 
    149             if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
    150                 retArea = styleArea; 
    151             if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
    152             { 
    153                 retLine = styleLine; 
    154                 linestring = idx; 
    155             } 
    156             if((styleLine = ss.modifiers.get(idx)) != null) 
    157                 over.put(idx, styleLine); 
    158             idx = "x" + key; 
    159             if((styleArea = ss.areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority)) 
    160                 retArea = styleArea; 
    161             if((styleLine = ss.lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority)) 
    162             { 
    163                 retLine = styleLine; 
    164                 linestring = idx; 
    165             } 
    166             if((styleLine = ss.modifiers.get(idx)) != null) 
    167                 over.put(idx, styleLine); 
    168         } 
    169         over.remove(linestring); 
    170         if(over.size() != 0 && retLine != null) 
    171         { 
    172             List<LineElemStyle> s = new LinkedList<LineElemStyle>(over.values()); 
    173             Collections.sort(s); 
    174             retLine = new LineElemStyle(retLine, s); 
    175         } 
    176         if(retArea != null) 
    177         { 
    178             if(retLine != null) 
    179                 return new AreaElemStyle(retArea, retLine); 
    180             else 
    181                 return retArea; 
    182         } 
    183         return retLine; 
    184     } 
    185  
    186     public ElemStyle get(OsmPrimitive osm) 
    187     { 
    188         StyleSet ss = getStyleSet(null, false); 
    189         return (ss == null || osm.keys == null) ? null : 
    190         ((osm instanceof Node) ? getNode(osm.keys, ss) : get(osm.keys, ss)); 
    191     } 
    192  
    193     private boolean isArea(Map<String, String> keys, StyleSet ss) 
    194     { 
    195         Iterator<String> iterator = keys.keySet().iterator(); 
    196         while(iterator.hasNext()) 
    197         { 
    198             String key = iterator.next(); 
    199             String val = keys.get(key); 
    200             if(ss.areas.containsKey("n" + key + "=" + val) 
    201             || ss.areas.containsKey("b" + key + "=" + OsmUtils.getNamedOsmBoolean(val)) 
    202             || ss.areas.containsKey("x" + key)) 
    203                 return true; 
    204         } 
    205         return false; 
    206     } 
    207  
    208     public boolean isArea(OsmPrimitive o) 
    209     { 
    210         StyleSet ss = getStyleSet(null, false); 
    211         return (ss != null && o.keys != null && !(o instanceof Node)) 
    212         ? isArea(o.keys, ss) : false; 
    213     } 
    214  
    215     public boolean hasAreas() 
    216     { 
    217         StyleSet ss = getStyleSet(null, false); 
    218         return ss != null && ss.areas.size() > 0; 
     207    /* called from class users, never return null */ 
     208    public StyleSet getStyleSet() 
     209    { 
     210        return getStyleSet(null, false); 
    219211    } 
    220212} 
Note: See TracChangeset for help on using the changeset viewer.