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

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

sonar - squid:S3052 - Fields should not be initialized to default values

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