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

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

see #15182 - move NameFormatter* from gui to data.osm

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