1 | // License: GPL. See LICENSE file for details.
|
---|
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.tr;
|
---|
6 |
|
---|
7 | import java.awt.AWTEvent;
|
---|
8 | import java.awt.Color;
|
---|
9 | import java.awt.Component;
|
---|
10 | import java.awt.Cursor;
|
---|
11 | import java.awt.Dimension;
|
---|
12 | import java.awt.EventQueue;
|
---|
13 | import java.awt.Font;
|
---|
14 | import java.awt.GridBagLayout;
|
---|
15 | import java.awt.Point;
|
---|
16 | import java.awt.SystemColor;
|
---|
17 | import java.awt.Toolkit;
|
---|
18 | import java.awt.event.AWTEventListener;
|
---|
19 | import java.awt.event.InputEvent;
|
---|
20 | import java.awt.event.KeyAdapter;
|
---|
21 | import java.awt.event.KeyEvent;
|
---|
22 | import java.awt.event.MouseAdapter;
|
---|
23 | import java.awt.event.MouseEvent;
|
---|
24 | import java.awt.event.MouseListener;
|
---|
25 | import java.awt.event.MouseMotionListener;
|
---|
26 | import java.util.ArrayList;
|
---|
27 | import java.util.Collection;
|
---|
28 | import java.util.ConcurrentModificationException;
|
---|
29 | import java.util.List;
|
---|
30 |
|
---|
31 | import javax.swing.BorderFactory;
|
---|
32 | import javax.swing.JLabel;
|
---|
33 | import javax.swing.JPanel;
|
---|
34 | import javax.swing.JScrollPane;
|
---|
35 | import javax.swing.JTextField;
|
---|
36 | import javax.swing.Popup;
|
---|
37 | import javax.swing.PopupFactory;
|
---|
38 |
|
---|
39 | import org.openstreetmap.josm.Main;
|
---|
40 | import org.openstreetmap.josm.data.coor.CoordinateFormat;
|
---|
41 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
42 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
43 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
44 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
45 | import org.openstreetmap.josm.gui.help.Helpful;
|
---|
46 | import org.openstreetmap.josm.tools.GBC;
|
---|
47 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * A component that manages some status information display about the map.
|
---|
51 | * It keeps a status line below the map up to date and displays some tooltip
|
---|
52 | * information if the user hold the mouse long enough at some point.
|
---|
53 | *
|
---|
54 | * All this is done in background to not disturb other processes.
|
---|
55 | *
|
---|
56 | * The background thread does not alter any data of the map (read only thread).
|
---|
57 | * Also it is rather fail safe. In case of some error in the data, it just does
|
---|
58 | * nothing instead of whining and complaining.
|
---|
59 | *
|
---|
60 | * @author imi
|
---|
61 | */
|
---|
62 | public class MapStatus extends JPanel implements Helpful {
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The MapView this status belongs to.
|
---|
66 | */
|
---|
67 | final MapView mv;
|
---|
68 | final Collector collector;
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * A small user interface component that consists of an image label and
|
---|
72 | * a fixed text content to the right of the image.
|
---|
73 | */
|
---|
74 | static class ImageLabel extends JPanel {
|
---|
75 | private JLabel tf;
|
---|
76 | private int chars;
|
---|
77 | public ImageLabel(String img, String tooltip, int chars) {
|
---|
78 | super();
|
---|
79 | setLayout(new GridBagLayout());
|
---|
80 | setBackground(Color.decode("#b8cfe5"));
|
---|
81 | add(new JLabel(ImageProvider.get("statusline/"+img+".png")), GBC.std().anchor(GBC.WEST).insets(0,1,1,0));
|
---|
82 | add(tf = new JLabel(), GBC.std().fill(GBC.BOTH).anchor(GBC.WEST).insets(2,1,1,0));
|
---|
83 | setToolTipText(tooltip);
|
---|
84 | this.chars = chars;
|
---|
85 | }
|
---|
86 | public void setText(String t) {
|
---|
87 | tf.setText(t);
|
---|
88 | }
|
---|
89 | @Override public Dimension getPreferredSize() {
|
---|
90 | return new Dimension(25 + chars*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getPreferredSize().height);
|
---|
91 | }
|
---|
92 | @Override public Dimension getMinimumSize() {
|
---|
93 | return new Dimension(25 + chars*tf.getFontMetrics(tf.getFont()).charWidth('0'), super.getMinimumSize().height);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | ImageLabel lonText = new ImageLabel("lon", tr("The geographic longitude at the mouse pointer."), 11);
|
---|
98 | ImageLabel nameText = new ImageLabel("name", tr("The name of the object at the mouse pointer."), 20);
|
---|
99 | JTextField helpText = new JTextField();
|
---|
100 | ImageLabel latText = new ImageLabel("lat", tr("The geographic latitude at the mouse pointer."), 11);
|
---|
101 | ImageLabel angleText = new ImageLabel("angle", tr("The angle between the previous and the current way segment."), 6);
|
---|
102 | ImageLabel headingText = new ImageLabel("heading", tr("The (compass) heading of the line segment being drawn."), 6);
|
---|
103 | ImageLabel distText = new ImageLabel("dist", tr("The length of the new way segment being drawn."), 10);
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * This is the thread that runs in the background and collects the information displayed.
|
---|
107 | * It gets destroyed by MapFrame.java/destroy() when the MapFrame itself is destroyed.
|
---|
108 | */
|
---|
109 | public Thread thread;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * The collector class that waits for notification and then update
|
---|
113 | * the display objects.
|
---|
114 | *
|
---|
115 | * @author imi
|
---|
116 | */
|
---|
117 | private final class Collector implements Runnable {
|
---|
118 | /**
|
---|
119 | * the mouse position of the previous iteration. This is used to show
|
---|
120 | * the popup until the cursor is moved.
|
---|
121 | */
|
---|
122 | private Point oldMousePos;
|
---|
123 | /**
|
---|
124 | * Contains the labels that are currently shown in the information
|
---|
125 | * popup
|
---|
126 | */
|
---|
127 | private List<JLabel> popupLabels = null;
|
---|
128 | /**
|
---|
129 | * The popup displayed to show additional information
|
---|
130 | */
|
---|
131 | private Popup popup;
|
---|
132 |
|
---|
133 | private MapFrame parent;
|
---|
134 |
|
---|
135 | public Collector(MapFrame parent) {
|
---|
136 | this.parent = parent;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Execution function for the Collector.
|
---|
141 | */
|
---|
142 | public void run() {
|
---|
143 | registerListeners();
|
---|
144 | try {
|
---|
145 | for (;;) {
|
---|
146 |
|
---|
147 | final MouseState ms = new MouseState();
|
---|
148 | synchronized (this) {
|
---|
149 | // TODO Would be better if the timeout wasn't necessary
|
---|
150 | try {wait(1000);} catch (InterruptedException e) {}
|
---|
151 | ms.modifiers = mouseState.modifiers;
|
---|
152 | ms.mousePos = mouseState.mousePos;
|
---|
153 | }
|
---|
154 | if (parent != Main.map)
|
---|
155 | return; // exit, if new parent.
|
---|
156 |
|
---|
157 | // Do nothing, if required data is missing
|
---|
158 | if(ms.mousePos == null || mv.center == null) {
|
---|
159 | continue;
|
---|
160 | }
|
---|
161 |
|
---|
162 | try {
|
---|
163 | EventQueue.invokeAndWait(new Runnable() {
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void run() {
|
---|
167 | // Freeze display when holding down CTRL
|
---|
168 | if ((ms.modifiers & MouseEvent.CTRL_DOWN_MASK) != 0) {
|
---|
169 | // update the information popup's labels though, because
|
---|
170 | // the selection might have changed from the outside
|
---|
171 | popupUpdateLabels();
|
---|
172 | return;
|
---|
173 | }
|
---|
174 |
|
---|
175 | // This try/catch is a hack to stop the flooding bug reports about this.
|
---|
176 | // The exception needed to handle with in the first place, means that this
|
---|
177 | // access to the data need to be restarted, if the main thread modifies
|
---|
178 | // the data.
|
---|
179 | DataSet ds = null;
|
---|
180 | // The popup != null check is required because a left-click
|
---|
181 | // produces several events as well, which would make this
|
---|
182 | // variable true. Of course we only want the popup to show
|
---|
183 | // if the middle mouse button has been pressed in the first
|
---|
184 | // place
|
---|
185 | boolean isAtOldPosition = (oldMousePos != null
|
---|
186 | && oldMousePos.equals(ms.mousePos)
|
---|
187 | && popup != null);
|
---|
188 | boolean middleMouseDown = (ms.modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0;
|
---|
189 | try {
|
---|
190 | ds = mv.getCurrentDataSet();
|
---|
191 | if (ds != null) {
|
---|
192 | // This is not perfect, if current dataset was changed during execution, the lock would be useless
|
---|
193 | if(isAtOldPosition && middleMouseDown) {
|
---|
194 | // Write lock is necessary when selecting in popupCycleSelection
|
---|
195 | // locks can not be upgraded -> if do read lock here and write lock later (in OsmPrimitive.updateFlags)
|
---|
196 | // then always occurs deadlock (#5814)
|
---|
197 | ds.beginUpdate();
|
---|
198 | } else {
|
---|
199 | ds.getReadLock().lock();
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | // Set the text label in the bottom status bar
|
---|
204 | statusBarElementUpdate(ms);
|
---|
205 |
|
---|
206 |
|
---|
207 | // Popup Information
|
---|
208 | // display them if the middle mouse button is pressed and
|
---|
209 | // keep them until the mouse is moved
|
---|
210 | if (middleMouseDown || isAtOldPosition)
|
---|
211 | {
|
---|
212 | Collection<OsmPrimitive> osms = mv.getAllNearest(ms.mousePos, OsmPrimitive.isUsablePredicate);
|
---|
213 |
|
---|
214 | if (osms == null)
|
---|
215 | return;
|
---|
216 |
|
---|
217 | final JPanel c = new JPanel(new GridBagLayout());
|
---|
218 | final JLabel lbl = new JLabel(
|
---|
219 | "<html>"+tr("Middle click again to cycle through.<br>"+
|
---|
220 | "Hold CTRL to select directly from this list with the mouse.<hr>")+"</html>",
|
---|
221 | null,
|
---|
222 | JLabel.HORIZONTAL
|
---|
223 | );
|
---|
224 | lbl.setHorizontalAlignment(JLabel.LEFT);
|
---|
225 | c.add(lbl, GBC.eol().insets(2, 0, 2, 0));
|
---|
226 |
|
---|
227 | // Only cycle if the mouse has not been moved and the
|
---|
228 | // middle mouse button has been pressed at least twice
|
---|
229 | // (the reason for this is the popup != null check for
|
---|
230 | // isAtOldPosition, see above. This is a nice side
|
---|
231 | // effect though, because it does not change selection
|
---|
232 | // of the first middle click)
|
---|
233 | if(isAtOldPosition && middleMouseDown) {
|
---|
234 | // Hand down mouse modifiers so the SHIFT mod can be
|
---|
235 | // handled correctly (see funcion)
|
---|
236 | popupCycleSelection(osms, ms.modifiers);
|
---|
237 | }
|
---|
238 |
|
---|
239 | // These labels may need to be updated from the outside
|
---|
240 | // so collect them
|
---|
241 | List<JLabel> lbls = new ArrayList<JLabel>();
|
---|
242 | for (final OsmPrimitive osm : osms) {
|
---|
243 | JLabel l = popupBuildPrimitiveLabels(osm);
|
---|
244 | lbls.add(l);
|
---|
245 | c.add(l, GBC.eol().fill(GBC.HORIZONTAL).insets(2, 0, 2, 2));
|
---|
246 | }
|
---|
247 |
|
---|
248 | popupShowPopup(popupCreatePopup(c, ms), lbls);
|
---|
249 | } else {
|
---|
250 | popupHidePopup();
|
---|
251 | }
|
---|
252 |
|
---|
253 | oldMousePos = ms.mousePos;
|
---|
254 | } catch (ConcurrentModificationException x) {
|
---|
255 | //x.printStackTrace();
|
---|
256 | } catch (NullPointerException x) {
|
---|
257 | //x.printStackTrace();
|
---|
258 | } finally {
|
---|
259 | if (ds != null) {
|
---|
260 | if(isAtOldPosition && middleMouseDown) {
|
---|
261 | ds.endUpdate();
|
---|
262 | } else {
|
---|
263 | ds.getReadLock().unlock();
|
---|
264 | }
|
---|
265 | }
|
---|
266 | }
|
---|
267 | }
|
---|
268 | });
|
---|
269 | } catch (Exception e) {
|
---|
270 |
|
---|
271 | }
|
---|
272 | }
|
---|
273 | } finally {
|
---|
274 | unregisterListeners();
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | /**
|
---|
279 | * Creates a popup for the given content next to the cursor. Tries to
|
---|
280 | * keep the popup on screen and shows a vertical scrollbar, if the
|
---|
281 | * screen is too small.
|
---|
282 | * @param content
|
---|
283 | * @param ms
|
---|
284 | * @return popup
|
---|
285 | */
|
---|
286 | private final Popup popupCreatePopup(Component content, MouseState ms) {
|
---|
287 | Point p = mv.getLocationOnScreen();
|
---|
288 | Dimension scrn = Toolkit.getDefaultToolkit().getScreenSize();
|
---|
289 |
|
---|
290 | // Create a JScrollPane around the content, in case there's not
|
---|
291 | // enough space
|
---|
292 | JScrollPane sp = new JScrollPane(content);
|
---|
293 | sp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
|
---|
294 | sp.setBorder(BorderFactory.createRaisedBevelBorder());
|
---|
295 | // Implement max-size content-independent
|
---|
296 | Dimension prefsize = sp.getPreferredSize();
|
---|
297 | int w = Math.min(prefsize.width, Math.min(800, (scrn.width/2) - 16));
|
---|
298 | int h = Math.min(prefsize.height, scrn.height - 10);
|
---|
299 | sp.setPreferredSize(new Dimension(w, h));
|
---|
300 |
|
---|
301 | int xPos = p.x + ms.mousePos.x + 16;
|
---|
302 | // Display the popup to the left of the cursor if it would be cut
|
---|
303 | // off on its right, but only if more space is available
|
---|
304 | if(xPos + w > scrn.width && xPos > scrn.width/2) {
|
---|
305 | xPos = p.x + ms.mousePos.x - 4 - w;
|
---|
306 | }
|
---|
307 | int yPos = p.y + ms.mousePos.y + 16;
|
---|
308 | // Move the popup up if it would be cut off at its bottom but do not
|
---|
309 | // move it off screen on the top
|
---|
310 | if(yPos + h > scrn.height - 5) {
|
---|
311 | yPos = Math.max(5, scrn.height - h - 5);
|
---|
312 | }
|
---|
313 |
|
---|
314 | PopupFactory pf = PopupFactory.getSharedInstance();
|
---|
315 | return pf.getPopup(mv, sp, xPos, yPos);
|
---|
316 | }
|
---|
317 |
|
---|
318 | /**
|
---|
319 | * Calls this to update the element that is shown in the statusbar
|
---|
320 | * @param ms
|
---|
321 | */
|
---|
322 | private final void statusBarElementUpdate(MouseState ms) {
|
---|
323 | final OsmPrimitive osmNearest = mv.getNearestNodeOrWay(ms.mousePos, OsmPrimitive.isUsablePredicate, false);
|
---|
324 | if (osmNearest != null) {
|
---|
325 | nameText.setText(osmNearest.getDisplayName(DefaultNameFormatter.getInstance()));
|
---|
326 | } else {
|
---|
327 | nameText.setText(tr("(no object)"));
|
---|
328 | }
|
---|
329 | }
|
---|
330 |
|
---|
331 | /**
|
---|
332 | * Call this with a set of primitives to cycle through them. Method
|
---|
333 | * will automatically select the next item and update the map
|
---|
334 | * @param osms
|
---|
335 | * @param mouse modifiers
|
---|
336 | */
|
---|
337 | private final void popupCycleSelection(Collection<OsmPrimitive> osms, int mods) {
|
---|
338 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
339 | // Find some items that are required for cycling through
|
---|
340 | OsmPrimitive firstItem = null;
|
---|
341 | OsmPrimitive firstSelected = null;
|
---|
342 | OsmPrimitive nextSelected = null;
|
---|
343 | for (final OsmPrimitive osm : osms) {
|
---|
344 | if(firstItem == null) {
|
---|
345 | firstItem = osm;
|
---|
346 | }
|
---|
347 | if(firstSelected != null && nextSelected == null) {
|
---|
348 | nextSelected = osm;
|
---|
349 | }
|
---|
350 | if(firstSelected == null && ds.isSelected(osm)) {
|
---|
351 | firstSelected = osm;
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | // Clear previous selection if SHIFT (add to selection) is not
|
---|
356 | // pressed. Cannot use "setSelected()" because it will cause a
|
---|
357 | // fireSelectionChanged event which is unnecessary at this point.
|
---|
358 | if((mods & MouseEvent.SHIFT_DOWN_MASK) == 0) {
|
---|
359 | ds.clearSelection();
|
---|
360 | }
|
---|
361 |
|
---|
362 | // This will cycle through the available items.
|
---|
363 | if(firstSelected == null) {
|
---|
364 | ds.addSelected(firstItem);
|
---|
365 | } else {
|
---|
366 | ds.clearSelection(firstSelected);
|
---|
367 | if(nextSelected != null) {
|
---|
368 | ds.addSelected(nextSelected);
|
---|
369 | }
|
---|
370 | }
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Tries to hide the given popup
|
---|
375 | * @param popup
|
---|
376 | */
|
---|
377 | private final void popupHidePopup() {
|
---|
378 | popupLabels = null;
|
---|
379 | if(popup == null)
|
---|
380 | return;
|
---|
381 | final Popup staticPopup = popup;
|
---|
382 | popup = null;
|
---|
383 | EventQueue.invokeLater(new Runnable(){
|
---|
384 | public void run() { staticPopup.hide(); }});
|
---|
385 | }
|
---|
386 |
|
---|
387 | /**
|
---|
388 | * Tries to show the given popup, can be hidden using popupHideOldPopup
|
---|
389 | * If an old popup exists, it will be automatically hidden
|
---|
390 | * @param popup
|
---|
391 | */
|
---|
392 | private final void popupShowPopup(Popup newPopup, List<JLabel> lbls) {
|
---|
393 | final Popup staticPopup = newPopup;
|
---|
394 | if(this.popup != null) {
|
---|
395 | // If an old popup exists, remove it when the new popup has been
|
---|
396 | // drawn to keep flickering to a minimum
|
---|
397 | final Popup staticOldPopup = this.popup;
|
---|
398 | EventQueue.invokeLater(new Runnable(){
|
---|
399 | public void run() {
|
---|
400 | staticPopup.show();
|
---|
401 | staticOldPopup.hide();
|
---|
402 | }
|
---|
403 | });
|
---|
404 | } else {
|
---|
405 | // There is no old popup
|
---|
406 | EventQueue.invokeLater(new Runnable(){
|
---|
407 | public void run() { staticPopup.show(); }});
|
---|
408 | }
|
---|
409 | this.popupLabels = lbls;
|
---|
410 | this.popup = newPopup;
|
---|
411 | }
|
---|
412 |
|
---|
413 | /**
|
---|
414 | * This method should be called if the selection may have changed from
|
---|
415 | * outside of this class. This is the case when CTRL is pressed and the
|
---|
416 | * user clicks on the map instead of the popup.
|
---|
417 | */
|
---|
418 | private final void popupUpdateLabels() {
|
---|
419 | if(this.popup == null || this.popupLabels == null)
|
---|
420 | return;
|
---|
421 | for(JLabel l : this.popupLabels) {
|
---|
422 | l.validate();
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | /**
|
---|
427 | * Sets the colors for the given label depending on the selected status of
|
---|
428 | * the given OsmPrimitive
|
---|
429 | *
|
---|
430 | * @param lbl The label to color
|
---|
431 | * @param osm The primitive to derive the colors from
|
---|
432 | */
|
---|
433 | private final void popupSetLabelColors(JLabel lbl, OsmPrimitive osm) {
|
---|
434 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
435 | if(ds.isSelected(osm)) {
|
---|
436 | lbl.setBackground(SystemColor.textHighlight);
|
---|
437 | lbl.setForeground(SystemColor.textHighlightText);
|
---|
438 | } else {
|
---|
439 | lbl.setBackground(SystemColor.control);
|
---|
440 | lbl.setForeground(SystemColor.controlText);
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | /**
|
---|
445 | * Builds the labels with all necessary listeners for the info popup for the
|
---|
446 | * given OsmPrimitive
|
---|
447 | * @param osm The primitive to create the label for
|
---|
448 | * @return
|
---|
449 | */
|
---|
450 | private final JLabel popupBuildPrimitiveLabels(final OsmPrimitive osm) {
|
---|
451 | final StringBuilder text = new StringBuilder();
|
---|
452 | String name = osm.getDisplayName(DefaultNameFormatter.getInstance());
|
---|
453 | if (osm.isNewOrUndeleted() || osm.isModified()) {
|
---|
454 | name = "<i><b>"+ name + "*</b></i>";
|
---|
455 | }
|
---|
456 | text.append(name);
|
---|
457 |
|
---|
458 | if (!osm.isNew()) {
|
---|
459 | text.append(" [id="+osm.getId()+"]");
|
---|
460 | }
|
---|
461 |
|
---|
462 | if(osm.getUser() != null) {
|
---|
463 | text.append(" [" + tr("User:") + " " + osm.getUser().getName() + "]");
|
---|
464 | }
|
---|
465 |
|
---|
466 | for (String key : osm.keySet()) {
|
---|
467 | text.append("<br>" + key + "=" + osm.get(key));
|
---|
468 | }
|
---|
469 |
|
---|
470 | final JLabel l = new JLabel(
|
---|
471 | "<html>" +text.toString() + "</html>",
|
---|
472 | ImageProvider.get(OsmPrimitiveType.from(osm)),
|
---|
473 | JLabel.HORIZONTAL
|
---|
474 | ) {
|
---|
475 | // This is necessary so the label updates its colors when the
|
---|
476 | // selection is changed from the outside
|
---|
477 | @Override public void validate() {
|
---|
478 | super.validate();
|
---|
479 | popupSetLabelColors(this, osm);
|
---|
480 | }
|
---|
481 | };
|
---|
482 | l.setOpaque(true);
|
---|
483 | popupSetLabelColors(l, osm);
|
---|
484 | l.setFont(l.getFont().deriveFont(Font.PLAIN));
|
---|
485 | l.setVerticalTextPosition(JLabel.TOP);
|
---|
486 | l.setHorizontalAlignment(JLabel.LEFT);
|
---|
487 | l.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
|
---|
488 | l.addMouseListener(new MouseAdapter(){
|
---|
489 | @Override public void mouseEntered(MouseEvent e) {
|
---|
490 | l.setBackground(SystemColor.info);
|
---|
491 | l.setForeground(SystemColor.infoText);
|
---|
492 | }
|
---|
493 | @Override public void mouseExited(MouseEvent e) {
|
---|
494 | popupSetLabelColors(l, osm);
|
---|
495 | }
|
---|
496 | @Override public void mouseClicked(MouseEvent e) {
|
---|
497 | DataSet ds = Main.main.getCurrentDataSet();
|
---|
498 | // Let the user toggle the selection
|
---|
499 | ds.toggleSelected(osm);
|
---|
500 | l.validate();
|
---|
501 | }
|
---|
502 | });
|
---|
503 | // Sometimes the mouseEntered event is not catched, thus the label
|
---|
504 | // will not be highlighted, making it confusing. The MotionListener
|
---|
505 | // can correct this defect.
|
---|
506 | l.addMouseMotionListener(new MouseMotionListener() {
|
---|
507 | public void mouseMoved(MouseEvent e) {
|
---|
508 | l.setBackground(SystemColor.info);
|
---|
509 | l.setForeground(SystemColor.infoText);
|
---|
510 | }
|
---|
511 | public void mouseDragged(MouseEvent e) {
|
---|
512 | l.setBackground(SystemColor.info);
|
---|
513 | l.setForeground(SystemColor.infoText);
|
---|
514 | }
|
---|
515 | });
|
---|
516 | return l;
|
---|
517 | }
|
---|
518 | }
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Everything, the collector is interested of. Access must be synchronized.
|
---|
522 | * @author imi
|
---|
523 | */
|
---|
524 | static class MouseState {
|
---|
525 | Point mousePos;
|
---|
526 | int modifiers;
|
---|
527 | }
|
---|
528 | /**
|
---|
529 | * The last sent mouse movement event.
|
---|
530 | */
|
---|
531 | MouseState mouseState = new MouseState();
|
---|
532 |
|
---|
533 | private AWTEventListener awtListener = new AWTEventListener() {
|
---|
534 | public void eventDispatched(AWTEvent event) {
|
---|
535 | if (event instanceof InputEvent &&
|
---|
536 | ((InputEvent)event).getComponent() == mv) {
|
---|
537 | synchronized (collector) {
|
---|
538 | mouseState.modifiers = ((InputEvent)event).getModifiersEx();
|
---|
539 | if (event instanceof MouseEvent) {
|
---|
540 | mouseState.mousePos = ((MouseEvent)event).getPoint();
|
---|
541 | }
|
---|
542 | collector.notify();
|
---|
543 | }
|
---|
544 | }
|
---|
545 | }
|
---|
546 | };
|
---|
547 |
|
---|
548 | private MouseMotionListener mouseMotionListener = new MouseMotionListener() {
|
---|
549 | public void mouseMoved(MouseEvent e) {
|
---|
550 | synchronized (collector) {
|
---|
551 | mouseState.modifiers = e.getModifiersEx();
|
---|
552 | mouseState.mousePos = e.getPoint();
|
---|
553 | collector.notify();
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | public void mouseDragged(MouseEvent e) {
|
---|
558 | mouseMoved(e);
|
---|
559 | }
|
---|
560 | };
|
---|
561 |
|
---|
562 | private KeyAdapter keyAdapter = new KeyAdapter() {
|
---|
563 | @Override public void keyPressed(KeyEvent e) {
|
---|
564 | synchronized (collector) {
|
---|
565 | mouseState.modifiers = e.getModifiersEx();
|
---|
566 | collector.notify();
|
---|
567 | }
|
---|
568 | }
|
---|
569 |
|
---|
570 | @Override public void keyReleased(KeyEvent e) {
|
---|
571 | keyPressed(e);
|
---|
572 | }
|
---|
573 | };
|
---|
574 |
|
---|
575 | private void registerListeners() {
|
---|
576 | // Listen to keyboard/mouse events for pressing/releasing alt key and
|
---|
577 | // inform the collector.
|
---|
578 | try {
|
---|
579 | Toolkit.getDefaultToolkit().addAWTEventListener(awtListener,
|
---|
580 | AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
|
---|
581 | } catch (SecurityException ex) {
|
---|
582 | mv.addMouseMotionListener(mouseMotionListener);
|
---|
583 | mv.addKeyListener(keyAdapter);
|
---|
584 | }
|
---|
585 | }
|
---|
586 |
|
---|
587 | private void unregisterListeners() {
|
---|
588 | try {
|
---|
589 | Toolkit.getDefaultToolkit().removeAWTEventListener(awtListener);
|
---|
590 | } catch (SecurityException e) {
|
---|
591 | // Don't care, awtListener probably wasn't registered anyway
|
---|
592 | }
|
---|
593 | mv.removeMouseMotionListener(mouseMotionListener);
|
---|
594 | mv.removeKeyListener(keyAdapter);
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Construct a new MapStatus and attach it to the map view.
|
---|
600 | * @param mapFrame The MapFrame the status line is part of.
|
---|
601 | */
|
---|
602 | public MapStatus(final MapFrame mapFrame) {
|
---|
603 | this.mv = mapFrame.mapView;
|
---|
604 | this.collector = new Collector(mapFrame);
|
---|
605 |
|
---|
606 | lonText.addMouseListener(Main.main.menu.jumpToAct);
|
---|
607 | latText.addMouseListener(Main.main.menu.jumpToAct);
|
---|
608 |
|
---|
609 | // Listen for mouse movements and set the position text field
|
---|
610 | mv.addMouseMotionListener(new MouseMotionListener(){
|
---|
611 | public void mouseDragged(MouseEvent e) {
|
---|
612 | mouseMoved(e);
|
---|
613 | }
|
---|
614 | public void mouseMoved(MouseEvent e) {
|
---|
615 | if (mv.center == null)
|
---|
616 | return;
|
---|
617 | // Do not update the view if ctrl is pressed.
|
---|
618 | if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0) {
|
---|
619 | CoordinateFormat mCord = CoordinateFormat.getDefaultFormat();
|
---|
620 | LatLon p = mv.getLatLon(e.getX(),e.getY());
|
---|
621 | latText.setText(p.latToString(mCord));
|
---|
622 | lonText.setText(p.lonToString(mCord));
|
---|
623 | }
|
---|
624 | }
|
---|
625 | });
|
---|
626 |
|
---|
627 | setLayout(new GridBagLayout());
|
---|
628 | setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
|
---|
629 |
|
---|
630 | add(latText, GBC.std());
|
---|
631 | add(lonText, GBC.std().insets(3,0,0,0));
|
---|
632 | add(headingText, GBC.std().insets(3,0,0,0));
|
---|
633 | add(angleText, GBC.std().insets(3,0,0,0));
|
---|
634 | add(distText, GBC.std().insets(3,0,0,0));
|
---|
635 |
|
---|
636 | helpText.setEditable(false);
|
---|
637 | add(nameText, GBC.std().insets(3,0,0,0));
|
---|
638 | add(helpText, GBC.eol().insets(3,0,0,0).fill(GBC.HORIZONTAL));
|
---|
639 |
|
---|
640 | // The background thread
|
---|
641 | thread = new Thread(collector, "Map Status Collector");
|
---|
642 | thread.setDaemon(true);
|
---|
643 | thread.start();
|
---|
644 | }
|
---|
645 |
|
---|
646 | public String helpTopic() {
|
---|
647 | return ht("/Statusline");
|
---|
648 | }
|
---|
649 |
|
---|
650 | @Override
|
---|
651 | public synchronized void addMouseListener(MouseListener ml) {
|
---|
652 | //super.addMouseListener(ml);
|
---|
653 | lonText.addMouseListener(ml);
|
---|
654 | latText.addMouseListener(ml);
|
---|
655 | }
|
---|
656 |
|
---|
657 | public void setHelpText(String t) {
|
---|
658 | helpText.setText(t);
|
---|
659 | helpText.setToolTipText(t);
|
---|
660 | }
|
---|
661 | public void setAngle(double a) {
|
---|
662 | angleText.setText(a < 0 ? "--" : Math.round(a*10)/10.0 + " \u00B0");
|
---|
663 | }
|
---|
664 | public void setHeading(double h) {
|
---|
665 | headingText.setText(h < 0 ? "--" : Math.round(h*10)/10.0 + " \u00B0");
|
---|
666 | }
|
---|
667 | public void setDist(double dist) {
|
---|
668 | distText.setText(dist < 0 ? "--" : NavigatableComponent.getDistText(dist));
|
---|
669 | }
|
---|
670 | }
|
---|