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

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

fix #14613 - Special HTML characters not escaped in GUI error messages

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