Changeset 10375 in josm for trunk


Ignore:
Timestamp:
2016-06-14T19:46:27+02:00 (4 years ago)
Author:
Don-vip
Message:

fix #12959 - Move state changes to MapViewState class (patch by michael2402) - gscore-core + checkstyle fixes

Location:
trunk
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/AboutAction.java

    r10369 r10375  
    77import java.awt.Dimension;
    88import java.awt.GridBagLayout;
    9 import java.awt.Image;
    109import java.awt.event.ActionEvent;
    1110import java.awt.event.KeyEvent;
     
    1615
    1716import javax.swing.BorderFactory;
    18 import javax.swing.ImageIcon;
    1917import javax.swing.JLabel;
    2018import javax.swing.JPanel;
  • trunk/src/org/openstreetmap/josm/gui/MapStatus.java

    r10228 r10375  
    387387
    388388                        // Do nothing, if required data is missing
    389                         if (ms.mousePos == null || mv.center == null) {
     389                        if (ms.mousePos == null || mv.getCenter() == null) {
    390390                            continue;
    391391                        }
     
    868868            @Override
    869869            public void mouseMoved(MouseEvent e) {
    870                 if (mv.center == null)
     870                if (mv.getCenter() == null)
    871871                    return;
    872872                // Do not update the view if ctrl is pressed.
  • trunk/src/org/openstreetmap/josm/gui/MapView.java

    r10374 r10375  
    936936     */
    937937    public boolean prepareToDraw() {
     938        updateLocationState();
    938939        if (initialViewport != null) {
    939940            zoomTo(initialViewport);
  • trunk/src/org/openstreetmap/josm/gui/MapViewState.java

    r10343 r10375  
    22package org.openstreetmap.josm.gui;
    33
     4import java.awt.Container;
    45import java.awt.Point;
     6import java.awt.geom.AffineTransform;
    57import java.awt.geom.Point2D;
    68import java.awt.geom.Point2D.Double;
    79
    8 import javax.swing.SwingUtilities;
     10import javax.swing.JComponent;
    911
    1012import org.openstreetmap.josm.Main;
     
    1416import org.openstreetmap.josm.data.coor.LatLon;
    1517import org.openstreetmap.josm.data.projection.Projection;
     18import org.openstreetmap.josm.gui.download.DownloadDialog;
    1619
    1720/**
     
    2023 * @since 10343
    2124 */
    22 public class MapViewState {
    23 
    24     private final Projection projection = Main.getProjection();
     25public final class MapViewState {
     26
     27    private final Projection projection;
    2528
    2629    private final int viewWidth;
     
    3437    private final EastNorth topLeft;
    3538
    36     private final NavigatableComponent navigatableComponent;
    37 
    38     /**
    39      * Create a new {@link MapViewState} object for the given map view.
    40      * @param navigatableComponent The view.
    41      */
    42     public MapViewState(NavigatableComponent navigatableComponent) {
    43         this.navigatableComponent = navigatableComponent;
    44         viewWidth = navigatableComponent.getWidth();
    45         viewHeight = navigatableComponent.getHeight();
    46 
    47         scale = navigatableComponent.getScale();
    48         EastNorth center = navigatableComponent.getCenter();
    49         topLeft = new EastNorth(center.east() - viewWidth / 2.0 * scale, center.north() + viewHeight / 2.0 * scale);
     39    private final Point topLeftOnScreen;
     40    private final Point topLeftInWindow;
     41
     42    /**
     43     * Create a new {@link MapViewState}
     44     * @param projection The projection to use.
     45     * @param viewWidth The view width
     46     * @param viewHeight The view height
     47     * @param scale The scale to use
     48     * @param topLeft The top left corner in east/north space.
     49     */
     50    private MapViewState(Projection projection, int viewWidth, int viewHeight, double scale, EastNorth topLeft) {
     51        this.projection = projection;
     52        this.scale = scale;
     53        this.topLeft = topLeft;
     54
     55        this.viewWidth = viewWidth;
     56        this.viewHeight = viewHeight;
     57        topLeftInWindow = new Point(0, 0);
     58        topLeftOnScreen = new Point(0, 0);
     59    }
     60
     61    private MapViewState(EastNorth topLeft, MapViewState mapViewState) {
     62        this.projection = mapViewState.projection;
     63        this.scale = mapViewState.scale;
     64        this.topLeft = topLeft;
     65
     66        viewWidth = mapViewState.viewWidth;
     67        viewHeight = mapViewState.viewHeight;
     68        topLeftInWindow = mapViewState.topLeftInWindow;
     69        topLeftOnScreen = mapViewState.topLeftOnScreen;
     70    }
     71
     72    private MapViewState(double scale, MapViewState mapViewState) {
     73        this.projection = mapViewState.projection;
     74        this.scale = scale;
     75        this.topLeft = mapViewState.topLeft;
     76
     77        viewWidth = mapViewState.viewWidth;
     78        viewHeight = mapViewState.viewHeight;
     79        topLeftInWindow = mapViewState.topLeftInWindow;
     80        topLeftOnScreen = mapViewState.topLeftOnScreen;
     81    }
     82
     83    private MapViewState(JComponent position, MapViewState mapViewState) {
     84        this.projection = mapViewState.projection;
     85        this.scale = mapViewState.scale;
     86        this.topLeft = mapViewState.topLeft;
     87
     88        viewWidth = position.getWidth();
     89        viewHeight = position.getHeight();
     90        topLeftInWindow = new Point();
     91        // better than using swing utils, since this allows us to use the mehtod if no screen is present.
     92        Container component = position;
     93        while (component != null) {
     94            topLeftInWindow.x += component.getX();
     95            topLeftInWindow.y += component.getY();
     96            component = component.getParent();
     97        }
     98        topLeftOnScreen = position.getLocationOnScreen();
     99    }
     100
     101    /**
     102     * The scale in east/north units per pixel.
     103     * @return The scale.
     104     */
     105    public double getScale() {
     106        return scale;
    50107    }
    51108
     
    110167
    111168    /**
     169     * Creates an affine transform that is used to convert the east/north coordinates to view coordinates.
     170     * @return The affine transform. It should not be changed.
     171     * @since xxx
     172     */
     173    public AffineTransform getAffineTransform() {
     174        return new AffineTransform(1.0 / scale, 0.0, 0.0, -1.0 / scale, topLeft.east() / scale,
     175                topLeft.north() / scale);
     176    }
     177
     178    /**
     179     * Creates a new state that is the same as the current state except for that it is using a new center.
     180     * @param newCenter The new center coordinate.
     181     * @return The new state.
     182     * @since xxx
     183     */
     184    public MapViewState usingCenter(EastNorth newCenter) {
     185        return movedTo(getCenter(), newCenter);
     186    }
     187
     188    /**
     189     * @param mapViewPoint The reference point.
     190     * @param newEastNorthThere The east/north coordinate that should be there.
     191     * @return The new state.
     192     * @since xxx
     193     */
     194    public MapViewState movedTo(MapViewPoint mapViewPoint, EastNorth newEastNorthThere) {
     195        EastNorth delta = newEastNorthThere.subtract(mapViewPoint.getEastNorth());
     196        if (delta.distanceSq(0, 0) < .000001) {
     197            return this;
     198        } else {
     199            return new MapViewState(topLeft.add(delta), this);
     200        }
     201    }
     202
     203    /**
     204     * Creates a new state that is the same as the current state except for that it is using a new scale.
     205     * @param newScale The new scale to use.
     206     * @return The new state.
     207     * @since xxx
     208     */
     209    public MapViewState usingScale(double newScale) {
     210        return new MapViewState(newScale, this);
     211    }
     212
     213    /**
     214     * Creates a new state that is the same as the current state except for that it is using the location of the given component.
     215     * <p>
     216     * The view is moved so that the center is the same as the old center.
     217     * @param positon The new location to use.
     218     * @return The new state.
     219     * @since xxx
     220     */
     221    public MapViewState usingLocation(JComponent positon) {
     222        EastNorth center = this.getCenter().getEastNorth();
     223        return new MapViewState(positon, this).usingCenter(center);
     224    }
     225
     226    /**
     227     * Create the default {@link MapViewState} object for the given map view. The screen position won't be set so that this method can be used
     228     * before the view was added to the hirarchy.
     229     * @param width The view width
     230     * @param height The view height
     231     * @return The state
     232     * @since xxx
     233     */
     234    public static MapViewState createDefaultState(int width, int height) {
     235        Projection projection = Main.getProjection();
     236        double scale = projection.getDefaultZoomInPPD();
     237        MapViewState state = new MapViewState(projection, width, height, scale, new EastNorth(0, 0));
     238        EastNorth center = calculateDefaultCenter();
     239        return state.movedTo(state.getCenter(), center);
     240    }
     241
     242    private static EastNorth calculateDefaultCenter() {
     243        Bounds b = DownloadDialog.getSavedDownloadBounds();
     244        if (b == null) {
     245            b = Main.getProjection().getWorldBoundsLatLon();
     246        }
     247        return Main.getProjection().latlon2eastNorth(b.getCenter());
     248    }
     249
     250    /**
    112251     * A class representing a point in the map view. It allows to convert between the different coordinate systems.
    113252     * @author Michael Zangl
     
    132271         */
    133272        public Point2D getInWindow() {
    134             Point corner = SwingUtilities.convertPoint(navigatableComponent, new Point(0, 0), null);
    135             return getUsingCorner(corner);
     273            return getUsingCorner(topLeftInWindow);
    136274        }
    137275
     
    141279         */
    142280        public Point2D getOnScreen() {
    143             Point corner = new Point(0, 0);
    144             SwingUtilities.convertPointToScreen(corner, navigatableComponent);
    145             return getUsingCorner(corner);
     281            return getUsingCorner(topLeftOnScreen);
    146282        }
    147283
     
    268404        }
    269405    }
     406
    270407}
  • trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java

    r10343 r10375  
    55import java.awt.Point;
    66import java.awt.Rectangle;
     7import java.awt.event.ComponentAdapter;
     8import java.awt.event.ComponentEvent;
     9import java.awt.event.HierarchyEvent;
     10import java.awt.event.HierarchyListener;
    711import java.awt.geom.AffineTransform;
    812import java.awt.geom.Point2D;
     
    2529
    2630import javax.swing.JComponent;
     31import javax.swing.SwingUtilities;
    2732
    2833import org.openstreetmap.josm.Main;
     
    4853import org.openstreetmap.josm.data.projection.Projections;
    4954import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
    50 import org.openstreetmap.josm.gui.download.DownloadDialog;
    5155import org.openstreetmap.josm.gui.help.Helpful;
    5256import org.openstreetmap.josm.gui.layer.NativeScaleLayer;
     
    135139    }
    136140
    137     private double scale = Main.getProjection().getDefaultZoomInPPD();
    138     /**
    139      * Center n/e coordinate of the desired screen center.
    140      */
    141     protected EastNorth center = calculateDefaultCenter();
     141    // The only events that may move/resize this map view are window movements or changes to the map view size.
     142    // We can clean this up more by only recalculating the state on repaint.
     143    private final HierarchyListener hierarchyListener = new HierarchyListener() {
     144        @Override
     145        public void hierarchyChanged(HierarchyEvent e) {
     146            long interestingFlags = HierarchyEvent.ANCESTOR_MOVED | HierarchyEvent.SHOWING_CHANGED;
     147            if ((e.getChangeFlags() & interestingFlags) != 0) {
     148                updateLocationState();
     149            }
     150        }
     151    };
     152
     153    private final ComponentAdapter componentListener = new ComponentAdapter() {
     154        @Override
     155        public void componentShown(ComponentEvent e) {
     156            updateLocationState();
     157        }
     158
     159        @Override
     160        public void componentResized(ComponentEvent e) {
     161            updateLocationState();
     162        }
     163    };
    142164
    143165    protected transient ViewportData initialViewport;
    144166
    145167    protected final transient CursorManager cursorManager = new CursorManager(this);
     168
     169    /**
     170     * The current state (scale, center, ...) of this map view.
     171     */
     172    private MapViewState state;
    146173
    147174    /**
     
    150177    public NavigatableComponent() {
    151178        setLayout(null);
     179        state = MapViewState.createDefaultState(getWidth(), getHeight());
     180    }
     181
     182    @Override
     183    public void addNotify() {
     184        updateLocationState();
     185        addHierarchyListener(hierarchyListener);
     186        addComponentListener(componentListener);
     187        super.addNotify();
     188    }
     189
     190    @Override
     191    public void removeNotify() {
     192        removeHierarchyListener(hierarchyListener);
     193        removeComponentListener(componentListener);
     194        super.removeNotify();
    152195    }
    153196
     
    158201    public void setNativeScaleLayer(NativeScaleLayer nativeScaleLayer) {
    159202        this.nativeScaleLayer = nativeScaleLayer;
    160         zoomTo(center, scaleRound(scale));
     203        zoomTo(getCenter(), scaleRound(getScale()));
    161204        repaint();
    162205    }
     
    251294     */
    252295    public void zoomIn() {
    253         zoomTo(center, scaleZoomIn());
     296        zoomTo(getCenter(), scaleZoomIn());
    254297    }
    255298
     
    258301     */
    259302    public void zoomOut() {
    260         zoomTo(center, scaleZoomOut());
     303        zoomTo(getCenter(), scaleZoomOut());
    261304    }
    262305
     
    265308    }
    266309
    267     private static EastNorth calculateDefaultCenter() {
    268         Bounds b = DownloadDialog.getSavedDownloadBounds();
    269         if (b == null) {
    270             b = Main.getProjection().getWorldBoundsLatLon();
    271         }
    272         return Main.getProjection().latlon2eastNorth(b.getCenter());
     310    protected void updateLocationState() {
     311        if (SwingUtilities.getWindowAncestor(this) != null && isShowing()) {
     312            state = state.usingLocation(this);
     313        }
     314    }
     315
     316    /**
     317     * Gets the current view state. This includes the scale, the current view area and the position.
     318     * @return The current state.
     319     */
     320    public MapViewState getState() {
     321        return state;
    273322    }
    274323
     
    339388     */
    340389    public EastNorth getCenter() {
    341         return center;
     390        return state.getCenter().getEastNorth();
    342391    }
    343392
     
    350399     */
    351400    public double getScale() {
    352         return scale;
     401        return state.getScale();
    353402    }
    354403
     
    360409     */
    361410    public EastNorth getEastNorth(int x, int y) {
    362         return new EastNorth(
    363                 center.east() + (x - getWidth()/2.0)*scale,
    364                 center.north() - (y - getHeight()/2.0)*scale);
     411        return state.getForView(x, y).getEastNorth();
    365412    }
    366413
    367414    public ProjectionBounds getProjectionBounds() {
    368         return new MapViewState(this).getViewArea().getProjectionBounds();
     415        return getState().getViewArea().getProjectionBounds();
    369416    }
    370417
     
    378425    /* FIXME: replace with better method - used by Main to reset Bounds when projection changes, don't use otherwise */
    379426    public Bounds getRealBounds() {
    380         return new MapViewState(this).getViewArea().getCornerBounds();
     427        return getState().getViewArea().getCornerBounds();
    381428    }
    382429
     
    397444
    398445    public ProjectionBounds getProjectionBounds(Rectangle r) {
    399         MapViewState state = new MapViewState(this);
     446        MapViewState state = getState();
    400447        MapViewPoint p1 = state.getForView(r.getMinX(), r.getMinY());
    401448        MapViewPoint p2 = state.getForView(r.getMaxX(), r.getMaxY());
     
    412459
    413460    public AffineTransform getAffineTransform() {
    414         return new AffineTransform(
    415                 1.0/scale, 0.0, 0.0, -1.0/scale, getWidth()/2.0 - center.east()/scale, getHeight()/2.0 + center.north()/scale);
     461        return getState().getAffineTransform();
    416462    }
    417463
     
    425471        if (null == p)
    426472            return new Point();
    427         double x = (p.east()-center.east())/scale + getWidth()/2d;
    428         double y = (center.north()-p.north())/scale + getHeight()/2d;
    429         return new Point2D.Double(x, y);
     473        return getState().getPointFor(p).getInView();
    430474    }
    431475
     
    513557        if (ll1.isValid() && ll2.isValid() && b.contains(ll1) && b.contains(ll2)) {
    514558            double dm = ll1.greatCircleDistance(ll2);
    515             double den = 100 * scale;
     559            double den = 100 * getScale();
    516560            double scaleMin = 0.01 * den / dm / 100;
    517561            if (!Double.isInfinite(scaleMin) && newScale < scaleMin) {
     
    521565
    522566        // snap scale to imagery if needed
    523         scale = scaleRound(scale);
    524 
    525         if (!newCenter.equals(center) || !Utils.equalsEpsilon(scale, newScale)) {
     567        newScale = scaleRound(newScale);
     568
     569        if (!newCenter.equals(getCenter()) || !Utils.equalsEpsilon(getScale(), newScale)) {
    526570            if (!initial) {
    527                 pushZoomUndo(center, scale);
     571                pushZoomUndo(getCenter(), getScale());
    528572            }
    529573            zoomNoUndoTo(newCenter, newScale, initial);
     
    539583     */
    540584    private void zoomNoUndoTo(EastNorth newCenter, double newScale, boolean initial) {
    541         if (!newCenter.equals(center)) {
    542             EastNorth oldCenter = center;
    543             center = newCenter;
     585        if (!newCenter.equals(getCenter())) {
     586            EastNorth oldCenter = getCenter();
     587            state = state.usingCenter(newCenter);
    544588            if (!initial) {
    545589                firePropertyChange(PROPNAME_CENTER, oldCenter, newCenter);
    546590            }
    547591        }
    548         if (!Utils.equalsEpsilon(scale, newScale)) {
    549             double oldScale = scale;
    550             scale = newScale;
     592        if (!Utils.equalsEpsilon(getScale(), newScale)) {
     593            double oldScale = getScale();
     594            state = state.usingScale(newScale);
     595            // temporary. Zoom logic needs to be moved.
     596            state = state.movedTo(state.getCenter(), newCenter);
    551597            if (!initial) {
    552598                firePropertyChange(PROPNAME_SCALE, oldScale, newScale);
     
    561607
    562608    public void zoomTo(EastNorth newCenter) {
    563         zoomTo(newCenter, scale);
     609        zoomTo(newCenter, getScale());
    564610    }
    565611
     
    576622        final int fps = 20;     // animation frames per second
    577623        final int speed = 1500; // milliseconds for full-screen-width pan
    578         if (!newCenter.equals(center)) {
    579             final EastNorth oldCenter = center;
    580             final double distance = newCenter.distance(oldCenter) / scale;
     624        if (!newCenter.equals(getCenter())) {
     625            final EastNorth oldCenter = getCenter();
     626            final double distance = newCenter.distance(oldCenter) / getScale();
    581627            final double milliseconds = distance / getWidth() * speed;
    582628            final double frames = milliseconds * fps / 1000;
     
    601647
    602648    public void zoomManyTimes(double x, double y, int times) {
    603         double oldScale = scale;
     649        double oldScale = getScale();
    604650        double newScale = scaleZoomManyTimes(times);
    605651        zoomToFactor(x, y, newScale / oldScale);
     
    607653
    608654    public void zoomToFactor(double x, double y, double factor) {
    609         double newScale = scale*factor;
    610         // New center position so that point under the mouse pointer stays the same place as it was before zooming
    611         // You will get the formula by simplifying this expression: newCenter = oldCenter + mouseCoordinatesInNewZoom - mouseCoordinatesInOldZoom
    612         zoomTo(new EastNorth(
    613                 center.east() - (x - getWidth()/2.0) * (newScale - scale),
    614                 center.north() + (y - getHeight()/2.0) * (newScale - scale)),
    615                 newScale);
     655        double newScale = getScale()*factor;
     656        EastNorth oldUnderMouse = getState().getForView(x, y).getEastNorth();
     657        MapViewState newState = getState().usingScale(newScale);
     658        newState = newState.movedTo(newState.getForView(x, y), oldUnderMouse);
     659        zoomTo(newState.getCenter().getEastNorth(), newScale);
    616660    }
    617661
    618662    public void zoomToFactor(EastNorth newCenter, double factor) {
    619         zoomTo(newCenter, scale*factor);
     663        zoomTo(newCenter, getScale()*factor);
    620664    }
    621665
    622666    public void zoomToFactor(double factor) {
    623         zoomTo(center, scale*factor);
     667        zoomTo(getCenter(), getScale()*factor);
    624668    }
    625669
     
    714758        if (!zoomUndoBuffer.isEmpty()) {
    715759            ZoomData zoom = zoomUndoBuffer.pop();
    716             zoomRedoBuffer.push(new ZoomData(center, scale));
     760            zoomRedoBuffer.push(new ZoomData(getCenter(), getScale()));
    717761            zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale(), false);
    718762        }
     
    722766        if (!zoomRedoBuffer.isEmpty()) {
    723767            ZoomData zoom = zoomRedoBuffer.pop();
    724             zoomUndoBuffer.push(new ZoomData(center, scale));
     768            zoomUndoBuffer.push(new ZoomData(getCenter(), getScale()));
    725769            zoomNoUndoTo(zoom.getCenterEastNorth(), zoom.getScale(), false);
    726770        }
     
    14431487     */
    14441488    public Projection getProjection() {
    1445         return Main.getProjection();
     1489        return state.getProjection();
    14461490    }
    14471491
     
    14571501     */
    14581502    public int getViewID() {
    1459         String x = center.east() + '_' + center.north() + '_' + scale + '_' +
     1503        String x = getCenter().east() + '_' + getCenter().north() + '_' + getScale() + '_' +
    14601504                getWidth() + '_' + getHeight() + '_' + getProjection().toString();
    14611505        CRC32 id = new CRC32();
  • trunk/src/org/openstreetmap/josm/gui/mappaint/styleelement/BoxTextElement.java

    r10374 r10375  
    261261    }
    262262
    263 
    264263    private static void initDefaultParameters() {
    265264        if (defaultTextColorCache != null) return;
  • trunk/src/org/openstreetmap/josm/tools/bugreport/BugReport.java

    r10300 r10375  
    3131 * @since 10285
    3232 */
    33 public class BugReport {
     33public final class BugReport {
    3434    /**
    3535     * Create a new bug report
    3636     * @param e The {@link ReportedException} to use. No more data should be added after creating the report.
    3737     */
    38     public BugReport(ReportedException e) {
     38    private BugReport(ReportedException e) {
    3939        // TODO: Use this class to create the bug report.
    4040    }
  • trunk/test/unit/org/openstreetmap/josm/gui/MapViewStateTest.java

    r10343 r10375  
    33
    44import static org.junit.Assert.assertEquals;
    5 
    6 import java.awt.Rectangle;
    75
    86import org.junit.Before;
     
    1412import org.openstreetmap.josm.data.coor.LatLon;
    1513import org.openstreetmap.josm.gui.MapViewState.MapViewPoint;
    16 import org.openstreetmap.josm.gui.util.GuiHelper;
    1714
    1815/**
     
    2219public class MapViewStateTest {
    2320
     21    private static final int WIDTH = 300;
    2422    private static final int HEIGHT = 200;
    25     private static final int WIDTH = 300;
    26     private NavigatableComponent component;
    2723    private MapViewState state;
    2824
     
    3632
    3733    /**
    38      * Create a new, fresh {@link NavigatableComponent}
     34     * Create the default state.
    3935     */
    4036    @Before
    4137    public void setUp() {
    42         component = new NavigatableComponent();
    43         component.setBounds(new Rectangle(WIDTH, HEIGHT));
    44         // wait for the event to be propagated.
    45         GuiHelper.runInEDTAndWait(new Runnable() {
    46             @Override
    47             public void run() {
    48             }
    49         });
    50         state = new MapViewState(component);
     38        state = MapViewState.createDefaultState(WIDTH, HEIGHT);
    5139    }
    5240
     
    5947        assertHasViewCoords(WIDTH / 2, HEIGHT / 2, center);
    6048
    61         component.zoomTo(new LatLon(3, 4));
     49        MapViewState newState = state.movedTo(center, new EastNorth(3, 4));
    6250
    6351        // state should not change, but new state should.
     
    6553        assertHasViewCoords(WIDTH / 2, HEIGHT / 2, center);
    6654
    67         center = new MapViewState(component).getCenter();
    68         assertEquals("x", 3, center.getLatLon().lat(), 0.01);
    69         assertEquals("y", 4, center.getLatLon().lon(), 0.01);
     55        center = newState.getCenter();
     56        assertEquals("east", 3, center.getEastNorth().east(), 0.01);
     57        assertEquals("north", 4, center.getEastNorth().north(), 0.01);
    7058    }
    7159
     
    10997    @Test
    11098    public void testPointConversions() {
    111         MapViewPoint p = state.getForView(50, 70);
    112         assertHasViewCoords(50, 70, p);
     99        MapViewPoint p = state.getForView(WIDTH / 2, HEIGHT / 2);
     100        assertHasViewCoords(WIDTH / 2, HEIGHT / 2, p);
     101
    113102
    114103        EastNorth eastnorth = p.getEastNorth();
    115         EastNorth shouldEastNorth = component.getEastNorth(50, 70);
     104        LatLon shouldLatLon = Main.getProjection().getWorldBoundsLatLon().getCenter();
     105        EastNorth shouldEastNorth = Main.getProjection().latlon2eastNorth(shouldLatLon);
    116106        assertEquals("east", shouldEastNorth.east(), eastnorth.east(), 0.01);
    117107        assertEquals("north", shouldEastNorth.north(), eastnorth.north(), 0.01);
    118108        MapViewPoint reversed = state.getPointFor(shouldEastNorth);
    119         assertHasViewCoords(50, 70, reversed);
     109        assertHasViewCoords(WIDTH / 2, HEIGHT / 2, reversed);
    120110
    121111        LatLon latlon = p.getLatLon();
    122         LatLon shouldLatLon = Main.getProjection().eastNorth2latlon(shouldEastNorth);
    123112        assertEquals("lat", shouldLatLon.lat(), latlon.lat(), 0.01);
    124113        assertEquals("lon", shouldLatLon.lon(), latlon.lon(), 0.01);
  • trunk/test/unit/org/openstreetmap/josm/gui/NavigatableComponentTest.java

    r9958 r10375  
    55import static org.junit.Assert.assertThat;
    66
     7import java.awt.Point;
    78import java.awt.Rectangle;
    89import java.awt.geom.Point2D;
     10
     11import javax.swing.JFrame;
    912
    1013import org.CustomMatchers;
     
    4447    @Before
    4548    public void setUp() {
    46         component = new NavigatableComponent();
     49        component = new NavigatableComponent() {
     50            @Override
     51            public Point getLocationOnScreen() {
     52                return new Point(30, 40);
     53            }
     54        };
    4755        component.setBounds(new Rectangle(WIDTH, HEIGHT));
    4856        // wait for the event to be propagated.
     
    5260            }
    5361        });
     62        component.setVisible(true);
     63        JFrame window = new JFrame();
     64        window.add(component);
     65        component.updateLocationState();
    5466    }
    5567
Note: See TracChangeset for help on using the changeset viewer.