Changeset 12495 in josm for trunk


Ignore:
Timestamp:
2017-07-23T01:09:45+02:00 (7 years ago)
Author:
Don-vip
Message:

fix #5869 - Download dialog, bookmarks: add "home location" bookmark (if set in OSM user settings) + last 15 changesets bookmarks

Location:
trunk/src/org/openstreetmap/josm
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Changeset.java

    r12494 r12495  
    2323 * @since 625
    2424 */
    25 public final class Changeset implements Tagged {
     25public final class Changeset implements Tagged, Comparable<Changeset> {
    2626
    2727    /** The maximum changeset tag length allowed by API 0.6 **/
     
    120120     *         a value greater than {@code 0} if {@code getId() > other.getId()}
    121121     */
     122    @Override
    122123    public int compareTo(Changeset other) {
    123124        return Integer.compare(getId(), other.getId());
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManager.java

    r11713 r12495  
    635635        public void actionPerformed(ActionEvent e) {
    636636            Window parent = GuiHelper.getWindowAncestorFor(e);
    637             JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
    638             if (im.isAnonymous()) {
     637            try {
     638                ChangesetQuery query = ChangesetQuery.forCurrentUser();
     639                if (!GraphicsEnvironment.isHeadless()) {
     640                    ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query));
     641                }
     642            } catch (IllegalStateException ex) {
    639643                alertAnonymousUser(parent);
    640                 return;
    641             }
    642             ChangesetQuery query = new ChangesetQuery();
    643             if (im.isFullyIdentified()) {
    644                 query = query.forUser(im.getUserId());
    645             } else {
    646                 query = query.forUser(im.getUserName());
    647             }
    648             if (!GraphicsEnvironment.isHeadless()) {
    649                 ChangesetCacheManager.getInstance().runDownloadTask(new ChangesetQueryTask(parent, query));
     644                Main.trace(ex);
    650645            }
    651646        }
     
    695690
    696691    /**
     692     * Returns the changeset cache model.
     693     * @return the changeset cache model
     694     * @since 12495
     695     */
     696    public ChangesetCacheManagerModel getModel() {
     697        return model;
     698    }
     699
     700    /**
    697701     * Selects the changesets  in <code>changests</code>, provided the
    698702     * respective changesets are already present in the local changeset cache.
  • trunk/src/org/openstreetmap/josm/gui/dialogs/changeset/ChangesetCacheManagerModel.java

    r12297 r12495  
    139139
    140140    @Override
    141     public Object getValueAt(int row, int column) {
     141    public Changeset getValueAt(int row, int column) {
    142142        return data.get(row);
    143143    }
  • trunk/src/org/openstreetmap/josm/gui/download/BookmarkList.java

    r10627 r12495  
    55
    66import java.awt.Component;
     7import java.awt.GraphicsEnvironment;
    78import java.util.ArrayList;
    89import java.util.Arrays;
    910import java.util.Collection;
    1011import java.util.Collections;
     12import java.util.Comparator;
    1113import java.util.LinkedList;
    1214import java.util.List;
     
    1517
    1618import javax.swing.DefaultListModel;
     19import javax.swing.ImageIcon;
    1720import javax.swing.JLabel;
    1821import javax.swing.JList;
     
    2124
    2225import org.openstreetmap.josm.Main;
     26import org.openstreetmap.josm.actions.downloadtasks.ChangesetQueryTask;
    2327import org.openstreetmap.josm.data.Bounds;
     28import org.openstreetmap.josm.data.coor.LatLon;
     29import org.openstreetmap.josm.data.osm.Changeset;
     30import org.openstreetmap.josm.data.osm.UserInfo;
     31import org.openstreetmap.josm.data.preferences.IntegerProperty;
     32import org.openstreetmap.josm.data.projection.Projection;
     33import org.openstreetmap.josm.data.projection.Projections;
     34import org.openstreetmap.josm.gui.JosmUserIdentityManager;
     35import org.openstreetmap.josm.gui.MapViewState;
     36import org.openstreetmap.josm.gui.dialogs.changeset.ChangesetCacheManager;
     37import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
     38import org.openstreetmap.josm.gui.util.GuiHelper;
     39import org.openstreetmap.josm.io.ChangesetQuery;
    2440import org.openstreetmap.josm.tools.ImageProvider;
     41import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    2542
    2643/**
     
    3148
    3249    /**
     50     * The maximum number of changeset bookmarks to maintain in list.
     51     * @since 12495
     52     */
     53    public static final IntegerProperty MAX_CHANGESET_BOOKMARKS = new IntegerProperty("bookmarks.changesets.max-entries", 15);
     54
     55    /**
    3356     * Class holding one bookmarkentry.
    3457     */
     
    3659        private String name;
    3760        private Bounds area;
     61        private ImageIcon icon;
    3862
    3963        /**
     
    4872            if (array.size() < 5)
    4973                throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
     74            icon = ImageProvider.get("dialogs", "bookmark");
    5075            name = array.get(0);
    5176            area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
     
    5782         */
    5883        public Bookmark() {
    59             area = null;
    60             name = null;
     84            this(null, null);
    6185        }
    6286
     
    6690         */
    6791        public Bookmark(Bounds area) {
     92            this(null, area);
     93        }
     94
     95        /**
     96         * Constructs a new {@code Bookmark} for the given name and area.
     97         * @param name The bookmark name
     98         * @param area The bookmark area
     99         * @since 12495
     100         */
     101        protected Bookmark(String name, Bounds area) {
     102            this.icon = ImageProvider.get("dialogs", "bookmark");
     103            this.name = name;
    68104            this.area = area;
    69105        }
    70106
    71         @Override public String toString() {
     107        @Override
     108        public String toString() {
    72109            return name;
    73110        }
     
    89126            Bookmark bookmark = (Bookmark) obj;
    90127            return Objects.equals(name, bookmark.name) &&
    91                     Objects.equals(area, bookmark.area);
     128                   Objects.equals(area, bookmark.area);
    92129        }
    93130
     
    122159        public void setArea(Bounds area) {
    123160            this.area = area;
     161        }
     162
     163        /**
     164         * Returns the bookmark icon.
     165         * @return the bookmark icon
     166         * @since 12495
     167         */
     168        public ImageIcon getIcon() {
     169            return icon;
     170        }
     171
     172        /**
     173         * Sets the bookmark icon.
     174         * @param icon the bookmark icon
     175         * @since 12495
     176         */
     177        public void setIcon(ImageIcon icon) {
     178            this.icon = icon;
     179        }
     180    }
     181
     182    /**
     183     * A specific optional bookmark for the "home location" configured on osm.org website.
     184     * @since 12495
     185     */
     186    public static class HomeLocationBookmark extends Bookmark {
     187        /**
     188         * Constructs a new {@code HomeLocationBookmark}.
     189         */
     190        public HomeLocationBookmark() {
     191            setName(tr("Home location"));
     192            setIcon(ImageProvider.get("help", "home", ImageSizes.SMALLICON));
     193            UserInfo info = JosmUserIdentityManager.getInstance().getUserInfo();
     194            if (info == null) {
     195                throw new IllegalStateException("User not identified");
     196            }
     197            LatLon home = info.getHome();
     198            if (home == null) {
     199                throw new IllegalStateException("User home location not set");
     200            }
     201            int zoom = info.getHomeZoom();
     202            if (zoom <= 3) {
     203                // 3 is the default zoom level in OSM database, but the real zoom level was not correct
     204                // for a long time, see https://github.com/openstreetmap/openstreetmap-website/issues/1592
     205                zoom = 12;
     206            }
     207            Projection mercator = Projections.getProjectionByCode("EPSG:3857");
     208            setArea(MapViewState.createDefaultState(430, 400) // Size of map on osm.org user profile settings
     209                    .usingProjection(mercator)
     210                    .usingScale(Selector.GeneralSelector.level2scale(zoom) / 100)
     211                    .usingCenter(mercator.latlon2eastNorth(home))
     212                    .getViewArea()
     213                    .getLatLonBoundsBox());
     214        }
     215    }
     216
     217    /**
     218     * A specific optional bookmark for the boundaries of recent changesets.
     219     * @since 12495
     220     */
     221    public static class ChangesetBookmark extends Bookmark {
     222        /**
     223         * Constructs a new {@code ChangesetBookmark}.
     224         * @param cs changeset
     225         */
     226        public ChangesetBookmark(Changeset cs) {
     227            setName(String.format("%d - %tF - %s", cs.getId(), cs.getCreatedAt(), cs.getComment()));
     228            setIcon(ImageProvider.get("data", "changeset", ImageSizes.SMALLICON));
     229            setArea(cs.getBounds());
    124230        }
    125231    }
     
    136242
    137243    /**
    138      * Loads the bookmarks from file.
     244     * Loads the home location bookmark from OSM API,
     245     *       the manual bookmarks from preferences file,
     246     *       the changeset bookmarks from changeset cache.
    139247     */
    140248    public final void load() {
    141         DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel();
     249        final DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel();
    142250        model.removeAllElements();
     251        JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
     252        // Add home location bookmark first, if user fully identified
     253        if (im.isFullyIdentified()) {
     254            try {
     255                model.addElement(new HomeLocationBookmark());
     256            } catch (IllegalStateException e) {
     257                Main.info(e.getMessage());
     258                Main.trace(e);
     259            }
     260        }
     261        // Then add manual bookmarks previously saved in local preferences
    143262        Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
    144263        if (args != null) {
     
    156275            }
    157276        }
    158     }
    159 
    160     /**
    161      * Saves all bookmarks to the preferences file
     277        // Finally add recent changeset bookmarks, if user name is known
     278        final int n = MAX_CHANGESET_BOOKMARKS.get();
     279        if (n > 0 && !im.isAnonymous()) {
     280            final ChangesetCacheManager ccm = ChangesetCacheManager.getInstance();
     281            final int userId = im.getUserInfo().getId();
     282            int found = 0;
     283            for (int i = 0; i < ccm.getModel().getRowCount() && found < n; i++) {
     284                Changeset cs = ccm.getModel().getValueAt(i, 0);
     285                if (cs.getUser().getId() == userId && cs.getBounds() != null) {
     286                    model.addElement(new ChangesetBookmark(cs));
     287                    found++;
     288                }
     289            }
     290        }
     291    }
     292
     293    /**
     294     * Saves all manual bookmarks to the preferences file.
    162295     */
    163296    public final void save() {
    164297        List<Collection<String>> coll = new LinkedList<>();
    165298        for (Object o : ((DefaultListModel<Bookmark>) getModel()).toArray()) {
     299            if (o instanceof HomeLocationBookmark || o instanceof ChangesetBookmark) {
     300                continue;
     301            }
    166302            String[] array = new String[5];
    167303            Bookmark b = (Bookmark) o;
     
    177313    }
    178314
     315    /**
     316     * Refreshes the changeset bookmarks.
     317     * @since 12495
     318     */
     319    public void refreshChangesetBookmarks() {
     320        final int n = MAX_CHANGESET_BOOKMARKS.get();
     321        if (n > 0) {
     322            final DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel();
     323            for (int i = model.getSize() - 1; i >= 0; i--) {
     324                if (model.get(i) instanceof ChangesetBookmark) {
     325                    model.remove(i);
     326                }
     327            }
     328            ChangesetQuery query = ChangesetQuery.forCurrentUser();
     329            if (!GraphicsEnvironment.isHeadless()) {
     330                final ChangesetQueryTask task = new ChangesetQueryTask(this, query);
     331                ChangesetCacheManager.getInstance().runDownloadTask(task);
     332                Main.worker.submit(() -> {
     333                    if (task.isCanceled() || task.isFailed())
     334                        return;
     335                    GuiHelper.runInEDT(() -> task.getDownloadedData().stream()
     336                            .filter(cs -> cs.getBounds() != null)
     337                            .sorted(Comparator.reverseOrder())
     338                            .limit(n)
     339                            .forEachOrdered(cs -> model.addElement(new ChangesetBookmark(cs))));
     340                });
     341            }
     342        }
     343    }
     344
    179345    static class BookmarkCellRenderer extends JLabel implements ListCellRenderer<BookmarkList.Bookmark> {
    180346
     
    184350        BookmarkCellRenderer() {
    185351            setOpaque(true);
    186             setIcon(ImageProvider.get("dialogs", "bookmark"));
    187352        }
    188353
     
    200365            Bounds area = b.getArea();
    201366            StringBuilder sb = new StringBuilder(128);
    202             sb.append("<html>min[latitude,longitude]=<strong>[")
    203               .append(area.getMinLat()).append(',').append(area.getMinLon()).append("]</strong>"+
    204                       "<br>max[latitude,longitude]=<strong>[")
    205               .append(area.getMaxLat()).append(',').append(area.getMaxLon()).append("]</strong>"+
    206                       "</html>");
     367            if (area != null) {
     368                sb.append("<html>min[latitude,longitude]=<strong>[")
     369                  .append(area.getMinLat()).append(',').append(area.getMinLon()).append("]</strong>"+
     370                          "<br>max[latitude,longitude]=<strong>[")
     371                  .append(area.getMaxLat()).append(',').append(area.getMaxLon()).append("]</strong>"+
     372                          "</html>");
     373            }
    207374            return sb.toString();
    208375        }
     
    212379                boolean cellHasFocus) {
    213380            renderColor(isSelected);
     381            setIcon(value.getIcon());
    214382            setText(value.getName());
    215383            setToolTipText(buildToolTipText(value));
  • trunk/src/org/openstreetmap/josm/gui/download/BookmarkSelection.java

    r10634 r12495  
    2424import org.openstreetmap.josm.Main;
    2525import org.openstreetmap.josm.data.Bounds;
     26import org.openstreetmap.josm.gui.JosmUserIdentityManager;
    2627import org.openstreetmap.josm.gui.download.BookmarkList.Bookmark;
    2728import org.openstreetmap.josm.gui.widgets.JMultilineLabel;
     
    6970        bookmarks.addListSelectionListener(renameAction);
    7071        pnl.add(new JButton(renameAction), gc);
     72
     73        gc.gridy = 2;
     74        RefreshAction refreshAction = new RefreshAction();
     75        pnl.add(new JButton(refreshAction), gc);
    7176
    7277        gc.fill = GridBagConstraints.BOTH;
     
    278283    }
    279284
     285    class RefreshAction extends AbstractAction {
     286        /**
     287         * Constructs a new {@code RefreshAction}.
     288         */
     289        RefreshAction() {
     290            putValue(SMALL_ICON, ImageProvider.get("dialogs/changeset", "downloadchangeset"));
     291            putValue(SHORT_DESCRIPTION, tr("Download bookmarks for my {0} last changesets", BookmarkList.MAX_CHANGESET_BOOKMARKS.get()));
     292            setEnabled(!JosmUserIdentityManager.getInstance().isAnonymous());
     293        }
     294
     295        @Override
     296        public void actionPerformed(ActionEvent e) {
     297            bookmarks.refreshChangesetBookmarks();
     298        }
     299    }
     300
    280301    class DoubleClickAdapter extends MouseAdapter {
    281302        @Override
  • trunk/src/org/openstreetmap/josm/io/ChangesetQuery.java

    r12470 r12495  
    1919import org.openstreetmap.josm.data.Bounds;
    2020import org.openstreetmap.josm.data.coor.LatLon;
     21import org.openstreetmap.josm.gui.JosmUserIdentityManager;
    2122import org.openstreetmap.josm.tools.CheckParameterUtil;
    2223import org.openstreetmap.josm.tools.Utils;
     
    6162    public static ChangesetQuery buildFromUrlQuery(String query) throws ChangesetQueryUrlException {
    6263        return new ChangesetQueryUrlParser().parse(query);
     64    }
     65
     66    /**
     67     * Replies a changeset query object restricted to the current user, if known.
     68     * @return a changeset query object restricted to the current user, if known
     69     * @throws IllegalStateException if current user is anonymous
     70     * @since 12495
     71     */
     72    public static ChangesetQuery forCurrentUser() {
     73        JosmUserIdentityManager im = JosmUserIdentityManager.getInstance();
     74        if (im.isAnonymous()) {
     75            throw new IllegalStateException("anonymous user");
     76        }
     77        ChangesetQuery query = new ChangesetQuery();
     78        if (im.isFullyIdentified()) {
     79            return query.forUser(im.getUserId());
     80        } else {
     81            return query.forUser(im.getUserName());
     82        }
    6383    }
    6484
  • trunk/src/org/openstreetmap/josm/io/OsmChangesetParser.java

    r10212 r12495  
    153153                    throwException(tr("Illegal value for attribute ''{0}''. Got ''{1}''.", "max_lat", maxLatStr));
    154154                }
    155                 current.setMax(new LatLon(maxLon, maxLat));
     155                current.setMax(new LatLon(maxLat, maxLon));
    156156            }
    157157
Note: See TracChangeset for help on using the changeset viewer.