Changeset 1240 in josm


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

cleanup style handling

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

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/visitor/MapPaintVisitor.java

    r1236 r1240  
    4747    protected Stroke currentStroke = null; 
    4848    protected Font orderFont; 
    49     protected ElemStyles styles; 
     49    protected ElemStyles.StyleSet styles; 
    5050    protected double circum; 
    5151    protected String regionalNameOrder[]; 
     
    7878        // check, if the node is visible at all 
    7979        Point p = nc.getPoint(n.eastNorth); 
    80         if ((p.x < 0) || (p.y < 0) || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; 
    81  
    82         IconElemStyle nodeStyle = (IconElemStyle)styles.get(n); 
     80        if ((!selectedCall && n.selected) || (p.x < 0) || (p.y < 0) 
     81        || (p.x > nc.getWidth()) || (p.y > nc.getHeight())) return; 
     82 
     83        IconElemStyle nodeStyle = styles != null ? (IconElemStyle)styles.get(n) : null; 
    8384        if (nodeStyle != null && isZoomOk(nodeStyle)) 
    8485            drawNode(n, nodeStyle.icon, nodeStyle.annotate, n.selected); 
     
    9697     */ 
    9798    public void visit(Way w) { 
    98         if(w.nodes.size() < 2) 
     99        if(w.nodes.size() < 2 && (!selectedCall && w.selected)) 
    99100            return; 
    100101 
     
    104105            return; 
    105106 
    106         ElemStyle wayStyle = styles.get(w); 
     107        ElemStyle wayStyle = styles != null ? styles.get(w) : null; 
    107108 
    108109        if(!isZoomOk(wayStyle)) 
     
    322323                /* nodes drawn on second call */ 
    323324                if(!(m.member instanceof Node)) 
    324                     drawSelected(m.member, styles.get(m.member), true, true); 
     325                    drawSelected(m.member, styles != null ? styles.get(m.member) 
     326                    : null, true, true); 
    325327                alreadyDrawn.add(m.member); 
    326328            } 
     
    368370                    && m.member instanceof Node) 
    369371                    { 
    370                         drawSelected(m.member, styles.get(m.member), true, true); 
     372                        drawSelected(m.member, styles != null ? styles.get(m.member) : null, true, true); 
    371373                        alreadyDrawn.add(m.member); 
    372374                    } 
     
    420422                            outer.add(w); 
    421423                        else if(r.selected) 
    422                             drawSelected(m.member, styles.get(m.member), true, true); 
     424                            drawSelected(m.member, styles != null ? styles.get(m.member) : null, true, true); 
    423425                    } 
    424426                } 
     
    432434        } 
    433435 
    434         ElemStyle wayStyle = styles.get(r); 
     436        ElemStyle wayStyle = styles != null ? styles.get(r) : null; 
    435437        /* find one wayStyle, prefer the style from Relation or take the first 
    436438        one of outer rings */ 
    437         if(wayStyle == null || !(wayStyle instanceof AreaElemStyle)) 
     439        if(styles != null && (wayStyle == null || !(wayStyle instanceof AreaElemStyle))) 
    438440        { 
    439441            for (Way w : outer) 
     
    779781    } 
    780782 
    781     // NW 111106 Overridden from SimplePaintVisitor in josm-1.4-nw1 
    782783    // Shows areas before non-areas 
    783784    public void visitAll(DataSet data, Boolean virtual) { 
     
    798799        fillAlpha = Math.min(255, Math.max(0, Integer.valueOf(Main.pref.getInteger("mappaint.fillalpha", 50)))); 
    799800        circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter 
    800         styles = MapPaintStyles.getStyles(); 
     801        styles = MapPaintStyles.getStyles().getStyleSet(); 
    801802        drawMultipolygon = Main.pref.getBoolean("mappaint.multipolygon",false); 
    802803        orderFont = new Font(Main.pref.get("mappaint.font","Helvetica"), Font.PLAIN, Main.pref.getInteger("mappaint.fontsize", 8)); 
     
    808809        selectedCall = false; 
    809810 
    810         // update the style name, just in case the user changed it in the meantime 
    811         styles.updateStyleName(); 
    812  
    813811        if(profiler) 
    814812        { 
     
    817815        } 
    818816 
    819         if (fillAreas && styles.hasAreas()) { 
     817        if (fillAreas && styles != null && styles.hasAreas()) { 
    820818            Collection<Way> noAreaWays = new LinkedList<Way>(); 
    821819 
  • 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.