source: josm/trunk/src/org/openstreetmap/josm/gui/history/CoordinateInfoViewer.java@ 6340

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

see #8902 - Small performance enhancements / coding style (patch by shinigami):

  • removed useles null checks before instanceof
  • Property svn:eol-style set to native
File size: 12.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.history;
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.Color;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
8import java.awt.Insets;
9import java.util.Observable;
10import java.util.Observer;
11
12import javax.swing.BorderFactory;
13import javax.swing.JLabel;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.data.coor.CoordinateFormat;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.osm.history.HistoryNode;
19import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
20import org.openstreetmap.josm.gui.NavigatableComponent;
21import org.openstreetmap.josm.tools.CheckParameterUtil;
22
23/**
24 * An UI widget for displaying differences in the coordinates of two
25 * {@link HistoryNode}s.
26 *
27 */
28public class CoordinateInfoViewer extends JPanel {
29
30 /** background color used when the coordinates are different */
31 public final static Color BGCOLOR_DIFFERENCE = new Color(255,197,197);
32
33 /** the model */
34 private HistoryBrowserModel model;
35 /** the common info panel for the history node in role REFERENCE_POINT_IN_TIME */
36 private VersionInfoPanel referenceInfoPanel;
37 /** the common info panel for the history node in role CURRENT_POINT_IN_TIME */
38 private VersionInfoPanel currentInfoPanel;
39 /** the info panel for coordinates for the node in role REFERENCE_POINT_IN_TIME */
40 private LatLonViewer referenceLatLonViewer;
41 /** the info panel for coordinates for the node in role CURRENT_POINT_IN_TIME */
42 private LatLonViewer currentLatLonViewer;
43 /** the info panel for distance between the two coordinates */
44 private DistanceViewer distanceViewer;
45
46 protected void build() {
47 setLayout(new GridBagLayout());
48 GridBagConstraints gc = new GridBagConstraints();
49
50 // ---------------------------
51 gc.gridx = 0;
52 gc.gridy = 0;
53 gc.gridwidth = 1;
54 gc.gridheight = 1;
55 gc.weightx = 0.5;
56 gc.weighty = 0.0;
57 gc.insets = new Insets(5,5,5,0);
58 gc.fill = GridBagConstraints.HORIZONTAL;
59 gc.anchor = GridBagConstraints.FIRST_LINE_START;
60 referenceInfoPanel = new VersionInfoPanel(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
61 add(referenceInfoPanel,gc);
62
63 gc.gridx = 1;
64 gc.gridy = 0;
65 gc.fill = GridBagConstraints.HORIZONTAL;
66 gc.weightx = 0.5;
67 gc.weighty = 0.0;
68 gc.anchor = GridBagConstraints.FIRST_LINE_START;
69 currentInfoPanel = new VersionInfoPanel(model, PointInTimeType.CURRENT_POINT_IN_TIME);
70 add(currentInfoPanel,gc);
71
72 // ---------------------------
73 // the two coordinate panels
74 gc.gridx = 0;
75 gc.gridy = 1;
76 gc.weightx = 0.5;
77 gc.weighty = 1.0;
78 gc.fill = GridBagConstraints.BOTH;
79 gc.anchor = GridBagConstraints.NORTHWEST;
80 add(referenceLatLonViewer = new LatLonViewer(model, PointInTimeType.REFERENCE_POINT_IN_TIME), gc);
81
82 gc.gridx = 1;
83 gc.gridy = 1;
84 gc.weightx = 0.5;
85 gc.weighty = 1.0;
86 gc.fill = GridBagConstraints.BOTH;
87 gc.anchor = GridBagConstraints.NORTHWEST;
88 add(currentLatLonViewer = new LatLonViewer(model, PointInTimeType.CURRENT_POINT_IN_TIME), gc);
89
90 // --------------------
91 // the distance panel
92 gc.gridx = 0;
93 gc.gridy = 2;
94 gc.gridwidth = 2;
95 gc.fill = GridBagConstraints.HORIZONTAL;
96 gc.weightx = 1.0;
97 gc.weighty = 0.0;
98 add(distanceViewer = new DistanceViewer(model), gc);
99 }
100
101 /**
102 *
103 * @param model the model. Must not be null.
104 * @throws IllegalArgumentException thrown if model is null
105 */
106 public CoordinateInfoViewer(HistoryBrowserModel model) throws IllegalArgumentException{
107 CheckParameterUtil.ensureParameterNotNull(model, "model");
108 setModel(model);
109 build();
110 registerAsObserver(model);
111 }
112
113 protected void unregisterAsObserver(HistoryBrowserModel model) {
114 if (currentInfoPanel != null) {
115 model.deleteObserver(currentInfoPanel);
116 }
117 if (referenceInfoPanel != null) {
118 model.deleteObserver(referenceInfoPanel);
119 }
120 if (currentLatLonViewer != null) {
121 model.deleteObserver(currentLatLonViewer);
122 }
123 if (referenceLatLonViewer != null) {
124 model.deleteObserver(referenceLatLonViewer);
125 }
126 if (distanceViewer != null) {
127 model.deleteObserver(distanceViewer);
128 }
129 }
130
131 protected void registerAsObserver(HistoryBrowserModel model) {
132 if (currentInfoPanel != null) {
133 model.addObserver(currentInfoPanel);
134 }
135 if (referenceInfoPanel != null) {
136 model.addObserver(referenceInfoPanel);
137 }
138 if (currentLatLonViewer != null) {
139 model.addObserver(currentLatLonViewer);
140 }
141 if (referenceLatLonViewer != null) {
142 model.addObserver(referenceLatLonViewer);
143 }
144 if (distanceViewer != null) {
145 model.addObserver(distanceViewer);
146 }
147 }
148
149 /**
150 * Sets the model for this viewer
151 *
152 * @param model the model.
153 */
154 public void setModel(HistoryBrowserModel model) {
155 if (this.model != null) {
156 unregisterAsObserver(model);
157 }
158 this.model = model;
159 if (this.model != null) {
160 registerAsObserver(model);
161 }
162 }
163
164 /**
165 * A UI widgets which displays the Lan/Lon-coordinates of a
166 * {@link HistoryNode}.
167 *
168 */
169 private static class LatLonViewer extends JPanel implements Observer{
170
171 private JLabel lblLat;
172 private JLabel lblLon;
173 private HistoryBrowserModel model;
174 private PointInTimeType role;
175
176 protected HistoryOsmPrimitive getPrimitive() {
177 if (model == null || role == null)
178 return null;
179 return model.getPointInTime(role);
180 }
181
182 protected HistoryOsmPrimitive getOppositePrimitive() {
183 if (model == null || role == null)
184 return null;
185 return model.getPointInTime(role.opposite());
186 }
187
188 protected void build() {
189 setLayout(new GridBagLayout());
190 setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
191 GridBagConstraints gc = new GridBagConstraints();
192
193 // --------
194 gc.gridx = 0;
195 gc.gridy = 0;
196 gc.fill = GridBagConstraints.NONE;
197 gc.weightx = 0.0;
198 gc.insets = new Insets(5,5,5,5);
199 gc.anchor = GridBagConstraints.NORTHWEST;
200 add(new JLabel(tr("Latitude: ")), gc);
201
202 // --------
203 gc.gridx = 1;
204 gc.gridy = 0;
205 gc.fill = GridBagConstraints.HORIZONTAL;
206 gc.weightx = 1.0;
207 add(lblLat = new JLabel(), gc);
208 lblLat.setBackground(Color.WHITE);
209 lblLat.setOpaque(true);
210 lblLat.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
211
212 // --------
213 gc.gridx = 0;
214 gc.gridy = 1;
215 gc.fill = GridBagConstraints.NONE;
216 gc.weightx = 0.0;
217 gc.anchor = GridBagConstraints.NORTHWEST;
218 add(new JLabel(tr("Longitude: ")), gc);
219
220 // --------
221 gc.gridx = 1;
222 gc.gridy = 1;
223 gc.fill = GridBagConstraints.HORIZONTAL;
224 gc.weightx = 1.0;
225 add(lblLon = new JLabel(), gc);
226 lblLon.setBackground(Color.WHITE);
227 lblLon.setOpaque(true);
228 lblLon.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
229
230 // fill the remaining space
231 gc.gridx = 0;
232 gc.gridy = 2;
233 gc.gridwidth = 2;
234 gc.fill = GridBagConstraints.BOTH;
235 gc.weightx = 1.0;
236 gc.weighty = 1.0;
237 add(new JPanel(), gc);
238 }
239
240 /**
241 *
242 * @param model a model
243 * @param role the role for this viewer.
244 */
245 public LatLonViewer(HistoryBrowserModel model, PointInTimeType role) {
246 build();
247 this.model = model;
248 this.role = role;
249 }
250
251 protected void refresh() {
252 HistoryOsmPrimitive p = getPrimitive();
253 HistoryOsmPrimitive opposite = getOppositePrimitive();
254 if (!(p instanceof HistoryNode)) return;
255 if (!(opposite instanceof HistoryNode)) return;
256 HistoryNode node = (HistoryNode)p;
257 HistoryNode oppositeNode = (HistoryNode) opposite;
258
259 LatLon coord = node.getCoords();
260 LatLon oppositeCoord = oppositeNode.getCoords();
261
262 // display the coordinates
263 //
264 lblLat.setText(coord != null ? coord.latToString(CoordinateFormat.DECIMAL_DEGREES) : tr("(none)"));
265 lblLon.setText(coord != null ? coord.lonToString(CoordinateFormat.DECIMAL_DEGREES) : tr("(none)"));
266
267 // update background color to reflect differences in the coordinates
268 //
269 if (coord == oppositeCoord ||
270 (coord != null && oppositeCoord != null && coord.lat() == oppositeCoord.lat())) {
271 lblLat.setBackground(Color.WHITE);
272 } else {
273 lblLat.setBackground(BGCOLOR_DIFFERENCE);
274 }
275 if (coord == oppositeCoord ||
276 (coord != null && oppositeCoord != null && coord.lon() == oppositeCoord.lon())) {
277 lblLon.setBackground(Color.WHITE);
278 } else {
279 lblLon.setBackground(BGCOLOR_DIFFERENCE);
280 }
281 }
282
283 @Override
284 public void update(Observable o, Object arg) {
285 refresh();
286 }
287 }
288
289 private static class DistanceViewer extends LatLonViewer {
290
291 private JLabel lblDistance;
292
293 public DistanceViewer(HistoryBrowserModel model) {
294 super(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
295 }
296
297 @Override
298 protected void build() {
299 setLayout(new GridBagLayout());
300 setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
301 GridBagConstraints gc = new GridBagConstraints();
302
303 // --------
304 gc.gridx = 0;
305 gc.gridy = 0;
306 gc.fill = GridBagConstraints.NONE;
307 gc.weightx = 0.0;
308 gc.insets = new Insets(5,5,5,5);
309 gc.anchor = GridBagConstraints.NORTHWEST;
310 add(new JLabel(tr("Distance: ")), gc);
311
312 // --------
313 gc.gridx = 1;
314 gc.gridy = 0;
315 gc.fill = GridBagConstraints.HORIZONTAL;
316 gc.weightx = 1.0;
317 add(lblDistance = new JLabel(), gc);
318 lblDistance.setBackground(Color.WHITE);
319 lblDistance.setOpaque(true);
320 lblDistance.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
321 }
322
323 @Override
324 protected void refresh() {
325 HistoryOsmPrimitive p = getPrimitive();
326 HistoryOsmPrimitive opposite = getOppositePrimitive();
327 if (!(p instanceof HistoryNode)) return;
328 if (!(opposite instanceof HistoryNode)) return;
329 HistoryNode node = (HistoryNode) p;
330 HistoryNode oppositeNode = (HistoryNode) opposite;
331
332 LatLon coord = node.getCoords();
333 LatLon oppositeCoord = oppositeNode.getCoords();
334
335 // update distance
336 //
337 if (coord != null && oppositeCoord != null) {
338 double distance = coord.greatCircleDistance(oppositeCoord);
339 if (distance > 0) {
340 lblDistance.setBackground(BGCOLOR_DIFFERENCE);
341 } else {
342 lblDistance.setBackground(Color.WHITE);
343 }
344 lblDistance.setText(NavigatableComponent.getDistText(distance));
345 } else {
346 lblDistance.setBackground(coord != oppositeCoord ? BGCOLOR_DIFFERENCE : Color.WHITE);
347 lblDistance.setText(tr("(none)"));
348 }
349 }
350 }
351}
Note: See TracBrowser for help on using the repository browser.