Ticket #5869: home_bookmark.patch
File home_bookmark.patch, 7.1 KB (added by , 7 years ago) |
---|
-
src/org/openstreetmap/josm/gui/download/BookmarkList.java
14 14 import java.util.Objects; 15 15 16 16 import javax.swing.DefaultListModel; 17 import javax.swing.ImageIcon; 17 18 import javax.swing.JLabel; 18 19 import javax.swing.JList; 19 20 import javax.swing.ListCellRenderer; … … 21 22 22 23 import org.openstreetmap.josm.Main; 23 24 import org.openstreetmap.josm.data.Bounds; 25 import org.openstreetmap.josm.data.coor.LatLon; 26 import org.openstreetmap.josm.data.osm.UserInfo; 27 import org.openstreetmap.josm.data.projection.Projection; 28 import org.openstreetmap.josm.data.projection.Projections; 29 import org.openstreetmap.josm.gui.JosmUserIdentityManager; 30 import org.openstreetmap.josm.gui.MapViewState; 31 import org.openstreetmap.josm.gui.mappaint.mapcss.Selector; 24 32 import org.openstreetmap.josm.tools.ImageProvider; 33 import org.openstreetmap.josm.tools.ImageProvider.ImageSizes; 25 34 26 35 /** 27 36 * List class that read and save its content from the bookmark file. … … 35 44 public static class Bookmark implements Comparable<Bookmark> { 36 45 private String name; 37 46 private Bounds area; 47 private ImageIcon icon; 38 48 39 49 /** 40 50 * Constructs a new {@code Bookmark} with the given contents. … … 47 57 List<String> array = new ArrayList<>(list); 48 58 if (array.size() < 5) 49 59 throw new IllegalArgumentException(tr("Wrong number of arguments for bookmark")); 60 icon = ImageProvider.get("dialogs", "bookmark"); 50 61 name = array.get(0); 51 62 area = new Bounds(Double.parseDouble(array.get(1)), Double.parseDouble(array.get(2)), 52 63 Double.parseDouble(array.get(3)), Double.parseDouble(array.get(4))); … … 56 67 * Constructs a new empty {@code Bookmark}. 57 68 */ 58 69 public Bookmark() { 59 area = null; 60 name = null; 70 this(null, null); 61 71 } 62 72 63 73 /** … … 65 75 * @param area The bookmark area 66 76 */ 67 77 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; 68 90 this.area = area; 69 91 } 70 92 71 @Override public String toString() { 93 @Override 94 public String toString() { 72 95 return name; 73 96 } 74 97 … … 88 111 if (obj == null || getClass() != obj.getClass()) return false; 89 112 Bookmark bookmark = (Bookmark) obj; 90 113 return Objects.equals(name, bookmark.name) && 91 114 Objects.equals(area, bookmark.area); 92 115 } 93 116 94 117 /** … … 122 145 public void setArea(Bounds area) { 123 146 this.area = area; 124 147 } 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 } 125 166 } 126 167 127 168 /** 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 /** 128 199 * Creates a bookmark list as well as the Buttons add and remove. 129 200 */ 130 201 public BookmarkList() { … … 135 206 } 136 207 137 208 /** 138 * Loads the bookmarks from file.209 * Loads the manual bookmarks from preferences file, and the home location bookmark from OSM API. 139 210 */ 140 211 public final void load() { 141 212 DefaultListModel<Bookmark> model = (DefaultListModel<Bookmark>) getModel(); 142 213 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 } 143 222 Collection<Collection<String>> args = Main.pref.getArray("bookmarks", null); 144 223 if (args != null) { 145 224 List<Bookmark> bookmarks = new LinkedList<>(); … … 158 237 } 159 238 160 239 /** 161 * Saves all bookmarks to the preferences file240 * Saves all manual bookmarks to the preferences file. 162 241 */ 163 242 public final void save() { 164 243 List<Collection<String>> coll = new LinkedList<>(); 165 244 for (Object o : ((DefaultListModel<Bookmark>) getModel()).toArray()) { 245 if (o instanceof HomeLocationBookmark) { 246 continue; 247 } 166 248 String[] array = new String[5]; 167 249 Bookmark b = (Bookmark) o; 168 250 array[0] = b.getName(); … … 183 265 */ 184 266 BookmarkCellRenderer() { 185 267 setOpaque(true); 186 setIcon(ImageProvider.get("dialogs", "bookmark"));187 268 } 188 269 189 270 protected void renderColor(boolean selected) { … … 211 292 public Component getListCellRendererComponent(JList<? extends Bookmark> list, Bookmark value, int index, boolean isSelected, 212 293 boolean cellHasFocus) { 213 294 renderColor(isSelected); 295 setIcon(value.getIcon()); 214 296 setText(value.getName()); 215 297 setToolTipText(buildToolTipText(value)); 216 298 return this;