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