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

Last change on this file since 2450 was 2450, checked in by jttt, 15 years ago

Added parameter Bounds to MapView, draw only currently visible primitives in MapPaintVisititor

  • Property svn:eol-style set to native
File size: 17.3 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.layer.markerlayer;
3
4import static org.openstreetmap.josm.tools.I18n.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6import static org.openstreetmap.josm.tools.I18n.trn;
7
8import java.awt.Color;
9import java.awt.Component;
10import java.awt.Graphics2D;
11import java.awt.Point;
12import java.awt.event.ActionEvent;
13import java.awt.event.ActionListener;
14import java.awt.event.MouseAdapter;
15import java.awt.event.MouseEvent;
16import java.io.File;
17import java.net.URL;
18import java.util.ArrayList;
19import java.util.Collection;
20
21import javax.swing.AbstractAction;
22import javax.swing.Icon;
23import javax.swing.JColorChooser;
24import javax.swing.JMenuItem;
25import javax.swing.JOptionPane;
26import javax.swing.JSeparator;
27import javax.swing.SwingUtilities;
28
29import org.openstreetmap.josm.Main;
30import org.openstreetmap.josm.actions.RenameLayerAction;
31import org.openstreetmap.josm.data.Bounds;
32import org.openstreetmap.josm.data.coor.LatLon;
33import org.openstreetmap.josm.data.gpx.GpxData;
34import org.openstreetmap.josm.data.gpx.GpxLink;
35import org.openstreetmap.josm.data.gpx.WayPoint;
36import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
37import org.openstreetmap.josm.gui.MapView;
38import org.openstreetmap.josm.gui.dialogs.LayerListDialog;
39import org.openstreetmap.josm.gui.dialogs.LayerListPopup;
40import org.openstreetmap.josm.gui.layer.GpxLayer;
41import org.openstreetmap.josm.gui.layer.Layer;
42import org.openstreetmap.josm.tools.AudioPlayer;
43import org.openstreetmap.josm.tools.ImageProvider;
44
45/**
46 * A layer holding markers.
47 *
48 * Markers are GPS points with a name and, optionally, a symbol code attached;
49 * marker layers can be created from waypoints when importing raw GPS data,
50 * but they may also come from other sources.
51 *
52 * The symbol code is for future use.
53 *
54 * The data is read only.
55 */
56public class MarkerLayer extends Layer {
57
58 /**
59 * A list of markers.
60 */
61 public final Collection<Marker> data;
62 private boolean mousePressed = false;
63 public GpxLayer fromLayer = null;
64
65 @SuppressWarnings("unchecked")
66 public MarkerLayer(GpxData indata, String name, File associatedFile, GpxLayer fromLayer) {
67
68 super(name);
69 this.setAssociatedFile(associatedFile);
70 this.data = new ArrayList<Marker>();
71 this.fromLayer = fromLayer;
72 double firstTime = -1.0;
73 String lastLinkedFile = "";
74
75 for (WayPoint wpt : indata.waypoints) {
76 /* calculate time differences in waypoints */
77 double time = wpt.time;
78 boolean wpt_has_link = wpt.attr.containsKey(GpxData.META_LINKS);
79 if (firstTime < 0 && wpt_has_link) {
80 firstTime = time;
81 for (GpxLink oneLink : (Collection<GpxLink>) wpt.attr.get(GpxData.META_LINKS)) {
82 lastLinkedFile = oneLink.uri;
83 break;
84 }
85 }
86 if (wpt_has_link) {
87 for (GpxLink oneLink : (Collection<GpxLink>) wpt.attr.get(GpxData.META_LINKS)) {
88 if (!oneLink.uri.equals(lastLinkedFile)) {
89 firstTime = time;
90 }
91 lastLinkedFile = oneLink.uri;
92 break;
93 }
94 }
95 Marker m = Marker.createMarker(wpt, indata.storageFile, this, time, time - firstTime);
96 if (m != null) {
97 data.add(m);
98 }
99 }
100
101 SwingUtilities.invokeLater(new Runnable(){
102 public void run() {
103 Main.map.mapView.addMouseListener(new MouseAdapter() {
104 @Override public void mousePressed(MouseEvent e) {
105 if (e.getButton() != MouseEvent.BUTTON1)
106 return;
107 boolean mousePressedInButton = false;
108 if (e.getPoint() != null) {
109 for (Marker mkr : data) {
110 if (mkr.containsPoint(e.getPoint())) {
111 mousePressedInButton = true;
112 break;
113 }
114 }
115 }
116 if (! mousePressedInButton)
117 return;
118 mousePressed = true;
119 if (isVisible()) {
120 Main.map.mapView.repaint();
121 }
122 }
123 @Override public void mouseReleased(MouseEvent ev) {
124 if (ev.getButton() != MouseEvent.BUTTON1 || ! mousePressed)
125 return;
126 mousePressed = false;
127 if (!isVisible())
128 return;
129 if (ev.getPoint() != null) {
130 for (Marker mkr : data) {
131 if (mkr.containsPoint(ev.getPoint())) {
132 mkr.actionPerformed(new ActionEvent(this, 0, null));
133 }
134 }
135 }
136 Main.map.mapView.repaint();
137 }
138 });
139 }
140 });
141 }
142
143 /**
144 * Return a static icon.
145 */
146 @Override public Icon getIcon() {
147 return ImageProvider.get("layer", "marker_small");
148 }
149
150 static public Color getColor(String name)
151 {
152 return Main.pref.getColor(marktr("gps marker"), name != null ? "layer "+name : null, Color.gray);
153 }
154
155 @Override public void paint(Graphics2D g, MapView mv, Bounds box) {
156 boolean mousePressedTmp = mousePressed;
157 Point mousePos = mv.getMousePosition();
158 String mkrTextShow = Main.pref.get("marker.show "+getName(), "show");
159
160 g.setColor(getColor(getName()));
161
162 for (Marker mkr : data) {
163 if (mousePos != null && mkr.containsPoint(mousePos)) {
164 mkr.paint(g, mv, mousePressedTmp, mkrTextShow);
165 mousePressedTmp = false;
166 } else {
167 mkr.paint(g, mv, false, mkrTextShow);
168 }
169 }
170 }
171
172 @Override public String getToolTipText() {
173 return data.size()+" "+trn("marker", "markers", data.size());
174 }
175
176 @Override public void mergeFrom(Layer from) {
177 MarkerLayer layer = (MarkerLayer)from;
178 data.addAll(layer.data);
179 }
180
181 @Override public boolean isMergable(Layer other) {
182 return other instanceof MarkerLayer;
183 }
184
185 @Override public void visitBoundingBox(BoundingXYVisitor v) {
186 for (Marker mkr : data) {
187 v.visit(mkr.getEastNorth());
188 }
189 }
190
191 @Override public Object getInfoComponent() {
192 return "<html>"+trn("{0} consists of {1} marker", "{0} consists of {1} markers", data.size(), getName(), data.size()) + "</html>";
193 }
194
195 @Override public Component[] getMenuEntries() {
196 JMenuItem color = new JMenuItem(tr("Customize Color"), ImageProvider.get("colorchooser"));
197 color.putClientProperty("help", "Action/LayerCustomizeColor");
198 color.addActionListener(new ActionListener(){
199 public void actionPerformed(ActionEvent e) {
200 JColorChooser c = new JColorChooser(getColor(getName()));
201 Object[] options = new Object[]{tr("OK"), tr("Cancel"), tr("Default")};
202 int answer = JOptionPane.showOptionDialog(
203 Main.parent,
204 c,
205 tr("Choose a color"),
206 JOptionPane.OK_CANCEL_OPTION,
207 JOptionPane.PLAIN_MESSAGE,
208 null,
209 options,
210 options[0]
211 );
212 switch (answer) {
213 case 0:
214 Main.pref.putColor("layer "+getName(), c.getColor());
215 break;
216 case 1:
217 return;
218 case 2:
219 Main.pref.putColor("layer "+getName(), null);
220 break;
221 }
222 Main.map.repaint();
223 }
224 });
225
226 JMenuItem syncaudio = new JMenuItem(tr("Synchronize Audio"), ImageProvider.get("audio-sync"));
227 syncaudio.putClientProperty("help", "Action/SynchronizeAudio");
228 syncaudio.addActionListener(new ActionListener(){
229 public void actionPerformed(ActionEvent e) {
230 if (! AudioPlayer.paused()) {
231 JOptionPane.showMessageDialog(
232 Main.parent,
233 tr("You need to pause audio at the moment when you hear your synchronization cue."),
234 tr("Warning"),
235 JOptionPane.WARNING_MESSAGE
236 );
237 return;
238 }
239 AudioMarker recent = AudioMarker.recentlyPlayedMarker();
240 if (synchronizeAudioMarkers(recent)) {
241 JOptionPane.showMessageDialog(
242 Main.parent,
243 tr("Audio synchronized at point {0}.", recent.text),
244 tr("Information"),
245 JOptionPane.INFORMATION_MESSAGE
246 );
247 } else {
248 JOptionPane.showMessageDialog(
249 Main.parent,
250 tr("Unable to synchronize in layer being played."),
251 tr("Error"),
252 JOptionPane.ERROR_MESSAGE
253 );
254 }
255 }
256 });
257
258 JMenuItem moveaudio = new JMenuItem(tr("Make Audio Marker at Play Head"), ImageProvider.get("addmarkers"));
259 moveaudio.putClientProperty("help", "Action/MakeAudioMarkerAtPlayHead");
260 moveaudio.addActionListener(new ActionListener(){
261 public void actionPerformed(ActionEvent e) {
262 if (! AudioPlayer.paused()) {
263 JOptionPane.showMessageDialog(
264 Main.parent,
265 tr("You need to have paused audio at the point on the track where you want the marker."),
266 tr("Warning"),
267 JOptionPane.WARNING_MESSAGE
268 );
269 return;
270 }
271 PlayHeadMarker playHeadMarker = Main.map.mapView.playHeadMarker;
272 if (playHeadMarker == null)
273 return;
274 addAudioMarker(playHeadMarker.time, playHeadMarker.getCoor());
275 Main.map.mapView.repaint();
276 }
277 });
278
279 Collection<Component> components = new ArrayList<Component>();
280 components.add(new JMenuItem(LayerListDialog.getInstance().createShowHideLayerAction(this)));
281 components.add(new JMenuItem(new ShowHideMarkerText(this)));
282 components.add(new JMenuItem(LayerListDialog.getInstance().createDeleteLayerAction(this)));
283 components.add(new JSeparator());
284 components.add(color);
285 components.add(new JSeparator());
286 components.add(syncaudio);
287 if (Main.pref.getBoolean("marker.traceaudio", true)) {
288 components.add (moveaudio);
289 }
290 components.add(new JMenuItem(new RenameLayerAction(getAssociatedFile(), this)));
291 components.add(new JSeparator());
292 components.add(new JMenuItem(new LayerListPopup.InfoAction(this)));
293 return components.toArray(new Component[0]);
294 }
295
296 public boolean synchronizeAudioMarkers(AudioMarker startMarker) {
297 if (startMarker != null && ! data.contains(startMarker)) {
298 startMarker = null;
299 }
300 if (startMarker == null) {
301 // find the first audioMarker in this layer
302 for (Marker m : data) {
303 if (m instanceof AudioMarker) {
304 startMarker = (AudioMarker) m;
305 break;
306 }
307 }
308 }
309 if (startMarker == null)
310 return false;
311
312 // apply adjustment to all subsequent audio markers in the layer
313 double adjustment = AudioPlayer.position() - startMarker.offset; // in seconds
314 boolean seenStart = false;
315 URL url = startMarker.url();
316 for (Marker m : data) {
317 if (m == startMarker) {
318 seenStart = true;
319 }
320 if (seenStart) {
321 AudioMarker ma = (AudioMarker) m; // it must be an AudioMarker
322 if (ma.url().equals(url)) {
323 ma.adjustOffset(adjustment);
324 }
325 }
326 }
327 return true;
328 }
329
330 public AudioMarker addAudioMarker(double time, LatLon coor) {
331 // find first audio marker to get absolute start time
332 double offset = 0.0;
333 AudioMarker am = null;
334 for (Marker m : data) {
335 if (m.getClass() == AudioMarker.class) {
336 am = (AudioMarker)m;
337 offset = time - am.time;
338 break;
339 }
340 }
341 if (am == null) {
342 JOptionPane.showMessageDialog(
343 Main.parent,
344 tr("No existing audio markers in this layer to offset from."),
345 tr("Error"),
346 JOptionPane.ERROR_MESSAGE
347 );
348 return null;
349 }
350
351 // make our new marker
352 AudioMarker newAudioMarker = AudioMarker.create(coor,
353 AudioMarker.inventName(offset), AudioPlayer.url().toString(), this, time, offset);
354
355 // insert it at the right place in a copy the collection
356 Collection<Marker> newData = new ArrayList<Marker>();
357 am = null;
358 AudioMarker ret = newAudioMarker; // save to have return value
359 for (Marker m : data) {
360 if (m.getClass() == AudioMarker.class) {
361 am = (AudioMarker) m;
362 if (newAudioMarker != null && offset < am.offset) {
363 newAudioMarker.adjustOffset(am.syncOffset()); // i.e. same as predecessor
364 newData.add(newAudioMarker);
365 newAudioMarker = null;
366 }
367 }
368 newData.add(m);
369 }
370
371 if (newAudioMarker != null) {
372 if (am != null) {
373 newAudioMarker.adjustOffset(am.syncOffset()); // i.e. same as predecessor
374 }
375 newData.add(newAudioMarker); // insert at end
376 }
377
378 // replace the collection
379 data.clear();
380 data.addAll(newData);
381 return ret;
382 }
383
384 public static void playAudio() {
385 playAdjacentMarker(null, true);
386 }
387
388 public static void playNextMarker() {
389 playAdjacentMarker(AudioMarker.recentlyPlayedMarker(), true);
390 }
391
392 public static void playPreviousMarker() {
393 playAdjacentMarker(AudioMarker.recentlyPlayedMarker(), false);
394 }
395
396 private static Marker getAdjacentMarker(Marker startMarker, boolean next, Layer layer) {
397 Marker previousMarker = null;
398 boolean nextTime = false;
399 if (layer.getClass() == MarkerLayer.class) {
400 MarkerLayer markerLayer = (MarkerLayer) layer;
401 for (Marker marker : markerLayer.data) {
402 if (marker == startMarker) {
403 if (next) {
404 nextTime = true;
405 } else {
406 if (previousMarker == null) {
407 previousMarker = startMarker; // if no previous one, play the first one again
408 }
409 return previousMarker;
410 }
411 }
412 else if (marker.getClass() == AudioMarker.class)
413 {
414 if(nextTime || startMarker == null)
415 return marker;
416 previousMarker = marker;
417 }
418 }
419 if (nextTime) // there was no next marker in that layer, so play the last one again
420 return startMarker;
421 }
422 return null;
423 }
424
425 private static void playAdjacentMarker(Marker startMarker, boolean next) {
426 Marker m = null;
427 if (Main.map == null || Main.map.mapView == null)
428 return;
429 Layer l = Main.map.mapView.getActiveLayer();
430 if(l != null) {
431 m = getAdjacentMarker(startMarker, next, l);
432 }
433 if(m == null)
434 {
435 for (Layer layer : Main.map.mapView.getAllLayers())
436 {
437 m = getAdjacentMarker(startMarker, next, layer);
438 if(m != null) {
439 break;
440 }
441 }
442 }
443 if(m != null) {
444 ((AudioMarker)m).play();
445 }
446 }
447
448
449 public final class ShowHideMarkerText extends AbstractAction {
450 private final Layer layer;
451
452 public ShowHideMarkerText(Layer layer) {
453 super(tr("Show/Hide Text/Icons"), ImageProvider.get("dialogs", "showhide"));
454 putValue(SHORT_DESCRIPTION, tr("Toggle visible state of the marker text and icons."));
455 putValue("help", "Action/ShowHideTextIcons");
456 this.layer = layer;
457 }
458
459 public void actionPerformed(ActionEvent e) {
460 String current = Main.pref.get("marker.show "+layer.getName(),"show");
461 Main.pref.put("marker.show "+layer.getName(), current.equalsIgnoreCase("show") ? "hide" : "show");
462 Main.map.mapView.repaint();
463 }
464 }
465}
Note: See TracBrowser for help on using the repository browser.