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

Last change on this file since 4601 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

  • Property svn:eol-style set to native
File size: 8.7 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.osm.history.HistoryNode;
18import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
19import org.openstreetmap.josm.tools.CheckParameterUtil;
20
21/**
22 * An UI widget for displaying differences in the coordinates of two
23 * {@see HistoryNode}s.
24 *
25 */
26public class CoordinateInfoViewer extends JPanel {
27
28 /** background color used when the coordinates are different */
29 public final static Color BGCOLOR_DIFFERENCE = new Color(255,197,197);
30
31 /** the model */
32 private HistoryBrowserModel model;
33 /** the common info panel for the history node in role REFERENCE_POINT_IN_TIME */
34 private VersionInfoPanel referenceInfoPanel;
35 /** the common info panel for the history node in role CURRENT_POINT_IN_TIME */
36 private VersionInfoPanel currentInfoPanel;
37 /** the info panel for coordinates for the node in role REFERENCE_POINT_IN_TIME */
38 private LatLonViewer referenceLatLonViewer;
39 /** the info panel for coordinates for the node in role CURRENT_POINT_IN_TIME */
40 private LatLonViewer currentLatLonViewer;
41
42 protected void build() {
43 setLayout(new GridBagLayout());
44 GridBagConstraints gc = new GridBagConstraints();
45
46 // ---------------------------
47 gc.gridx = 0;
48 gc.gridy = 0;
49 gc.gridwidth = 1;
50 gc.gridheight = 1;
51 gc.weightx = 0.5;
52 gc.weighty = 0.0;
53 gc.insets = new Insets(5,5,5,0);
54 gc.fill = GridBagConstraints.HORIZONTAL;
55 gc.anchor = GridBagConstraints.FIRST_LINE_START;
56 referenceInfoPanel = new VersionInfoPanel(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
57 add(referenceInfoPanel,gc);
58
59 gc.gridx = 1;
60 gc.gridy = 0;
61 gc.fill = GridBagConstraints.HORIZONTAL;
62 gc.weightx = 0.5;
63 gc.weighty = 0.0;
64 gc.anchor = GridBagConstraints.FIRST_LINE_START;
65 currentInfoPanel = new VersionInfoPanel(model, PointInTimeType.CURRENT_POINT_IN_TIME);
66 add(currentInfoPanel,gc);
67
68 // ---------------------------
69 // the two coordinate panels
70 gc.gridx = 0;
71 gc.gridy = 1;
72 gc.weightx = 0.5;
73 gc.weighty = 1.0;
74 gc.fill = GridBagConstraints.BOTH;
75 gc.anchor = GridBagConstraints.NORTHWEST;
76 add(referenceLatLonViewer = new LatLonViewer(model, PointInTimeType.REFERENCE_POINT_IN_TIME), gc);
77
78 gc.gridx = 1;
79 gc.gridy = 1;
80 gc.weightx = 0.5;
81 gc.weighty = 1.0;
82 gc.fill = GridBagConstraints.BOTH;
83 gc.anchor = GridBagConstraints.NORTHWEST;
84 add(currentLatLonViewer = new LatLonViewer(model, PointInTimeType.CURRENT_POINT_IN_TIME), gc);
85 }
86
87 /**
88 *
89 * @param model the model. Must not be null.
90 * @throws IllegalArgumentException thrown if model is null
91 */
92 public CoordinateInfoViewer(HistoryBrowserModel model) throws IllegalArgumentException{
93 CheckParameterUtil.ensureParameterNotNull(model, "model");
94 setModel(model);
95 build();
96 registerAsObserver(model);
97 }
98
99 protected void unregisterAsObserver(HistoryBrowserModel model) {
100 if (currentInfoPanel != null) {
101 model.deleteObserver(currentInfoPanel);
102 }
103 if (referenceInfoPanel != null) {
104 model.deleteObserver(referenceInfoPanel);
105 }
106 if (currentLatLonViewer != null) {
107 model.deleteObserver(currentLatLonViewer);
108 }
109 if (referenceLatLonViewer != null) {
110 model.deleteObserver(referenceLatLonViewer);
111 }
112 }
113
114 protected void registerAsObserver(HistoryBrowserModel model) {
115 if (currentInfoPanel != null) {
116 model.addObserver(currentInfoPanel);
117 }
118 if (referenceInfoPanel != null) {
119 model.addObserver(referenceInfoPanel);
120 }
121 if (currentLatLonViewer != null) {
122 model.addObserver(currentLatLonViewer);
123 }
124 if (referenceLatLonViewer != null) {
125 model.addObserver(referenceLatLonViewer);
126 }
127 }
128
129 /**
130 * Sets the model for this viewer
131 *
132 * @param model the model.
133 */
134 public void setModel(HistoryBrowserModel model) {
135 if (this.model != null) {
136 unregisterAsObserver(model);
137 }
138 this.model = model;
139 if (this.model != null) {
140 registerAsObserver(model);
141 }
142 }
143
144 /**
145 * A UI widgets which displays the Lan/Lon-coordinates of a
146 * {@see HistoryNode}.
147 *
148 */
149 private static class LatLonViewer extends JPanel implements Observer{
150
151 private JLabel lblLat;
152 private JLabel lblLon;
153 private HistoryBrowserModel model;
154 private PointInTimeType role;
155
156 protected HistoryOsmPrimitive getPrimitive() {
157 if (model == null || role == null)
158 return null;
159 return model.getPointInTime(role);
160 }
161
162 protected HistoryOsmPrimitive getOppositePrimitive() {
163 if (model == null || role == null)
164 return null;
165 return model.getPointInTime(role.opposite());
166 }
167
168 protected void build() {
169 setLayout(new GridBagLayout());
170 setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
171 GridBagConstraints gc = new GridBagConstraints();
172
173 // --------
174 gc.gridx = 0;
175 gc.gridy = 0;
176 gc.fill = GridBagConstraints.NONE;
177 gc.weightx = 0.0;
178 gc.insets = new Insets(5,5,5,5);
179 gc.anchor = GridBagConstraints.NORTHWEST;
180 add(new JLabel(tr("Latitude: ")), gc);
181
182 // --------
183 gc.gridx = 1;
184 gc.gridy = 0;
185 gc.fill = GridBagConstraints.HORIZONTAL;
186 gc.weightx = 1.0;
187 add(lblLat = new JLabel(), gc);
188 lblLat.setBackground(Color.WHITE);
189 lblLat.setOpaque(true);
190 lblLat.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
191
192 // --------
193 gc.gridx = 0;
194 gc.gridy = 1;
195 gc.fill = GridBagConstraints.NONE;
196 gc.weightx = 0.0;
197 gc.anchor = GridBagConstraints.NORTHWEST;
198 add(new JLabel(tr("Longitude: ")), gc);
199
200 // --------
201 gc.gridx = 1;
202 gc.gridy = 1;
203 gc.fill = GridBagConstraints.HORIZONTAL;
204 gc.weightx = 1.0;
205 add(lblLon = new JLabel(), gc);
206 lblLon.setBackground(Color.WHITE);
207 lblLon.setOpaque(true);
208 lblLon.setBorder(BorderFactory.createEmptyBorder(2,2,2,2));
209
210 // fill the remaining space
211 gc.gridx = 0;
212 gc.gridy = 2;
213 gc.gridwidth = 2;
214 gc.fill = GridBagConstraints.BOTH;
215 gc.weightx = 1.0;
216 gc.weighty = 1.0;
217 add(new JPanel(), gc);
218 }
219
220 /**
221 *
222 * @param model a model
223 * @param role the role for this viewer.
224 */
225 public LatLonViewer(HistoryBrowserModel model, PointInTimeType role) {
226 build();
227 this.model = model;
228 this.role = role;
229 }
230
231 protected void refresh() {
232 HistoryOsmPrimitive p = getPrimitive();
233 HistoryOsmPrimitive opposite = getOppositePrimitive();
234 if (p == null || ! ( p instanceof HistoryNode)) return;
235 if (opposite == null || ! (opposite instanceof HistoryNode)) return;
236 HistoryNode node = (HistoryNode)p;
237 HistoryNode oppositeNode = (HistoryNode) opposite;
238
239 // display the coordinates
240 //
241 lblLat.setText(node.getCoords().latToString(CoordinateFormat.DECIMAL_DEGREES));
242 lblLon.setText(node.getCoords().lonToString(CoordinateFormat.DECIMAL_DEGREES));
243
244 // update background color to reflect differences in the coordinates
245 //
246 if (node.getCoords().lat() == oppositeNode.getCoords().lat()) {
247 lblLat.setBackground(Color.WHITE);
248 } else {
249 lblLat.setBackground(BGCOLOR_DIFFERENCE);
250 }
251 if (node.getCoords().lon() == oppositeNode.getCoords().lon()) {
252 lblLon.setBackground(Color.WHITE);
253 } else {
254 lblLon.setBackground(BGCOLOR_DIFFERENCE);
255 }
256
257 }
258
259 public void update(Observable o, Object arg) {
260 refresh();
261 }
262 }
263}
Note: See TracBrowser for help on using the repository browser.