source: josm/trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/Marker.java@ 17047

Last change on this file since 17047 was 17047, checked in by GerdP, 4 years ago

fix #19789: memory leak with gpx waypoints

  • implement destroy in class Marker and call it for each object in a destroyed MarkerLayer
  • handle also AbstractModifiableLayer in AutosaveTask
  • Property svn:eol-style set to native
File size: 15.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.markerlayer;
3
4import java.awt.AlphaComposite;
5import java.awt.Color;
6import java.awt.Graphics;
7import java.awt.Graphics2D;
8import java.awt.Point;
9import java.awt.Stroke;
10import java.awt.event.ActionEvent;
11import java.awt.image.BufferedImage;
12import java.io.File;
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.HashMap;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Map;
19import java.util.Objects;
20
21import javax.swing.ImageIcon;
22
23import org.openstreetmap.josm.data.Preferences;
24import org.openstreetmap.josm.data.coor.CachedLatLon;
25import org.openstreetmap.josm.data.coor.EastNorth;
26import org.openstreetmap.josm.data.coor.ILatLon;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.data.gpx.GpxConstants;
29import org.openstreetmap.josm.data.gpx.WayPoint;
30import org.openstreetmap.josm.data.osm.search.SearchCompiler.Match;
31import org.openstreetmap.josm.gui.MapView;
32import org.openstreetmap.josm.gui.layer.GpxLayer;
33import org.openstreetmap.josm.gui.preferences.display.GPXSettingsPanel;
34import org.openstreetmap.josm.spi.preferences.PreferenceChangedListener;
35import org.openstreetmap.josm.tools.Destroyable;
36import org.openstreetmap.josm.tools.ImageProvider;
37import org.openstreetmap.josm.tools.Logging;
38import org.openstreetmap.josm.tools.template_engine.ParseError;
39import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
40import org.openstreetmap.josm.tools.template_engine.TemplateEntry;
41import org.openstreetmap.josm.tools.template_engine.TemplateParser;
42
43/**
44 * Basic marker class. Requires a position, and supports
45 * a custom icon and a name.
46 *
47 * This class is also used to create appropriate Marker-type objects
48 * when waypoints are imported.
49 *
50 * It hosts a public list object, named makers, containing implementations of
51 * the MarkerMaker interface. Whenever a Marker needs to be created, each
52 * object in makers is called with the waypoint parameters (Lat/Lon and tag
53 * data), and the first one to return a Marker object wins.
54 *
55 * By default, one the list contains one default "Maker" implementation that
56 * will create AudioMarkers for supported audio files, ImageMarkers for supported image
57 * files, and WebMarkers for everything else. (The creation of a WebMarker will
58 * fail if there's no valid URL in the <link> tag, so it might still make sense
59 * to add Makers for such waypoints at the end of the list.)
60 *
61 * The default implementation only looks at the value of the <link> tag inside
62 * the <wpt> tag of the GPX file.
63 *
64 * <h2>HowTo implement a new Marker</h2>
65 * <ul>
66 * <li> Subclass Marker or ButtonMarker and override <code>containsPoint</code>
67 * if you like to respond to user clicks</li>
68 * <li> Override paint, if you want a custom marker look (not "a label and a symbol")</li>
69 * <li> Implement MarkerCreator to return a new instance of your marker class</li>
70 * <li> In you plugin constructor, add an instance of your MarkerCreator
71 * implementation either on top or bottom of Marker.markerProducers.
72 * Add at top, if your marker should overwrite an current marker or at bottom
73 * if you only add a new marker style.</li>
74 * </ul>
75 *
76 * @author Frederik Ramm
77 */
78public class Marker implements TemplateEngineDataProvider, ILatLon, Destroyable {
79
80 /**
81 * Plugins can add their Marker creation stuff at the bottom or top of this list
82 * (depending on whether they want to override default behaviour or just add new stuff).
83 */
84 private static final List<MarkerProducers> markerProducers = new LinkedList<>();
85
86 // Add one Marker specifying the default behaviour.
87 static {
88 Marker.markerProducers.add(new DefaultMarkerProducers());
89 }
90
91 /**
92 * Add a new marker producers at the end of the JOSM list.
93 * @param mp a new marker producers
94 * @since 11850
95 */
96 public static void appendMarkerProducer(MarkerProducers mp) {
97 markerProducers.add(mp);
98 }
99
100 /**
101 * Add a new marker producers at the beginning of the JOSM list.
102 * @param mp a new marker producers
103 * @since 11850
104 */
105 public static void prependMarkerProducer(MarkerProducers mp) {
106 markerProducers.add(0, mp);
107 }
108
109 /**
110 * Returns an object of class Marker or one of its subclasses
111 * created from the parameters given.
112 *
113 * @param wpt waypoint data for marker
114 * @param relativePath An path to use for constructing relative URLs or
115 * <code>null</code> for no relative URLs
116 * @param parentLayer the <code>MarkerLayer</code> that will contain the created <code>Marker</code>
117 * @param time time of the marker in seconds since epoch
118 * @param offset double in seconds as the time offset of this marker from
119 * the GPX file from which it was derived (if any).
120 * @return a new Marker object
121 */
122 public static Collection<Marker> createMarkers(WayPoint wpt, File relativePath, MarkerLayer parentLayer, double time, double offset) {
123 return Marker.markerProducers.stream()
124 .map(maker -> maker.createMarkers(wpt, relativePath, parentLayer, time, offset))
125 .filter(Objects::nonNull)
126 .findFirst().orElse(null);
127 }
128
129 public static final String MARKER_OFFSET = "waypointOffset";
130 public static final String MARKER_FORMATTED_OFFSET = "formattedWaypointOffset";
131
132 public static final String LABEL_PATTERN_AUTO = "?{ '{name} ({desc})' | '{name} ({cmt})' | '{name}' | '{desc}' | '{cmt}' }";
133 public static final String LABEL_PATTERN_NAME = "{name}";
134 public static final String LABEL_PATTERN_DESC = "{desc}";
135
136 private final TemplateEngineDataProvider dataProvider;
137 private final String text;
138
139 protected final ImageIcon symbol;
140 private BufferedImage redSymbol;
141 public final MarkerLayer parentLayer;
142 /** Absolute time of marker in seconds since epoch */
143 public double time;
144 /** 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 */
145 public double offset;
146
147 private String cachedText;
148 private static Map<GpxLayer, String> cachedTemplates = new HashMap<>();
149 private String cachedDefaultTemplate;
150
151 private CachedLatLon coor;
152 private PreferenceChangedListener listener = l -> updateText();
153
154 private boolean erroneous;
155
156 public Marker(LatLon ll, TemplateEngineDataProvider dataProvider, String iconName, MarkerLayer parentLayer,
157 double time, double offset) {
158 this(ll, dataProvider, null, iconName, parentLayer, time, offset);
159 }
160
161 public Marker(LatLon ll, String text, String iconName, MarkerLayer parentLayer, double time, double offset) {
162 this(ll, null, text, iconName, parentLayer, time, offset);
163 }
164
165 private Marker(LatLon ll, TemplateEngineDataProvider dataProvider, String text, String iconName, MarkerLayer parentLayer,
166 double time, double offset) {
167 setCoor(ll);
168
169 this.offset = offset;
170 this.time = time;
171 /* tell icon checking that we expect these names to exist */
172 // /* ICON(markers/) */"Bridge"
173 // /* ICON(markers/) */"Crossing"
174 this.symbol = iconName != null ? ImageProvider.getIfAvailable("markers", iconName) : null;
175 this.parentLayer = parentLayer;
176
177 this.dataProvider = dataProvider;
178 this.text = text;
179
180 Preferences.main().addKeyPreferenceChangeListener(getPreferenceKey(), listener);
181 }
182
183 /**
184 * Convert Marker to WayPoint so it can be exported to a GPX file.
185 *
186 * Override in subclasses to add all necessary attributes.
187 *
188 * @return the corresponding WayPoint with all relevant attributes
189 */
190 public WayPoint convertToWayPoint() {
191 WayPoint wpt = new WayPoint(getCoor());
192 wpt.setTimeInMillis((long) (time * 1000));
193 if (text != null) {
194 wpt.getExtensions().add("josm", "text", text);
195 } else if (dataProvider != null) {
196 for (String key : dataProvider.getTemplateKeys()) {
197 Object value = dataProvider.getTemplateValue(key, false);
198 if (value != null && GpxConstants.WPT_KEYS.contains(key)) {
199 wpt.put(key, value);
200 }
201 }
202 }
203 return wpt;
204 }
205
206 /**
207 * Sets the marker's coordinates.
208 * @param coor The marker's coordinates (lat/lon)
209 */
210 public final void setCoor(LatLon coor) {
211 this.coor = new CachedLatLon(coor);
212 }
213
214 /**
215 * Returns the marker's coordinates.
216 * @return The marker's coordinates (lat/lon)
217 */
218 public final LatLon getCoor() {
219 return coor;
220 }
221
222 /**
223 * Sets the marker's projected coordinates.
224 * @param eastNorth The marker's projected coordinates (easting/northing)
225 */
226 public final void setEastNorth(EastNorth eastNorth) {
227 this.coor = new CachedLatLon(eastNorth);
228 }
229
230 /**
231 * @since 12725
232 */
233 @Override
234 public double lon() {
235 return coor == null ? Double.NaN : coor.lon();
236 }
237
238 /**
239 * @since 12725
240 */
241 @Override
242 public double lat() {
243 return coor == null ? Double.NaN : coor.lat();
244 }
245
246 /**
247 * Checks whether the marker display area contains the given point.
248 * Markers not interested in mouse clicks may always return false.
249 *
250 * @param p The point to check
251 * @return <code>true</code> if the marker "hotspot" contains the point.
252 */
253 public boolean containsPoint(Point p) {
254 return false;
255 }
256
257 /**
258 * Called when the mouse is clicked in the marker's hotspot. Never
259 * called for markers which always return false from containsPoint.
260 *
261 * @param ev A dummy ActionEvent
262 */
263 public void actionPerformed(ActionEvent ev) {
264 // Do nothing
265 }
266
267 /**
268 * Paints the marker.
269 * @param g graphics context
270 * @param mv map view
271 * @param mousePressed true if the left mouse button is pressed
272 * @param showTextOrIcon true if text and icon shall be drawn
273 */
274 public void paint(Graphics2D g, MapView mv, boolean mousePressed, boolean showTextOrIcon) {
275 Point screen = mv.getPoint(this);
276 int size2 = parentLayer.markerSize / 2;
277
278 if (symbol != null && showTextOrIcon) {
279 paintIcon(mv, g, screen.x-symbol.getIconWidth()/2, screen.y-symbol.getIconHeight()/2);
280 } else {
281 Stroke stroke = g.getStroke();
282 g.setStroke(parentLayer.markerStroke);
283 g.drawLine(screen.x - size2, screen.y - size2, screen.x + size2, screen.y + size2);
284 g.drawLine(screen.x + size2, screen.y - size2, screen.x - size2, screen.y + size2);
285 g.setStroke(stroke);
286 }
287
288 String labelText = getText();
289 if (!labelText.isEmpty() && showTextOrIcon) {
290 g.drawString(labelText, screen.x + size2 + 2, screen.y + size2);
291 }
292 }
293
294 protected void paintIcon(MapView mv, Graphics g, int x, int y) {
295 if (!erroneous) {
296 symbol.paintIcon(mv, g, x, y);
297 } else {
298 if (redSymbol == null) {
299 int width = symbol.getIconWidth();
300 int height = symbol.getIconHeight();
301
302 redSymbol = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
303 Graphics2D gbi = redSymbol.createGraphics();
304 gbi.drawImage(symbol.getImage(), 0, 0, null);
305 gbi.setColor(Color.RED);
306 gbi.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.666f));
307 gbi.fillRect(0, 0, width, height);
308 gbi.dispose();
309 }
310 g.drawImage(redSymbol, x, y, mv);
311 }
312 }
313
314 protected String getTextTemplateKey() {
315 return "markers.pattern";
316 }
317
318 private String getTextTemplate() {
319 String tmpl;
320 if (cachedTemplates.containsKey(parentLayer.fromLayer)) {
321 tmpl = cachedTemplates.get(parentLayer.fromLayer);
322 } else {
323 tmpl = GPXSettingsPanel.getLayerPref(parentLayer.fromLayer, getTextTemplateKey());
324 cachedTemplates.put(parentLayer.fromLayer, tmpl);
325 }
326 return tmpl;
327 }
328
329 private String getDefaultTextTemplate() {
330 if (cachedDefaultTemplate == null) {
331 cachedDefaultTemplate = GPXSettingsPanel.getLayerPref(null, getTextTemplateKey());
332 }
333 return cachedDefaultTemplate;
334 }
335
336 /**
337 * Returns the Text which should be displayed, depending on chosen preference
338 * @return Text of the label
339 */
340 public String getText() {
341 if (text != null) {
342 return text;
343 } else if (cachedText == null) {
344 TemplateEntry template;
345 String templateString = getTextTemplate();
346 try {
347 template = new TemplateParser(templateString).parse();
348 } catch (ParseError e) {
349 Logging.debug(e);
350 String def = getDefaultTextTemplate();
351 Logging.warn("Unable to parse template engine pattern ''{0}'' for property {1}. Using default (''{2}'') instead",
352 templateString, getTextTemplateKey(), def);
353 try {
354 template = new TemplateParser(def).parse();
355 } catch (ParseError e1) {
356 Logging.error(e1);
357 cachedText = "";
358 return "";
359 }
360 }
361 StringBuilder sb = new StringBuilder();
362 template.appendText(sb, this);
363 cachedText = sb.toString();
364
365 }
366 return cachedText;
367 }
368
369 /**
370 * Called when the template changes
371 */
372 public void updateText() {
373 cachedText = null;
374 cachedDefaultTemplate = null;
375 cachedTemplates.clear();
376 }
377
378 @Override
379 public Collection<String> getTemplateKeys() {
380 Collection<String> result;
381 if (dataProvider != null) {
382 result = dataProvider.getTemplateKeys();
383 } else {
384 result = new ArrayList<>();
385 }
386 result.add(MARKER_FORMATTED_OFFSET);
387 result.add(MARKER_OFFSET);
388 return result;
389 }
390
391 private String formatOffset() {
392 int wholeSeconds = (int) (offset + 0.5);
393 if (wholeSeconds < 60)
394 return Integer.toString(wholeSeconds);
395 else if (wholeSeconds < 3600)
396 return String.format("%d:%02d", wholeSeconds / 60, wholeSeconds % 60);
397 else
398 return String.format("%d:%02d:%02d", wholeSeconds / 3600, (wholeSeconds % 3600)/60, wholeSeconds % 60);
399 }
400
401 @Override
402 public Object getTemplateValue(String name, boolean special) {
403 if (MARKER_FORMATTED_OFFSET.equals(name))
404 return formatOffset();
405 else if (MARKER_OFFSET.equals(name))
406 return offset;
407 else if (dataProvider != null)
408 return dataProvider.getTemplateValue(name, special);
409 else
410 return null;
411 }
412
413 @Override
414 public boolean evaluateCondition(Match condition) {
415 throw new UnsupportedOperationException();
416 }
417
418 /**
419 * Determines if this marker is erroneous.
420 * @return {@code true} if this markers has any kind of error, {@code false} otherwise
421 * @since 6299
422 */
423 public final boolean isErroneous() {
424 return erroneous;
425 }
426
427 /**
428 * Sets this marker erroneous or not.
429 * @param erroneous {@code true} if this markers has any kind of error, {@code false} otherwise
430 * @since 6299
431 */
432 public final void setErroneous(boolean erroneous) {
433 this.erroneous = erroneous;
434 if (!erroneous) {
435 redSymbol = null;
436 }
437 }
438
439 @Override
440 public void destroy() {
441 cachedTemplates.clear();
442 Preferences.main().removeKeyPreferenceChangeListener(getPreferenceKey(), listener);
443 }
444
445 private String getPreferenceKey() {
446 return "draw.rawgps." + getTextTemplateKey();
447 }
448}
Note: See TracBrowser for help on using the repository browser.