Ticket #5869: home_bookmark.patch

File home_bookmark.patch, 7.1 KB (added by Don-vip, 7 years ago)
  • src/org/openstreetmap/josm/gui/download/BookmarkList.java

     
    1414import java.util.Objects;
    1515
    1616import javax.swing.DefaultListModel;
     17import javax.swing.ImageIcon;
    1718import javax.swing.JLabel;
    1819import javax.swing.JList;
    1920import javax.swing.ListCellRenderer;
     
    2122
    2223import org.openstreetmap.josm.Main;
    2324import org.openstreetmap.josm.data.Bounds;
     25import org.openstreetmap.josm.data.coor.LatLon;
     26import org.openstreetmap.josm.data.osm.UserInfo;
     27import org.openstreetmap.josm.data.projection.Projection;
     28import org.openstreetmap.josm.data.projection.Projections;
     29import org.openstreetmap.josm.gui.JosmUserIdentityManager;
     30import org.openstreetmap.josm.gui.MapViewState;
     31import org.openstreetmap.josm.gui.mappaint.mapcss.Selector;
    2432import org.openstreetmap.josm.tools.ImageProvider;
     33import org.openstreetmap.josm.tools.ImageProvider.ImageSizes;
    2534
    2635/**
    2736 * List class that read and save its content from the bookmark file.
     
    3544    public static class Bookmark implements Comparable<Bookmark> {
    3645        private String name;
    3746        private Bounds area;
     47        private ImageIcon icon;
    3848
    3949        /**
    4050         * Constructs a new {@code Bookmark} with the given contents.
     
    4757            List<String> array = new ArrayList<>(list);
    4858            if (array.size() < 5)
    4959                throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark"));
     60            icon = ImageProvider.get("dialogs", "bookmark");
    5061            name = array.get(0);
    5162            area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)),
    5263                              Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4)));
     
    5667         * Constructs a new empty {@code Bookmark}.
    5768         */
    5869        public Bookmark() {
    59             area = null;
    60             name = null;
     70            this(null, null);
    6171        }
    6272
    6373        /**
     
    6575         * @param area The bookmark area
    6676         */
    6777        public Bookmark(Bounds area) {
     78            this(null, area);
     79        }
     80
     81        /**
     82         * Constructs a new {@code Bookmark} for the given name and area.
     83         * @param name The bookmark name
     84         * @param area The bookmark area
     85         * @since xxx
     86         */
     87        protected Bookmark(String name, Bounds area) {
     88            this.icon = ImageProvider.get("dialogs", "bookmark");
     89            this.name = name;
    6890            this.area = area;
    6991        }
    7092
    71         @Override public String toString() {
     93        @Override
     94        public String toString() {
    7295            return name;
    7396        }
    7497
     
    88111            if (obj == null || getClass() != obj.getClass()) return false;
    89112            Bookmark bookmark = (Bookmark) obj;
    90113            return Objects.equals(name, bookmark.name) &&
    91                     Objects.equals(area, bookmark.area);
     114                   Objects.equals(area, bookmark.area);
    92115        }
    93116
    94117        /**
     
    122145        public void setArea(Bounds area) {
    123146            this.area = area;
    124147        }
     148
     149        /**
     150         * Returns the bookmark icon.
     151         * @return the bookmark icon
     152         * @since xxx
     153         */
     154        public ImageIcon getIcon() {
     155            return icon;
     156        }
     157
     158        /**
     159         * Sets the bookmark icon.
     160         * @param icon the bookmark icon
     161         * @since xxx
     162         */
     163        public void setIcon(ImageIcon icon) {
     164            this.icon = icon;
     165        }
    125166    }
    126167
    127168    /**
     169     * A specific optional bookmark for the "home location" configured on osm.org website.
     170     * @since xxx
     171     */
     172    public static class HomeLocationBookmark extends Bookmark {
     173        /**
     174         * Constructs a new {@code HomeLocationBookmark}.
     175         */
     176        public HomeLocationBookmark() {
     177            setName(tr("Home location"));
     178            setIcon(ImageProvider.get("help", "home", ImageSizes.SMALLICON));
     179            final UserInfo info = JosmUserIdentityManager.getInstance().getUserInfo();
     180            if (info == null) {
     181                throw new IllegalStateException("User not identified");
     182            }
     183            final LatLon home = info.getHome();
     184            if (home == null) {
     185                throw new IllegalStateException("User home location not set");
     186            }
     187            final int zoom = info.getHomeZoom();
     188            Projection mercator = Projections.getProjectionByCode("EPSG:3857");
     189            setArea(MapViewState.createDefaultState(430, 400) // Size of map on osm.org user profile settings
     190                    .usingProjection(mercator)
     191                    .usingScale(Selector.GeneralSelector.level2scale(zoom) / 50000)
     192                    .usingCenter(mercator.latlon2eastNorth(home))
     193                    .getViewArea()
     194                    .getLatLonBoundsBox());
     195        }
     196    }
     197
     198    /**
    128199     * Creates a bookmark list as well as the Buttons add and remove.
    129200     */
    130201    public BookmarkList() {
     
    135206    }
    136207
    137208    /**
    138      * Loads the bookmarks from file.
     209     * Loads the manual bookmarks from preferences file, and the home location bookmark from OSM API.
    139210     */
    140211    public final void load() {
    141212        DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel();
    142213        model.removeAllElements();
     214        if (JosmUserIdentityManager.getInstance().isFullyIdentified()) {
     215            try {
     216                model.addElement(new HomeLocationBookmark());
     217            } catch (IllegalStateException e) {
     218                Main.info(e.getMessage());
     219                Main.trace(e);
     220            }
     221        }
    143222        Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null);
    144223        if (args != null) {
    145224            List<Bookmark> bookmarks = new LinkedList<>();
     
    158237    }
    159238
    160239    /**
    161      * Saves all bookmarks to the preferences file
     240     * Saves all manual bookmarks to the preferences file.
    162241     */
    163242    public final void save() {
    164243        List<Collection<String>> coll = new LinkedList<>();
    165244        for (Object o : ((DefaultListModel<Bookmark>) getModel()).toArray()) {
     245            if (o instanceof HomeLocationBookmark) {
     246                continue;
     247            }
    166248            String[] array = new String[5];
    167249            Bookmark b = (Bookmark) o;
    168250            array[0] = b.getName();
     
    183265         */
    184266        BookmarkCellRenderer() {
    185267            setOpaque(true);
    186             setIcon(ImageProvider.get("dialogs", "bookmark"));
    187268        }
    188269
    189270        protected void renderColor(boolean selected) {
     
    211292        public Component getListCellRendererComponent(JList<? extends Bookmark> list, Bookmark value, int index, boolean isSelected,
    212293                boolean cellHasFocus) {
    213294            renderColor(isSelected);
     295            setIcon(value.getIcon());
    214296            setText(value.getName());
    215297            setToolTipText(buildToolTipText(value));
    216298            return this;