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

Last change on this file since 12778 was 12735, checked in by bastiK, 7 years ago

see #15229 - move CoordinateFormat code out of LatLon class

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