source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/Environment.java@ 8206

Last change on this file since 8206 was 8206, checked in by simon04, 9 years ago

fix #10299 - MapCSS index for last element of object

Negative index numbers count from last to first, so >[index=-1] matches the last one.

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint;
3
4import org.openstreetmap.josm.data.osm.OsmPrimitive;
5import org.openstreetmap.josm.data.osm.Relation;
6import org.openstreetmap.josm.gui.mappaint.mapcss.Condition.Context;
7import org.openstreetmap.josm.gui.mappaint.mapcss.Selector.LinkSelector;
8import org.openstreetmap.josm.tools.CheckParameterUtil;
9
10/**
11 * Environment is a data object to provide access to various "global" parameters.
12 * It is used during processing of MapCSS rules and for the generation of
13 * style elements.
14 */
15public class Environment {
16
17 public OsmPrimitive osm;
18
19 public MultiCascade mc;
20 public String layer;
21 public StyleSource source;
22 private Context context = Context.PRIMITIVE;
23 public static final String DEFAULT_LAYER = "default";
24
25 /**
26 * If not null, this is the matching parent object if a condition or an expression
27 * is evaluated in a {@link LinkSelector} (within a child selector)
28 */
29 public OsmPrimitive parent;
30
31 /**
32 * The same for parent selector. Only one of the 2 fields (parent or child) is not null in any environment.
33 */
34 public OsmPrimitive child;
35
36 /**
37 * index of node in parent way or member in parent relation. Must be != null in LINK context.
38 */
39 public Integer index = null;
40
41 /**
42 * count of nodes in parent way or members in parent relation. Must be != null in LINK context.
43 */
44 public Integer count = null;
45
46 /**
47 * Creates a new uninitialized environment.
48 */
49 public Environment() {}
50
51 /**
52 * Creates a new environment.
53 */
54 public Environment(OsmPrimitive osm, MultiCascade mc, String layer, StyleSource source) {
55 this.osm = osm;
56 this.mc = mc;
57 this.layer = layer;
58 this.source = source;
59 }
60
61 /**
62 * Creates a clone of the environment {@code other}.
63 *
64 * @param other the other environment. Must not be null.
65 * @throws IllegalArgumentException if {@code param} is {@code null}
66 */
67 public Environment(Environment other) throws IllegalArgumentException {
68 CheckParameterUtil.ensureParameterNotNull(other);
69 this.osm = other.osm;
70 this.mc = other.mc;
71 this.layer = other.layer;
72 this.parent = other.parent;
73 this.child = other.child;
74 this.source = other.source;
75 this.index = other.index;
76 this.count = other.count;
77 this.context = other.getContext();
78 }
79
80 /**
81 * Creates a clone of this environment, with the specified primitive.
82 * @return A clone of this environment, with the specified primitive
83 * @see #osm
84 */
85 public Environment withPrimitive(OsmPrimitive osm) {
86 Environment e = new Environment(this);
87 e.osm = osm;
88 return e;
89 }
90
91 /**
92 * Creates a clone of this environment, with the specified parent.
93 * @param parent the matching parent object
94 * @return A clone of this environment, with the specified parent
95 * @see #parent
96 */
97 public Environment withParent(OsmPrimitive parent) {
98 Environment e = new Environment(this);
99 e.parent = parent;
100 return e;
101 }
102
103 /**
104 * Creates a clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}.
105 * @param parent the matching parent object
106 * @param index index of node in parent way or member in parent relation
107 * @param count count of nodes in parent way or members in parent relation
108 * @return A clone of this environment, with the specified parent, index, and context set to {@link Context#LINK}
109 * @since 6175
110 * @see #parent
111 * @see #index
112 */
113 public Environment withParentAndIndexAndLinkContext(OsmPrimitive parent, int index, int count) {
114 Environment e = new Environment(this);
115 e.parent = parent;
116 e.index = index;
117 e.count = count;
118 e.context = Context.LINK;
119 return e;
120 }
121
122 /**
123 * Creates a clone of this environment, with the specified child.
124 * @param child the matching child object
125 * @return A clone of this environment, with the specified child
126 * @see #child
127 */
128 public Environment withChild(OsmPrimitive child) {
129 Environment e = new Environment(this);
130 e.child = child;
131 return e;
132 }
133
134 /**
135 * Creates a clone of this environment, with the specified child, index, and context set to {@link Context#LINK}.
136 * @param child the matching child object
137 * @param index index of node in parent way or member in parent relation
138 * @param count count of nodes in parent way or members in parent relation
139 * @return A clone of this environment, with the specified child, index, and context set to {@code Context#LINK}
140 * @since 6175
141 * @see #child
142 * @see #index
143 */
144 public Environment withChildAndIndexAndLinkContext(OsmPrimitive child, int index, int count) {
145 Environment e = new Environment(this);
146 e.child = child;
147 e.index = index;
148 e.count = count;
149 e.context = Context.LINK;
150 return e;
151 }
152
153 /**
154 * Creates a clone of this environment, with the specified index.
155 * @param index index of node in parent way or member in parent relation
156 * @param count count of nodes in parent way or members in parent relation
157 * @return A clone of this environment, with the specified index
158 * @see #index
159 */
160 public Environment withIndex(int index, int count) {
161 Environment e = new Environment(this);
162 e.index = index;
163 e.count = count;
164 return e;
165 }
166
167 /**
168 * Creates a clone of this environment, with the specified {@link Context}.
169 * @return A clone of this environment, with the specified {@code Context}
170 */
171 public Environment withContext(Context context) {
172 Environment e = new Environment(this);
173 e.context = context == null ? Context.PRIMITIVE : context;
174 return e;
175 }
176
177 /**
178 * Creates a clone of this environment, with context set to {@link Context#LINK}.
179 * @return A clone of this environment, with context set to {@code Context#LINK}
180 */
181 public Environment withLinkContext() {
182 Environment e = new Environment(this);
183 e.context = Context.LINK;
184 return e;
185 }
186
187 /**
188 * Determines if the context of this environment is {@link Context#LINK}.
189 * @return {@code true} if the context of this environment is {@code Context#LINK}, {@code false} otherwise
190 */
191 public boolean isLinkContext() {
192 return Context.LINK.equals(context);
193 }
194
195 /**
196 * Determines if this environment has a relation as parent.
197 * @return {@code true} if this environment has a relation as parent, {@code false} otherwise
198 * @see #parent
199 */
200 public boolean hasParentRelation() {
201 return parent instanceof Relation;
202 }
203
204 /**
205 * Replies the current context.
206 *
207 * @return the current context
208 */
209 public Context getContext() {
210 return context == null ? Context.PRIMITIVE : context;
211 }
212
213 public String getRole() {
214 if (getContext().equals(Context.PRIMITIVE))
215 return null;
216
217 if (parent instanceof Relation)
218 return ((Relation) parent).getMember(index).getRole();
219 if (child != null && osm instanceof Relation)
220 return ((Relation) osm).getMember(index).getRole();
221 return null;
222 }
223
224 public void clearSelectorMatchingInformation() {
225 parent = null;
226 child = null;
227 index = null;
228 count = null;
229 }
230
231 public Cascade getCascade(String layer) {
232 return mc == null ? null : mc.getCascade(layer == null ? this.layer : layer);
233 }
234}
Note: See TracBrowser for help on using the repository browser.