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

Last change on this file since 172 was 172, checked in by imi, 17 years ago
  • added support for Applet mode again (the basics)
  • added customizable toolbar
  • fixed shortcut for "New Empty Layer"
File size: 7.9 KB
Line 
1package org.openstreetmap.josm.gui;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.AWTEvent;
6import java.awt.Cursor;
7import java.awt.Font;
8import java.awt.GridBagLayout;
9import java.awt.Point;
10import java.awt.Toolkit;
11import java.awt.event.AWTEventListener;
12import java.awt.event.InputEvent;
13import java.awt.event.MouseAdapter;
14import java.awt.event.MouseEvent;
15import java.awt.event.MouseMotionListener;
16import java.util.Collection;
17import java.util.ConcurrentModificationException;
18import java.util.Map.Entry;
19
20import javax.swing.BorderFactory;
21import javax.swing.BoxLayout;
22import javax.swing.JLabel;
23import javax.swing.JPanel;
24import javax.swing.JTextField;
25import javax.swing.Popup;
26import javax.swing.PopupFactory;
27
28import org.openstreetmap.josm.Main;
29import org.openstreetmap.josm.actions.HelpAction.Helpful;
30import org.openstreetmap.josm.data.coor.LatLon;
31import org.openstreetmap.josm.data.osm.OsmPrimitive;
32import org.openstreetmap.josm.data.osm.visitor.NameVisitor;
33import org.openstreetmap.josm.tools.GBC;
34
35/**
36 * A component that manages some status information display about the map.
37 * It keeps a status line below the map up to date and displays some tooltip
38 * information if the user hold the mouse long enough at some point.
39 *
40 * All this is done in background to not disturb other processes.
41 *
42 * The background thread does not alter any data of the map (read only thread).
43 * Also it is rather fail safe. In case of some error in the data, it just do
44 * nothing instead of whining and complaining.
45 *
46 * @author imi
47 */
48public class MapStatus extends JPanel implements Helpful {
49
50 /**
51 * The MapView this status belongs.
52 */
53 final MapView mv;
54 /**
55 * The position of the mouse cursor.
56 */
57 JTextField positionText = new JTextField("-000.00000000000000 -000.00000000000000".length());
58 /**
59 * The field holding the name of the object under the mouse.
60 */
61 JTextField nameText = new JTextField(30);
62
63 /**
64 * The collector class that waits for notification and then update
65 * the display objects.
66 *
67 * @author imi
68 */
69 private final class Collector implements Runnable {
70 /**
71 * The last object displayed in status line.
72 */
73 Collection<OsmPrimitive> osmStatus;
74 /**
75 * The old modifiers, that was pressed the last time this collector ran.
76 */
77 private int oldModifiers;
78 /**
79 * The popup displayed to show additional information
80 */
81 private Popup popup;
82
83 private MapFrame parent;
84
85 public Collector(MapFrame parent) {
86 this.parent = parent;
87 }
88
89 /**
90 * Execution function for the Collector.
91 */
92 public void run() {
93 for (;;) {
94 MouseState ms = new MouseState();
95 synchronized (this) {
96 try {wait();} catch (InterruptedException e) {}
97 ms.modifiers = mouseState.modifiers;
98 ms.mousePos = mouseState.mousePos;
99 }
100 if (parent != Main.map)
101 return; // exit, if new parent.
102 if ((ms.modifiers & MouseEvent.CTRL_DOWN_MASK) != 0 || ms.mousePos == null)
103 continue; // freeze display when holding down ctrl
104
105 if (mv.center == null)
106 continue;
107
108 // This try/catch is a hack to stop the flooding bug reports about this.
109 // The exception needed to handle with in the first place, means that this
110 // access to the data need to be restarted, if the main thread modifies
111 // the data.
112 try {
113 Collection<OsmPrimitive> osms = mv.getAllNearest(ms.mousePos);
114
115 if (osms == null && osmStatus == null && ms.modifiers == oldModifiers)
116 continue;
117 if (osms != null && osms.equals(osmStatus) && ms.modifiers == oldModifiers)
118 continue;
119
120 osmStatus = osms;
121 oldModifiers = ms.modifiers;
122
123 OsmPrimitive osmNearest = null;
124 // Set the text label in the bottom status bar
125 osmNearest = mv.getNearest(ms.mousePos, (ms.modifiers & MouseEvent.ALT_DOWN_MASK) != 0);
126 if (osmNearest != null) {
127 NameVisitor visitor = new NameVisitor();
128 osmNearest.visit(visitor);
129 nameText.setText(visitor.name);
130 } else
131 nameText.setText("");
132
133 // Popup Information
134 if ((ms.modifiers & MouseEvent.BUTTON2_DOWN_MASK) != 0 && osms != null) {
135 if (popup != null)
136 popup.hide();
137
138 JPanel c = new JPanel(new GridBagLayout());
139 for (final OsmPrimitive osm : osms) {
140 NameVisitor visitor = new NameVisitor();
141 osm.visit(visitor);
142 final StringBuilder text = new StringBuilder();
143 if (osm.id == 0 || osm.modified)
144 visitor.name = "<i><b>"+visitor.name+"*</b></i>";
145 text.append(visitor.name);
146 if (osm.id != 0)
147 text.append("<br>id="+osm.id);
148 for (Entry<String, String> e : osm.entrySet())
149 text.append("<br>"+e.getKey()+"="+e.getValue());
150 final JLabel l = new JLabel("<html>"+text.toString()+"</html>", visitor.icon, JLabel.HORIZONTAL);
151 l.setFont(l.getFont().deriveFont(Font.PLAIN));
152 l.setVerticalTextPosition(JLabel.TOP);
153 l.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
154 l.addMouseListener(new MouseAdapter(){
155 @Override public void mouseEntered(MouseEvent e) {
156 l.setText("<html><u color='blue'>"+text.toString()+"</u></html>");
157 }
158 @Override public void mouseExited(MouseEvent e) {
159 l.setText("<html>"+text.toString()+"</html>");
160 }
161 @Override public void mouseClicked(MouseEvent e) {
162 Main.ds.setSelected(osm);
163 mv.repaint();
164 }
165 });
166 c.add(l, GBC.eol());
167 }
168
169 Point p = mv.getLocationOnScreen();
170 popup = PopupFactory.getSharedInstance().getPopup(mv, c, p.x+ms.mousePos.x+16, p.y+ms.mousePos.y+16);
171 popup.show();
172 } else if (popup != null) {
173 popup.hide();
174 popup = null;
175 }
176 } catch (ConcurrentModificationException x) {
177 } catch (NullPointerException x) {
178 }
179 }
180 }
181 }
182
183 /**
184 * Everything, the collector is interested of. Access must be synchronized.
185 * @author imi
186 */
187 class MouseState {
188 Point mousePos;
189 int modifiers;
190 }
191 /**
192 * The last sent mouse movement event.
193 */
194 MouseState mouseState = new MouseState();
195
196 /**
197 * Construct a new MapStatus and attach it to the map view.
198 * @param mv The MapView the status line is part of.
199 */
200 public MapStatus(final MapFrame mapFrame) {
201 this.mv = mapFrame.mapView;
202
203 // Listen for mouse movements and set the position text field
204 mv.addMouseMotionListener(new MouseMotionListener(){
205 public void mouseDragged(MouseEvent e) {
206 mouseMoved(e);
207 }
208 public void mouseMoved(MouseEvent e) {
209 if (mv.center == null)
210 return;
211 // Do not update the view, if ctrl is pressed.
212 if ((e.getModifiersEx() & MouseEvent.CTRL_DOWN_MASK) == 0) {
213 LatLon p = mv.getLatLon(e.getX(),e.getY());
214 positionText.setText(p.lat()+" "+p.lon());
215 }
216 }
217 });
218
219 positionText.setEditable(false);
220 nameText.setEditable(false);
221 setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
222 setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
223 add(new JLabel(tr("Lat/Lon")+" "));
224 add(positionText);
225 add(new JLabel(" "+tr("Object")+" "));
226 add(nameText);
227
228 // The background thread
229 final Collector collector = new Collector(mapFrame);
230 new Thread(collector).start();
231
232 // Listen to keyboard/mouse events for pressing/releasing alt key and
233 // inform the collector.
234 Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener(){
235 public void eventDispatched(AWTEvent event) {
236 synchronized (collector) {
237 mouseState.modifiers = ((InputEvent)event).getModifiersEx();
238 if (event instanceof MouseEvent)
239 mouseState.mousePos = ((MouseEvent)event).getPoint();
240 collector.notify();
241 }
242 }
243 }, AWTEvent.KEY_EVENT_MASK | AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK);
244 }
245
246 public String helpTopic() {
247 return "Statusline";
248 }
249}
Note: See TracBrowser for help on using the repository browser.