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