001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.history;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.awt.Color;
007import java.awt.GridBagConstraints;
008import java.awt.GridBagLayout;
009import java.awt.Insets;
010import java.awt.event.ActionEvent;
011import java.awt.event.MouseAdapter;
012import java.awt.event.MouseEvent;
013
014import javax.swing.AbstractAction;
015import javax.swing.BorderFactory;
016import javax.swing.JLabel;
017import javax.swing.JPanel;
018import javax.swing.JPopupMenu;
019import javax.swing.UIManager;
020import javax.swing.event.ChangeEvent;
021import javax.swing.event.ChangeListener;
022
023import org.openstreetmap.gui.jmapviewer.JMapViewer;
024import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
025import org.openstreetmap.josm.command.MoveCommand;
026import org.openstreetmap.josm.data.UndoRedoHandler;
027import org.openstreetmap.josm.data.coor.ILatLon;
028import org.openstreetmap.josm.data.coor.LatLon;
029import org.openstreetmap.josm.data.coor.conversion.DecimalDegreesCoordinateFormat;
030import org.openstreetmap.josm.data.osm.Node;
031import org.openstreetmap.josm.data.osm.OsmPrimitive;
032import org.openstreetmap.josm.data.osm.history.HistoryNode;
033import org.openstreetmap.josm.data.osm.history.HistoryOsmPrimitive;
034import org.openstreetmap.josm.gui.NavigatableComponent;
035import org.openstreetmap.josm.gui.bbox.JosmMapViewer;
036import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
037import org.openstreetmap.josm.gui.util.GuiHelper;
038import org.openstreetmap.josm.gui.widgets.JosmTextArea;
039import org.openstreetmap.josm.tools.CheckParameterUtil;
040import org.openstreetmap.josm.tools.Destroyable;
041import org.openstreetmap.josm.tools.ImageProvider;
042import org.openstreetmap.josm.tools.Pair;
043
044/**
045 * An UI widget for displaying differences in the coordinates of two
046 * {@link HistoryNode}s.
047 * @since 2243
048 */
049public class CoordinateInfoViewer extends HistoryBrowserPanel {
050
051    /** the info panel for coordinates for the node in role REFERENCE_POINT_IN_TIME */
052    private LatLonViewer referenceLatLonViewer;
053    /** the info panel for coordinates for the node in role CURRENT_POINT_IN_TIME */
054    private LatLonViewer currentLatLonViewer;
055    /** the info panel for distance between the two coordinates */
056    private DistanceViewer distanceViewer;
057    /** the map panel showing the old+new coordinate */
058    private MapViewer mapViewer;
059
060    protected void build() {
061        GridBagConstraints gc = new GridBagConstraints();
062
063        // ---------------------------
064        gc.gridx = 0;
065        gc.gridy = 0;
066        gc.gridwidth = 1;
067        gc.gridheight = 1;
068        gc.weightx = 0.5;
069        gc.weighty = 0.0;
070        gc.insets = new Insets(5, 5, 5, 0);
071        gc.fill = GridBagConstraints.HORIZONTAL;
072        gc.anchor = GridBagConstraints.FIRST_LINE_START;
073        referenceInfoPanel = new VersionInfoPanel(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
074        add(referenceInfoPanel, gc);
075
076        gc.gridx = 1;
077        gc.gridy = 0;
078        gc.fill = GridBagConstraints.HORIZONTAL;
079        gc.weightx = 0.5;
080        gc.weighty = 0.0;
081        gc.anchor = GridBagConstraints.FIRST_LINE_START;
082        currentInfoPanel = new VersionInfoPanel(model, PointInTimeType.CURRENT_POINT_IN_TIME);
083        add(currentInfoPanel, gc);
084
085        // ---------------------------
086        // the two coordinate panels
087        gc.gridx = 0;
088        gc.gridy = 1;
089        gc.weightx = 0.5;
090        gc.weighty = 0.0;
091        gc.fill = GridBagConstraints.HORIZONTAL;
092        gc.anchor = GridBagConstraints.NORTHWEST;
093        referenceLatLonViewer = new LatLonViewer(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
094        add(referenceLatLonViewer, gc);
095
096        gc.gridx = 1;
097        gc.gridy = 1;
098        gc.weightx = 0.5;
099        gc.weighty = 0.0;
100        gc.fill = GridBagConstraints.HORIZONTAL;
101        gc.anchor = GridBagConstraints.NORTHWEST;
102        currentLatLonViewer = new LatLonViewer(model, PointInTimeType.CURRENT_POINT_IN_TIME);
103        add(currentLatLonViewer, gc);
104
105        // --------------------
106        // the distance panel
107        gc.gridx = 0;
108        gc.gridy = 2;
109        gc.gridwidth = 2;
110        gc.fill = GridBagConstraints.HORIZONTAL;
111        gc.weightx = 1.0;
112        gc.weighty = 0.0;
113        distanceViewer = new DistanceViewer(model);
114        add(distanceViewer, gc);
115
116        // the map panel
117        gc.gridx = 0;
118        gc.gridy = 3;
119        gc.gridwidth = 2;
120        gc.fill = GridBagConstraints.BOTH;
121        gc.weightx = 1.0;
122        gc.weighty = 1.0;
123        mapViewer = new MapViewer(model);
124        add(mapViewer, gc);
125        mapViewer.setZoomControlsVisible(false);
126
127        JPopupMenu popupMenu = new JPopupMenu();
128        popupMenu.add(new RestoreCoordinateAction());
129        setComponentPopupMenu(popupMenu);
130        mapViewer.setComponentPopupMenu(popupMenu);
131    }
132
133    /**
134     * Constructs a new {@code CoordinateInfoViewer}.
135     * @param model the model. Must not be null.
136     * @throws IllegalArgumentException if model is null
137     */
138    public CoordinateInfoViewer(HistoryBrowserModel model) {
139        CheckParameterUtil.ensureParameterNotNull(model, "model");
140        setModel(model);
141        build();
142        registerAsChangeListener(model);
143    }
144
145    @Override
146    protected void unregisterAsChangeListener(HistoryBrowserModel model) {
147        super.unregisterAsChangeListener(model);
148        if (currentLatLonViewer != null) {
149            model.removeChangeListener(currentLatLonViewer);
150        }
151        if (referenceLatLonViewer != null) {
152            model.removeChangeListener(referenceLatLonViewer);
153        }
154        if (distanceViewer != null) {
155            model.removeChangeListener(distanceViewer);
156        }
157        if (mapViewer != null) {
158            model.removeChangeListener(mapViewer);
159        }
160    }
161
162    @Override
163    protected void registerAsChangeListener(HistoryBrowserModel model) {
164        super.registerAsChangeListener(model);
165        if (currentLatLonViewer != null) {
166            model.addChangeListener(currentLatLonViewer);
167        }
168        if (referenceLatLonViewer != null) {
169            model.addChangeListener(referenceLatLonViewer);
170        }
171        if (distanceViewer != null) {
172            model.addChangeListener(distanceViewer);
173        }
174        if (mapViewer != null) {
175            model.addChangeListener(mapViewer);
176        }
177    }
178
179    @Override
180    public void destroy() {
181        super.destroy();
182        referenceLatLonViewer.destroy();
183        currentLatLonViewer.destroy();
184        distanceViewer.destroy();
185    }
186
187    /**
188     * Pans the map to the old+new coordinate
189     * @see JMapViewer#setDisplayToFitMapMarkers()
190     */
191    public void setDisplayToFitMapMarkers() {
192        mapViewer.setDisplayToFitMapMarkers();
193    }
194
195    private static JosmTextArea newTextArea() {
196        JosmTextArea area = new JosmTextArea();
197        GuiHelper.setBackgroundReadable(area, Color.WHITE);
198        area.setEditable(false);
199        area.setOpaque(true);
200        area.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
201        area.setFont(UIManager.getFont("Label.font"));
202        return area;
203    }
204
205    private static class Updater {
206        private final HistoryBrowserModel model;
207        private final PointInTimeType role;
208
209        protected Updater(HistoryBrowserModel model, PointInTimeType role) {
210            this.model = model;
211            this.role = role;
212        }
213
214        protected HistoryOsmPrimitive getPrimitive() {
215            if (model == null || role == null)
216                return null;
217            return model.getPointInTime(role);
218        }
219
220        protected HistoryOsmPrimitive getOppositePrimitive() {
221            if (model == null || role == null)
222                return null;
223            return model.getPointInTime(role.opposite());
224        }
225
226        protected final Pair<LatLon, LatLon> getCoordinates() {
227            HistoryOsmPrimitive p = getPrimitive();
228            if (!(p instanceof HistoryNode)) return null;
229            HistoryOsmPrimitive opposite = getOppositePrimitive();
230            if (!(opposite instanceof HistoryNode)) return null;
231            HistoryNode node = (HistoryNode) p;
232            HistoryNode oppositeNode = (HistoryNode) opposite;
233
234            return Pair.create(node.getCoords(), oppositeNode.getCoords());
235        }
236    }
237
238    /**
239     * A UI widgets which displays the Lan/Lon-coordinates of a {@link HistoryNode}.
240     */
241    private static class LatLonViewer extends JPanel implements ChangeListener, Destroyable {
242
243        private final JosmTextArea lblLat = newTextArea();
244        private final JosmTextArea lblLon = newTextArea();
245        private final transient Updater updater;
246        private final Color modifiedColor;
247
248        protected void build() {
249            setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
250            GridBagConstraints gc = new GridBagConstraints();
251
252            // --------
253            gc.gridx = 0;
254            gc.gridy = 0;
255            gc.fill = GridBagConstraints.NONE;
256            gc.weightx = 0.0;
257            gc.insets = new Insets(5, 5, 5, 5);
258            gc.anchor = GridBagConstraints.NORTHWEST;
259            add(new JLabel(tr("Latitude: ")), gc);
260
261            // --------
262            gc.gridx = 1;
263            gc.gridy = 0;
264            gc.fill = GridBagConstraints.HORIZONTAL;
265            gc.weightx = 1.0;
266            add(lblLat, gc);
267
268            // --------
269            gc.gridx = 0;
270            gc.gridy = 1;
271            gc.fill = GridBagConstraints.NONE;
272            gc.weightx = 0.0;
273            gc.anchor = GridBagConstraints.NORTHWEST;
274            add(new JLabel(tr("Longitude: ")), gc);
275
276            // --------
277            gc.gridx = 1;
278            gc.gridy = 1;
279            gc.fill = GridBagConstraints.HORIZONTAL;
280            gc.weightx = 1.0;
281            add(lblLon, gc);
282        }
283
284        /**
285         * Constructs a new {@code LatLonViewer}.
286         * @param model a model
287         * @param role the role for this viewer.
288         */
289        LatLonViewer(HistoryBrowserModel model, PointInTimeType role) {
290            super(new GridBagLayout());
291            this.updater = new Updater(model, role);
292            this.modifiedColor = PointInTimeType.CURRENT_POINT_IN_TIME == role
293                    ? TwoColumnDiff.Item.DiffItemType.INSERTED.getColor()
294                    : TwoColumnDiff.Item.DiffItemType.DELETED.getColor();
295            build();
296        }
297
298        protected void refresh() {
299            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
300            if (coordinates == null) return;
301            final LatLon coord = coordinates.a;
302            final LatLon oppositeCoord = coordinates.b;
303
304            // display the coordinates
305            lblLat.setText(coord != null ? DecimalDegreesCoordinateFormat.INSTANCE.latToString(coord) : tr("(none)"));
306            lblLon.setText(coord != null ? DecimalDegreesCoordinateFormat.INSTANCE.lonToString(coord) : tr("(none)"));
307
308            // update background color to reflect differences in the coordinates
309            if (coord == oppositeCoord ||
310                    (coord != null && oppositeCoord != null && coord.lat() == oppositeCoord.lat())) {
311                GuiHelper.setBackgroundReadable(lblLat, Color.WHITE);
312            } else {
313                GuiHelper.setBackgroundReadable(lblLat, modifiedColor);
314            }
315            if (coord == oppositeCoord ||
316                    (coord != null && oppositeCoord != null && coord.lon() == oppositeCoord.lon())) {
317                GuiHelper.setBackgroundReadable(lblLon, Color.WHITE);
318            } else {
319                GuiHelper.setBackgroundReadable(lblLon, modifiedColor);
320            }
321        }
322
323        @Override
324        public void stateChanged(ChangeEvent e) {
325            refresh();
326        }
327
328        @Override
329        public void destroy() {
330            lblLat.destroy();
331            lblLon.destroy();
332        }
333    }
334
335    private static class MapViewer extends JosmMapViewer implements ChangeListener {
336
337        private final transient Updater updater;
338
339        MapViewer(HistoryBrowserModel model) {
340            this.updater = new Updater(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
341            setTileSource(SlippyMapBBoxChooser.DefaultOsmTileSourceProvider.get()); // for attribution
342            setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
343            addMouseListener(new MouseAdapter() {
344                @Override
345                public void mouseClicked(MouseEvent e) {
346                    if (e.getButton() == MouseEvent.BUTTON1) {
347                        getAttribution().handleAttribution(e.getPoint(), true);
348                    }
349                }
350            });
351        }
352
353        @Override
354        public void stateChanged(ChangeEvent e) {
355            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
356            if (coordinates == null) {
357                return;
358            }
359
360            removeAllMapMarkers();
361
362            if (coordinates.a != null) {
363                final MapMarkerDot oldMarker = new MapMarkerDot(coordinates.a.lat(), coordinates.a.lon());
364                oldMarker.setBackColor(TwoColumnDiff.Item.DiffItemType.DELETED.getColor());
365                addMapMarker(oldMarker);
366            }
367            if (coordinates.b != null) {
368                final MapMarkerDot newMarker = new MapMarkerDot(coordinates.b.lat(), coordinates.b.lon());
369                newMarker.setBackColor(TwoColumnDiff.Item.DiffItemType.INSERTED.getColor());
370                addMapMarker(newMarker);
371            }
372
373            super.setDisplayToFitMapMarkers();
374        }
375    }
376
377    private static class DistanceViewer extends JPanel implements ChangeListener, Destroyable {
378
379        private final JosmTextArea lblDistance = newTextArea();
380        private final transient Updater updater;
381
382        DistanceViewer(HistoryBrowserModel model) {
383            super(new GridBagLayout());
384            updater = new Updater(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
385            build();
386        }
387
388        protected void build() {
389            setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
390            GridBagConstraints gc = new GridBagConstraints();
391
392            // --------
393            gc.gridx = 0;
394            gc.gridy = 0;
395            gc.fill = GridBagConstraints.NONE;
396            gc.weightx = 0.0;
397            gc.insets = new Insets(5, 5, 5, 5);
398            gc.anchor = GridBagConstraints.NORTHWEST;
399            add(new JLabel(tr("Distance: ")), gc);
400
401            // --------
402            gc.gridx = 1;
403            gc.gridy = 0;
404            gc.fill = GridBagConstraints.HORIZONTAL;
405            gc.weightx = 1.0;
406            add(lblDistance, gc);
407        }
408
409        protected void refresh() {
410            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
411            if (coordinates == null) return;
412            final ILatLon coord = coordinates.a;
413            final ILatLon oppositeCoord = coordinates.b;
414
415            // update distance
416            //
417            if (coord != null && oppositeCoord != null) {
418                double distance = coord.greatCircleDistance(oppositeCoord);
419                GuiHelper.setBackgroundReadable(lblDistance, distance > 0
420                        ? TwoColumnDiff.Item.DiffItemType.CHANGED.getColor()
421                        : Color.WHITE);
422                lblDistance.setText(NavigatableComponent.getDistText(distance));
423            } else {
424                GuiHelper.setBackgroundReadable(lblDistance, coord != oppositeCoord
425                        ? TwoColumnDiff.Item.DiffItemType.CHANGED.getColor()
426                        : Color.WHITE);
427                lblDistance.setText(tr("(none)"));
428            }
429        }
430
431        @Override
432        public void stateChanged(ChangeEvent e) {
433            refresh();
434        }
435
436        @Override
437        public void destroy() {
438            lblDistance.destroy();
439        }
440    }
441
442    private class RestoreCoordinateAction extends AbstractAction {
443
444        RestoreCoordinateAction() {
445            super(tr("Restore"));
446            new ImageProvider("undo").getResource().attachImageIcon(this, true);
447        }
448
449        @Override
450        public void actionPerformed(ActionEvent e) {
451            OsmPrimitive primitive = getPrimitiveFromDataSet(PointInTimeType.REFERENCE_POINT_IN_TIME);
452            if (!(primitive instanceof Node) || ((Node) primitive).getEastNorth() == null) {
453                return;
454            }
455            HistoryOsmPrimitive historyPrimitive = model.getPointInTime(PointInTimeType.REFERENCE_POINT_IN_TIME);
456            if (!(historyPrimitive instanceof HistoryNode) || ((HistoryNode) historyPrimitive).getCoords() == null) {
457                return;
458            }
459            MoveCommand command = new MoveCommand(((Node) primitive), ((HistoryNode) historyPrimitive).getCoords());
460            UndoRedoHandler.getInstance().add(command);
461        }
462    }
463}