Changeset 13243 in josm


Ignore:
Timestamp:
2017-12-26T17:50:55+01:00 (7 years ago)
Author:
michael2402
Message:

See #15310: Replace use of dx/dy in bookmark loading/storing code.

Location:
trunk
Files:
6 edited

Legend:

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

    r13050 r13243  
    3333import org.openstreetmap.josm.gui.MapView;
    3434import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
    35 import org.openstreetmap.josm.gui.layer.imagery.TileSourceDisplaySettings;
    3635import org.openstreetmap.josm.gui.util.WindowGeometry;
    3736import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
     
    8685                layer.getInfo().getName(),
    8786                null,
    88                 curOff.east(), curOff.north(), center.lon(), center.lat());
     87                curOff, center);
    8988        layer.getDisplaySettings().setOffsetBookmark(tempOffset);
    9089        addListeners();
     
    293292                // US locale to force decimal separator to be '.'
    294293                try (Formatter us = new Formatter(Locale.US)) {
    295                     TileSourceDisplaySettings ds = layer.getDisplaySettings();
     294                    EastNorth displacement = layer.getDisplaySettings().getDisplacement();
    296295                    tOffset.setText(us.format(new StringBuilder()
    297296                        .append("%1.").append(precision).append("f; %1.").append(precision).append('f').toString(),
    298                         ds.getDx(), ds.getDy()).toString());
     297                        displacement.east(), displacement.north()).toString());
    299298                }
    300299            }
  • trunk/src/org/openstreetmap/josm/data/imagery/OffsetBookmark.java

    r12851 r13243  
    1616import org.openstreetmap.josm.data.StructUtils.WriteExplicitly;
    1717import org.openstreetmap.josm.data.coor.EastNorth;
     18import org.openstreetmap.josm.data.coor.ILatLon;
    1819import org.openstreetmap.josm.data.coor.LatLon;
    1920import org.openstreetmap.josm.data.projection.Projection;
     
    4041    @StructEntry private double center_lon, center_lat;
    4142
     43    /**
     44     * Test if an image is usable for the given imagery layer.
     45     * @param layer The layer to use the image at
     46     * @return <code>true</code> if it is usable on the projection of the layer and the imagery name matches.
     47     */
    4248    public boolean isUsable(ImageryLayer layer) {
    4349        if (projection_code == null) return false;
     
    5561    }
    5662
     63    /**
     64     * Create a new {@link OffsetBookmark} object using (0, 0) as center
     65     * <p>
     66     * The use of the {@link #OffsetBookmark(String, String, String, EastNorth, ILatLon)} constructor is preferred.
     67     * @param projectionCode The projection for which this object was created
     68     * @param imageryName The name of the imagery on the layer
     69     * @param name The name of the new bookmark
     70     * @param dx The x displacement
     71     * @param dy The y displacement
     72     */
    5773    public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy) {
    5874        this(projectionCode, imageryName, name, dx, dy, 0, 0);
    5975    }
    6076
     77    /**
     78     * Create a new {@link OffsetBookmark} object
     79     * @param projectionCode The projection for which this object was created
     80     * @param imageryName The name of the imagery on the layer
     81     * @param name The name of the new bookmark
     82     * @param displacement The displacement in east/north space.
     83     * @param center The point on earth that was used as reference to align the image.
     84     * @since 13243
     85     */
     86    public OffsetBookmark(String projectionCode, String imageryName, String name, EastNorth displacement, ILatLon center) {
     87        this(projectionCode, imageryName, name, displacement.east(), displacement.north(), center.lon(), center.lat());
     88    }
     89
     90    /**
     91     * Create a new {@link OffsetBookmark} by specifying all values.
     92     * <p>
     93     * The use of the {@link #OffsetBookmark(String, String, String, EastNorth, ILatLon)} constructor is preferred.
     94     * @param projectionCode The projection for which this object was created
     95     * @param imageryName The name of the imagery on the layer
     96     * @param name The name of the new bookmark
     97     * @param dx The x displacement
     98     * @param dy The y displacement
     99     * @param centerLon The point on earth that was used as reference to align the image.
     100     * @param centerLat The point on earth that was used as reference to align the image.
     101     */
    61102    public OffsetBookmark(String projectionCode, String imageryName, String name, double dx, double dy, double centerLon, double centerLat) {
    62103        this.projection_code = projectionCode;
     
    69110    }
    70111
     112    /**
     113     * Loads an old bookmark. For backward compatibility with settings. Do not use.
     114     * @param list The settings that were read
     115     */
    71116    public OffsetBookmark(Collection<String> list) {
    72117        List<String> array = new ArrayList<>(list);
     
    85130    }
    86131
     132    /**
     133     * Get the projection code for which this bookmark was created.
     134     * @return The projection.
     135     */
    87136    public String getProjectionCode() {
    88137        return projection_code;
    89138    }
    90139
     140    /**
     141     * Get the name of this bookmark. This name can e.g. be displayed in menus.
     142     * @return The name
     143     */
    91144    public String getName() {
    92145        return name;
    93146    }
    94147
     148    /**
     149     * Get the name of the imagery for which this bookmark was created. It is used to match the bookmark to the right layers.
     150     * @return The name
     151     */
    95152    public String getImageryName() {
    96153        return imagery_name;
     
    149206    }
    150207
     208    /**
     209     * Set the projection code for which this bookmark was created
     210     * @param projectionCode The projection
     211     */
    151212    public void setProjectionCode(String projectionCode) {
    152213        this.projection_code = projectionCode;
    153214    }
    154215
     216    /**
     217     * Set the name of the bookmark
     218     * @param name The name
     219     * @see #getName()
     220     */
    155221    public void setName(String name) {
    156222        this.name = name;
    157223    }
    158224
     225    /**
     226     * Sets the name of the imagery
     227     * @param imageryName The name
     228     * @see #getImageryName()
     229     */
    159230    public void setImageryName(String imageryName) {
    160231        this.imagery_name = imageryName;
    161232    }
    162233
     234    /**
     235     * Update the displacement of this imagery.
     236     * @param displacement The displacement
     237     */
    163238    public void setDisplacement(EastNorth displacement) {
    164239        this.dx = displacement.east();
     
    166241    }
    167242
     243    /**
     244     * Load the global list of bookmarks from preferences.
     245     */
    168246    public static void loadBookmarks() {
    169247        List<OffsetBookmark> bookmarks = StructUtils.getListOfStructs(
     
    184262    }
    185263
     264    /**
     265     * Stores the bookmakrs in the settings.
     266     */
    186267    public static void saveBookmarks() {
    187268        StructUtils.putListOfStructs(Config.getPref(), "imagery.offsetbookmarks", allBookmarks, OffsetBookmark.class);
     
    238319    }
    239320
     321    /**
     322     * Gets a bookmark that is usable on the given layer by it's name.
     323     * @param layer The layer to use the bookmark at
     324     * @param name The name of the bookmark
     325     * @return The bookmark if found, <code>null</code> if not.
     326     */
    240327    public static OffsetBookmark getBookmarkByName(ImageryLayer layer, String name) {
    241328        for (OffsetBookmark b : allBookmarks) {
     
    246333    }
    247334
    248     public static void bookmarkOffset(String name, AbstractTileSourceLayer layer) {
     335    /**
     336     * Add a bookmark for the displacement of that layer
     337     * @param name The bookmark name
     338     * @param layer The layer to store the bookmark for
     339     */
     340    public static void bookmarkOffset(String name, AbstractTileSourceLayer<?> layer) {
    249341        LatLon center;
    250342        if (MainApplication.isDisplayingMapView()) {
     
    255347        OffsetBookmark nb = new OffsetBookmark(
    256348                Main.getProjection().toCode(), layer.getInfo().getName(),
    257                 name, layer.getDisplaySettings().getDx(), layer.getDisplaySettings().getDy(), center.lon(), center.lat());
     349                name, layer.getDisplaySettings().getDisplacement(), center);
    258350        for (ListIterator<OffsetBookmark> it = allBookmarks.listIterator(); it.hasNext();) {
    259351            OffsetBookmark b = it.next();
  • trunk/src/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayer.java

    r13222 r13243  
    660660            break;
    661661        default:
    662             // trigger a redraw just to be sure.
     662            // e.g. displacement
     663            // trigger a redraw in every case
    663664            invalidate();
    664665        }
     
    19221923        }
    19231924    }
     1925
     1926    @Override
     1927    protected List<OffsetMenuEntry> getOffsetMenuEntries() {
     1928        return OffsetBookmark.getBookmarks()
     1929            .stream()
     1930            .filter(b -> b.isUsable(this))
     1931            .map(OffsetMenuBookmarkEntry::new)
     1932            .collect(Collectors.toList());
     1933    }
     1934
     1935    /**
     1936     * An entry for a bookmark in the offset menu.
     1937     * @author Michael Zangl
     1938     */
     1939    private class OffsetMenuBookmarkEntry implements OffsetMenuEntry {
     1940        private final OffsetBookmark bookmark;
     1941
     1942        OffsetMenuBookmarkEntry(OffsetBookmark bookmark) {
     1943            this.bookmark = bookmark;
     1944
     1945        }
     1946
     1947        @Override
     1948        public String getLabel() {
     1949            return bookmark.getName();
     1950        }
     1951
     1952        @Override
     1953        public boolean isActive() {
     1954            EastNorth offset = bookmark.getDisplacement(Main.getProjection());
     1955            EastNorth active = getDisplaySettings().getDisplacement();
     1956            return Utils.equalsEpsilon(offset.east(), active.east()) && Utils.equalsEpsilon(offset.north(), active.north());
     1957        }
     1958
     1959        @Override
     1960        public void actionPerformed() {
     1961            getDisplaySettings().setOffsetBookmark(bookmark);
     1962        }
     1963    }
    19241964}
  • trunk/src/org/openstreetmap/josm/gui/layer/ImageryLayer.java

    r13214 r13243  
    3131import org.openstreetmap.josm.Main;
    3232import org.openstreetmap.josm.data.ProjectionBounds;
    33 import org.openstreetmap.josm.data.coor.EastNorth;
    3433import org.openstreetmap.josm.data.imagery.ImageryInfo;
    3534import org.openstreetmap.josm.data.imagery.OffsetBookmark;
     
    4544import org.openstreetmap.josm.tools.ImageProvider;
    4645import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    47 import org.openstreetmap.josm.tools.Utils;
    4846
    4947/**
     
    5452public abstract class ImageryLayer extends Layer {
    5553
     54    /**
     55     * The default value for the sharpen filter for each imagery layer.
     56     */
    5657    public static final IntegerProperty PROP_SHARPEN_LEVEL = new IntegerProperty("imagery.sharpen_level", 0);
    5758
     
    187188    }
    188189
     190    /**
     191     * Create a new imagery layer
     192     * @param info The imagery info to use as base
     193     * @return The created layer
     194     */
    189195    public static ImageryLayer create(ImageryInfo info) {
    190196        switch(info.getImageryType()) {
     
    203209
    204210    class ApplyOffsetAction extends AbstractAction {
    205         private final transient OffsetBookmark b;
    206 
    207         ApplyOffsetAction(OffsetBookmark b) {
    208             super(b.getName());
    209             this.b = b;
     211        private final transient OffsetMenuEntry menuEntry;
     212
     213        ApplyOffsetAction(OffsetMenuEntry menuEntry) {
     214            super(menuEntry.getLabel());
     215            this.menuEntry = menuEntry;
    210216        }
    211217
    212218        @Override
    213219        public void actionPerformed(ActionEvent ev) {
    214             setOffset(b);
     220            menuEntry.actionPerformed();
     221            //TODO: Use some form of listeners for this.
    215222            MainApplication.getMenu().imageryMenu.refreshOffsetMenu();
    216             MainApplication.getMap().repaint();
    217223        }
    218224    }
     
    235241    }
    236242
     243    /**
     244     * Create the menu item that should be added to the offset menu.
     245     * It may have a sub menu of e.g. bookmarks added to it.
     246     * @return The menu item to add to the imagery menu.
     247     */
    237248    public JMenuItem getOffsetMenuItem() {
    238249        JMenu subMenu = new JMenu(trc("layer", "Offset"));
     
    241252    }
    242253
     254    /**
     255     * Create the submenu or the menu item to set the offset of the layer.
     256     *
     257     * If only one menu item for this layer exists, it is returned by this method.
     258     *
     259     * If there are multiple, this method appends them to the subMenu and then returns the reference to the subMenu.
     260     * @param subMenu The subMenu to use
     261     * @return A single menu item to adjust the layer or the passed subMenu to which the menu items were appended.
     262     */
    243263    public JComponent getOffsetMenuItem(JComponent subMenu) {
    244264        JMenuItem adjustMenuItem = new JMenuItem(getAdjustAction());
    245         List<OffsetBookmark> allBookmarks = OffsetBookmark.getBookmarks();
    246         if (allBookmarks.isEmpty()) return adjustMenuItem;
     265        List<OffsetMenuEntry> usableBookmarks = getOffsetMenuEntries();
     266        if (usableBookmarks.isEmpty()) {
     267            return adjustMenuItem;
     268        }
    247269
    248270        subMenu.add(adjustMenuItem);
    249271        subMenu.add(new JSeparator());
    250         boolean hasBookmarks = false;
    251272        int menuItemHeight = 0;
    252         for (OffsetBookmark b : allBookmarks) {
    253             if (!b.isUsable(this)) {
    254                 continue;
    255             }
     273        for (OffsetMenuEntry b : usableBookmarks) {
    256274            JCheckBoxMenuItem item = new JCheckBoxMenuItem(new ApplyOffsetAction(b));
    257             EastNorth offset = b.getDisplacement(Main.getProjection());
    258             if (Utils.equalsEpsilon(offset.east(), getDx()) && Utils.equalsEpsilon(offset.north(), getDy())) {
    259                 item.setSelected(true);
    260             }
     275            item.setSelected(b.isActive());
    261276            subMenu.add(item);
    262277            menuItemHeight = item.getPreferredSize().height;
    263             hasBookmarks = true;
    264278        }
    265279        if (menuItemHeight > 0) {
     
    270284            }
    271285        }
    272         return hasBookmarks ? subMenu : adjustMenuItem;
     286        return subMenu;
    273287    }
    274288
    275289    protected abstract Action getAdjustAction();
     290
     291    protected abstract List<OffsetMenuEntry> getOffsetMenuEntries();
    276292
    277293    /**
     
    340356    }
    341357
     358    /**
     359     * An additional menu entry in the imagery offset menu.
     360     * @author Michael Zangl
     361     * @since 13243
     362     * @see ImageryLayer#getOffsetMenuEntries()
     363     */
     364    public static interface OffsetMenuEntry {
     365        /**
     366         * Get the label to use for this menu item
     367         * @return The label to display in the menu.
     368         */
     369        String getLabel();
     370
     371        /**
     372         * Test whether this bookmark is currently active
     373         * @return <code>true</code> if it is active
     374         */
     375        boolean isActive();
     376
     377        /**
     378         * Load this bookmark
     379         */
     380        void actionPerformed();
     381    }
     382
    342383    @Override
    343384    public String toString() {
  • trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileSourceDisplaySettings.java

    r12846 r13243  
    171171     * @return The displacement.
    172172     * @since 10571
     173     * @see #getDisplacement()
    173174     */
    174175    public double getDx() {
    175         return displacement.east();
     176        return getDisplacement().east();
    176177    }
    177178
     
    180181     * @return The displacement.
    181182     * @since 10571
     183     * @see #getDisplacement()
    182184     */
    183185    public double getDy() {
    184         return displacement.north();
     186        return getDisplacement().north();
    185187    }
    186188
     
    195197
    196198    /**
    197      * Sets an offset bookmark to use.
     199     * Sets an offset bookmark to use. Loads the displacement from the bookmark.
    198200     *
    199201     * @param offsetBookmark the offset bookmark, may be null
  • trunk/test/unit/org/openstreetmap/josm/io/session/SessionReaderTest.java

    r12636 r13243  
    1313import org.junit.Test;
    1414import org.openstreetmap.josm.TestUtils;
     15import org.openstreetmap.josm.data.coor.EastNorth;
    1516import org.openstreetmap.josm.gui.MainApplication;
    1617import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
     
    129130        final AbstractTileSourceLayer<?> image = (AbstractTileSourceLayer<?>) layers.get(0);
    130131        assertEquals("Bing aerial imagery", image.getName());
    131         assertEquals(-2.671667778864503, image.getDisplaySettings().getDx(), 1e-9);
    132         assertEquals(13.89643478114158, image.getDisplaySettings().getDy(), 1e-9);
     132        EastNorth displacement = image.getDisplaySettings().getDisplacement();
     133        assertEquals(-2.671667778864503, displacement.east(), 1e-9);
     134        assertEquals(13.89643478114158, displacement.north(), 1e-9);
    133135    }
    134136
Note: See TracChangeset for help on using the changeset viewer.