Changeset 9441 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2016-01-14T17:26:20+01:00 (8 years ago)
Author:
simon04
Message:

fix #12364 - History dialog: show old and new coordinate on map

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/history/CoordinateInfoViewer.java

    r9223 r9441  
    1414import javax.swing.JPanel;
    1515
     16import org.openstreetmap.gui.jmapviewer.JMapViewer;
     17import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
    1618import org.openstreetmap.josm.data.coor.CoordinateFormat;
    1719import org.openstreetmap.josm.data.coor.LatLon;
     
    2123import org.openstreetmap.josm.gui.util.GuiHelper;
    2224import org.openstreetmap.josm.tools.CheckParameterUtil;
     25import org.openstreetmap.josm.tools.Pair;
    2326
    2427/**
     
    2831 */
    2932public class CoordinateInfoViewer extends JPanel {
    30 
    31     /** background color used when the coordinates are different */
    32     public static final Color BGCOLOR_DIFFERENCE = new Color(255, 197, 197);
    3333
    3434    /** the model */
     
    4444    /** the info panel for distance between the two coordinates */
    4545    private DistanceViewer distanceViewer;
     46    /** the map panel showing the old+new coordinate */
     47    private MapViewer mapViewer;
    4648
    4749    protected void build() {
     
    7678        gc.gridy = 1;
    7779        gc.weightx = 0.5;
    78         gc.weighty = 1.0;
    79         gc.fill = GridBagConstraints.BOTH;
     80        gc.weighty = 0.0;
     81        gc.fill = GridBagConstraints.HORIZONTAL;
    8082        gc.anchor = GridBagConstraints.NORTHWEST;
    8183        add(referenceLatLonViewer = new LatLonViewer(model, PointInTimeType.REFERENCE_POINT_IN_TIME), gc);
     
    8486        gc.gridy = 1;
    8587        gc.weightx = 0.5;
    86         gc.weighty = 1.0;
    87         gc.fill = GridBagConstraints.BOTH;
     88        gc.weighty = 0.0;
     89        gc.fill = GridBagConstraints.HORIZONTAL;
    8890        gc.anchor = GridBagConstraints.NORTHWEST;
    8991        add(currentLatLonViewer = new LatLonViewer(model, PointInTimeType.CURRENT_POINT_IN_TIME), gc);
     
    98100        gc.weighty = 0.0;
    99101        add(distanceViewer = new DistanceViewer(model), gc);
     102
     103        // the map panel
     104        gc.gridx = 0;
     105        gc.gridy = 3;
     106        gc.gridwidth = 2;
     107        gc.fill = GridBagConstraints.BOTH;
     108        gc.weightx = 1.0;
     109        gc.weighty = 1.0;
     110        gc.insets = new Insets(5, 5, 5, 5);
     111        add(mapViewer = new MapViewer(model), gc);
     112        mapViewer.setZoomContolsVisible(false);
    100113    }
    101114
     
    128141            model.deleteObserver(distanceViewer);
    129142        }
     143        if (mapViewer != null) {
     144            model.deleteObserver(mapViewer);
     145        }
    130146    }
    131147
     
    145161        if (distanceViewer != null) {
    146162            model.addObserver(distanceViewer);
     163        }
     164        if (mapViewer != null) {
     165            model.addObserver(mapViewer);
    147166        }
    148167    }
     
    163182    }
    164183
     184    private static class Updater {
     185        private final transient HistoryBrowserModel model;
     186        private final PointInTimeType role;
     187
     188        public Updater(HistoryBrowserModel model, PointInTimeType role) {
     189            this.model = model;
     190            this.role = role;
     191        }
     192
     193        protected HistoryOsmPrimitive getPrimitive() {
     194            if (model == null || role == null)
     195                return null;
     196            return model.getPointInTime(role);
     197        }
     198
     199        protected HistoryOsmPrimitive getOppositePrimitive() {
     200            if (model == null || role == null)
     201                return null;
     202            return model.getPointInTime(role.opposite());
     203        }
     204
     205        protected final Pair<LatLon, LatLon> getCoordinates() {
     206            HistoryOsmPrimitive p = getPrimitive();
     207            HistoryOsmPrimitive opposite = getOppositePrimitive();
     208            if (!(p instanceof HistoryNode)) return null;
     209            if (!(opposite instanceof HistoryNode)) return null;
     210            HistoryNode node = (HistoryNode) p;
     211            HistoryNode oppositeNode = (HistoryNode) opposite;
     212
     213            return Pair.create(node.getCoords(), oppositeNode.getCoords());
     214        }
     215
     216    }
     217
    165218    /**
    166219     * A UI widgets which displays the Lan/Lon-coordinates of a
     
    172225        private JLabel lblLat;
    173226        private JLabel lblLon;
    174         private final transient HistoryBrowserModel model;
    175         private final PointInTimeType role;
    176 
    177         protected LatLon coord;
    178         protected LatLon oppositeCoord;
    179 
    180         protected HistoryOsmPrimitive getPrimitive() {
    181             if (model == null || role == null)
    182                 return null;
    183             return model.getPointInTime(role);
    184         }
    185 
    186         protected HistoryOsmPrimitive getOppositePrimitive() {
    187             if (model == null || role == null)
    188                 return null;
    189             return model.getPointInTime(role.opposite());
    190         }
     227        private final Updater updater;
     228        private final Color modifiedColor;
    191229
    192230        protected void build() {
     
    231269            lblLon.setOpaque(true);
    232270            lblLon.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
    233 
    234             // fill the remaining space
    235             gc.gridx = 0;
    236             gc.gridy = 2;
    237             gc.gridwidth = 2;
    238             gc.fill = GridBagConstraints.BOTH;
    239             gc.weightx = 1.0;
    240             gc.weighty = 1.0;
    241             add(new JPanel(), gc);
    242271        }
    243272
     
    248277         */
    249278        LatLonViewer(HistoryBrowserModel model, PointInTimeType role) {
     279            this.updater = new Updater(model, role);
     280            this.modifiedColor = PointInTimeType.CURRENT_POINT_IN_TIME.equals(role)
     281                    ? TwoColumnDiff.Item.DiffItemType.INSERTED.getColor()
     282                    : TwoColumnDiff.Item.DiffItemType.DELETED.getColor();
    250283            build();
    251             this.model = model;
    252             this.role = role;
    253         }
    254 
    255         protected final boolean prepareRefresh() {
    256             HistoryOsmPrimitive p = getPrimitive();
    257             HistoryOsmPrimitive  opposite = getOppositePrimitive();
    258             if (!(p instanceof HistoryNode)) return false;
    259             if (!(opposite instanceof HistoryNode)) return false;
    260             HistoryNode node = (HistoryNode) p;
    261             HistoryNode oppositeNode = (HistoryNode) opposite;
    262 
    263             coord = node.getCoords();
    264             oppositeCoord = oppositeNode.getCoords();
    265             return true;
    266284        }
    267285
    268286        protected void refresh() {
    269             if (!prepareRefresh()) return;
     287            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
     288            if (coordinates == null) return;
     289            final LatLon coord = coordinates.a;
     290            final LatLon oppositeCoord = coordinates.b;
    270291
    271292            // display the coordinates
     
    278299                GuiHelper.setBackgroundReadable(lblLat, Color.WHITE);
    279300            } else {
    280                 GuiHelper.setBackgroundReadable(lblLat, BGCOLOR_DIFFERENCE);
     301                GuiHelper.setBackgroundReadable(lblLat, modifiedColor);
    281302            }
    282303            if (coord == oppositeCoord ||
     
    284305                GuiHelper.setBackgroundReadable(lblLon, Color.WHITE);
    285306            } else {
    286                 GuiHelper.setBackgroundReadable(lblLon, BGCOLOR_DIFFERENCE);
     307                GuiHelper.setBackgroundReadable(lblLon, modifiedColor);
    287308            }
    288309        }
     
    294315    }
    295316
    296     private static class DistanceViewer extends LatLonViewer {
     317    private static class MapViewer extends JMapViewer implements Observer {
     318
     319        private final Updater updater;
     320
     321        public MapViewer(HistoryBrowserModel model) {
     322            this.updater = new Updater(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
     323            setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
     324        }
     325
     326        @Override
     327        public void update(Observable o, Object arg) {
     328            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
     329            if (coordinates == null) {
     330                return;
     331            }
     332
     333            final MapMarkerDot oldMarker = new MapMarkerDot(coordinates.a.lat(), coordinates.a.lon());
     334            final MapMarkerDot newMarker = new MapMarkerDot(coordinates.b.lat(), coordinates.b.lon());
     335            oldMarker.setBackColor(TwoColumnDiff.Item.DiffItemType.DELETED.getColor());
     336            newMarker.setBackColor(TwoColumnDiff.Item.DiffItemType.INSERTED.getColor());
     337
     338            removeAllMapMarkers();
     339            addMapMarker(oldMarker);
     340            addMapMarker(newMarker);
     341            setDisplayToFitMapMarkers();
     342        }
     343    }
     344
     345    private static class DistanceViewer extends JPanel implements Observer {
    297346
    298347        private JLabel lblDistance;
     348        private final Updater updater;
    299349
    300350        DistanceViewer(HistoryBrowserModel model) {
    301             super(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
    302         }
    303 
    304         @Override
     351            this.updater = new Updater(model, PointInTimeType.REFERENCE_POINT_IN_TIME);
     352            build();
     353        }
     354
    305355        protected void build() {
    306356            setLayout(new GridBagLayout());
     
    328378        }
    329379
    330         @Override
    331380        protected void refresh() {
    332             if (!prepareRefresh()) return;
     381            final Pair<LatLon, LatLon> coordinates = updater.getCoordinates();
     382            if (coordinates == null) return;
     383            final LatLon coord = coordinates.a;
     384            final LatLon oppositeCoord = coordinates.b;
    333385
    334386            // update distance
     
    336388            if (coord != null && oppositeCoord != null) {
    337389                double distance = coord.greatCircleDistance(oppositeCoord);
    338                 GuiHelper.setBackgroundReadable(lblDistance, distance > 0 ? BGCOLOR_DIFFERENCE : Color.WHITE);
     390                GuiHelper.setBackgroundReadable(lblDistance, distance > 0
     391                        ? TwoColumnDiff.Item.DiffItemType.CHANGED.getColor()
     392                        : Color.WHITE);
    339393                lblDistance.setText(NavigatableComponent.getDistText(distance));
    340394            } else {
    341                 GuiHelper.setBackgroundReadable(lblDistance, coord != oppositeCoord ? BGCOLOR_DIFFERENCE : Color.WHITE);
     395                GuiHelper.setBackgroundReadable(lblDistance, coord != oppositeCoord
     396                        ? TwoColumnDiff.Item.DiffItemType.CHANGED.getColor()
     397                        : Color.WHITE);
    342398                lblDistance.setText(tr("(none)"));
    343399            }
    344400        }
     401
     402        @Override
     403        public void update(Observable o, Object arg) {
     404            refresh();
     405        }
    345406    }
    346407}
Note: See TracChangeset for help on using the changeset viewer.