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

Last change on this file since 2936 was 2850, checked in by mjulius, 14 years ago

fix messages for gui

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