Changeset 885 in josm for trunk/src


Ignore:
Timestamp:
2008-08-28T22:07:04+02:00 (16 years ago)
Author:
stoecker
Message:

added multi-style mappaint support. Closes #1186

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

Legend:

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

    r880 r885  
    4848
    4949        protected boolean isZoomOk(ElemStyle e) {
     50                if (!zoomLevelDisplay) /* show everything if the user wishes so */
     51                        return true;
     52
    5053                double circum = Main.map.mapView.getScale()*100*Main.proj.scaleFactor()*40041455; // circumference of the earth in meter
    5154
    52                 /* show everything if the user wishes so */
    53                 if (!zoomLevelDisplay) {
    54                         return true;
    55                 }
    56 
    57                 if (e == null) {
    58                         /* the default for things that don't have a rule (show, if scale is smaller than 1500m) */
    59                         if (circum < 1500)
    60                                 return true;
    61                         return false;
    62                 }
     55                if(e == null) /* the default for things that don't have a rule (show, if scale is smaller than 1500m) */
     56                        return (circum < 1500);
    6357
    6458                // formula to calculate a map scale: natural size / map size = scale
     
    6761                // so the exact "correcting value" below depends only on the screen size and resolution
    6862                // XXX - do we need a Preference setting for this (if things vary widely)?
    69                 /*System.out.println(
    70                 "Circum: " + circum +
    71                 " max: " + e.getMaxScale() + "(" + e.getMaxScale()/22 + ")" +
    72                 " min:" + e.getMinScale() + "(" + e.getMinScale()/22 + ")");*/
    73                 if(circum>=e.getMaxScale() / 22 || circum<e.getMinScale() / 22)
    74                         return false;
    75                 return true;
     63                return !(circum >= e.maxScale / 22 || circum < e.minScale / 22);
    7664        }
    7765
     
    8371         */
    8472        public void visit(Node n) {
    85                 ElemStyle nodeStyle = MapPaintStyles.getStyle(n);
    86                 if (nodeStyle!=null) {
    87                         if (nodeStyle instanceof IconElemStyle) {
    88                                 if (isZoomOk(nodeStyle)) {
    89                                         drawNode(n, ((IconElemStyle)nodeStyle).getIcon(), ((IconElemStyle)nodeStyle).doAnnotate());
    90                                 }
    91                         } else {
    92                                 // throw some sort of exception
    93                         }
    94                 } else {
     73                IconElemStyle nodeStyle = MapPaintStyles.getStyles().get(n);
     74                if (nodeStyle != null && isZoomOk(nodeStyle))
     75                        drawNode(n, nodeStyle.icon, nodeStyle.annotate);
     76                else {
    9577                        if (n.selected)
    9678                                drawNode(n, selectedColor, selectedNodeSize, selectedNodeRadius, fillSelectedNode);
     
    11395                 && (!showRelevantDirectionsOnly || w.hasDirectionKeys)));
    11496
    115                 Color colour = untaggedColor;
    116                 Color areacolour = untaggedColor;
     97                Color color = untaggedColor;
     98                Color areacolor = untaggedColor;
    11799                int width = defaultSegmentWidth;
    118100                int realWidth = 0; //the real width of the element in meters
    119101                boolean dashed = false;
    120102                boolean area = false;
    121                 ElemStyle wayStyle = MapPaintStyles.getStyle(w);
    122 
    123                 if(!isZoomOk(wayStyle)) {
     103                ElemStyle wayStyle = MapPaintStyles.getStyles().get(w);
     104
     105                if(!isZoomOk(wayStyle))
    124106                        return;
    125                 }
    126 
     107
     108                LineElemStyle l = null;
    127109                if(wayStyle!=null)
    128110                {
    129                         LineElemStyle l = null;
    130111                        if(wayStyle instanceof LineElemStyle)
    131                         {
    132112                                l = (LineElemStyle)wayStyle;
    133                         }
    134113                        else if (wayStyle instanceof AreaElemStyle)
    135114                        {
    136                                 areacolour = ((AreaElemStyle)wayStyle).colour;
    137                                 colour = areacolour;
     115                                areacolor = ((AreaElemStyle)wayStyle).color;
     116                                color = areacolor;
    138117                                l = ((AreaElemStyle)wayStyle).line;
    139118                                area = true;
     
    141120                        if(l != null)
    142121                        {
    143                                 colour = l.colour;
     122                                color = l.color;
    144123                                width = l.width;
    145124                                realWidth = l.realWidth;
     
    149128
    150129                if (area && fillAreas)
    151                         drawWayAsArea(w, areacolour);
    152                 int orderNumber = 0;
    153 
    154                 Node lastN = null;
    155                 for (Node n : w.nodes) {
    156                         if (lastN == null) {
     130                        drawWayAsArea(w, areacolor);
     131                if (realWidth > 0 && useRealWidth && !showDirection)
     132                {
     133                        int tmpWidth = (int) (100 /  (float) (circum / realWidth));
     134                        if (tmpWidth > width) width = tmpWidth;
     135                }
     136
     137                Node lastN;
     138                if(l != null && l.overlays != null)
     139                {
     140                        for(LineElemStyle s : l.overlays)
     141                        {
     142                                if(!s.over)
     143                                {
     144                                        lastN = null;
     145                                        for(Node n : w.nodes)
     146                                        {
     147                                                if(lastN != null)
     148                                                        drawSeg(lastN, n, s.color != null ? s.color : color, false, s.getWidth(width), s.dashed);
     149                                                lastN = n;
     150                                        }
     151                                }
     152                        }
     153                }
     154
     155                lastN = null;
     156                for(Node n : w.nodes)
     157                {
     158                        if(lastN != null)
     159                                drawSeg(lastN, n, w.selected ? selectedColor : color, showDirection, width, dashed);
     160                        lastN = n;
     161                }
     162
     163                if(l != null && l.overlays != null)
     164                {
     165                        for(LineElemStyle s : l.overlays)
     166                        {
     167                                if(s.over)
     168                                {
     169                                        lastN = null;
     170                                        for(Node n : w.nodes)
     171                                        {
     172                                                if(lastN != null)
     173                                                        drawSeg(lastN, n, s.color != null ? s.color : color, false, s.getWidth(width), s.dashed);
     174                                                lastN = n;
     175                                        }
     176                                }
     177                        }
     178                }
     179
     180                if(showOrderNumber)
     181                {
     182                        int orderNumber = 0;
     183                        lastN = null;
     184                        for(Node n : w.nodes)
     185                        {
     186                                if(lastN != null)
     187                                {
     188                                        orderNumber++;
     189                                        drawOrderNumber(lastN, n, orderNumber);
     190                                }
    157191                                lastN = n;
    158                                 continue;
    159                         }
    160                         orderNumber++;
    161 
    162                         if (realWidth > 0 && useRealWidth && !showDirection)
    163                         {
    164                                 int tmpWidth = (int) (100 /  (float) (circum / realWidth));
    165                                 if (tmpWidth > width) width = tmpWidth;
    166                         }
    167                         drawSeg(lastN, n, w.selected ? selectedColor : colour, showDirection, width, dashed);
    168 
    169                         if (showOrderNumber)
    170                                 drawOrderNumber(lastN, n, orderNumber);
    171 
    172                         lastN = n;
     192                        }
    173193                }
    174194        }
     
    179199
    180200        // This assumes that all segments are aligned in the same direction!
    181         protected void drawWayAsArea(Way w, Color colour)
     201        protected void drawWayAsArea(Way w, Color color)
    182202        {
    183203                Polygon polygon = new Polygon();
     
    189209                }
    190210
    191                 Color mycolor = w.selected ? selectedColor : colour;
     211                Color mycolor = w.selected ? selectedColor : color;
    192212                // set the opacity (alpha) level of the filled polygon
    193213                g.setColor(new Color( mycolor.getRed(), mycolor.getGreen(), mycolor.getBlue(), fillAlpha));
     
    299319
    300320                for (final OsmPrimitive osm : data.ways)
    301                         if (!osm.incomplete && !osm.deleted && MapPaintStyles.isArea(osm))
     321                        if (!osm.incomplete && !osm.deleted && MapPaintStyles.getStyles().isArea((Way)osm))
    302322                                osm.visit(this);
    303323                        else if (!osm.deleted && !osm.incomplete)
  • trunk/src/org/openstreetmap/josm/gui/mappaint/AreaElemStyle.java

    r874 r885  
    44public class AreaElemStyle extends ElemStyle
    55{
    6         public Color colour;
     6        public Color color;
    77        public LineElemStyle line = null;
    88
    9         public AreaElemStyle (Color colour, long maxScale, long minScale) {
    10                 this.colour = colour;
     9        public AreaElemStyle (AreaElemStyle a, long maxScale, long minScale) {
     10                this.color = a.color;
     11                this.priority = a.priority;
    1112                this.maxScale = maxScale;
    1213                this.minScale = minScale;
     
    1516        public AreaElemStyle(AreaElemStyle a, LineElemStyle l)
    1617        {
    17                 this.colour = a.colour;
     18                this.color = a.color;
     19                this.priority = a.priority;
    1820                this.maxScale = a.maxScale;
    1921                this.minScale = a.minScale;
     
    2123        }
    2224
    23         @Override public String toString() {
    24                 return "AreaElemStyle:   colour=" + colour;
     25        public AreaElemStyle() { init(); }
     26
     27        public void init()
     28        {
     29                color = null;
     30                priority = 0;
    2531        }
    2632}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyle.java

    r627 r885  
    44{
    55        // zoom range to display the feature
    6         protected long minScale;
    7         protected long maxScale;
     6        public long minScale;
     7        public long maxScale;
    88
    9         public long getMinScale() {
    10                 return minScale;
    11         }
    12         public long getMaxScale() {
    13                 return maxScale;
    14         }
     9        public int priority;
    1510}
    1611
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyleHandler.java

    r874 r885  
    22
    33import java.awt.Color;
    4 import java.awt.Toolkit;
    5 import java.io.File;
    6 import java.net.URL;
    7 
    8 import javax.swing.ImageIcon;
    94
    105import org.openstreetmap.josm.tools.ColorHelper;
    11 import org.openstreetmap.josm.Main;
    126import org.xml.sax.Attributes;
    137import org.xml.sax.helpers.DefaultHandler;
    148
     9import org.openstreetmap.josm.Main;
     10
    1511public class ElemStyleHandler extends DefaultHandler
    1612{
    17         boolean inDoc, inRule, inCondition, inElemStyle, inLine, inIcon, inArea, inScaleMax, inScaleMin;
    18         String curKey = null;
    19         String curValue = null;
    20         String curBoolean = null;
    21         int curLineWidth = -1;
    22         int curLineRealWidth = 0;
    23         boolean curLineDashed = false;
    24         Color curLineColour = null;
    25         Color curAreaColour = null;
    26         ImageIcon curIcon = null;
    27         boolean curIconAnnotate = true;
    28         long curScaleMax = 1000000000;
    29         long curScaleMin = 0;
     13        boolean inDoc, inRule, inCondition, inElemStyle, inLine, inLineMod, inIcon, inArea, inScaleMax, inScaleMin;
     14        boolean hadLine, hadLineMod, hadIcon, hadArea;
     15        ElemStyles styles;
     16        RuleElem rule = new RuleElem();
     17
     18        class RuleElem {
     19                String key;
     20                String value;
     21                String boolValue;
     22                long scaleMax;
     23                long scaleMin;
     24                LineElemStyle line = new LineElemStyle();
     25                LineElemStyle linemod = new LineElemStyle();
     26                AreaElemStyle area = new AreaElemStyle();
     27                IconElemStyle icon = new IconElemStyle();
     28                public void init()
     29                {
     30                        key = value = boolValue = null;
     31                        scaleMax = 1000000000;
     32                        scaleMin = 0;
     33                        line.init();
     34                        linemod.init();
     35                        area.init();
     36                        icon.init();
     37                }
     38        };
    3039
    3140        public ElemStyleHandler() {
    3241                inDoc=inRule=inCondition=inElemStyle=inLine=inIcon=inArea=false;
     42                rule.init();
     43                styles = MapPaintStyles.getStyles();
    3344        }
    3445
     
    5465        }
    5566
    56         @Override public void startElement(String uri,String name, String qName,
    57                         Attributes atts) {
    58                 if (inDoc==true)        {
    59                         if (qName.equals("rule")) {
     67        @Override public void startElement(String uri,String name, String qName, Attributes atts) {
     68                if (inDoc==true)
     69                {
     70                        if (qName.equals("rule"))
    6071                                inRule=true;
    61                         }
    62                         else if (qName.equals("condition") && inRule) {
     72                        else if (qName.equals("scale_max"))
     73                                inScaleMax = true;
     74                        else if (qName.equals("scale_min"))
     75                                inScaleMin = true;
     76                        else if (qName.equals("condition") && inRule)
     77                        {
    6378                                inCondition=true;
    64                                 for (int count=0; count<atts.getLength(); count++) {
     79                                for (int count=0; count<atts.getLength(); count++)
     80                                {
    6581                                        if(atts.getQName(count).equals("k"))
     82                                                rule.key = atts.getValue(count);
     83                                        else if(atts.getQName(count).equals("v"))
     84                                                rule.value = atts.getValue(count);
     85                                        else if(atts.getQName(count).equals("b"))
     86                                                rule.boolValue = atts.getValue(count);
     87                                }
     88                        }
     89                        else if (qName.equals("line"))
     90                        {
     91                                hadLine = inLine = true;
     92                                for (int count=0; count<atts.getLength(); count++)
     93                                {
     94                                        if(atts.getQName(count).equals("width"))
     95                                                rule.line.width = Integer.parseInt(atts.getValue(count));
     96                                        else if (atts.getQName(count).equals("colour"))
     97                                                rule.line.color=convertColor(atts.getValue(count));
     98                                        else if (atts.getQName(count).equals("realwidth"))
     99                                                rule.line.realWidth=Integer.parseInt(atts.getValue(count));
     100                                        else if (atts.getQName(count).equals("dashed"))
     101                                                rule.line.dashed=Boolean.parseBoolean(atts.getValue(count));
     102                                        else if(atts.getQName(count).equals("priority"))
     103                                                rule.line.priority = Integer.parseInt(atts.getValue(count));
     104                                }
     105                        }
     106                        else if (qName.equals("linemod"))
     107                        {
     108                                hadLineMod = inLine = true;
     109                                for (int count=0; count<atts.getLength(); count++)
     110                                {
     111                                        if(atts.getQName(count).equals("width"))
    66112                                        {
    67                                                 curKey = atts.getValue(count);
    68                                                 curBoolean = null;
    69                                                 curValue = null;
     113                                                String val = atts.getValue(count);
     114                                                if(val.startsWith("+"))
     115                                                {
     116                                                        rule.line.width = Integer.parseInt(val.substring(1));
     117                                                        rule.line.widthMode = LineElemStyle.WidthMode.OFFSET;
     118                                                }
     119                                                else if(val.startsWith("-"))
     120                                                {
     121                                                        rule.line.width = Integer.parseInt(val);
     122                                                        rule.line.widthMode = LineElemStyle.WidthMode.OFFSET;
     123                                                }
     124                                                else if(val.endsWith("%"))
     125                                                {
     126                                                        rule.line.width = Integer.parseInt(val.substring(0, val.length()-1));
     127                                                        rule.line.widthMode = LineElemStyle.WidthMode.PERCENT;
     128                                                }
     129                                                else
     130                                                        rule.line.width = Integer.parseInt(val);
    70131                                        }
    71                                         else if(atts.getQName(count).equals("v"))
    72                                                 curValue = atts.getValue(count);
    73                                         else if(atts.getQName(count).equals("b"))
    74                                                 curBoolean = atts.getValue(count);
    75                                 }
    76                         } else if (qName.equals("line")) {
    77                                 inLine = true;
    78                                 for (int count=0; count<atts.getLength(); count++) {
    79                                         if(atts.getQName(count).equals("width"))
    80                                                 curLineWidth = Integer.parseInt(atts.getValue(count));
    81132                                        else if (atts.getQName(count).equals("colour"))
    82                                                 curLineColour=convertColor(atts.getValue(count));
     133                                                rule.line.color=convertColor(atts.getValue(count));
    83134                                        else if (atts.getQName(count).equals("realwidth"))
    84                                                 curLineRealWidth=Integer.parseInt(atts.getValue(count));
     135                                                rule.line.realWidth=Integer.parseInt(atts.getValue(count));
    85136                                        else if (atts.getQName(count).equals("dashed"))
    86                                                 curLineDashed=Boolean.parseBoolean(atts.getValue(count));
    87                                 }
    88                         } else if (qName.equals("scale_max")) {
    89                                 inScaleMax = true;
    90                         } else if (qName.equals("scale_min")) {
    91                                 inScaleMin = true;
    92                         } else if (qName.equals("icon")) {
    93                                 inIcon = true;
    94                                 for (int count=0; count<atts.getLength(); count++) {
    95                                         if (atts.getQName(count).equals("src")) {
    96                                                 if(!MapPaintStyles.isInternal())
    97                                                 {
    98                                                         String imageFile = MapPaintStyles.getImageDir()+atts.getValue(count);
    99                                                         File f = new File(imageFile);
    100                                                         if (f.exists()) {
    101                                                                 //open icon from user directory
    102                                                                 curIcon = new ImageIcon(imageFile);
    103                                                                 continue;
    104                                                         }
    105                                                 }
    106                                                 try {
    107                                                         URL path = getClass().getResource(MapPaintStyles.getInternalImageDir()+atts.getValue(count));
    108                                                         if (path == null) {
    109                                                                 /* icon not found, using default */
    110                                                                 System.out.println("Mappaint: Icon " + atts.getValue(count) + " not found, using default icon");
    111                                                                 path = getClass().getResource(MapPaintStyles.getInternalImageDir()+"misc/no_icon.png");
    112                                                                 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
    113                                                         } else {
    114                                                                 curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
    115                                                         }
    116                                                 }
    117                                                 catch (Exception e){
    118                                                         URL path = getClass().getResource(MapPaintStyles.getInternalImageDir()+"incomming/amenity.png");
    119                                                         curIcon = new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
    120                                                 }
    121                                         } else if (atts.getQName(count).equals("annotate")) {
    122                                                 curIconAnnotate = Boolean.parseBoolean (atts.getValue(count));
    123                                         }
     137                                                rule.line.dashed=Boolean.parseBoolean(atts.getValue(count));
     138                                        else if(atts.getQName(count).equals("priority"))
     139                                                rule.line.priority = Integer.parseInt(atts.getValue(count));
     140                                        else if(atts.getQName(count).equals("mode"))
     141                                                rule.line.over = !atts.getValue(count).equals("under");
     142                                }
     143                        }
     144                        else if (qName.equals("icon"))
     145                        {
     146                                hadIcon = inIcon = true;
     147                                for (int count=0; count<atts.getLength(); count++)
     148                                {
     149                                        if (atts.getQName(count).equals("src"))
     150                                                rule.icon.icon = MapPaintStyles.getIcon(atts.getValue(count));
     151                                        else if (atts.getQName(count).equals("annotate"))
     152                                                rule.icon.annotate = Boolean.parseBoolean (atts.getValue(count));
     153                                        else if(atts.getQName(count).equals("priority"))
     154                                                rule.icon.priority = Integer.parseInt(atts.getValue(count));
    124155                                }
    125156                        }
    126157                        else if (qName.equals("area"))
    127158                        {
    128                                 inArea = true;
     159                                hadArea = inArea = true;
    129160                                for (int count=0; count<atts.getLength(); count++)
    130161                                {
    131162                                        if (atts.getQName(count).equals("colour"))
    132                                                 curAreaColour=convertColor(atts.getValue(count));
     163                                                rule.area.color=convertColor(atts.getValue(count));
     164                                        else if(atts.getQName(count).equals("priority"))
     165                                                rule.area.priority = Integer.parseInt(atts.getValue(count));
    133166                                }
    134167                        }
     
    138171        @Override public void endElement(String uri,String name, String qName)
    139172        {
    140                 if (inRule && qName.equals("rule")) {
    141                         ElemStyle newStyle;
     173                if (inRule && qName.equals("rule"))
     174                {
     175                        if(hadLine)
     176                                styles.add(rule.key, rule.value, rule.boolValue,
     177                                new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
     178                        if(hadLineMod)
     179                                styles.addModifier(rule.key, rule.value, rule.boolValue,
     180                                new LineElemStyle(rule.line, rule.scaleMax, rule.scaleMin));
     181                        if(hadIcon)
     182                                styles.add(rule.key, rule.value, rule.boolValue,
     183                                new IconElemStyle(rule.icon, rule.scaleMax, rule.scaleMin));
     184                        if(hadArea)
     185                                styles.add(rule.key, rule.value, rule.boolValue,
     186                                new AreaElemStyle(rule.area, rule.scaleMax, rule.scaleMin));
    142187                        inRule = false;
    143                         if (curLineWidth != -1) {
    144                                 newStyle = new LineElemStyle(curLineWidth, curLineRealWidth, curLineColour,
    145                                                 curLineDashed, curScaleMax, curScaleMin);
    146                                 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
    147                                 curLineWidth = -1;
    148                                 curLineRealWidth= 0;
    149                                 curLineDashed = false;
    150                                 curLineColour = null;
    151                         }
    152                        
    153                         if (curIcon != null) {
    154                                 newStyle = new IconElemStyle(curIcon, curIconAnnotate, curScaleMax, curScaleMin);
    155                                 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
    156                                 curIcon = null;
    157                                 curIconAnnotate = true;
    158                         }
    159                         if (curAreaColour != null) {
    160                                 newStyle = new AreaElemStyle (curAreaColour, curScaleMax, curScaleMin);
    161                                 MapPaintStyles.add(curKey, curValue, curBoolean, newStyle);
    162                                 curAreaColour = null;
    163                         }
    164                         curScaleMax = 1000000000;
    165                         curScaleMin = 0;
    166 
     188                        hadLine = hadLineMod = hadIcon = hadArea = false;
     189                        rule.init();
    167190                }
    168191                else if (inCondition && qName.equals("condition"))
     
    180203        }
    181204
    182         @Override public void characters(char ch[], int start, int length) {
    183                 if (inScaleMax == true) {
    184                         String content = new String(ch, start, length);
    185                         curScaleMax = Long.parseLong(content);
    186                 }
    187                 if (inScaleMin == true) {
    188                         String content = new String(ch, start, length);
    189                         curScaleMin = Long.parseLong(content);
    190                 }
     205        @Override public void characters(char ch[], int start, int length)
     206        {
     207                if (inScaleMax == true)
     208                        rule.scaleMax = Long.parseLong(new String(ch, start, length));
     209                else if (inScaleMin == true)
     210                        rule.scaleMin = Long.parseLong(new String(ch, start, length));
    191211        }
    192212}
    193 
    194 
    195 
    196 
  • trunk/src/org/openstreetmap/josm/gui/mappaint/ElemStyles.java

    r874 r885  
    11package org.openstreetmap.josm.gui.mappaint;
    22
     3import java.util.Collections;
    34import java.util.HashMap;
     5import java.util.LinkedList;
     6import java.util.List;
    47import java.util.Iterator;
    58
     9import org.openstreetmap.josm.data.osm.Node;
    610import org.openstreetmap.josm.data.osm.OsmPrimitive;
    711import org.openstreetmap.josm.data.osm.OsmUtils;
     12import org.openstreetmap.josm.data.osm.Way;
    813
    914public class ElemStyles
    1015{
    11         private HashMap<String, ElemStyle> styles;
     16        private HashMap<String, IconElemStyle> icons;
     17        private HashMap<String, LineElemStyle> lines;
     18        private HashMap<String, AreaElemStyle> areas;
     19        private HashMap<String, LineElemStyle> modifiers;
    1220
    1321        public ElemStyles()
    1422        {
    15                 styles = new HashMap<String, ElemStyle>();
     23                icons = new HashMap<String, IconElemStyle>();
     24                lines = new HashMap<String, LineElemStyle>();
     25                modifiers = new HashMap<String, LineElemStyle>();
     26                areas = new HashMap<String, AreaElemStyle>();
    1627        }
    1728
    18         public void add(String k, String v, String b, ElemStyle style)
     29        private String getKey(String k, String v, String b)
    1930        {
    20                 ElemStyle  old_style;
    21                 String key;
    22 
    23                 /* unfortunately, there don't seem to be an efficient way to */
    24                 /* find out, if a given OsmPrimitive is an area or not, */
    25                 /* so distinguish only between way and node here - for now */
    26                 if (style instanceof AreaElemStyle)
    27                         key = "w";
    28                 else if (style instanceof LineElemStyle)
    29                         key = "w";
    30                 else if (style instanceof IconElemStyle)
    31                         key = "n";
     31                if(v != null)
     32                        return "n" + k + "=" + v;
     33                else if(b != null)
     34                        return "b" + k  + "=" + OsmUtils.getNamedOsmBoolean(b);
    3235                else
    33                         key = "";
    34 
    35                 if(v != null)
    36                         key += "n" + k + "=" + v;
    37                 else if(b != null)
    38                         key += "b" + k  + "=" + OsmUtils.getNamedOsmBoolean(b);
    39                 else
    40                         key += "x" + k;
    41 
    42                 /* avoid duplicates - for now */
    43                 old_style = styles.get(key);
    44                 if (old_style == null) {
    45                         /* new key/value, insert */
    46                         styles.put(key, style);
    47                 } else {
    48                         if (style.getMaxScale() < old_style.getMaxScale()) {
    49                                 /* existing larger scale key/value, replace */
    50                                 styles.remove(old_style);
    51                                 styles.put(key, style);
    52                         }
    53                 }
     36                        return "x" + k;
    5437        }
    5538
    56         public ElemStyle get(OsmPrimitive p, Boolean area)
     39        public void add(String k, String v, String b, LineElemStyle style)
    5740        {
    58                 if (p.keys!=null) {
    59                         String classname;
    60                         String kv = null;
     41                lines.put(getKey(k,v,b), style);
     42        }
    6143
    62                         if (p instanceof org.openstreetmap.josm.data.osm.Node) {
    63                                 if(area)
    64                                         return null;
    65                                 classname = "n";
    66                         } else {
    67                                 classname = "w";
    68                         }
    69                         Iterator<String> iterator = p.keys.keySet().iterator();
    70                         while (iterator.hasNext())
     44        public void addModifier(String k, String v, String b, LineElemStyle style)
     45        {
     46                modifiers.put(getKey(k,v,b), style);
     47        }
     48
     49        public void add(String k, String v, String b, AreaElemStyle style)
     50        {
     51                areas.put(getKey(k,v,b), style);
     52        }
     53
     54        public void add(String k, String v, String b, IconElemStyle style)
     55        {
     56                icons.put(getKey(k,v,b), style);
     57        }
     58
     59        public IconElemStyle get(Node n)
     60        {
     61                IconElemStyle ret = null;
     62                if(n.keys != null)
     63                {
     64                        Iterator<String> iterator = n.keys.keySet().iterator();
     65                        while(iterator.hasNext())
    7166                        {
    7267                                String key = iterator.next();
    73                                 ElemStyle style = null;
    74                                 kv = classname + "n" + key + "=" + p.keys.get(key);
    75                                 if (styles.containsKey(kv))
     68                                String val = n.keys.get(key);
     69                                IconElemStyle style;
     70                                if((style = icons.get("n" + key + "=" + val)) != null)
    7671                                {
    77                                         style = styles.get(kv);
    78                                         if(area == style instanceof AreaElemStyle)
    79                                                 return style;
     72                                        if(ret == null || style.priority > ret.priority)
     73                                                ret = style;
    8074                                }
    81                                 kv = classname + "b" + key + "=" + OsmUtils.getNamedOsmBoolean(p.keys.get(key));
    82                                 if (styles.containsKey(kv))
     75                                if((style = icons.get("n" + key + "=" + OsmUtils.getNamedOsmBoolean(val))) != null)
    8376                                {
    84                                         style = styles.get(kv);
    85                                         if(area == style instanceof AreaElemStyle)
    86                                                 return style;
     77                                        if(ret == null || style.priority > ret.priority)
     78                                                ret = style;
    8779                                }
    88                                 kv = classname + "x" + key;
    89                                 if (styles.containsKey(kv))
     80                                if((style = icons.get("x" + key)) != null)
    9081                                {
    91                                         style = styles.get(kv);
    92                                         if(area == style instanceof AreaElemStyle)
    93                                                 return style;
     82                                        if(ret == null || style.priority > ret.priority)
     83                                                ret = style;
    9484                                }
    9585                        }
    9686                }
    97 
    98                 return null;
     87                return ret;
    9988        }
    10089
    101         public boolean isArea(OsmPrimitive p)
     90        public ElemStyle get(Way w)
    10291        {
    103                 return get(p, true) instanceof AreaElemStyle;
     92                AreaElemStyle retArea = null;
     93                LineElemStyle retLine = null;
     94                List<LineElemStyle> over = new LinkedList<LineElemStyle>();
     95                if(w.keys != null)
     96                {
     97                        Iterator<String> iterator = w.keys.keySet().iterator();
     98                        while(iterator.hasNext())
     99                        {
     100                                String key = iterator.next();
     101                                String val = w.keys.get(key);
     102                                AreaElemStyle styleArea;
     103                                LineElemStyle styleLine;
     104                                String idx = "n" + key + "=" + val;
     105                                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
     106                                        retArea = styleArea;
     107                                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
     108                                        retLine = styleLine;
     109                                if((styleLine = modifiers.get(idx)) != null)
     110                                        over.add(styleLine);
     111                                idx = "b" + key + "=" + OsmUtils.getNamedOsmBoolean(val);
     112                                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
     113                                        retArea = styleArea;
     114                                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
     115                                        retLine = styleLine;
     116                                if((styleLine = modifiers.get(idx)) != null)
     117                                        over.add(styleLine);
     118                                idx = "x" + key;
     119                                if((styleArea = areas.get(idx)) != null && (retArea == null || styleArea.priority > retArea.priority))
     120                                        retArea = styleArea;
     121                                if((styleLine = lines.get(idx)) != null && (retLine == null || styleLine.priority > retLine.priority))
     122                                        retLine = styleLine;
     123                                if((styleLine = modifiers.get(idx)) != null)
     124                                        over.add(styleLine);
     125                        }
     126                }
     127                if(over.size() != 0 && retLine != null)
     128                {
     129                        Collections.sort(over);
     130                        retLine = new LineElemStyle(retLine, over);
     131                }
     132                if(retArea != null)
     133                {
     134                        if(retLine != null)
     135                                return new AreaElemStyle(retArea, retLine);
     136                        else
     137                                return retArea;
     138                }
     139                return retLine;
     140        }
     141
     142        public boolean isArea(Way w)
     143        {
     144                if(w.keys != null)
     145                {
     146                        Iterator<String> iterator = w.keys.keySet().iterator();
     147                        while(iterator.hasNext())
     148                        {
     149                                String key = iterator.next();
     150                                String val = w.keys.get(key);
     151                                if(areas.containsKey("n" + key + "=" + val)
     152                                || areas.containsKey("n" + key + "=" + OsmUtils.getNamedOsmBoolean(val))
     153                                || areas.containsKey("x" + key))
     154                                        return true;
     155                        }
     156                }
     157                return false;
    104158        }
    105159}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/IconElemStyle.java

    r627 r885  
    44public class IconElemStyle extends ElemStyle
    55{
    6         ImageIcon icon;
    7         boolean annotate;
     6        public ImageIcon icon;
     7        public boolean annotate;
    88
    9         public IconElemStyle (ImageIcon icon, boolean annotate, long maxScale, long minScale) {
    10                 this.icon=icon;
    11                 this.annotate=annotate;
     9        public IconElemStyle (IconElemStyle i, long maxScale, long minScale) {
     10                this.icon = i.icon;
     11                this.annotate = i.annotate;
     12                this.priority = i.priority;
    1213                this.maxScale = maxScale;
    1314                this.minScale = minScale;
    14         }       
    15        
    16         public ImageIcon getIcon() {
    17                 return icon;
    1815        }
     16        public IconElemStyle() { init(); }
    1917
    20         public boolean doAnnotate() {
    21                 return annotate;
    22         }
    23 
    24         @Override public String toString()
     18        public void init()
    2519        {
    26                 return "IconElemStyle:  icon= " + icon +  " annotate=" + annotate;
     20                icon = null;
     21                priority = 0;
     22                annotate = true;
    2723        }
    2824}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/LineElemStyle.java

    r627 r885  
    11package org.openstreetmap.josm.gui.mappaint;
     2
    23import java.awt.Color;
     4import java.util.LinkedList;
     5import java.util.List;
    36
    4 public class LineElemStyle extends ElemStyle
     7public class LineElemStyle extends ElemStyle implements Comparable<LineElemStyle>
    58{
    69        public int width;
    7         public int realWidth = 0; //the real width of this line in meter
    8         public Color colour;
    9         public boolean dashed = false;
     10        public int realWidth; //the real width of this line in meter
     11        public Color color;
     12        public boolean dashed;
    1013
    11         public LineElemStyle (int width, int realWidth, Color colour, boolean dashed, long maxScale, long minScale) {
    12                 this.width = width;
    13                 this.realWidth = realWidth;
    14                 this.colour = colour;
    15                 this.dashed = dashed;
     14        public boolean over;
     15        public enum WidthMode { ABSOLUTE, PERCENT, OFFSET };
     16        public WidthMode widthMode;
     17
     18        public List<LineElemStyle> overlays;
     19
     20        public LineElemStyle(LineElemStyle s, long maxScale, long minScale) {
     21                this.width = s.width;
     22                this.realWidth = s.realWidth;
     23                this.color = s.color;
     24                this.dashed = s.dashed;
     25                this.over = s.over;
     26                this.widthMode = s.widthMode;
     27
     28                this.priority = s.priority;
    1629                this.maxScale = maxScale;
    1730                this.minScale = minScale;
    1831        }
    1932
    20         @Override public String toString() {
    21                 return "LineElemStyle:  width= " + width + "realWidth= " + realWidth +  " colour=" + colour + " dashed=" + dashed;
     33        public LineElemStyle(LineElemStyle s, List<LineElemStyle> overlays) {
     34                this.width = s.width;
     35                this.realWidth = s.realWidth;
     36                this.color = s.color;
     37                this.dashed = s.dashed;
     38                this.over = s.over;
     39                this.widthMode = s.widthMode;
     40
     41                this.priority = s.priority;
     42                this.maxScale = s.maxScale;
     43                this.minScale = s.minScale;
     44
     45                this.overlays = overlays;
     46        }
     47
     48        public LineElemStyle() { init(); }
     49
     50        public void init()
     51        {
     52                width = 1;
     53                realWidth = 0;
     54                dashed = false;
     55                priority = 0;
     56                color = null;
     57                over = true; // only used for line modifications
     58                widthMode = WidthMode.ABSOLUTE;
     59                overlays = null;
     60        };
     61
     62        // get width for overlays
     63        public int getWidth(int ref)
     64        {
     65                int res;
     66                if(widthMode == WidthMode.ABSOLUTE)
     67                        res = width;
     68                else if(widthMode == WidthMode.OFFSET)
     69                        res = ref + width;
     70                else
     71                {
     72                        if(width < 0)
     73                                res = 0;
     74                        else
     75                                res = ref*width/100;
     76                }
     77                return res <= 0 ? 1 : res;
     78        }
     79
     80        public int compareTo(LineElemStyle s)
     81        {
     82                if(s.priority != priority)
     83                        return s.priority > priority ? 1 : -1;
     84                if(!over && s.over)
     85                        return -1;
     86                // we have no idea how to order other objects :-)
     87                return 0;
    2288        }
    2389}
  • trunk/src/org/openstreetmap/josm/gui/mappaint/MapPaintStyles.java

    r874 r885  
    11package org.openstreetmap.josm.gui.mappaint;
    22
     3import java.awt.Toolkit;
    34import java.io.File;
    45import java.io.FileReader;
     
    67import java.util.HashMap;
    78import java.util.Iterator;
     9
     10import javax.swing.ImageIcon;
    811
    912import org.openstreetmap.josm.Main;
     
    2225        private static ElemStyles styles = new ElemStyles();
    2326       
    24         public static String getStyleDir(){
    25                 return styleDir;
     27        public static ElemStyles getStyles()
     28        {
     29                return styles;
    2630        }
    27         public static String getImageDir(){
    28                 return imageDir;
    29         }
    30         public static String getInternalImageDir(){
    31                 return internalImageDir;
    32         }
    33         public static Boolean isInternal(){
    34                 return isInternal;
    35         }
    36         public static void add(String k, String v, String b, ElemStyle style)
     31
     32        public static ImageIcon getIcon(String name)
    3733        {
    38                 styles.add(k, v, b, style);
    39         }
    40         public static ElemStyle getStyle(OsmPrimitive osm)
    41         {
    42                 ElemStyle s = styles.get(osm, true);
    43                 if(s != null)
     34                try {
     35                        if(isInternal)
     36                        {
     37                                String imageFile = imageDir+name;
     38                                File f = new File(imageFile);
     39                                if(f.exists())
     40                                {
     41                                        //open icon from user directory
     42                                        return new ImageIcon(imageFile);
     43                                }
     44                        }
     45                        URL path = Main.class.getResource(internalImageDir+name);
     46                        if(path == null)
     47                        {
     48                                System.out.println("Mappaint: Icon " + name + " not found, using default icon");
     49                                path = Main.class.getResource(internalImageDir+"misc/no_icon.png");
     50                        }
     51                        return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
     52                }
     53                catch (Exception e)
    4454                {
    45                         ElemStyle l = styles.get(osm, false);
    46                         if(l != null && l instanceof LineElemStyle)
    47                         {
    48                                 s = new AreaElemStyle((AreaElemStyle)s, (LineElemStyle)l);
    49                         }
     55                        URL path = Main.class.getResource(internalImageDir+"incomming/amenity.png");
     56                        return new ImageIcon(Toolkit.getDefaultToolkit().createImage(path));
    5057                }
    51                 else
    52                         s = styles.get(osm, false);
    53                 return s;
    54         }
    55         public static boolean isArea(OsmPrimitive osm)
    56         {
    57                 return styles.isArea(osm);
    5858        }
    5959
    6060        public static void readFromPreferences() {
    61 
    6261                String styleName = Main.pref.get("mappaint.style", "standard");
    6362                // fallback to standard name for internal case, as we only have one internal style
    6463                String internalStyleName = "standard";
    6564                styleDir = Main.pref.get("mappaint.styledir", Main.pref.getPreferencesDir()+"plugins/mappaint/"+styleName+"/");
    66                 String elemStylesFile = getStyleDir()+"elemstyles.xml";
     65                String elemStylesFile = styleDir+"elemstyles.xml";
    6766                imageDir = styleDir+"icons/";
    6867                internalImageDir = "/images/styles/"+internalStyleName+"/";
  • trunk/src/org/openstreetmap/josm/tools/ColorHelper.java

    r627 r885  
    1212                if (html.length() > 0 && html.charAt(0) == '#')
    1313                        html = html.substring(1);
    14                 if (html.length() != 6)
     14                else if (html.length() != 6 && html.length() != 8)
    1515                        return null;
    1616                try {
     
    1818                                        Integer.parseInt(html.substring(0,2),16),
    1919                                        Integer.parseInt(html.substring(2,4),16),
    20                                         Integer.parseInt(html.substring(4,6),16));
     20                                        Integer.parseInt(html.substring(4,6),16),
     21                                        (html.length() == 8 ? Integer.parseInt(html.substring(6,8),16) : 255));
    2122                } catch (NumberFormatException e) {
    2223                        return null;
Note: See TracChangeset for help on using the changeset viewer.