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

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

fix #15119 - length of way not displayed in status bar without selection panel

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