source: josm/trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/MarkerLayer.java@ 8818

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

fix #11925, see #11897 - Re-enable merging of gpx, marker, geoimage layers

Regression of r8728.

  • Property svn:eol-style set to native
File size: 20.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.markerlayer;
3
4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.marktr;
6import static org.openstreetmap.josm.tools.I18n.tr;
7import static org.openstreetmap.josm.tools.I18n.trn;
8
9import java.awt.Color;
10import java.awt.Component;
11import java.awt.Graphics2D;
12import java.awt.Point;
13import java.awt.event.ActionEvent;
14import java.awt.event.MouseAdapter;
15import java.awt.event.MouseEvent;
16import java.io.File;
17import java.net.URI;
18import java.net.URISyntaxException;
19import java.util.ArrayList;
20import java.util.Collection;
21import java.util.Collections;
22import java.util.Comparator;
23import java.util.List;
24
25import javax.swing.AbstractAction;
26import javax.swing.Action;
27import javax.swing.Icon;
28import javax.swing.JCheckBoxMenuItem;
29import javax.swing.JOptionPane;
30
31import org.openstreetmap.josm.Main;
32import org.openstreetmap.josm.actions.RenameLayerAction;
33import org.openstreetmap.josm.data.Bounds;
34import org.openstreetmap.josm.data.coor.LatLon;
35import org.openstreetmap.josm.data.gpx.Extensions;
36import org.openstreetmap.josm.data.gpx.GpxConstants;
37import org.openstreetmap.josm.data.gpx.GpxData;
38import org.openstreetmap.josm.data.gpx.GpxLink;
39import org.openstreetmap.josm.data.gpx.WayPoint;
40import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
41import org.openstreetmap.josm.gui.MapView;
42import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
43import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
44import org.openstreetmap.josm.gui.layer.CustomizeColor;
45import org.openstreetmap.josm.gui.layer.GpxLayer;
46import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToMarkerLayer;
47import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToNextMarker;
48import org.openstreetmap.josm.gui.layer.JumpToMarkerActions.JumpToPreviousMarker;
49import org.openstreetmap.josm.gui.layer.Layer;
50import org.openstreetmap.josm.gui.layer.gpx.ConvertToDataLayerAction;
51import org.openstreetmap.josm.tools.AudioPlayer;
52import org.openstreetmap.josm.tools.ImageProvider;
53
54/**
55 * A layer holding markers.
56 *
57 * Markers are GPS points with a name and, optionally, a symbol code attached;
58 * marker layers can be created from waypoints when importing raw GPS data,
59 * but they may also come from other sources.
60 *
61 * The symbol code is for future use.
62 *
63 * The data is read only.
64 */
65public class MarkerLayer extends Layer implements JumpToMarkerLayer {
66
67 /**
68 * A list of markers.
69 */
70 public final List<Marker> data;
71 private boolean mousePressed = false;
72 public GpxLayer fromLayer = null;
73 private Marker currentMarker;
74 public AudioMarker syncAudioMarker = null;
75
76 /**
77 * Constructs a new {@code MarkerLayer}.
78 * @param indata The GPX data for this layer
79 * @param name The marker layer name
80 * @param associatedFile The associated GPX file
81 * @param fromLayer The associated GPX layer
82 */
83 public MarkerLayer(GpxData indata, String name, File associatedFile, GpxLayer fromLayer) {
84 super(name);
85 this.setAssociatedFile(associatedFile);
86 this.data = new ArrayList<>();
87 this.fromLayer = fromLayer;
88 double firstTime = -1.0;
89 String lastLinkedFile = "";
90
91 for (WayPoint wpt : indata.waypoints) {
92 /* calculate time differences in waypoints */
93 double time = wpt.time;
94 boolean wpt_has_link = wpt.attr.containsKey(GpxConstants.META_LINKS);
95 if (firstTime < 0 && wpt_has_link) {
96 firstTime = time;
97 for (GpxLink oneLink : wpt.<GpxLink>getCollection(GpxConstants.META_LINKS)) {
98 lastLinkedFile = oneLink.uri;
99 break;
100 }
101 }
102 if (wpt_has_link) {
103 for (GpxLink oneLink : wpt.<GpxLink>getCollection(GpxConstants.META_LINKS)) {
104 String uri = oneLink.uri;
105 if (uri != null) {
106 if (!uri.equals(lastLinkedFile)) {
107 firstTime = time;
108 }
109 lastLinkedFile = uri;
110 break;
111 }
112 }
113 }
114 Double offset = null;
115 // If we have an explicit offset, take it.
116 // Otherwise, for a group of markers with the same Link-URI (e.g. an
117 // audio file) calculate the offset relative to the first marker of
118 // that group. This way the user can jump to the corresponding
119 // playback positions in a long audio track.
120 Extensions exts = (Extensions) wpt.get(GpxConstants.META_EXTENSIONS);
121 if (exts != null && exts.containsKey("offset")) {
122 try {
123 offset = Double.valueOf(exts.get("offset"));
124 } catch (NumberFormatException nfe) {
125 Main.warn(nfe);
126 }
127 }
128 if (offset == null) {
129 offset = time - firstTime;
130 }
131 final Collection<Marker> markers = Marker.createMarkers(wpt, indata.storageFile, this, time, offset);
132 if (markers != null) {
133 data.addAll(markers);
134 }
135 }
136 }
137
138 @Override
139 public void hookUpMapView() {
140 Main.map.mapView.addMouseListener(new MouseAdapter() {
141 @Override
142 public void mousePressed(MouseEvent e) {
143 if (e.getButton() != MouseEvent.BUTTON1)
144 return;
145 boolean mousePressedInButton = false;
146 if (e.getPoint() != null) {
147 for (Marker mkr : data) {
148 if (mkr.containsPoint(e.getPoint())) {
149 mousePressedInButton = true;
150 break;
151 }
152 }
153 }
154 if (!mousePressedInButton)
155 return;
156 mousePressed = true;
157 if (isVisible()) {
158 Main.map.mapView.repaint();
159 }
160 }
161
162 @Override
163 public void mouseReleased(MouseEvent ev) {
164 if (ev.getButton() != MouseEvent.BUTTON1 || !mousePressed)
165 return;
166 mousePressed = false;
167 if (!isVisible())
168 return;
169 if (ev.getPoint() != null) {
170 for (Marker mkr : data) {
171 if (mkr.containsPoint(ev.getPoint())) {
172 mkr.actionPerformed(new ActionEvent(this, 0, null));
173 }
174 }
175 }
176 Main.map.mapView.repaint();
177 }
178 });
179 }
180
181 /**
182 * Return a static icon.
183 */
184 @Override
185 public Icon getIcon() {
186 return ImageProvider.get("layer", "marker_small");
187 }
188
189 @Override
190 public Color getColor(boolean ignoreCustom) {
191 String name = getName();
192 return Main.pref.getColor(marktr("gps marker"), name != null ? "layer "+name : null, Color.gray);
193 }
194
195 /* for preferences */
196 public static Color getGenericColor() {
197 return Main.pref.getColor(marktr("gps marker"), Color.gray);
198 }
199
200 @Override
201 public void paint(Graphics2D g, MapView mv, Bounds box) {
202 boolean showTextOrIcon = isTextOrIconShown();
203 g.setColor(getColor(true));
204
205 if (mousePressed) {
206 boolean mousePressedTmp = mousePressed;
207 Point mousePos = mv.getMousePosition(); // Get mouse position only when necessary (it's the slowest part of marker layer painting)
208 for (Marker mkr : data) {
209 if (mousePos != null && mkr.containsPoint(mousePos)) {
210 mkr.paint(g, mv, mousePressedTmp, showTextOrIcon);
211 mousePressedTmp = false;
212 }
213 }
214 } else {
215 for (Marker mkr : data) {
216 mkr.paint(g, mv, false, showTextOrIcon);
217 }
218 }
219 }
220
221 @Override public String getToolTipText() {
222 return data.size()+" "+trn("marker", "markers", data.size());
223 }
224
225 @Override public void mergeFrom(Layer from) {
226 MarkerLayer layer = (MarkerLayer) from;
227 data.addAll(layer.data);
228 Collections.sort(data, new Comparator<Marker>() {
229 @Override
230 public int compare(Marker o1, Marker o2) {
231 return Double.compare(o1.time, o2.time);
232 }
233 });
234 }
235
236 @Override public boolean isMergable(Layer other) {
237 return other instanceof MarkerLayer;
238 }
239
240 @Override public void visitBoundingBox(BoundingXYVisitor v) {
241 for (Marker mkr : data) {
242 v.visit(mkr.getEastNorth());
243 }
244 }
245
246 @Override public Object getInfoComponent() {
247 return "<html>"+trn("{0} consists of {1} marker", "{0} consists of {1} markers", data.size(), getName(), data.size()) + "</html>";
248 }
249
250 @Override public Action[] getMenuEntries() {
251 Collection<Action> components = new ArrayList<>();
252 components.add(LayerListDialog.getInstance().createShowHideLayerAction());
253 components.add(new ShowHideMarkerText(this));
254 components.add(LayerListDialog.getInstance().createDeleteLayerAction());
255 components.add(LayerListDialog.getInstance().createMergeLayerAction(this));
256 components.add(SeparatorLayerAction.INSTANCE);
257 components.add(new CustomizeColor(this));
258 components.add(SeparatorLayerAction.INSTANCE);
259 components.add(new SynchronizeAudio());
260 if (Main.pref.getBoolean("marker.traceaudio", true)) {
261 components.add(new MoveAudio());
262 }
263 components.add(new JumpToNextMarker(this));
264 components.add(new JumpToPreviousMarker(this));
265 components.add(new ConvertToDataLayerAction.FromMarkerLayer(this));
266 components.add(new RenameLayerAction(getAssociatedFile(), this));
267 components.add(SeparatorLayerAction.INSTANCE);
268 components.add(new LayerListPopup.InfoAction(this));
269 return components.toArray(new Action[components.size()]);
270 }
271
272 public boolean synchronizeAudioMarkers(final AudioMarker startMarker) {
273 syncAudioMarker = startMarker;
274 if (syncAudioMarker != null && !data.contains(syncAudioMarker)) {
275 syncAudioMarker = null;
276 }
277 if (syncAudioMarker == null) {
278 // find the first audioMarker in this layer
279 for (Marker m : data) {
280 if (m instanceof AudioMarker) {
281 syncAudioMarker = (AudioMarker) m;
282 break;
283 }
284 }
285 }
286 if (syncAudioMarker == null)
287 return false;
288
289 // apply adjustment to all subsequent audio markers in the layer
290 double adjustment = AudioPlayer.position() - syncAudioMarker.offset; // in seconds
291 boolean seenStart = false;
292 try {
293 URI uri = syncAudioMarker.url().toURI();
294 for (Marker m : data) {
295 if (m == syncAudioMarker) {
296 seenStart = true;
297 }
298 if (seenStart && m instanceof AudioMarker) {
299 AudioMarker ma = (AudioMarker) m;
300 // Do not ever call URL.equals but use URI.equals instead to avoid Internet connection
301 // See http://michaelscharf.blogspot.fr/2006/11/javaneturlequals-and-hashcode-make.html for details
302 if (ma.url().toURI().equals(uri)) {
303 ma.adjustOffset(adjustment);
304 }
305 }
306 }
307 } catch (URISyntaxException e) {
308 Main.warn(e);
309 }
310 return true;
311 }
312
313 public AudioMarker addAudioMarker(double time, LatLon coor) {
314 // find first audio marker to get absolute start time
315 double offset = 0.0;
316 AudioMarker am = null;
317 for (Marker m : data) {
318 if (m.getClass() == AudioMarker.class) {
319 am = (AudioMarker) m;
320 offset = time - am.time;
321 break;
322 }
323 }
324 if (am == null) {
325 JOptionPane.showMessageDialog(
326 Main.parent,
327 tr("No existing audio markers in this layer to offset from."),
328 tr("Error"),
329 JOptionPane.ERROR_MESSAGE
330 );
331 return null;
332 }
333
334 // make our new marker
335 AudioMarker newAudioMarker = new AudioMarker(coor,
336 null, AudioPlayer.url(), this, time, offset);
337
338 // insert it at the right place in a copy the collection
339 Collection<Marker> newData = new ArrayList<>();
340 am = null;
341 AudioMarker ret = newAudioMarker; // save to have return value
342 for (Marker m : data) {
343 if (m.getClass() == AudioMarker.class) {
344 am = (AudioMarker) m;
345 if (newAudioMarker != null && offset < am.offset) {
346 newAudioMarker.adjustOffset(am.syncOffset()); // i.e. same as predecessor
347 newData.add(newAudioMarker);
348 newAudioMarker = null;
349 }
350 }
351 newData.add(m);
352 }
353
354 if (newAudioMarker != null) {
355 if (am != null) {
356 newAudioMarker.adjustOffset(am.syncOffset()); // i.e. same as predecessor
357 }
358 newData.add(newAudioMarker); // insert at end
359 }
360
361 // replace the collection
362 data.clear();
363 data.addAll(newData);
364 return ret;
365 }
366
367 @Override
368 public void jumpToNextMarker() {
369 if (currentMarker == null) {
370 currentMarker = data.get(0);
371 } else {
372 boolean foundCurrent = false;
373 for (Marker m: data) {
374 if (foundCurrent) {
375 currentMarker = m;
376 break;
377 } else if (currentMarker == m) {
378 foundCurrent = true;
379 }
380 }
381 }
382 Main.map.mapView.zoomTo(currentMarker.getEastNorth());
383 }
384
385 @Override
386 public void jumpToPreviousMarker() {
387 if (currentMarker == null) {
388 currentMarker = data.get(data.size() - 1);
389 } else {
390 boolean foundCurrent = false;
391 for (int i = data.size() - 1; i >= 0; i--) {
392 Marker m = data.get(i);
393 if (foundCurrent) {
394 currentMarker = m;
395 break;
396 } else if (currentMarker == m) {
397 foundCurrent = true;
398 }
399 }
400 }
401 Main.map.mapView.zoomTo(currentMarker.getEastNorth());
402 }
403
404 public static void playAudio() {
405 playAdjacentMarker(null, true);
406 }
407
408 public static void playNextMarker() {
409 playAdjacentMarker(AudioMarker.recentlyPlayedMarker(), true);
410 }
411
412 public static void playPreviousMarker() {
413 playAdjacentMarker(AudioMarker.recentlyPlayedMarker(), false);
414 }
415
416 private static Marker getAdjacentMarker(Marker startMarker, boolean next, Layer layer) {
417 Marker previousMarker = null;
418 boolean nextTime = false;
419 if (layer.getClass() == MarkerLayer.class) {
420 MarkerLayer markerLayer = (MarkerLayer) layer;
421 for (Marker marker : markerLayer.data) {
422 if (marker == startMarker) {
423 if (next) {
424 nextTime = true;
425 } else {
426 if (previousMarker == null) {
427 previousMarker = startMarker; // if no previous one, play the first one again
428 }
429 return previousMarker;
430 }
431 } else if (marker.getClass() == AudioMarker.class) {
432 if (nextTime || startMarker == null)
433 return marker;
434 previousMarker = marker;
435 }
436 }
437 if (nextTime) // there was no next marker in that layer, so play the last one again
438 return startMarker;
439 }
440 return null;
441 }
442
443 private static void playAdjacentMarker(Marker startMarker, boolean next) {
444 if (!Main.isDisplayingMapView())
445 return;
446 Marker m = null;
447 Layer l = Main.map.mapView.getActiveLayer();
448 if (l != null) {
449 m = getAdjacentMarker(startMarker, next, l);
450 }
451 if (m == null) {
452 for (Layer layer : Main.map.mapView.getAllLayers()) {
453 m = getAdjacentMarker(startMarker, next, layer);
454 if (m != null) {
455 break;
456 }
457 }
458 }
459 if (m != null) {
460 ((AudioMarker) m).play();
461 }
462 }
463
464 /**
465 * Get state of text display.
466 * @return <code>true</code> if text should be shown, <code>false</code> otherwise.
467 */
468 private boolean isTextOrIconShown() {
469 String current = Main.pref.get("marker.show "+getName(), "show");
470 return "show".equalsIgnoreCase(current);
471 }
472
473 public static final class ShowHideMarkerText extends AbstractAction implements LayerAction {
474 private final transient MarkerLayer layer;
475
476 public ShowHideMarkerText(MarkerLayer layer) {
477 super(tr("Show Text/Icons"), ImageProvider.get("dialogs", "showhide"));
478 putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the marker text and icons."));
479 putValue("help", ht("/Action/ShowHideTextIcons"));
480 this.layer = layer;
481 }
482
483 @Override
484 public void actionPerformed(ActionEvent e) {
485 Main.pref.put("marker.show "+layer.getName(), layer.isTextOrIconShown() ? "hide" : "show");
486 Main.map.mapView.repaint();
487 }
488
489 @Override
490 public Component createMenuComponent() {
491 JCheckBoxMenuItem showMarkerTextItem = new JCheckBoxMenuItem(this);
492 showMarkerTextItem.setState(layer.isTextOrIconShown());
493 return showMarkerTextItem;
494 }
495
496 @Override
497 public boolean supportLayers(List<Layer> layers) {
498 return layers.size() == 1 && layers.get(0) instanceof MarkerLayer;
499 }
500 }
501
502 private class SynchronizeAudio extends AbstractAction {
503
504 /**
505 * Constructs a new {@code SynchronizeAudio} action.
506 */
507 public SynchronizeAudio() {
508 super(tr("Synchronize Audio"), ImageProvider.get("audio-sync"));
509 putValue("help", ht("/Action/SynchronizeAudio"));
510 }
511
512 @Override
513 public void actionPerformed(ActionEvent e) {
514 if (!AudioPlayer.paused()) {
515 JOptionPane.showMessageDialog(
516 Main.parent,
517 tr("You need to pause audio at the moment when you hear your synchronization cue."),
518 tr("Warning"),
519 JOptionPane.WARNING_MESSAGE
520 );
521 return;
522 }
523 AudioMarker recent = AudioMarker.recentlyPlayedMarker();
524 if (synchronizeAudioMarkers(recent)) {
525 JOptionPane.showMessageDialog(
526 Main.parent,
527 tr("Audio synchronized at point {0}.", syncAudioMarker.getText()),
528 tr("Information"),
529 JOptionPane.INFORMATION_MESSAGE
530 );
531 } else {
532 JOptionPane.showMessageDialog(
533 Main.parent,
534 tr("Unable to synchronize in layer being played."),
535 tr("Error"),
536 JOptionPane.ERROR_MESSAGE
537 );
538 }
539 }
540 }
541
542 private class MoveAudio extends AbstractAction {
543
544 public MoveAudio() {
545 super(tr("Make Audio Marker at Play Head"), ImageProvider.get("addmarkers"));
546 putValue("help", ht("/Action/MakeAudioMarkerAtPlayHead"));
547 }
548
549 @Override
550 public void actionPerformed(ActionEvent e) {
551 if (!AudioPlayer.paused()) {
552 JOptionPane.showMessageDialog(
553 Main.parent,
554 tr("You need to have paused audio at the point on the track where you want the marker."),
555 tr("Warning"),
556 JOptionPane.WARNING_MESSAGE
557 );
558 return;
559 }
560 PlayHeadMarker playHeadMarker = Main.map.mapView.playHeadMarker;
561 if (playHeadMarker == null)
562 return;
563 addAudioMarker(playHeadMarker.time, playHeadMarker.getCoor());
564 Main.map.mapView.repaint();
565 }
566 }
567
568}
Note: See TracBrowser for help on using the repository browser.