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

Last change on this file since 10759 was 10716, checked in by simon04, 8 years ago

see #11390, see #12890 - Deprecate predicates in OsmPrimitive class

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