Changeset 3803 in josm for trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java
- Timestamp:
- 2011-01-22T19:27:33+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/mappaint/StyleSource.java
r3799 r3803 2 2 package org.openstreetmap.josm.gui.mappaint; 3 3 4 import java.util.Collection; 4 5 import java.util.Collections; 5 6 import java.util.HashMap; … … 7 8 import java.util.LinkedList; 8 9 import java.util.List; 10 import java.util.regex.Matcher; 11 import java.util.regex.Pattern; 9 12 import org.openstreetmap.josm.data.osm.Node; 10 13 import org.openstreetmap.josm.data.osm.OsmPrimitive; 11 14 import org.openstreetmap.josm.data.osm.OsmUtils; 12 15 import 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; 16 import org.openstreetmap.josm.gui.preferences.SourceEntry; 17 18 public 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) { 38 40 for (String key : primitive.keySet()) { 39 41 String val = primitive.get(key); 40 42 IconElemStyle style; 41 43 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; 44 46 } 45 47 } 46 48 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; 49 51 } 50 52 } 51 53 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; 54 56 } 55 57 } 56 58 } 57 59 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>(); 70 70 for (String key : primitive.keySet()) { 71 71 String val = primitive.get(key); … … 73 73 LineElemStyle styleLine; 74 74 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 line string= 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; 81 81 } 82 82 if ((styleLine = modifiers.get(idx)) != null) { 83 over .put(idx, styleLine);83 overlayMap.put(idx, styleLine); 84 84 } 85 85 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 line string= 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; 92 92 } 93 93 if ((styleLine = modifiers.get(idx)) != null) { 94 over .put(idx, styleLine);94 overlayMap.put(idx, styleLine); 95 95 } 96 96 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 line string= 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; 103 103 } 104 104 if ((styleLine = modifiers.get(idx)) != null) { 105 over .put(idx, styleLine);105 overlayMap.put(idx, styleLine); 106 106 } 107 107 } 108 108 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; 111 111 } 112 112 } 113 113 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; 116 116 } 117 117 } 118 118 for (LineElemStyle s : modifiersList) { 119 119 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); 132 136 } 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; 156 141 } 157 142 … … 186 171 return areas.size() > 0; 187 172 } 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 } 188 259 }
Note:
See TracChangeset
for help on using the changeset viewer.