source: josm/trunk/src/org/openstreetmap/josm/gui/MapStatus.java@ 12216

Last change on this file since 12216 was 12065, checked in by michael2402, 7 years ago

More ds.beginUpdate()/endUpdate() try/finally fixes, checked all calls of beginUpdate().

  • Property svn:eol-style set to native
File size: 44.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui;
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;
7
8import java.awt.AWTEvent;
9import java.awt.Color;
10import java.awt.Component;
11import java.awt.Cursor;
12import java.awt.Dimension;
13import java.awt.EventQueue;
14import java.awt.Font;
15import java.awt.GridBagLayout;
16import java.awt.Point;
17import java.awt.SystemColor;
18import java.awt.Toolkit;
19import java.awt.event.AWTEventListener;
20import java.awt.event.ActionEvent;
21import java.awt.event.ComponentAdapter;
22import java.awt.event.ComponentEvent;
23import java.awt.event.InputEvent;
24import java.awt.event.KeyAdapter;
25import java.awt.event.KeyEvent;
26import java.awt.event.MouseAdapter;
27import java.awt.event.MouseEvent;
28import java.awt.event.MouseListener;
29import java.awt.event.MouseMotionListener;
30import java.lang.reflect.InvocationTargetException;
31import java.text.DecimalFormat;
32import java.util.ArrayList;
33import java.util.Collection;
34import java.util.ConcurrentModificationException;
35import java.util.List;
36import java.util.Objects;
37import java.util.TreeSet;
38import java.util.concurrent.BlockingQueue;
39import java.util.concurrent.LinkedBlockingQueue;
40
41import javax.swing.AbstractAction;
42import javax.swing.BorderFactory;
43import javax.swing.JCheckBoxMenuItem;
44import javax.swing.JLabel;
45import javax.swing.JMenuItem;
46import javax.swing.JPanel;
47import javax.swing.JPopupMenu;
48import javax.swing.JProgressBar;
49import javax.swing.JScrollPane;
50import javax.swing.JSeparator;
51import javax.swing.Popup;
52import javax.swing.PopupFactory;
53import javax.swing.UIManager;
54import javax.swing.event.PopupMenuEvent;
55import javax.swing.event.PopupMenuListener;
56
57import org.openstreetmap.josm.Main;
58import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
59import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
60import org.openstreetmap.josm.data.SystemOfMeasurement;
61import org.openstreetmap.josm.data.SystemOfMeasurement.SoMChangeListener;
62import org.openstreetmap.josm.data.coor.CoordinateFormat;
63import org.openstreetmap.josm.data.coor.LatLon;
64import org.openstreetmap.josm.data.osm.DataSet;
65import org.openstreetmap.josm.data.osm.OsmPrimitive;
66import org.openstreetmap.josm.data.osm.Way;
67import org.openstreetmap.josm.data.preferences.AbstractProperty;
68import org.openstreetmap.josm.data.preferences.BooleanProperty;
69import org.openstreetmap.josm.data.preferences.ColorProperty;
70import org.openstreetmap.josm.data.preferences.DoubleProperty;
71import org.openstreetmap.josm.gui.help.Helpful;
72import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
73import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor;
74import org.openstreetmap.josm.gui.progress.PleaseWaitProgressMonitor.ProgressMonitorDialog;
75import org.openstreetmap.josm.gui.util.GuiHelper;
76import org.openstreetmap.josm.gui.widgets.ImageLabel;
77import org.openstreetmap.josm.gui.widgets.JosmTextField;
78import org.openstreetmap.josm.tools.Destroyable;
79import org.openstreetmap.josm.tools.GBC;
80import org.openstreetmap.josm.tools.ImageProvider;
81import org.openstreetmap.josm.tools.Utils;
82
83/**
84 * A component that manages some status information display about the map.
85 * It keeps a status line below the map up to date and displays some tooltip
86 * information if the user hold the mouse long enough at some point.
87 *
88 * All this is done in background to not disturb other processes.
89 *
90 * The background thread does not alter any data of the map (read only thread).
91 * Also it is rather fail safe. In case of some error in the data, it just does
92 * nothing instead of whining and complaining.
93 *
94 * @author imi
95 */
96public final class MapStatus extends JPanel implements Helpful, Destroyable, PreferenceChangedListener, SoMChangeListener {
97
98 private final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(Main.pref.get("statusbar.decimal-format", "0.0"));
99 private static final AbstractProperty<Double> DISTANCE_THRESHOLD = new DoubleProperty("statusbar.distance-threshold", 0.01).cached();
100
101 private static final AbstractProperty<Boolean> SHOW_ID = new BooleanProperty("osm-primitives.showid", false);
102
103 /**
104 * Property for map status background color.
105 * @since 6789
106 */
107 public static final ColorProperty PROP_BACKGROUND_COLOR = new ColorProperty(
108 marktr("Status bar background"), "#b8cfe5");
109
110 /**
111 * Property for map status background color (active state).
112 * @since 6789
113 */
114 public static final ColorProperty PROP_ACTIVE_BACKGROUND_COLOR = new ColorProperty(
115 marktr("Status bar background: active"), "#aaff5e");
116
117 /**
118 * Property for map status foreground color.
119 * @since 6789
120 */
121 public static final ColorProperty PROP_FOREGROUND_COLOR = new ColorProperty(
122 marktr("Status bar foreground"), Color.black);
123
124 /**
125 * Property for map status foreground color (active state).
126 * @since 6789
127 */
128 public static final ColorProperty PROP_ACTIVE_FOREGROUND_COLOR = new ColorProperty(
129 marktr("Status bar foreground: active"), Color.black);
130
131 /**
132 * The MapView this status belongs to.
133 */
134 private final MapView mv;
135 private final transient Collector collector;
136
137 static final class ShowMonitorDialogMouseAdapter extends MouseAdapter {
138 @Override
139 public void mouseClicked(MouseEvent e) {
140 PleaseWaitProgressMonitor monitor = Main.currentProgressMonitor;
141 if (monitor != null) {
142 monitor.showForegroundDialog();
143 }
144 }
145 }
146
147 static final class JumpToOnLeftClickMouseAdapter extends MouseAdapter {
148 @Override
149 public void mouseClicked(MouseEvent e) {
150 if (e.getButton() != MouseEvent.BUTTON3) {
151 Main.main.menu.jumpToAct.showJumpToDialog();
152 }
153 }
154 }
155
156 public class BackgroundProgressMonitor implements ProgressMonitorDialog {
157
158 private String title;
159 private String customText;
160
161 private void updateText() {
162 if (customText != null && !customText.isEmpty()) {
163 progressBar.setToolTipText(tr("{0} ({1})", title, customText));
164 } else {
165 progressBar.setToolTipText(title);
166 }
167 }
168
169 @Override
170 public void setVisible(boolean visible) {
171 progressBar.setVisible(visible);
172 }
173
174 @Override
175 public void updateProgress(int progress) {
176 progressBar.setValue(progress);
177 progressBar.repaint();
178 MapStatus.this.doLayout();
179 }
180
181 @Override
182 public void setCustomText(String text) {
183 this.customText = text;
184 updateText();
185 }
186
187 @Override
188 public void setCurrentAction(String text) {
189 this.title = text;
190 updateText();
191 }
192
193 @Override
194 public void setIndeterminate(boolean newValue) {
195 UIManager.put("ProgressBar.cycleTime", UIManager.getInt("ProgressBar.repaintInterval") * 100);
196 progressBar.setIndeterminate(newValue);
197 }
198
199 @Override
200 public void appendLogMessage(String message) {
201 if (message != null && !message.isEmpty()) {
202 Main.info("appendLogMessage not implemented for background tasks. Message was: " + message);
203 }
204 }
205
206 }
207
208 /** The {@link CoordinateFormat} set in the previous update */
209 private transient CoordinateFormat previousCoordinateFormat;
210 private final ImageLabel latText = new ImageLabel("lat",
211 null, LatLon.SOUTH_POLE.latToString(CoordinateFormat.DEGREES_MINUTES_SECONDS).length(), PROP_BACKGROUND_COLOR.get());
212 private final ImageLabel lonText = new ImageLabel("lon",
213 null, new LatLon(0, 180).lonToString(CoordinateFormat.DEGREES_MINUTES_SECONDS).length(), PROP_BACKGROUND_COLOR.get());
214 private final ImageLabel headingText = new ImageLabel("heading",
215 tr("The (compass) heading of the line segment being drawn."),
216 DECIMAL_FORMAT.format(360).length() + 1, PROP_BACKGROUND_COLOR.get());
217 private final ImageLabel angleText = new ImageLabel("angle",
218 tr("The angle between the previous and the current way segment."),
219 DECIMAL_FORMAT.format(360).length() + 1, PROP_BACKGROUND_COLOR.get());
220 private final ImageLabel distText = new ImageLabel("dist",
221 tr("The length of the new way segment being drawn."), 10, PROP_BACKGROUND_COLOR.get());
222 private final ImageLabel nameText = new ImageLabel("name",
223 tr("The name of the object at the mouse pointer."), getNameLabelCharacterCount(Main.parent), PROP_BACKGROUND_COLOR.get());
224 private final JosmTextField helpText = new JosmTextField();
225 private final JProgressBar progressBar = new JProgressBar();
226 private final transient ComponentAdapter mvComponentAdapter;
227 public final transient BackgroundProgressMonitor progressMonitor = new BackgroundProgressMonitor();
228
229 // Distance value displayed in distText, stored if refresh needed after a change of system of measurement
230 private double distValue;
231
232 // Determines if angle panel is enabled or not
233 private boolean angleEnabled;
234
235 /**
236 * This is the thread that runs in the background and collects the information displayed.
237 * It gets destroyed by destroy() when the MapFrame itself is destroyed.
238 */
239 private final transient Thread thread;
240
241 private final transient List<StatusTextHistory> statusText = new ArrayList<>();
242
243 protected static final class StatusTextHistory {
244 private final Object id;
245 private final String text;
246
247 StatusTextHistory(Object id, String text) {
248 this.id = id;
249 this.text = text;
250 }
251
252 @Override
253 public boolean equals(Object obj) {
254 return obj instanceof StatusTextHistory && ((StatusTextHistory) obj).id == id;
255 }
256
257 @Override
258 public int hashCode() {
259 return System.identityHashCode(id);
260 }
261 }
262
263 /**
264 * The collector class that waits for notification and then update the display objects.
265 *
266 * @author imi
267 */
268 private final class Collector implements Runnable {
269 private final class CollectorWorker implements Runnable {
270 private final MouseState ms;
271
272 private CollectorWorker(MouseState ms) {
273 this.ms = ms;
274 }
275
276 @Override
277 public void run() {
278 // Freeze display when holding down CTRL
279 if ((ms.modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) {
280 // update the information popup's labels though, because the selection might have changed from the outside
281 popupUpdateLabels();
282 return;
283 }
284
285 // This try/catch is a hack to stop the flooding bug reports about this.
286 // The exception needed to handle with in the first place, means that this
287 // access to the data need to be restarted, if the main thread modifies the data.
288 DataSet ds = null;
289 // The popup != null check is required because a left-click produces several events as well,
290 // which would make this variable true. Of course we only want the popup to show
291 // if the middle mouse button has been pressed in the first place
292 boolean mouseNotMoved = oldMousePos != null && oldMousePos.equals(ms.mousePos);
293 boolean isAtOldPosition = mouseNotMoved && popup != null;
294 boolean middleMouseDown = (ms.modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0;
295
296 ds = mv.getLayerManager().getEditDataSet();
297 if (ds != null) {
298 // This is not perfect, if current dataset was changed during execution, the lock would be useless
299 if (isAtOldPosition && middleMouseDown) {
300 // Write lock is necessary when selecting in popupCycleSelection
301 // locks can not be upgraded -> if do read lock here and write lock later
302 // (in OsmPrimitive.updateFlags) then always occurs deadlock (#5814)
303 ds.beginUpdate();
304 } else {
305 ds.getReadLock().lock();
306 }
307 }
308 try {
309 // Set the text label in the bottom status bar
310 // "if mouse moved only" was added to stop heap growing
311 if (!mouseNotMoved) {
312 statusBarElementUpdate(ms);
313 }
314
315 // Popup Information
316 // display them if the middle mouse button is pressed and keep them until the mouse is moved
317 if (middleMouseDown || isAtOldPosition) {
318 Collection<OsmPrimitive> osms = mv.getAllNearest(ms.mousePos, OsmPrimitive::isSelectable);
319
320 final JPanel c = new JPanel(new GridBagLayout());
321 final JLabel lbl = new JLabel(
322 "<html>"+tr("Middle click again to cycle through.<br>"+
323 "Hold CTRL to select directly from this list with the mouse.<hr>")+"</html>",
324 null,
325 JLabel.HORIZONTAL
326 );
327 lbl.setHorizontalAlignment(JLabel.LEFT);
328 c.add(lbl, GBC.eol().insets(2, 0, 2, 0));
329
330 // Only cycle if the mouse has not been moved and the middle mouse button has been pressed at least
331 // twice (the reason for this is the popup != null check for isAtOldPosition, see above.
332 // This is a nice side effect though, because it does not change selection of the first middle click)
333 if (isAtOldPosition && middleMouseDown) {
334 // Hand down mouse modifiers so the SHIFT mod can be handled correctly (see function)
335 popupCycleSelection(osms, ms.modifiers);
336 }
337
338 // These labels may need to be updated from the outside so collect them
339 List<JLabel> lbls = new ArrayList<>(osms.size());
340 for (final OsmPrimitive osm : osms) {
341 JLabel l = popupBuildPrimitiveLabels(osm);
342 lbls.add(l);
343 c.add(l, GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 2));
344 }
345
346 popupShowPopup(popupCreatePopup(c, ms), lbls);
347 } else {
348 popupHidePopup();
349 }
350
351 oldMousePos = ms.mousePos;
352 } catch (ConcurrentModificationException ex) {
353 Main.warn(ex);
354 } finally {
355 if (ds != null) {
356 if (isAtOldPosition && middleMouseDown) {
357 ds.endUpdate();
358 } else {
359 ds.getReadLock().unlock();
360 }
361 }
362 }
363 }
364 }
365
366 /**
367 * the mouse position of the previous iteration. This is used to show
368 * the popup until the cursor is moved.
369 */
370 private Point oldMousePos;
371 /**
372 * Contains the labels that are currently shown in the information
373 * popup
374 */
375 private List<JLabel> popupLabels;
376 /**
377 * The popup displayed to show additional information
378 */
379 private Popup popup;
380
381 private final MapFrame parent;
382
383 private final BlockingQueue<MouseState> incomingMouseState = new LinkedBlockingQueue<>();
384
385 private Point lastMousePos;
386
387 Collector(MapFrame parent) {
388 this.parent = parent;
389 }
390
391 /**
392 * Execution function for the Collector.
393 */
394 @Override
395 public void run() {
396 registerListeners();
397 try {
398 for (;;) {
399 try {
400 final MouseState ms = incomingMouseState.take();
401 if (parent != Main.map)
402 return; // exit, if new parent.
403
404 // Do nothing, if required data is missing
405 if (ms.mousePos == null || mv.getCenter() == null) {
406 continue;
407 }
408
409 EventQueue.invokeAndWait(new CollectorWorker(ms));
410 } catch (InvocationTargetException e) {
411 Main.warn(e);
412 }
413 }
414 } catch (InterruptedException e) {
415 // Occurs frequently during JOSM shutdown, log set to trace only
416 Main.trace("InterruptedException in "+MapStatus.class.getSimpleName());
417 Thread.currentThread().interrupt();
418 } finally {
419 unregisterListeners();
420 }
421 }
422
423 /**
424 * Creates a popup for the given content next to the cursor. Tries to
425 * keep the popup on screen and shows a vertical scrollbar, if the
426 * screen is too small.
427 * @param content popup content
428 * @param ms mouse state
429 * @return popup
430 */
431 private Popup popupCreatePopup(Component content, MouseState ms) {
432 Point p = mv.getLocationOnScreen();
433 Dimension scrn = GuiHelper.getScreenSize();
434
435 // Create a JScrollPane around the content, in case there's not enough space
436 JScrollPane sp = GuiHelper.embedInVerticalScrollPane(content);
437 sp.setBorder(BorderFactory.createRaisedBevelBorder());
438 // Implement max-size content-independent
439 Dimension prefsize = sp.getPreferredSize();
440 int w = Math.min(prefsize.width, Math.min(800, (scrn.width/2) - 16));
441 int h = Math.min(prefsize.height, scrn.height - 10);
442 sp.setPreferredSize(new Dimension(w, h));
443
444 int xPos = p.x + ms.mousePos.x + 16;
445 // Display the popup to the left of the cursor if it would be cut
446 // off on its right, but only if more space is available
447 if (xPos + w > scrn.width && xPos > scrn.width/2) {
448 xPos = p.x + ms.mousePos.x - 4 - w;
449 }
450 int yPos = p.y + ms.mousePos.y + 16;
451 // Move the popup up if it would be cut off at its bottom but do not
452 // move it off screen on the top
453 if (yPos + h > scrn.height - 5) {
454 yPos = Math.max(5, scrn.height - h - 5);
455 }
456
457 PopupFactory pf = PopupFactory.getSharedInstance();
458 return pf.getPopup(mv, sp, xPos, yPos);
459 }
460
461 /**
462 * Calls this to update the element that is shown in the statusbar
463 * @param ms mouse state
464 */
465 private void statusBarElementUpdate(MouseState ms) {
466 final OsmPrimitive osmNearest = mv.getNearestNodeOrWay(ms.mousePos, OsmPrimitive::isUsable, false);
467 if (osmNearest != null) {
468 nameText.setText(osmNearest.getDisplayName(DefaultNameFormatter.getInstance()));
469 } else {
470 nameText.setText(tr("(no object)"));
471 }
472 }
473
474 /**
475 * Call this with a set of primitives to cycle through them. Method
476 * will automatically select the next item and update the map
477 * @param osms primitives to cycle through
478 * @param mods modifiers (i.e. control keys)
479 */
480 private void popupCycleSelection(Collection<OsmPrimitive> osms, int mods) {
481 DataSet ds = Main.getLayerManager().getEditDataSet();
482 // Find some items that are required for cycling through
483 OsmPrimitive firstItem = null;
484 OsmPrimitive firstSelected = null;
485 OsmPrimitive nextSelected = null;
486 for (final OsmPrimitive osm : osms) {
487 if (firstItem == null) {
488 firstItem = osm;
489 }
490 if (firstSelected != null && nextSelected == null) {
491 nextSelected = osm;
492 }
493 if (firstSelected == null && ds.isSelected(osm)) {
494 firstSelected = osm;
495 }
496 }
497
498 // Clear previous selection if SHIFT (add to selection) is not
499 // pressed. Cannot use "setSelected()" because it will cause a
500 // fireSelectionChanged event which is unnecessary at this point.
501 if ((mods & MouseEvent.SHIFT_DOWN_MASK) == 0) {
502 ds.clearSelection();
503 }
504
505 // This will cycle through the available items.
506 if (firstSelected != null) {
507 ds.clearSelection(firstSelected);
508 if (nextSelected != null) {
509 ds.addSelected(nextSelected);
510 }
511 } else if (firstItem != null) {
512 ds.addSelected(firstItem);
513 }
514 }
515
516 /**
517 * Tries to hide the given popup
518 */
519 private void popupHidePopup() {
520 popupLabels = null;
521 if (popup == null)
522 return;
523 final Popup staticPopup = popup;
524 popup = null;
525 EventQueue.invokeLater(staticPopup::hide);
526 }
527
528 /**
529 * Tries to show the given popup, can be hidden using {@link #popupHidePopup}
530 * If an old popup exists, it will be automatically hidden
531 * @param newPopup popup to show
532 * @param lbls lables to show (see {@link #popupLabels})
533 */
534 private void popupShowPopup(Popup newPopup, List<JLabel> lbls) {
535 final Popup staticPopup = newPopup;
536 if (this.popup != null) {
537 // If an old popup exists, remove it when the new popup has been drawn to keep flickering to a minimum
538 final Popup staticOldPopup = this.popup;
539 EventQueue.invokeLater(() -> {
540 staticPopup.show();
541 staticOldPopup.hide();
542 });
543 } else {
544 // There is no old popup
545 EventQueue.invokeLater(staticPopup::show);
546 }
547 this.popupLabels = lbls;
548 this.popup = newPopup;
549 }
550
551 /**
552 * This method should be called if the selection may have changed from
553 * outside of this class. This is the case when CTRL is pressed and the
554 * user clicks on the map instead of the popup.
555 */
556 private void popupUpdateLabels() {
557 if (this.popup == null || this.popupLabels == null)
558 return;
559 for (JLabel l : this.popupLabels) {
560 l.validate();
561 }
562 }
563
564 /**
565 * Sets the colors for the given label depending on the selected status of
566 * the given OsmPrimitive
567 *
568 * @param lbl The label to color
569 * @param osm The primitive to derive the colors from
570 */
571 private void popupSetLabelColors(JLabel lbl, OsmPrimitive osm) {
572 DataSet ds = Main.getLayerManager().getEditDataSet();
573 if (ds.isSelected(osm)) {
574 lbl.setBackground(SystemColor.textHighlight);
575 lbl.setForeground(SystemColor.textHighlightText);
576 } else {
577 lbl.setBackground(SystemColor.control);
578 lbl.setForeground(SystemColor.controlText);
579 }
580 }
581
582 /**
583 * Builds the labels with all necessary listeners for the info popup for the
584 * given OsmPrimitive
585 * @param osm The primitive to create the label for
586 * @return labels for info popup
587 */
588 private JLabel popupBuildPrimitiveLabels(final OsmPrimitive osm) {
589 final StringBuilder text = new StringBuilder(32);
590 String name = Utils.escapeReservedCharactersHTML(osm.getDisplayName(DefaultNameFormatter.getInstance()));
591 if (osm.isNewOrUndeleted() || osm.isModified()) {
592 name = "<i><b>"+ name + "*</b></i>";
593 }
594 text.append(name);
595
596 boolean idShown = SHOW_ID.get();
597 // fix #7557 - do not show ID twice
598
599 if (!osm.isNew() && !idShown) {
600 text.append(" [id=").append(osm.getId()).append(']');
601 }
602
603 if (osm.getUser() != null) {
604 text.append(" [").append(tr("User:")).append(' ')
605 .append(Utils.escapeReservedCharactersHTML(osm.getUser().getName())).append(']');
606 }
607
608 for (String key : osm.keySet()) {
609 text.append("<br>").append(key).append('=').append(osm.get(key));
610 }
611
612 final JLabel l = new JLabel(
613 "<html>" + text.toString() + "</html>",
614 ImageProvider.get(osm.getDisplayType()),
615 JLabel.HORIZONTAL
616 ) {
617 // This is necessary so the label updates its colors when the
618 // selection is changed from the outside
619 @Override
620 public void validate() {
621 super.validate();
622 popupSetLabelColors(this, osm);
623 }
624 };
625 l.setOpaque(true);
626 popupSetLabelColors(l, osm);
627 l.setFont(l.getFont().deriveFont(Font.PLAIN));
628 l.setVerticalTextPosition(JLabel.TOP);
629 l.setHorizontalAlignment(JLabel.LEFT);
630 l.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
631 l.addMouseListener(new MouseAdapter() {
632 @Override
633 public void mouseEntered(MouseEvent e) {
634 l.setBackground(SystemColor.info);
635 l.setForeground(SystemColor.infoText);
636 }
637
638 @Override
639 public void mouseExited(MouseEvent e) {
640 popupSetLabelColors(l, osm);
641 }
642
643 @Override
644 public void mouseClicked(MouseEvent e) {
645 DataSet ds = Main.getLayerManager().getEditDataSet();
646 // Let the user toggle the selection
647 ds.toggleSelected(osm);
648 l.validate();
649 }
650 });
651 // Sometimes the mouseEntered event is not catched, thus the label
652 // will not be highlighted, making it confusing. The MotionListener can correct this defect.
653 l.addMouseMotionListener(new MouseMotionListener() {
654 @Override
655 public void mouseMoved(MouseEvent e) {
656 l.setBackground(SystemColor.info);
657 l.setForeground(SystemColor.infoText);
658 }
659
660 @Override
661 public void mouseDragged(MouseEvent e) {
662 l.setBackground(SystemColor.info);
663 l.setForeground(SystemColor.infoText);
664 }
665 });
666 return l;
667 }
668
669 /**
670 * Called whenever the mouse position or modifiers changed.
671 * @param mousePos The new mouse position. <code>null</code> if it did not change.
672 * @param modifiers The new modifiers.
673 */
674 public synchronized void updateMousePosition(Point mousePos, int modifiers) {
675 if (mousePos != null) {
676 lastMousePos = mousePos;
677 }
678 MouseState ms = new MouseState(lastMousePos, modifiers);
679 // remove mouse states that are in the queue. Our mouse state is newer.
680 incomingMouseState.clear();
681 if (!incomingMouseState.offer(ms)) {
682 Main.warn("Unable to handle new MouseState: " + ms);
683 }
684 }
685 }
686
687 /**
688 * Everything, the collector is interested of. Access must be synchronized.
689 * @author imi
690 */
691 private static class MouseState {
692 private final Point mousePos;
693 private final int modifiers;
694
695 MouseState(Point mousePos, int modifiers) {
696 this.mousePos = mousePos;
697 this.modifiers = modifiers;
698 }
699 }
700
701 private final transient AWTEventListener awtListener;
702
703 private final transient MouseMotionListener mouseMotionListener = new MouseMotionListener() {
704 @Override
705 public void mouseMoved(MouseEvent e) {
706 synchronized (collector) {
707 collector.updateMousePosition(e.getPoint(), e.getModifiersEx());
708 }
709 }
710
711 @Override
712 public void mouseDragged(MouseEvent e) {
713 mouseMoved(e);
714 }
715 };
716
717 private final transient KeyAdapter keyAdapter = new KeyAdapter() {
718 @Override public void keyPressed(KeyEvent e) {
719 synchronized (collector) {
720 collector.updateMousePosition(null, e.getModifiersEx());
721 }
722 }
723
724 @Override public void keyReleased(KeyEvent e) {
725 keyPressed(e);
726 }
727 };
728
729 private void registerListeners() {
730 // Listen to keyboard/mouse events for pressing/releasing alt key and inform the collector.
731 try {
732 Toolkit.getDefaultToolkit().addAWTEventListener(awtListener,
733 AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
734 } catch (SecurityException ex) {
735 Main.trace(ex);
736 mv.addMouseMotionListener(mouseMotionListener);
737 mv.addKeyListener(keyAdapter);
738 }
739 }
740
741 private void unregisterListeners() {
742 try {
743 Toolkit.getDefaultToolkit().removeAWTEventListener(awtListener);
744 } catch (SecurityException e) {
745 // Don't care, awtListener probably wasn't registered anyway
746 Main.trace(e);
747 }
748 mv.removeMouseMotionListener(mouseMotionListener);
749 mv.removeKeyListener(keyAdapter);
750 }
751
752 private class MapStatusPopupMenu extends JPopupMenu {
753
754 private final JMenuItem jumpButton = add(Main.main.menu.jumpToAct);
755
756 /** Icons for selecting {@link SystemOfMeasurement} */
757 private final Collection<JCheckBoxMenuItem> somItems = new ArrayList<>();
758 /** Icons for selecting {@link CoordinateFormat} */
759 private final Collection<JCheckBoxMenuItem> coordinateFormatItems = new ArrayList<>();
760
761 private final JSeparator separator = new JSeparator();
762
763 private final JMenuItem doNotHide = new JCheckBoxMenuItem(new AbstractAction(tr("Do not hide status bar")) {
764 @Override
765 public void actionPerformed(ActionEvent e) {
766 boolean sel = ((JCheckBoxMenuItem) e.getSource()).getState();
767 Main.pref.put("statusbar.always-visible", sel);
768 }
769 });
770
771 MapStatusPopupMenu() {
772 for (final String key : new TreeSet<>(SystemOfMeasurement.ALL_SYSTEMS.keySet())) {
773 JCheckBoxMenuItem item = new JCheckBoxMenuItem(new AbstractAction(key) {
774 @Override
775 public void actionPerformed(ActionEvent e) {
776 updateSystemOfMeasurement(key);
777 }
778 });
779 somItems.add(item);
780 add(item);
781 }
782 for (final CoordinateFormat format : CoordinateFormat.values()) {
783 JCheckBoxMenuItem item = new JCheckBoxMenuItem(new AbstractAction(format.getDisplayName()) {
784 @Override
785 public void actionPerformed(ActionEvent e) {
786 CoordinateFormat.setCoordinateFormat(format);
787 }
788 });
789 coordinateFormatItems.add(item);
790 add(item);
791 }
792
793 add(separator);
794 add(doNotHide);
795
796 addPopupMenuListener(new PopupMenuListener() {
797 @Override
798 public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
799 Component invoker = ((JPopupMenu) e.getSource()).getInvoker();
800 jumpButton.setVisible(latText.equals(invoker) || lonText.equals(invoker));
801 String currentSOM = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
802 for (JMenuItem item : somItems) {
803 item.setSelected(item.getText().equals(currentSOM));
804 item.setVisible(distText.equals(invoker));
805 }
806 final String currentCorrdinateFormat = CoordinateFormat.getDefaultFormat().getDisplayName();
807 for (JMenuItem item : coordinateFormatItems) {
808 item.setSelected(currentCorrdinateFormat.equals(item.getText()));
809 item.setVisible(latText.equals(invoker) || lonText.equals(invoker));
810 }
811 separator.setVisible(distText.equals(invoker) || latText.equals(invoker) || lonText.equals(invoker));
812 doNotHide.setSelected(Main.pref.getBoolean("statusbar.always-visible", true));
813 }
814
815 @Override
816 public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
817 // Do nothing
818 }
819
820 @Override
821 public void popupMenuCanceled(PopupMenuEvent e) {
822 // Do nothing
823 }
824 });
825 }
826 }
827
828 /**
829 * Construct a new MapStatus and attach it to the map view.
830 * @param mapFrame The MapFrame the status line is part of.
831 */
832 public MapStatus(final MapFrame mapFrame) {
833 this.mv = mapFrame.mapView;
834 this.collector = new Collector(mapFrame);
835 this.awtListener = event -> {
836 if (event instanceof InputEvent &&
837 ((InputEvent) event).getComponent() == mv) {
838 synchronized (collector) {
839 int modifiers = ((InputEvent) event).getModifiersEx();
840 Point mousePos = null;
841 if (event instanceof MouseEvent) {
842 mousePos = ((MouseEvent) event).getPoint();
843 }
844 collector.updateMousePosition(mousePos, modifiers);
845 }
846 }
847 };
848
849 // Context menu of status bar
850 setComponentPopupMenu(new MapStatusPopupMenu());
851
852 // also show Jump To dialog on mouse click (except context menu)
853 MouseListener jumpToOnLeftClick = new JumpToOnLeftClickMouseAdapter();
854
855 // Listen for mouse movements and set the position text field
856 mv.addMouseMotionListener(new MouseMotionListener() {
857 @Override
858 public void mouseDragged(MouseEvent e) {
859 mouseMoved(e);
860 }
861
862 @Override
863 public void mouseMoved(MouseEvent e) {
864 if (mv.getCenter() == null)
865 return;
866 // Do not update the view if ctrl is pressed.
867 if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0) {
868 CoordinateFormat mCord = CoordinateFormat.getDefaultFormat();
869 LatLon p = mv.getLatLon(e.getX(), e.getY());
870 latText.setText(p.latToString(mCord));
871 lonText.setText(p.lonToString(mCord));
872 if (Objects.equals(previousCoordinateFormat, mCord)) {
873 // do nothing
874 } else if (CoordinateFormat.EAST_NORTH.equals(mCord)) {
875 latText.setIcon("northing");
876 lonText.setIcon("easting");
877 latText.setToolTipText(tr("The northing at the mouse pointer."));
878 lonText.setToolTipText(tr("The easting at the mouse pointer."));
879 previousCoordinateFormat = mCord;
880 } else {
881 latText.setIcon("lat");
882 lonText.setIcon("lon");
883 latText.setToolTipText(tr("The geographic latitude at the mouse pointer."));
884 lonText.setToolTipText(tr("The geographic longitude at the mouse pointer."));
885 previousCoordinateFormat = mCord;
886 }
887 }
888 }
889 });
890
891 setLayout(new GridBagLayout());
892 setBorder(BorderFactory.createEmptyBorder(1, 2, 1, 2));
893
894 latText.setInheritsPopupMenu(true);
895 lonText.setInheritsPopupMenu(true);
896 headingText.setInheritsPopupMenu(true);
897 distText.setInheritsPopupMenu(true);
898 nameText.setInheritsPopupMenu(true);
899
900 add(latText, GBC.std());
901 add(lonText, GBC.std().insets(3, 0, 0, 0));
902 add(headingText, GBC.std().insets(3, 0, 0, 0));
903 add(angleText, GBC.std().insets(3, 0, 0, 0));
904 add(distText, GBC.std().insets(3, 0, 0, 0));
905
906 if (Main.pref.getBoolean("statusbar.change-system-of-measurement-on-click", true)) {
907 distText.addMouseListener(new MouseAdapter() {
908 private final List<String> soms = new ArrayList<>(new TreeSet<>(SystemOfMeasurement.ALL_SYSTEMS.keySet()));
909
910 @Override
911 public void mouseClicked(MouseEvent e) {
912 if (!e.isPopupTrigger() && e.getButton() == MouseEvent.BUTTON1) {
913 String som = ProjectionPreference.PROP_SYSTEM_OF_MEASUREMENT.get();
914 String newsom = soms.get((soms.indexOf(som)+1) % soms.size());
915 updateSystemOfMeasurement(newsom);
916 }
917 }
918 });
919 }
920
921 SystemOfMeasurement.addSoMChangeListener(this);
922
923 latText.addMouseListener(jumpToOnLeftClick);
924 lonText.addMouseListener(jumpToOnLeftClick);
925
926 helpText.setEditable(false);
927 add(nameText, GBC.std().insets(3, 0, 0, 0));
928 add(helpText, GBC.std().insets(3, 0, 0, 0).fill(GBC.HORIZONTAL));
929
930 progressBar.setMaximum(PleaseWaitProgressMonitor.PROGRESS_BAR_MAX);
931 progressBar.setVisible(false);
932 GBC gbc = GBC.eol();
933 gbc.ipadx = 100;
934 add(progressBar, gbc);
935 progressBar.addMouseListener(new ShowMonitorDialogMouseAdapter());
936
937 Main.pref.addPreferenceChangeListener(this);
938
939 mvComponentAdapter = new ComponentAdapter() {
940 @Override
941 public void componentResized(ComponentEvent e) {
942 nameText.setCharCount(getNameLabelCharacterCount(Main.parent));
943 revalidate();
944 }
945 };
946 mv.addComponentListener(mvComponentAdapter);
947
948 // The background thread
949 thread = new Thread(collector, "Map Status Collector");
950 thread.setDaemon(true);
951 thread.start();
952 }
953
954 @Override
955 public void systemOfMeasurementChanged(String oldSoM, String newSoM) {
956 setDist(distValue);
957 }
958
959 /**
960 * Updates the system of measurement and displays a notification.
961 * @param newsom The new system of measurement to set
962 * @since 6960
963 */
964 public void updateSystemOfMeasurement(String newsom) {
965 SystemOfMeasurement.setSystemOfMeasurement(newsom);
966 if (Main.pref.getBoolean("statusbar.notify.change-system-of-measurement", true)) {
967 new Notification(tr("System of measurement changed to {0}", newsom))
968 .setDuration(Notification.TIME_SHORT)
969 .show();
970 }
971 }
972
973 public JPanel getAnglePanel() {
974 return angleText;
975 }
976
977 @Override
978 public String helpTopic() {
979 return ht("/StatusBar");
980 }
981
982 @Override
983 public synchronized void addMouseListener(MouseListener ml) {
984 lonText.addMouseListener(ml);
985 latText.addMouseListener(ml);
986 }
987
988 public void setHelpText(String t) {
989 setHelpText(null, t);
990 }
991
992 public void setHelpText(Object id, final String text) {
993
994 StatusTextHistory entry = new StatusTextHistory(id, text);
995
996 statusText.remove(entry);
997 statusText.add(entry);
998
999 GuiHelper.runInEDT(() -> {
1000 helpText.setText(text);
1001 helpText.setToolTipText(text);
1002 });
1003 }
1004
1005 public void resetHelpText(Object id) {
1006 if (statusText.isEmpty())
1007 return;
1008
1009 StatusTextHistory entry = new StatusTextHistory(id, null);
1010 if (statusText.get(statusText.size() - 1).equals(entry)) {
1011 if (statusText.size() == 1) {
1012 setHelpText("");
1013 } else {
1014 StatusTextHistory history = statusText.get(statusText.size() - 2);
1015 setHelpText(history.id, history.text);
1016 }
1017 }
1018 statusText.remove(entry);
1019 }
1020
1021 public void setAngle(double a) {
1022 angleText.setText(a < 0 ? "--" : DECIMAL_FORMAT.format(a) + " \u00B0");
1023 }
1024
1025 public void setHeading(double h) {
1026 headingText.setText(h < 0 ? "--" : DECIMAL_FORMAT.format(h) + " \u00B0");
1027 }
1028
1029 /**
1030 * Sets the distance text to the given value
1031 * @param dist The distance value to display, in meters
1032 */
1033 public void setDist(double dist) {
1034 distValue = dist;
1035 distText.setText(dist < 0 ? "--" : NavigatableComponent.getDistText(dist, DECIMAL_FORMAT, DISTANCE_THRESHOLD.get()));
1036 }
1037
1038 /**
1039 * Sets the distance text to the total sum of given ways length
1040 * @param ways The ways to consider for the total distance
1041 * @since 5991
1042 */
1043 public void setDist(Collection<Way> ways) {
1044 double dist = -1;
1045 // Compute total length of selected way(s) until an arbitrary limit set to 250 ways
1046 // in order to prevent performance issue if a large number of ways are selected (old behaviour kept in that case, see #8403)
1047 int maxWays = Math.max(1, Main.pref.getInteger("selection.max-ways-for-statusline", 250));
1048 if (!ways.isEmpty() && ways.size() <= maxWays) {
1049 dist = 0.0;
1050 for (Way w : ways) {
1051 dist += w.getLength();
1052 }
1053 }
1054 setDist(dist);
1055 }
1056
1057 /**
1058 * Activates the angle panel.
1059 * @param activeFlag {@code true} to activate it, {@code false} to deactivate it
1060 */
1061 public void activateAnglePanel(boolean activeFlag) {
1062 angleEnabled = activeFlag;
1063 refreshAnglePanel();
1064 }
1065
1066 private void refreshAnglePanel() {
1067 angleText.setBackground(angleEnabled ? PROP_ACTIVE_BACKGROUND_COLOR.get() : PROP_BACKGROUND_COLOR.get());
1068 angleText.setForeground(angleEnabled ? PROP_ACTIVE_FOREGROUND_COLOR.get() : PROP_FOREGROUND_COLOR.get());
1069 }
1070
1071 @Override
1072 public void destroy() {
1073 SystemOfMeasurement.removeSoMChangeListener(this);
1074 Main.pref.removePreferenceChangeListener(this);
1075 mv.removeComponentListener(mvComponentAdapter);
1076
1077 // MapFrame gets destroyed when the last layer is removed, but the status line background
1078 // thread that collects the information doesn't get destroyed automatically.
1079 if (thread != null) {
1080 try {
1081 thread.interrupt();
1082 } catch (SecurityException e) {
1083 Main.error(e);
1084 }
1085 }
1086 }
1087
1088 @Override
1089 public void preferenceChanged(PreferenceChangeEvent e) {
1090 String key = e.getKey();
1091 if (key.startsWith("color.")) {
1092 key = key.substring("color.".length());
1093 if (PROP_BACKGROUND_COLOR.getKey().equals(key) || PROP_FOREGROUND_COLOR.getKey().equals(key)) {
1094 for (ImageLabel il : new ImageLabel[]{latText, lonText, headingText, distText, nameText}) {
1095 il.setBackground(PROP_BACKGROUND_COLOR.get());
1096 il.setForeground(PROP_FOREGROUND_COLOR.get());
1097 }
1098 refreshAnglePanel();
1099 } else if (PROP_ACTIVE_BACKGROUND_COLOR.getKey().equals(key) || PROP_ACTIVE_FOREGROUND_COLOR.getKey().equals(key)) {
1100 refreshAnglePanel();
1101 }
1102 }
1103 }
1104
1105 /**
1106 * Loads all colors from preferences.
1107 * @since 6789
1108 */
1109 public static void getColors() {
1110 PROP_BACKGROUND_COLOR.get();
1111 PROP_FOREGROUND_COLOR.get();
1112 PROP_ACTIVE_BACKGROUND_COLOR.get();
1113 PROP_ACTIVE_FOREGROUND_COLOR.get();
1114 }
1115
1116 private static int getNameLabelCharacterCount(Component parent) {
1117 int w = parent != null ? parent.getWidth() : 800;
1118 return Math.min(80, 20 + Math.max(0, w-1280) * 60 / (1920-1280));
1119 }
1120}
Note: See TracBrowser for help on using the repository browser.