From 3315ca88d765a33d2d9f5669bd7d2b74bc028c0e Mon Sep 17 00:00:00 2001 From: rebeccasc <65017227+rebeccasc@users.noreply.github.com> Date: Tue, 25 May 2021 16:51:55 +0200 Subject: [PATCH] Clean code changes --- .../josm/plugins/piclayer/PicLayerPlugin.java | 2 +- .../autocalibrate/AutoCalibrateHandler.java | 19 +++---- .../AutoCalibratePictureAction.java | 2 +- .../autocalibrate/AutoCalibration.java | 55 +++++++++---------- .../autocalibrate/utils/GeoLine.java | 10 +--- .../utils/ObservableArrayList.java | 2 +- .../autocalibrate/CalibrationErrorView.java | 4 +- .../gui/autocalibrate/CalibrationWindow.java | 13 ++--- .../piclayer/layer/PicLayerAbstract.java | 10 ++-- .../piclayer/transform/PictureTransform.java | 2 +- 10 files changed, 50 insertions(+), 69 deletions(-) rename src/org/openstreetmap/josm/plugins/piclayer/actions/{transform => }/autocalibrate/AutoCalibrateHandler.java (97%) rename src/org/openstreetmap/josm/plugins/piclayer/actions/{transform => }/autocalibrate/AutoCalibratePictureAction.java (95%) rename src/org/openstreetmap/josm/plugins/piclayer/actions/{transform => }/autocalibrate/AutoCalibration.java (81%) rename src/org/openstreetmap/josm/plugins/piclayer/actions/{transform => }/autocalibrate/utils/GeoLine.java (85%) rename src/org/openstreetmap/josm/plugins/piclayer/actions/{transform => }/autocalibrate/utils/ObservableArrayList.java (96%) diff --git a/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java b/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java index 22701ef..a362807 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/PicLayerPlugin.java @@ -37,7 +37,7 @@ import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction; import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.RemovePointAction; import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.TransformPointAction; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.AutoCalibratePictureAction; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.AutoCalibratePictureAction; import org.openstreetmap.josm.plugins.piclayer.io.session.FileSessionExporter; import org.openstreetmap.josm.plugins.piclayer.io.session.KMLSessionExporter; import org.openstreetmap.josm.plugins.piclayer.io.session.PicLayerSessionImporter; diff --git a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibrateHandler.java b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibrateHandler.java similarity index 97% rename from src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibrateHandler.java rename to src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibrateHandler.java index 57e561f..05372e5 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibrateHandler.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibrateHandler.java @@ -1,5 +1,5 @@ // License: GPL. For details, see LICENSE file. -package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate; +package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate; import java.awt.event.*; @@ -24,9 +24,9 @@ import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.help.HelpBrowser; import org.openstreetmap.josm.gui.layer.Layer; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.ObservableArrayList; import org.openstreetmap.josm.plugins.piclayer.actions.transform.affine.MovePointAction; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.ObservableArrayList; import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationErrorView; import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationWindow; import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.ReferenceOptionView; @@ -36,7 +36,6 @@ /** * Class handling connection between {@link AutoCalibratePictureAction} and GUIs. - * Info at https://wiki.openstreetmap.org/wiki/User:Rebsc */ public class AutoCalibrateHandler { @@ -45,10 +44,10 @@ public class AutoCalibrateHandler { private File referenceFile; private Layer referenceLayer; private final AutoCalibration calibration; - private ObservableArrayList originPointList; // points set on picture to calibrate, scaled in LatLon - private ObservableArrayList referencePointList; // points of reference data, scaled in LatLon - private double distance1To2; // in meter - private double distance2To3; // in meter + private ObservableArrayList originPointList; // local points in LatLon + private ObservableArrayList referencePointList; // reference points in LatLon + private double distance1To2; // meter + private double distance2To3; // meter public AutoCalibrateHandler() { this.originPointList = new ObservableArrayList<>(3); @@ -88,7 +87,6 @@ private static class HelpButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent e) { String topic = "Plugin/PicLayer"; - // open help browser HelpBrowser.setUrlForHelpTopic(Optional.of(topic).orElse("/")); } } @@ -133,10 +131,8 @@ private class SelectLayerButtonListener implements ActionListener { @Override public void actionPerformed(ActionEvent event) { mainWindow.setVisible(false); - selector = new SelectLayerView(); selector.setVisible(true); - selector.setOkButtonListener(new SelectorOkButtonListener()); selector.setCancelButtonListener(new SelectorCancelButtonListener()); } @@ -229,7 +225,6 @@ private class EdgePointsButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { mainWindow.setVisible(false); MainApplication.getLayerManager().setActiveLayer(currentPicLayer); - // switch to select mode MovePointAction selectPointMode = new MovePointAction(); MainApplication.getMap().selectMapMode(selectPointMode); } diff --git a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibratePictureAction.java b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibratePictureAction.java similarity index 95% rename from src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibratePictureAction.java rename to src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibratePictureAction.java index 61066f8..2623068 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibratePictureAction.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibratePictureAction.java @@ -1,5 +1,5 @@ // License: GPL. For details, see LICENSE file. -package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate; +package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate; import static org.openstreetmap.josm.tools.I18n.tr; diff --git a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibration.java b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibration.java similarity index 81% rename from src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibration.java rename to src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibration.java index 1953d23..7bbea65 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/AutoCalibration.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/AutoCalibration.java @@ -1,5 +1,5 @@ // License: GPL. For details, see LICENSE file. -package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate; +package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate; import java.awt.geom.NoninvertibleTransformException; import java.awt.geom.Point2D; @@ -9,23 +9,22 @@ import org.openstreetmap.josm.data.coor.LatLon; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.MapViewState.MapViewPoint; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine; import org.openstreetmap.josm.plugins.piclayer.gui.autocalibrate.CalibrationErrorView; import org.openstreetmap.josm.plugins.piclayer.layer.PicLayerAbstract; import org.openstreetmap.josm.tools.Logging; /** - * Class for image calibration. - * Info at https://wiki.openstreetmap.org/wiki/User:Rebsc + * Class for image calibration */ public class AutoCalibration { private PicLayerAbstract currentLayer; - private List startPositions; // raw data - LatLon scale - private List endPositions; // raw data - LatLon scale - private double distance1To2; // in meter - private double distance2To3; // in meter + private List startPositions; // raw data - LatLon + private List endPositions; // raw data - LatLon + private double distance1To2; // meter + private double distance2To3; // meter public AutoCalibration() { @@ -41,11 +40,10 @@ public AutoCalibration() { * Sets start points to end points and corrects end points by passed distances between points. */ public void calibrate() { - // get start / end points + // get start/end points List startPointList = currentLayer.getTransformer().getOriginPoints(); // in current layer scale - List endPointList = correctedPoints(endPositions, distance1To2, distance2To3); // in lat/lon scale - translation follows + List endPointList = correctedPoints(endPositions, distance1To2, distance2To3); // in lat/lon scale - // calibrate if (currentLayer != null && startPointList != null && endPointList != null && startPointList.size() == 3 && endPointList.size() == 3 && distance1To2 != 0.0 && distance2To3 != 0.0) { @@ -56,25 +54,18 @@ public void calibrate() { // move all points to final state position for (Point2D endPos : endPointList) { - // get translated start point suitable to end point index = endPointList.indexOf(endPos); tsPoint = startPointList.get(index); - - // transform end point into current layer scale tePoint = translatePointToCurrentScale(endPos); - - // move start point to end point currentLayer.getTransformer().updatePair(tsPoint, tePoint); } - // check if image got too distorted after calibration, if, reset and show error. - // Input start positions (lat/lon), corrected end positions (lat/lon) + // check if image got distorted after calibration, if true reset and show error. if (!checkCalibration(startPositions, endPointList)) { currentLayer.getTransformer().resetCalibration(); showErrorView(CalibrationErrorView.DIMENSION_ERROR); } } else { - // calibration failed showErrorView(CalibrationErrorView.CALIBRATION_ERROR); } @@ -108,7 +99,7 @@ private boolean checkCalibration(List list, List compareList) /** * Corrects points with given distances. Calculates new points on lines - * between given points at given distances. + * between given points with given distances. * * @param points need to be corrected * @param distance12 distance between point 1 and point 2 in meter @@ -119,20 +110,22 @@ private List correctedPoints(List points, double distance12, d if (points != null && points.size() == 3) { List correctedList = new ArrayList<>(); - // get line between point1 and point2 + // get line between point1 and point2, point2 and point3 GeoLine line12 = new GeoLine(points.get(0), points.get(1)); - // get line between point2 and point3 GeoLine line23 = new GeoLine(points.get(1), points.get(2)); - correctedList.add(points.get(0)); // anchor - correctedList.add(line12.pointOnLine(distance12)); // point on line12 at distance12 from start on + // add point 0 - anchor + correctedList.add(points.get(0)); + // add point on line12 at distance12 + correctedList.add(line12.pointOnLine(distance12)); // get lat/lon offset of line12 point to origin point2 double lonOffset = line12.pointOnLine(distance12).getX() - points.get(1).getX(); double latOffset = line12.pointOnLine(distance12).getY() - points.get(1).getY(); - // get point on line23 and add offset - to not deform the image + // get point on line23, add offset Point2D pointOnLine23 = line23.pointOnLine(distance23); Point2D correctedPointOnLine23 = new Point2D.Double(pointOnLine23.getX() + lonOffset, pointOnLine23.getY() + latOffset); - correctedList.add(correctedPointOnLine23); // point on line23 at distance23 from start on corrected with offset from point on line12 + // add point on line23 at distance23 corrected with offset from point on line12 + correctedList.add(correctedPointOnLine23); return correctedList; } @@ -147,8 +140,8 @@ private List correctedPoints(List points, double distance12, d */ private Point2D translatePointToCurrentScale(Point2D point) { Point2D translatedPoint = null; - LatLon ll; // LatLon object from raw Point2D - MapViewPoint en; // MapViewPoint object from LatLon(ll) scaled in EastNorth(en) + LatLon ll; // LatLon object from raw Point2D + MapViewPoint en; // MapViewPoint object from LatLon(ll) scaled in EastNorth(en) // put raw Point2D endPos into LatLon and transform LatLon into MapViewPoint (EastNorth) ll = new LatLon(point.getY(), point.getX()); @@ -174,7 +167,11 @@ public void showErrorView(String msg) { handler.getErrorView().show(msg); } - + /** + * Set current active layer + * + * @param currentLayer to set active + */ public void setCurrentLayer(PicLayerAbstract currentLayer) { this.currentLayer = currentLayer; } diff --git a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/GeoLine.java b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/GeoLine.java similarity index 85% rename from src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/GeoLine.java rename to src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/GeoLine.java index 3e8c68f..cc052e2 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/GeoLine.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/GeoLine.java @@ -1,5 +1,5 @@ // License: GPL. For details, see LICENSE file. -package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils; +package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils; import org.openstreetmap.gui.jmapviewer.OsmMercator; @@ -24,14 +24,6 @@ public GeoLine(Point2D startPoint, Point2D endPoint) { this.distance = getDistance(); } - public GeoLine(double startLat, double startLon, double endLat, double endLon) { - this.lat1 = startLat; - this.lon1 = startLon; - this.lat2 = endLat; - this.lon2 = endLon; - this.distance = getDistance(); - } - public Point2D getStartPoint() { return new Point2D.Double(lon1, lat1); } diff --git a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/ObservableArrayList.java b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/ObservableArrayList.java similarity index 96% rename from src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/ObservableArrayList.java rename to src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/ObservableArrayList.java index 3630db2..f6d2313 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/actions/transform/autocalibrate/utils/ObservableArrayList.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/actions/autocalibrate/utils/ObservableArrayList.java @@ -1,5 +1,5 @@ // License: GPL. For details, see LICENSE file. -package org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils; +package org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils; import java.beans.PropertyChangeListener; import java.beans.PropertyChangeSupport; diff --git a/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationErrorView.java b/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationErrorView.java index a6cacd3..f8dd721 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationErrorView.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationErrorView.java @@ -11,18 +11,16 @@ /** * Class providing error views for calibration action. - * Info at https://wiki.openstreetmap.org/wiki/User:Rebsc */ public class CalibrationErrorView { - public final static String OUTLINE_FILE_ERROR = tr("Could not load outline file!"); public final static String SELECT_LAYER_ERROR = tr("Could not select layer!"); public final static String CALIBRATION_ERROR = tr("Calibration failed!"); public final static String DIMENSION_ERROR = tr(" Calibration failed!
" + "There must be a mistake -
" + "dimension of object differ too much from original.
" + ""); - private JLabel textLabel; + private final JLabel textLabel; public CalibrationErrorView() { textLabel = new JLabel(); diff --git a/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationWindow.java b/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationWindow.java index eb19bdb..3532b29 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationWindow.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/gui/autocalibrate/CalibrationWindow.java @@ -16,16 +16,15 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; +import java.util.Objects; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.border.EmptyBorder; import javax.swing.filechooser.FileNameExtensionFilter; -import org.openstreetmap.josm.gui.MainApplication; -import org.openstreetmap.josm.gui.ProgramArguments; import org.openstreetmap.josm.plugins.piclayer.PicLayerPlugin; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.AutoCalibratePictureAction; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.AutoCalibratePictureAction; import org.openstreetmap.josm.tools.I18n; import org.openstreetmap.josm.tools.Logging; @@ -312,9 +311,9 @@ private void setRefFileName() { private void setSelectLayerButton() { String imageName = "layerlist.png"; - Image image = null; + Image image; try { - image = ImageIO.read(getClass().getResource("/images/" + imageName)); + image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName))); } catch (Exception ex) { Logging.error("Could not load image " + imageName + "," + ex); return; @@ -331,7 +330,7 @@ private void setOpenButton() { String imageName = "open.png"; Image image; try { - image = ImageIO.read(getClass().getResource("/images/" + imageName)); + image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName))); } catch (Exception ex) { Logging.error("Could not load image " + imageName + "," + ex); return; @@ -632,7 +631,7 @@ private ImageIcon getCheckedIcon() { String imageName = "checked.png"; Image image; try { - image = ImageIO.read(getClass().getResource("/images/" + imageName)); + image = ImageIO.read(Objects.requireNonNull(getClass().getResource("/images/" + imageName))); } catch (Exception ex) { Logging.error("Could not load image " + imageName + "," + ex); return null; diff --git a/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java b/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java index 0e2d6e3..2d30309 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/layer/PicLayerAbstract.java @@ -39,7 +39,7 @@ import org.openstreetmap.josm.plugins.piclayer.actions.ResetCalibrationAction; import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationAction; import org.openstreetmap.josm.plugins.piclayer.actions.SavePictureCalibrationToWorldAction; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.GeoLine; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.GeoLine; import org.openstreetmap.josm.plugins.piclayer.transform.PictureTransform; import org.openstreetmap.josm.tools.JosmDecimalFormatSymbolsProvider; import org.openstreetmap.josm.tools.Logging; @@ -241,6 +241,10 @@ public List getImages() { return list; } + public PictureTransform getTransformer() { + return transformer; + } + public GeoLine getRefLine1To2() { return refLine1To2; } @@ -249,10 +253,6 @@ public GeoLine getRefLine2To3() { return refLine2To3; } - public PictureTransform getTransformer() { - return transformer; - } - public void setDrawOriginPoints(boolean value) { drawOriginMarkers = value; } diff --git a/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java b/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java index 1504ff3..1422fc4 100644 --- a/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java +++ b/src/org/openstreetmap/josm/plugins/piclayer/transform/PictureTransform.java @@ -7,7 +7,7 @@ import java.util.List; import org.openstreetmap.josm.data.coor.EastNorth; -import org.openstreetmap.josm.plugins.piclayer.actions.transform.autocalibrate.utils.ObservableArrayList; +import org.openstreetmap.josm.plugins.piclayer.actions.autocalibrate.utils.ObservableArrayList; import org.openstreetmap.josm.tools.Logging; public class PictureTransform {