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

Last change on this file since 13115 was 12987, checked in by bastiK, 7 years ago

see #15410 - change preferences scheme for named colors - makes runtime color name registry obsolete

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