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

Last change on this file since 12505 was 12391, checked in by michael2402, 7 years ago

See #14794: Documentation for the gui package

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