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

Last change on this file since 6038 was 5991, checked in by Don-vip, 11 years ago

see #8752 - update distance text in status line when rescaling ways

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