Ticket #24951: sonar-grid-oauth.patch

File sonar-grid-oauth.patch, 15.6 KB (added by wangi, 4 weeks ago)
  • src/org/openstreetmap/josm/actions/AlignGridRotationAction.java

    diff --git a/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java b/src/org/openstreetmap/josm/actions/AlignGridRotationAction.java
    index 5f7459a839..8062902e44 100644
    a b import org.openstreetmap.josm.gui.layer.MapGridPaintable.GridType;  
    2626 */
    2727public class AlignGridRotationAction extends JosmAction {
    2828
     29    /** returned when the selection does not define a direction */
     30    private static final EastNorth[] EMPTY_DIRECTION = new EastNorth[0];
     31
    2932    /**
    3033     * Constructs a new {@code AlignGridRotationAction}.
    3134     */
    … … public class AlignGridRotationAction extends JosmAction {  
    3942    /**
    4043     * Determines the direction given by the selection: a single way with at least two nodes, or exactly two nodes.
    4144     * @param ds the data set, may be null
    42      * @return start and end point, or {@code null} if the selection does not define a direction
     45     * @return start and end point, or an empty array if the selection does not define a direction
    4346     */
    4447    static EastNorth[] getSelectedDirection(DataSet ds) {
    4548        if (ds == null) {
    46             return null;
     49            return EMPTY_DIRECTION;
    4750        }
    4851        Collection<Way> ways = ds.getSelectedWays();
    4952        Collection<Node> nodes = ds.getSelectedNodes();
    … … public class AlignGridRotationAction extends JosmAction {  
    6164            b = it.next();
    6265        }
    6366        if (a == null || b == null || !a.isLatLonKnown() || !b.isLatLonKnown()) {
    64             return null;
     67            return EMPTY_DIRECTION;
    6568        }
    6669        EastNorth[] result = {a.getEastNorth(), b.getEastNorth()};
    67         return result[0].equalsEpsilon(result[1], 1e-9) ? null : result;
     70        return result[0].equalsEpsilon(result[1], 1e-9) ? EMPTY_DIRECTION : result;
    6871    }
    6972
    7073    /**
    … … public class AlignGridRotationAction extends JosmAction {  
    8588
    8689    @Override
    8790    protected void updateEnabledState() {
    88         setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()) != null);
     91        setEnabled(getSelectedDirection(getLayerManager().getEditDataSet()).length == 2);
    8992    }
    9093
    9194    @Override
    … … public class AlignGridRotationAction extends JosmAction {  
    9699    @Override
    97100    public void actionPerformed(ActionEvent e) {
    98101        EastNorth[] direction = getSelectedDirection(getLayerManager().getEditDataSet());
    99         if (direction == null) {
     102        if (direction.length != 2) {
    100103            return;
    101104        }
    102105        MapGridPaintable.ROTATION.put(rotationOf(direction[0], direction[1]));
  • src/org/openstreetmap/josm/gui/MapFrame.java

    diff --git a/src/org/openstreetmap/josm/gui/MapFrame.java b/src/org/openstreetmap/josm/gui/MapFrame.java
    index 341371d0c4..a9864246c5 100644
    a b public class MapFrame extends JPanel implements Destroyable, ActiveLayerChangeLi  
    124124     */
    125125    public final MapView mapView;
    126126    /** The grid drawn over the map view, see {@link MapGridPaintable} */
    127     private final MapGridPaintable gridOverlay = new MapGridPaintable();
     127    private final transient MapGridPaintable gridOverlay = new MapGridPaintable();
    128128
    129129    /**
    130130     * This object allows to detect key press and release events
  • src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java

    diff --git a/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java b/src/org/openstreetmap/josm/gui/layer/MapGridPaintable.java
    index f6fc491529..0a5bc43691 100644
    a b import java.awt.geom.Path2D;  
    1212import java.awt.geom.Point2D;
    1313import java.util.ArrayList;
    1414import java.util.List;
     15import java.util.concurrent.atomic.AtomicReference;
    1516
    1617import org.openstreetmap.josm.data.Bounds;
    1718import org.openstreetmap.josm.data.ProjectionBounds;
    1819import org.openstreetmap.josm.data.coor.EastNorth;
    19 import org.openstreetmap.josm.data.coor.ILatLon;
    2020import org.openstreetmap.josm.data.coor.LatLon;
    2121import org.openstreetmap.josm.data.preferences.BooleanProperty;
    2222import org.openstreetmap.josm.data.preferences.DoubleProperty;
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    137137    private static final int MAX_LINES = 500;
    138138
    139139    /** written by {@link #paint} and read by {@link #preferenceChanged}, which may run on another thread */
    140     private volatile MapView mapView;
     140    private final AtomicReference<MapView> mapView = new AtomicReference<>();
    141141
    142142    /**
    143143     * Constructs a new {@code MapGridPaintable}.
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    148148
    149149    @Override
    150150    public void paint(Graphics2D g, MapView mv, Bounds bbox) {
    151         mapView = mv;
     151        mapView.set(mv);
    152152        if (!Boolean.TRUE.equals(ENABLED.get()) || mv.getWidth() <= 0 || mv.getHeight() <= 0) {
    153153            return;
    154154        }
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    318318            if (!a.isValid() || !b.isValid()) {
    319319                return 1;
    320320            }
    321             double metres = a.greatCircleDistance((ILatLon) b);
     321            double metres = a.greatCircleDistance(b);
    322322            return metres > 0 && Double.isFinite(metres) ? step / metres : 1;
    323323        } catch (IllegalArgumentException e) {
    324324            Logging.trace(e);
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    357357    public static List<ProjectedGridLine> getProjectedGridLines(ProjectionBounds area, double spacingX, double spacingY,
    358358            double rotationDegrees, double originX, double originY) {
    359359        List<ProjectedGridLine> lines = new ArrayList<>();
    360         if (!(spacingX > 0) || !(spacingY > 0)) {
     360        if (!isPositive(spacingX) || !isPositive(spacingY)) {
    361361            return lines;
    362362        }
    363363        double angle = Math.toRadians(rotationDegrees);
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    420420    public static List<LatLonGridLine> getLatLonGridLines(Bounds area, Bounds world, double spacingLon, double spacingLat,
    421421            double originLon, double originLat, int segments) {
    422422        List<LatLonGridLine> lines = new ArrayList<>();
    423         if (!(spacingLon > 0) || !(spacingLat > 0) || segments < 1) {
     423        if (!isPositive(spacingLon) || !isPositive(spacingLat) || segments < 1) {
    424424            return lines;
    425425        }
    426426        double minLat = Math.max(area.getMinLat(), world.getMinLat());
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    445445        boolean limitedLon = world.getMinLon() > -180 || world.getMaxLon() < 180;
    446446        for (long k = kLonMin; k <= kLonMax; k++) {
    447447            double lon = LatLon.toIntervalLon(originLon + k * spacingLon);
    448             if (limitedLon && (lon < world.getMinLon() || lon > world.getMaxLon())) {
    449                 continue;
    450             }
    451             List<LatLon> line = new ArrayList<>(segments + 1);
    452             for (int i = 0; i <= segments; i++) {
    453                 line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
     448            if (!limitedLon || (lon >= world.getMinLon() && lon <= world.getMaxLon())) {
     449                addMeridian(lines, lon, minLat, maxLat, segments);
    454450            }
    455             lines.add(new LatLonGridLine(line, true));
    456451        }
    457452        for (long k = kLatMin; k <= kLatMax; k++) {
    458453            double lat = originLat + k * spacingLat;
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    464459        return lines;
    465460    }
    466461
     462    /**
     463     * Adds a meridian running from one latitude to another.
     464     * @param lines the list to add to
     465     * @param lon the longitude of the meridian
     466     * @param minLat the latitude to start at
     467     * @param maxLat the latitude to end at
     468     * @param segments number of segments of the polyline
     469     */
     470    private static void addMeridian(List<LatLonGridLine> lines, double lon, double minLat, double maxLat, int segments) {
     471        List<LatLon> line = new ArrayList<>(segments + 1);
     472        for (int i = 0; i <= segments; i++) {
     473            line.add(new LatLon(minLat + (maxLat - minLat) * i / segments, lon));
     474        }
     475        lines.add(new LatLonGridLine(line, true));
     476    }
     477
    467478    /**
    468479     * Adds a parallel running from one longitude to another. A parallel which crosses the antimeridian is added
    469480     * as two lines, one on each side of it: wrapping the longitudes of a single polyline would instead make it
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    480491            addParallel(lines, lat, -180, maxLon - 360, segments);
    481492            return;
    482493        }
    483         if (!(maxLon > minLon)) {
     494        if (maxLon <= minLon) {
    484495            return;
    485496        }
    486497        List<LatLon> line = new ArrayList<>(segments + 1);
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    490501        lines.add(new LatLonGridLine(line, false));
    491502    }
    492503
     504    /**
     505     * Determines if a value is strictly positive. Unlike {@code value <= 0}, this rejects NaN.
     506     * @param value the value
     507     * @return {@code true} if the value is greater than 0
     508     */
     509    private static boolean isPositive(double value) {
     510        return value > 0;
     511    }
     512
    493513    @Override
    494514    public void preferenceChanged(PreferenceChangeEvent e) {
    495515        if (e.getKey().startsWith(PREFIX) || e.getKey().equals(COLOR_EAST.getKey()) || e.getKey().equals(COLOR_NORTH.getKey())) {
    496516            invalidate();
    497             if (mapView != null) {
    498                 mapView.repaint();
     517            MapView mv = mapView.get();
     518            if (mv != null) {
     519                mv.repaint();
    499520            }
    500521        }
    501522    }
    … … public class MapGridPaintable extends AbstractMapViewPaintable implements Prefer  
    503524    @Override
    504525    public void destroy() {
    505526        Config.getPref().removePreferenceChangeListener(this);
    506         mapView = null;
     527        mapView.set(null);
    507528    }
    508529}
  • src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java

    diff --git a/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java b/src/org/openstreetmap/josm/gui/layer/imagery/ShowTileBordersAction.java
    index 8c2cf5ecbe..8a53b22978 100644
    a b import org.openstreetmap.josm.gui.layer.Layer.LayerAction;  
    2020 */
    2121public class ShowTileBordersAction extends AbstractAction implements LayerAction {
    2222
    23     private final AbstractTileSourceLayer<?> layer;
     23    private final transient AbstractTileSourceLayer<?> layer;
    2424
    2525    /**
    2626     * Constructs a new {@code ShowTileBordersAction}.
  • src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java

    diff --git a/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java b/src/org/openstreetmap/josm/gui/preferences/display/GridPreference.java
    index 5899b72d03..1af89faf58 100644
    a b package org.openstreetmap.josm.gui.preferences.display;  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    66import java.awt.Component;
     7import java.awt.GridBagConstraints;
    78import java.awt.GridBagLayout;
    89import java.text.DecimalFormat;
    910import java.text.DecimalFormatSymbols;
    … … public class GridPreference extends DefaultTabPreferenceSetting {  
    100101
    101102        panel.add(enabled, GBC.eol().insets(0, 0, 0, 10));
    102103        panel.add(new JLabel(tr("Grid type")), GBC.std().insets(5, 0, 5, 5));
    103         panel.add(type, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
     104        panel.add(type, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
    104105        panel.add(spacingXLabel, GBC.std().insets(5, 0, 5, 5));
    105106        panel.add(spacingX, GBC.eol().insets(0, 0, 0, 5));
    106107        panel.add(spacingYLabel, GBC.std().insets(5, 0, 5, 5));
    … … public class GridPreference extends DefaultTabPreferenceSetting {  
    116117        panel.add(new JLabel(tr("Line width (pixels)")), GBC.std().insets(5, 0, 5, 5));
    117118        panel.add(lineWidth, GBC.eol().insets(0, 0, 0, 5));
    118119        panel.add(new JLabel(tr("Blend mode")), GBC.std().insets(5, 0, 5, 5));
    119         panel.add(blendMode, GBC.eol().fill(GBC.HORIZONTAL).insets(0, 0, 0, 5));
    120         panel.add(GBC.glue(0, 0), GBC.eol().fill(GBC.BOTH));
     120        panel.add(blendMode, GBC.eol().fill(GridBagConstraints.HORIZONTAL).insets(0, 0, 0, 5));
     121        panel.add(GBC.glue(0, 0), GBC.eol().fill(GridBagConstraints.BOTH));
    121122
    122123        createPreferenceTabWithScrollPane(gui, panel);
    123124    }
    … … public class GridPreference extends DefaultTabPreferenceSetting {  
    142143
    143144    /** Renders enum values with a translated name */
    144145    private static final class TranslatedRenderer<T> extends DefaultListCellRenderer {
    145         private final Function<T, String> name;
     146        private final transient Function<T, String> name;
    146147
    147148        TranslatedRenderer(Function<T, String> name) {
    148149            this.name = name;
    … … public class GridPreference extends DefaultTabPreferenceSetting {  
    178179    private static void save(JosmTextField field, DoubleProperty property, boolean positive) {
    179180        try {
    180181            double value = FORMAT.parse(field.getText().trim()).doubleValue();
    181             if (positive && !(value > 0)) {
     182            if (positive && (Double.isNaN(value) || value <= 0)) {
    182183                Logging.warn("Ignoring invalid grid setting {0}: {1}", property.getKey(), field.getText());
    183184                return;
    184185            }
  • test/unit/org/openstreetmap/josm/actions/GridActionsTest.java

    diff --git a/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java b/test/unit/org/openstreetmap/josm/actions/GridActionsTest.java
    index 37e9d667c6..5d59504846 100644
    a b class GridActionsTest {  
    164164            ds.addPrimitive(w);
    165165
    166166            ds.setSelected();
    167             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     167            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    168168            AlignGridRotationAction action = new AlignGridRotationAction();
    169169            assertFalse(action.isEnabled());
    170170            ds.setSelected(a);
    171             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     171            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    172172            assertFalse(action.isEnabled());
    173173            ds.setSelected(a, b, c);
    174             assertNull(AlignGridRotationAction.getSelectedDirection(ds));
     174            assertEquals(0, AlignGridRotationAction.getSelectedDirection(ds).length);
    175175            assertFalse(action.isEnabled());
    176176
    177177            ds.setSelected(a, b);
  • test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java

    diff --git a/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java b/test/unit/org/openstreetmap/josm/io/OsmConnectionTest.java
    index f742a38422..61fb9b5c11 100644
    a b import static org.junit.jupiter.api.Assertions.assertThrows;  
    88import static org.junit.jupiter.api.Assertions.assertTrue;
    99
    1010import java.net.MalformedURLException;
    11 import java.net.URL;
     11import java.net.URI;
    1212
    1313import javax.swing.JOptionPane;
    1414
    … … class OsmConnectionTest {  
    5959    }
    6060
    6161    private static HttpClient apiRequest() throws MalformedURLException {
    62         return HttpClient.create(new URL(API_URL + "/0.6/changeset/create"), "PUT");
     62        return HttpClient.create(URI.create(API_URL + "/0.6/changeset/create").toURL(), "PUT");
    6363    }
    6464
    6565    /**