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

Last change on this file since 12661 was 12643, checked in by Don-vip, 7 years ago

see #15182 - deprecate Main.main.menu. Replacement: gui.MainApplication.getMenu()

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