Changeset 10968 in josm for trunk


Ignore:
Timestamp:
2016-09-06T23:50:40+02:00 (3 years ago)
Author:
Don-vip
Message:

see #13537 - refactor constructors and add parameters validation to detect root cause

File:
1 edited

Legend:

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

    r10910 r10968  
    2323import org.openstreetmap.josm.data.projection.Projection;
    2424import org.openstreetmap.josm.gui.download.DownloadDialog;
     25import org.openstreetmap.josm.tools.CheckParameterUtil;
    2526import org.openstreetmap.josm.tools.bugreport.BugReport;
    2627
     
    8081     * @param scale The scale to use
    8182     * @param topLeft The top left corner in east/north space.
    82      */
    83     private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
     83     * @param topLeftInWindow The top left point in window
     84     * @param topLeftOnScreen The top left point on screen
     85     */
     86    private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft,
     87            Point topLeftInWindow, Point topLeftOnScreen) {
     88        CheckParameterUtil.ensureParameterNotNull(projection, "projection");
     89        CheckParameterUtil.ensureParameterNotNull(topLeft, "topLeft");
     90        CheckParameterUtil.ensureParameterNotNull(topLeftInWindow, "topLeftInWindow");
     91        CheckParameterUtil.ensureParameterNotNull(topLeftOnScreen, "topLeftOnScreen");
     92
    8493        this.projecting = projection;
    8594        this.scale = scale;
     
    8897        this.viewWidth = viewWidth;
    8998        this.viewHeight = viewHeight;
    90         topLeftInWindow = new Point(0, 0);
    91         topLeftOnScreen = new Point(0, 0);
    92     }
    93 
    94     private MapViewState(EastNorth topLeft, MapViewState mapViewState) {
    95         this.projecting = mapViewState.projecting;
    96         this.scale = mapViewState.scale;
    97         this.topLeft = topLeft;
    98 
    99         viewWidth = mapViewState.viewWidth;
    100         viewHeight = mapViewState.viewHeight;
    101         topLeftInWindow = mapViewState.topLeftInWindow;
    102         topLeftOnScreen = mapViewState.topLeftOnScreen;
    103     }
    104 
    105     private MapViewState(double scale, MapViewState mapViewState) {
    106         this.projecting = mapViewState.projecting;
    107         this.scale = scale;
    108         this.topLeft = mapViewState.topLeft;
    109 
    110         viewWidth = mapViewState.viewWidth;
    111         viewHeight = mapViewState.viewHeight;
    112         topLeftInWindow = mapViewState.topLeftInWindow;
    113         topLeftOnScreen = mapViewState.topLeftOnScreen;
    114     }
    115 
    116     private MapViewState(JComponent position, MapViewState mapViewState) {
    117         this.projecting = mapViewState.projecting;
    118         this.scale = mapViewState.scale;
    119         this.topLeft = mapViewState.topLeft;
    120 
    121         viewWidth = position.getWidth();
    122         viewHeight = position.getHeight();
    123         topLeftInWindow = new Point();
    124         // better than using swing utils, since this allows us to use the mehtod if no screen is present.
     99        this.topLeftInWindow = topLeftInWindow;
     100        this.topLeftOnScreen = topLeftOnScreen;
     101    }
     102
     103    private MapViewState(Projecting projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
     104        this(projection, viewWidth, viewHeight, scale, topLeft, new Point(0, 0), new Point(0, 0));
     105    }
     106
     107    private MapViewState(EastNorth topLeft, MapViewState mvs) {
     108        this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
     109    }
     110
     111    private MapViewState(double scale, MapViewState mvs) {
     112        this(mvs.projecting, mvs.viewWidth, mvs.viewHeight, scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
     113    }
     114
     115    private MapViewState(JComponent position, MapViewState mvs) {
     116        this(mvs.projecting, position.getWidth(), position.getHeight(), mvs.scale, mvs.topLeft,
     117                findTopLeftInWindow(position), findTopLeftOnScreen(position));
     118    }
     119
     120    private MapViewState(Projecting projecting, MapViewState mvs) {
     121        this(projecting, mvs.viewWidth, mvs.viewHeight, mvs.scale, mvs.topLeft, mvs.topLeftInWindow, mvs.topLeftOnScreen);
     122    }
     123
     124    private static Point findTopLeftInWindow(JComponent position) {
     125        Point result = new Point();
     126        // better than using swing utils, since this allows us to use the method if no screen is present.
    125127        Container component = position;
    126128        while (component != null) {
    127             topLeftInWindow.x += component.getX();
    128             topLeftInWindow.y += component.getY();
     129            result.x += component.getX();
     130            result.y += component.getY();
    129131            component = component.getParent();
    130132        }
     133        return result;
     134    }
     135
     136    private static Point findTopLeftOnScreen(JComponent position) {
    131137        try {
    132             topLeftOnScreen = position.getLocationOnScreen();
     138            return position.getLocationOnScreen();
    133139        } catch (RuntimeException e) {
    134140            throw BugReport.intercept(e).put("position", position).put("parent", position::getParent);
    135141        }
    136     }
    137 
    138     private MapViewState(Projecting projecting, MapViewState mapViewState) {
    139         this.projecting = projecting;
    140         this.scale = mapViewState.scale;
    141         this.topLeft = mapViewState.topLeft;
    142 
    143         viewWidth = mapViewState.viewWidth;
    144         viewHeight = mapViewState.viewHeight;
    145         topLeftInWindow = mapViewState.topLeftInWindow;
    146         topLeftOnScreen = mapViewState.topLeftOnScreen;
    147142    }
    148143
     
    574569
    575570        MapViewEastNorthPoint(EastNorth eastNorth) {
     571            CheckParameterUtil.ensureParameterNotNull(eastNorth, "eastNorth");
    576572            this.eastNorth = eastNorth;
    577573        }
Note: See TracChangeset for help on using the changeset viewer.