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

Last change on this file since 13170 was 13159, checked in by Don-vip, 6 years ago

see #13883, #15586 - avoid HeadlessException in unit tests

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