1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.markerlayer;
|
---|
3 |
|
---|
4 | import java.awt.AlphaComposite;
|
---|
5 | import java.awt.Color;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.Graphics2D;
|
---|
8 | import java.awt.Point;
|
---|
9 | import java.awt.event.ActionEvent;
|
---|
10 | import java.awt.image.BufferedImage;
|
---|
11 | import java.io.File;
|
---|
12 | import java.net.MalformedURLException;
|
---|
13 | import java.net.URL;
|
---|
14 | import java.text.DateFormat;
|
---|
15 | import java.text.SimpleDateFormat;
|
---|
16 | import java.util.ArrayList;
|
---|
17 | import java.util.Collection;
|
---|
18 | import java.util.Date;
|
---|
19 | import java.util.HashMap;
|
---|
20 | import java.util.LinkedList;
|
---|
21 | import java.util.List;
|
---|
22 | import java.util.Map;
|
---|
23 | import java.util.TimeZone;
|
---|
24 |
|
---|
25 | import javax.swing.ImageIcon;
|
---|
26 |
|
---|
27 | import org.openstreetmap.josm.Main;
|
---|
28 | import org.openstreetmap.josm.actions.search.SearchCompiler.Match;
|
---|
29 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
30 | import org.openstreetmap.josm.data.coor.CachedLatLon;
|
---|
31 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
32 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
33 | import org.openstreetmap.josm.data.gpx.Extensions;
|
---|
34 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
35 | import org.openstreetmap.josm.data.gpx.GpxLink;
|
---|
36 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
37 | import org.openstreetmap.josm.data.preferences.CachedProperty;
|
---|
38 | import org.openstreetmap.josm.data.preferences.IntegerProperty;
|
---|
39 | import org.openstreetmap.josm.gui.MapView;
|
---|
40 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
41 | import org.openstreetmap.josm.tools.Utils;
|
---|
42 | import org.openstreetmap.josm.tools.template_engine.ParseError;
|
---|
43 | import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
|
---|
44 | import org.openstreetmap.josm.tools.template_engine.TemplateEntry;
|
---|
45 | import org.openstreetmap.josm.tools.template_engine.TemplateParser;
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Basic marker class. Requires a position, and supports
|
---|
49 | * a custom icon and a name.
|
---|
50 | *
|
---|
51 | * This class is also used to create appropriate Marker-type objects
|
---|
52 | * when waypoints are imported.
|
---|
53 | *
|
---|
54 | * It hosts a public list object, named makers, containing implementations of
|
---|
55 | * the MarkerMaker interface. Whenever a Marker needs to be created, each
|
---|
56 | * object in makers is called with the waypoint parameters (Lat/Lon and tag
|
---|
57 | * data), and the first one to return a Marker object wins.
|
---|
58 | *
|
---|
59 | * By default, one the list contains one default "Maker" implementation that
|
---|
60 | * will create AudioMarkers for .wav files, ImageMarkers for .png/.jpg/.jpeg
|
---|
61 | * files, and WebMarkers for everything else. (The creation of a WebMarker will
|
---|
62 | * fail if there's no valid URL in the <link> tag, so it might still make sense
|
---|
63 | * to add Makers for such waypoints at the end of the list.)
|
---|
64 | *
|
---|
65 | * The default implementation only looks at the value of the <link> tag inside
|
---|
66 | * the <wpt> tag of the GPX file.
|
---|
67 | *
|
---|
68 | * <h2>HowTo implement a new Marker</h2>
|
---|
69 | * <ul>
|
---|
70 | * <li> Subclass Marker or ButtonMarker and override <code>containsPoint</code>
|
---|
71 | * if you like to respond to user clicks</li>
|
---|
72 | * <li> Override paint, if you want a custom marker look (not "a label and a symbol")</li>
|
---|
73 | * <li> Implement MarkerCreator to return a new instance of your marker class</li>
|
---|
74 | * <li> In you plugin constructor, add an instance of your MarkerCreator
|
---|
75 | * implementation either on top or bottom of Marker.markerProducers.
|
---|
76 | * Add at top, if your marker should overwrite an current marker or at bottom
|
---|
77 | * if you only add a new marker style.</li>
|
---|
78 | * </ul>
|
---|
79 | *
|
---|
80 | * @author Frederik Ramm <frederik@remote.org>
|
---|
81 | */
|
---|
82 | public class Marker implements TemplateEngineDataProvider {
|
---|
83 |
|
---|
84 | public static final class TemplateEntryProperty extends CachedProperty<TemplateEntry> {
|
---|
85 | // This class is a bit complicated because it supports both global and per layer settings. I've added per layer settings because
|
---|
86 | // GPXSettingsPanel had possibility to set waypoint label but then I've realized that markers use different layer then gpx data
|
---|
87 | // so per layer settings is useless. Anyway it's possible to specify marker layer pattern in Einstein preferences and maybe somebody
|
---|
88 | // will make gui for it so I'm keeping it here
|
---|
89 |
|
---|
90 | private final static Map<String, TemplateEntryProperty> cache = new HashMap<String, TemplateEntryProperty>();
|
---|
91 |
|
---|
92 | // Legacy code - convert label from int to template engine expression
|
---|
93 | private static final IntegerProperty PROP_LABEL = new IntegerProperty("draw.rawgps.layer.wpt", 0 );
|
---|
94 | private static String getDefaultLabelPattern() {
|
---|
95 | switch (PROP_LABEL.get()) {
|
---|
96 | case 1:
|
---|
97 | return LABEL_PATTERN_NAME;
|
---|
98 | case 2:
|
---|
99 | return LABEL_PATTERN_DESC;
|
---|
100 | case 0:
|
---|
101 | case 3:
|
---|
102 | return LABEL_PATTERN_AUTO;
|
---|
103 | default:
|
---|
104 | return "";
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static TemplateEntryProperty forMarker(String layerName) {
|
---|
109 | String key = "draw.rawgps.layer.wpt.pattern";
|
---|
110 | if (layerName != null) {
|
---|
111 | key += "." + layerName;
|
---|
112 | }
|
---|
113 | TemplateEntryProperty result = cache.get(key);
|
---|
114 | if (result == null) {
|
---|
115 | String defaultValue = layerName == null ? getDefaultLabelPattern():"";
|
---|
116 | TemplateEntryProperty parent = layerName == null ? null : forMarker(null);
|
---|
117 | try {
|
---|
118 | result = new TemplateEntryProperty(key, defaultValue, parent);
|
---|
119 | cache.put(key, result);
|
---|
120 | } catch (ParseError e) {
|
---|
121 | Main.warn("Unable to parse template engine pattern ''{0}'' for property {1}", defaultValue, key);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | return result;
|
---|
125 | }
|
---|
126 |
|
---|
127 | public static TemplateEntryProperty forAudioMarker(String layerName) {
|
---|
128 | String key = "draw.rawgps.layer.audiowpt.pattern";
|
---|
129 | if (layerName != null) {
|
---|
130 | key += "." + layerName;
|
---|
131 | }
|
---|
132 | TemplateEntryProperty result = cache.get(key);
|
---|
133 | if (result == null) {
|
---|
134 | String defaultValue = layerName == null?"?{ '{name}' | '{desc}' | '{" + Marker.MARKER_FORMATTED_OFFSET + "}' }":"";
|
---|
135 | TemplateEntryProperty parent = layerName == null ? null : forAudioMarker(null);
|
---|
136 | try {
|
---|
137 | result = new TemplateEntryProperty(key, defaultValue, parent);
|
---|
138 | cache.put(key, result);
|
---|
139 | } catch (ParseError e) {
|
---|
140 | Main.warn("Unable to parse template engine pattern ''{0}'' for property {1}", defaultValue, key);
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return result;
|
---|
144 | }
|
---|
145 |
|
---|
146 | private TemplateEntryProperty parent;
|
---|
147 |
|
---|
148 |
|
---|
149 | private TemplateEntryProperty(String key, String defaultValue, TemplateEntryProperty parent) throws ParseError {
|
---|
150 | super(key, defaultValue);
|
---|
151 | this.parent = parent;
|
---|
152 | updateValue(); // Needs to be called because parent wasn't know in super constructor
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | protected TemplateEntry fromString(String s) {
|
---|
157 | try {
|
---|
158 | return new TemplateParser(s).parse();
|
---|
159 | } catch (ParseError e) {
|
---|
160 | Main.warn("Unable to parse template engine pattern ''{0}'' for property {1}. Using default (''{2}'') instead",
|
---|
161 | s, getKey(), super.getDefaultValueAsString());
|
---|
162 | return getDefaultValue();
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | @Override
|
---|
167 | public String getDefaultValueAsString() {
|
---|
168 | if (parent == null)
|
---|
169 | return super.getDefaultValueAsString();
|
---|
170 | else
|
---|
171 | return parent.getAsString();
|
---|
172 | }
|
---|
173 |
|
---|
174 | @Override
|
---|
175 | public void preferenceChanged(PreferenceChangeEvent e) {
|
---|
176 | if (e.getKey().equals(key) || (parent != null && e.getKey().equals(parent.getKey()))) {
|
---|
177 | updateValue();
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Plugins can add their Marker creation stuff at the bottom or top of this list
|
---|
184 | * (depending on whether they want to override default behaviour or just add new
|
---|
185 | * stuff).
|
---|
186 | */
|
---|
187 | public static final List<MarkerProducers> markerProducers = new LinkedList<MarkerProducers>();
|
---|
188 |
|
---|
189 | // Add one Marker specifying the default behaviour.
|
---|
190 | static {
|
---|
191 | Marker.markerProducers.add(new MarkerProducers() {
|
---|
192 | @SuppressWarnings("unchecked")
|
---|
193 | @Override
|
---|
194 | public Marker createMarker(WayPoint wpt, File relativePath, MarkerLayer parentLayer, double time, double offset) {
|
---|
195 | String uri = null;
|
---|
196 | // cheapest way to check whether "link" object exists and is a non-empty
|
---|
197 | // collection of GpxLink objects...
|
---|
198 | Collection<GpxLink> links = (Collection<GpxLink>)wpt.attr.get(GpxConstants.META_LINKS);
|
---|
199 | if (links != null) {
|
---|
200 | for (GpxLink oneLink : links ) {
|
---|
201 | uri = oneLink.uri;
|
---|
202 | break;
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | URL url = null;
|
---|
207 | if (uri != null) {
|
---|
208 | try {
|
---|
209 | url = new URL(uri);
|
---|
210 | } catch (MalformedURLException e) {
|
---|
211 | // Try a relative file:// url, if the link is not in an URL-compatible form
|
---|
212 | if (relativePath != null) {
|
---|
213 | url = Utils.fileToURL(new File(relativePath.getParentFile(), uri));
|
---|
214 | }
|
---|
215 | }
|
---|
216 | }
|
---|
217 |
|
---|
218 | if (url == null) {
|
---|
219 | String symbolName = wpt.getString("symbol");
|
---|
220 | if (symbolName == null) {
|
---|
221 | symbolName = wpt.getString("sym");
|
---|
222 | }
|
---|
223 | return new Marker(wpt.getCoor(), wpt, symbolName, parentLayer, time, offset);
|
---|
224 | }
|
---|
225 | else if (url.toString().endsWith(".wav")) {
|
---|
226 | AudioMarker audioMarker = new AudioMarker(wpt.getCoor(), wpt, url, parentLayer, time, offset);
|
---|
227 | Extensions exts = (Extensions) wpt.get(GpxConstants.META_EXTENSIONS);
|
---|
228 | if (exts != null && exts.containsKey("offset")) {
|
---|
229 | try {
|
---|
230 | double syncOffset = Double.parseDouble(exts.get("sync-offset"));
|
---|
231 | audioMarker.syncOffset = syncOffset;
|
---|
232 | } catch (NumberFormatException nfe) {
|
---|
233 | Main.warn(nfe);
|
---|
234 | }
|
---|
235 | }
|
---|
236 | return audioMarker;
|
---|
237 | } else if (url.toString().endsWith(".png") || url.toString().endsWith(".jpg") || url.toString().endsWith(".jpeg") || url.toString().endsWith(".gif")) {
|
---|
238 | return new ImageMarker(wpt.getCoor(), url, parentLayer, time, offset);
|
---|
239 | } else {
|
---|
240 | return new WebMarker(wpt.getCoor(), url, parentLayer, time, offset);
|
---|
241 | }
|
---|
242 | }
|
---|
243 | });
|
---|
244 | }
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Returns an object of class Marker or one of its subclasses
|
---|
248 | * created from the parameters given.
|
---|
249 | *
|
---|
250 | * @param wpt waypoint data for marker
|
---|
251 | * @param relativePath An path to use for constructing relative URLs or
|
---|
252 | * <code>null</code> for no relative URLs
|
---|
253 | * @param parentLayer the <code>MarkerLayer</code> that will contain the created <code>Marker</code>
|
---|
254 | * @param time time of the marker in seconds since epoch
|
---|
255 | * @param offset double in seconds as the time offset of this marker from
|
---|
256 | * the GPX file from which it was derived (if any).
|
---|
257 | * @return a new Marker object
|
---|
258 | */
|
---|
259 | public static Marker createMarker(WayPoint wpt, File relativePath, MarkerLayer parentLayer, double time, double offset) {
|
---|
260 | for (MarkerProducers maker : Marker.markerProducers) {
|
---|
261 | Marker marker = maker.createMarker(wpt, relativePath, parentLayer, time, offset);
|
---|
262 | if (marker != null)
|
---|
263 | return marker;
|
---|
264 | }
|
---|
265 | return null;
|
---|
266 | }
|
---|
267 |
|
---|
268 | private static final DateFormat timeFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
|
---|
269 | static {
|
---|
270 | TimeZone tz = TimeZone.getTimeZone("UTC");
|
---|
271 | timeFormatter.setTimeZone(tz);
|
---|
272 | }
|
---|
273 |
|
---|
274 | public static final String MARKER_OFFSET = "waypointOffset";
|
---|
275 | public static final String MARKER_FORMATTED_OFFSET = "formattedWaypointOffset";
|
---|
276 |
|
---|
277 | public static final String LABEL_PATTERN_AUTO = "?{ '{name} - {desc}' | '{name}' | '{desc}' }";
|
---|
278 | public static final String LABEL_PATTERN_NAME = "{name}";
|
---|
279 | public static final String LABEL_PATTERN_DESC = "{desc}";
|
---|
280 |
|
---|
281 | private final TemplateEngineDataProvider dataProvider;
|
---|
282 | private final String text;
|
---|
283 |
|
---|
284 | protected final ImageIcon symbol;
|
---|
285 | private BufferedImage redSymbol = null;
|
---|
286 | public final MarkerLayer parentLayer;
|
---|
287 | /** Absolute time of marker in seconds since epoch */
|
---|
288 | public double time;
|
---|
289 | /** Time offset in seconds from the gpx point from which it was derived, may be adjusted later to sync with other data, so not final */
|
---|
290 | public double offset;
|
---|
291 |
|
---|
292 | private String cachedText;
|
---|
293 | private int textVersion = -1;
|
---|
294 | private CachedLatLon coor;
|
---|
295 |
|
---|
296 | private boolean erroneous = false;
|
---|
297 |
|
---|
298 | public Marker(LatLon ll, TemplateEngineDataProvider dataProvider, String iconName, MarkerLayer parentLayer, double time, double offset) {
|
---|
299 | setCoor(ll);
|
---|
300 |
|
---|
301 | this.offset = offset;
|
---|
302 | this.time = time;
|
---|
303 | this.symbol = iconName != null ? ImageProvider.getIfAvailable("markers",iconName) : null;
|
---|
304 | this.parentLayer = parentLayer;
|
---|
305 |
|
---|
306 | this.dataProvider = dataProvider;
|
---|
307 | this.text = null;
|
---|
308 | }
|
---|
309 |
|
---|
310 | public Marker(LatLon ll, String text, String iconName, MarkerLayer parentLayer, double time, double offset) {
|
---|
311 | setCoor(ll);
|
---|
312 |
|
---|
313 | this.offset = offset;
|
---|
314 | this.time = time;
|
---|
315 | this.symbol = iconName != null ? ImageProvider.getIfAvailable("markers",iconName) : null;
|
---|
316 | this.parentLayer = parentLayer;
|
---|
317 |
|
---|
318 | this.dataProvider = null;
|
---|
319 | this.text = text;
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * Convert Marker to WayPoint so it can be exported to a GPX file.
|
---|
324 | *
|
---|
325 | * Override in subclasses to add all necessary attributes.
|
---|
326 | *
|
---|
327 | * @return the corresponding WayPoint with all relevant attributes
|
---|
328 | */
|
---|
329 | public WayPoint convertToWayPoint() {
|
---|
330 | WayPoint wpt = new WayPoint(getCoor());
|
---|
331 | wpt.put("time", timeFormatter.format(new Date(Math.round(time * 1000))));
|
---|
332 | if (text != null) {
|
---|
333 | wpt.addExtension("text", text);
|
---|
334 | } else if (dataProvider != null) {
|
---|
335 | for (String key : dataProvider.getTemplateKeys()) {
|
---|
336 | Object value = dataProvider.getTemplateValue(key, false);
|
---|
337 | if (value != null && GpxConstants.WPT_KEYS.contains(key)) {
|
---|
338 | wpt.put(key, value);
|
---|
339 | }
|
---|
340 | }
|
---|
341 | }
|
---|
342 | return wpt;
|
---|
343 | }
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Sets the marker's coordinates.
|
---|
347 | * @param coor The marker's coordinates (lat/lon)
|
---|
348 | */
|
---|
349 | public final void setCoor(LatLon coor) {
|
---|
350 | this.coor = new CachedLatLon(coor);
|
---|
351 | }
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Returns the marker's coordinates.
|
---|
355 | * @return The marker's coordinates (lat/lon)
|
---|
356 | */
|
---|
357 | public final LatLon getCoor() {
|
---|
358 | return coor;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * Sets the marker's projected coordinates.
|
---|
363 | * @param eastNorth The marker's projected coordinates (easting/northing)
|
---|
364 | */
|
---|
365 | public final void setEastNorth(EastNorth eastNorth) {
|
---|
366 | this.coor = new CachedLatLon(eastNorth);
|
---|
367 | }
|
---|
368 |
|
---|
369 | /**
|
---|
370 | * Returns the marker's projected coordinates.
|
---|
371 | * @return The marker's projected coordinates (easting/northing)
|
---|
372 | */
|
---|
373 | public final EastNorth getEastNorth() {
|
---|
374 | return coor.getEastNorth();
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Checks whether the marker display area contains the given point.
|
---|
379 | * Markers not interested in mouse clicks may always return false.
|
---|
380 | *
|
---|
381 | * @param p The point to check
|
---|
382 | * @return <code>true</code> if the marker "hotspot" contains the point.
|
---|
383 | */
|
---|
384 | public boolean containsPoint(Point p) {
|
---|
385 | return false;
|
---|
386 | }
|
---|
387 |
|
---|
388 | /**
|
---|
389 | * Called when the mouse is clicked in the marker's hotspot. Never
|
---|
390 | * called for markers which always return false from containsPoint.
|
---|
391 | *
|
---|
392 | * @param ev A dummy ActionEvent
|
---|
393 | */
|
---|
394 | public void actionPerformed(ActionEvent ev) {
|
---|
395 | }
|
---|
396 |
|
---|
397 | /**
|
---|
398 | * Paints the marker.
|
---|
399 | * @param g graphics context
|
---|
400 | * @param mv map view
|
---|
401 | * @param mousePressed true if the left mouse button is pressed
|
---|
402 | * @param showTextOrIcon true if text and icon shall be drawn
|
---|
403 | */
|
---|
404 | public void paint(Graphics g, MapView mv, boolean mousePressed, boolean showTextOrIcon) {
|
---|
405 | Point screen = mv.getPoint(getEastNorth());
|
---|
406 | if (symbol != null && showTextOrIcon) {
|
---|
407 | paintIcon(mv, g, screen.x-symbol.getIconWidth()/2, screen.y-symbol.getIconHeight()/2);
|
---|
408 | } else {
|
---|
409 | g.drawLine(screen.x-2, screen.y-2, screen.x+2, screen.y+2);
|
---|
410 | g.drawLine(screen.x+2, screen.y-2, screen.x-2, screen.y+2);
|
---|
411 | }
|
---|
412 |
|
---|
413 | String labelText = getText();
|
---|
414 | if ((labelText != null) && showTextOrIcon) {
|
---|
415 | g.drawString(labelText, screen.x+4, screen.y+2);
|
---|
416 | }
|
---|
417 | }
|
---|
418 |
|
---|
419 | protected void paintIcon(MapView mv, Graphics g, int x, int y) {
|
---|
420 | if (!erroneous) {
|
---|
421 | symbol.paintIcon(mv, g, x, y);
|
---|
422 | } else {
|
---|
423 | if (redSymbol == null) {
|
---|
424 | int width = symbol.getIconWidth();
|
---|
425 | int height = symbol.getIconHeight();
|
---|
426 |
|
---|
427 | redSymbol = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
---|
428 | Graphics2D gbi = redSymbol.createGraphics();
|
---|
429 | gbi.drawImage(symbol.getImage(), 0, 0, null);
|
---|
430 | gbi.setColor(Color.RED);
|
---|
431 | gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.666f));
|
---|
432 | gbi.fillRect(0, 0, width, height);
|
---|
433 | gbi.dispose();
|
---|
434 | }
|
---|
435 | g.drawImage(redSymbol, x, y, mv);
|
---|
436 | }
|
---|
437 | }
|
---|
438 |
|
---|
439 | protected TemplateEntryProperty getTextTemplate() {
|
---|
440 | return TemplateEntryProperty.forMarker(parentLayer.getName());
|
---|
441 | }
|
---|
442 |
|
---|
443 | /**
|
---|
444 | * Returns the Text which should be displayed, depending on chosen preference
|
---|
445 | * @return Text of the label
|
---|
446 | */
|
---|
447 | public String getText() {
|
---|
448 | if (text != null)
|
---|
449 | return text;
|
---|
450 | else {
|
---|
451 | TemplateEntryProperty property = getTextTemplate();
|
---|
452 | if (property.getUpdateCount() != textVersion) {
|
---|
453 | TemplateEntry templateEntry = property.get();
|
---|
454 | StringBuilder sb = new StringBuilder();
|
---|
455 | templateEntry.appendText(sb, this);
|
---|
456 |
|
---|
457 | cachedText = sb.toString();
|
---|
458 | textVersion = property.getUpdateCount();
|
---|
459 | }
|
---|
460 | return cachedText;
|
---|
461 | }
|
---|
462 | }
|
---|
463 |
|
---|
464 | @Override
|
---|
465 | public Collection<String> getTemplateKeys() {
|
---|
466 | Collection<String> result;
|
---|
467 | if (dataProvider != null) {
|
---|
468 | result = dataProvider.getTemplateKeys();
|
---|
469 | } else {
|
---|
470 | result = new ArrayList<String>();
|
---|
471 | }
|
---|
472 | result.add(MARKER_FORMATTED_OFFSET);
|
---|
473 | result.add(MARKER_OFFSET);
|
---|
474 | return result;
|
---|
475 | }
|
---|
476 |
|
---|
477 | private String formatOffset() {
|
---|
478 | int wholeSeconds = (int)(offset + 0.5);
|
---|
479 | if (wholeSeconds < 60)
|
---|
480 | return Integer.toString(wholeSeconds);
|
---|
481 | else if (wholeSeconds < 3600)
|
---|
482 | return String.format("%d:%02d", wholeSeconds / 60, wholeSeconds % 60);
|
---|
483 | else
|
---|
484 | return String.format("%d:%02d:%02d", wholeSeconds / 3600, (wholeSeconds % 3600)/60, wholeSeconds % 60);
|
---|
485 | }
|
---|
486 |
|
---|
487 | @Override
|
---|
488 | public Object getTemplateValue(String name, boolean special) {
|
---|
489 | if (MARKER_FORMATTED_OFFSET.equals(name))
|
---|
490 | return formatOffset();
|
---|
491 | else if (MARKER_OFFSET.equals(name))
|
---|
492 | return offset;
|
---|
493 | else if (dataProvider != null)
|
---|
494 | return dataProvider.getTemplateValue(name, special);
|
---|
495 | else
|
---|
496 | return null;
|
---|
497 | }
|
---|
498 |
|
---|
499 | @Override
|
---|
500 | public boolean evaluateCondition(Match condition) {
|
---|
501 | throw new UnsupportedOperationException();
|
---|
502 | }
|
---|
503 |
|
---|
504 | /**
|
---|
505 | * Determines if this marker is erroneous.
|
---|
506 | * @return {@code true} if this markers has any kind of error, {@code false} otherwise
|
---|
507 | * @since 6299
|
---|
508 | */
|
---|
509 | public final boolean isErroneous() {
|
---|
510 | return erroneous;
|
---|
511 | }
|
---|
512 |
|
---|
513 | /**
|
---|
514 | * Sets this marker erroneous or not.
|
---|
515 | * @param erroneous {@code true} if this markers has any kind of error, {@code false} otherwise
|
---|
516 | * @since 6299
|
---|
517 | */
|
---|
518 | public final void setErroneous(boolean erroneous) {
|
---|
519 | this.erroneous = erroneous;
|
---|
520 | if (!erroneous) {
|
---|
521 | redSymbol = null;
|
---|
522 | }
|
---|
523 | }
|
---|
524 | }
|
---|