1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.imagery;
|
---|
3 |
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Map;
|
---|
6 | import java.util.concurrent.CopyOnWriteArrayList;
|
---|
7 |
|
---|
8 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
---|
9 | import org.openstreetmap.josm.Main;
|
---|
10 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
11 | import org.openstreetmap.josm.data.imagery.OffsetBookmark;
|
---|
12 | import org.openstreetmap.josm.data.preferences.BooleanProperty;
|
---|
13 | import org.openstreetmap.josm.gui.layer.AbstractTileSourceLayer;
|
---|
14 | import org.openstreetmap.josm.io.session.SessionAwareReadApply;
|
---|
15 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
16 | import org.openstreetmap.josm.tools.JosmRuntimeException;
|
---|
17 | import org.openstreetmap.josm.tools.bugreport.BugReport;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * This are the preferences of how to display a {@link TileSource}.
|
---|
21 | * <p>
|
---|
22 | * They have been extracted from the {@link AbstractTileSourceLayer}. Each layer has one set of such settings.
|
---|
23 | * @author michael
|
---|
24 | * @since 10568
|
---|
25 | */
|
---|
26 | public class TileSourceDisplaySettings implements SessionAwareReadApply {
|
---|
27 | /**
|
---|
28 | * A string returned by {@link DisplaySettingsChangeEvent#getChangedSetting()} if auto load was changed.
|
---|
29 | * @see TileSourceDisplaySettings#isAutoLoad()
|
---|
30 | */
|
---|
31 | public static final String AUTO_LOAD = "automatic-downloading";
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * A string returned by {@link DisplaySettingsChangeEvent#getChangedSetting()} if auto zoom was changed.
|
---|
35 | * @see TileSourceDisplaySettings#isAutoZoom()
|
---|
36 | */
|
---|
37 | public static final String AUTO_ZOOM = "automatically-change-resolution";
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * A string returned by {@link DisplaySettingsChangeEvent#getChangedSetting()} if the sow errors property was changed.
|
---|
41 | * @see TileSourceDisplaySettings#isShowErrors()
|
---|
42 | */
|
---|
43 | private static final String SHOW_ERRORS = "show-errors";
|
---|
44 |
|
---|
45 | private static final String DISPLACEMENT = "displacement";
|
---|
46 |
|
---|
47 | private static final String PREFERENCE_PREFIX = "imagery.generic";
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * The default auto load property
|
---|
51 | */
|
---|
52 | public static final BooleanProperty PROP_AUTO_LOAD = new BooleanProperty(PREFERENCE_PREFIX + ".default_autoload", true);
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * The default auto zoom property
|
---|
56 | */
|
---|
57 | public static final BooleanProperty PROP_AUTO_ZOOM = new BooleanProperty(PREFERENCE_PREFIX + ".default_autozoom", true);
|
---|
58 |
|
---|
59 |
|
---|
60 | /** if layers changes automatically, when user zooms in */
|
---|
61 | private boolean autoZoom;
|
---|
62 | /** if layer automatically loads new tiles */
|
---|
63 | private boolean autoLoad;
|
---|
64 | /** if layer should show errors on tiles */
|
---|
65 | private boolean showErrors;
|
---|
66 |
|
---|
67 | private OffsetBookmark offsetBookmark;
|
---|
68 | /**
|
---|
69 | * the displacement (basically caches the displacement from the offsetBookmark
|
---|
70 | * in the current projection)
|
---|
71 | */
|
---|
72 | private EastNorth displacement = EastNorth.ZERO;
|
---|
73 |
|
---|
74 | private final CopyOnWriteArrayList<DisplaySettingsChangeListener> listeners = new CopyOnWriteArrayList<>();
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Create a new {@link TileSourceDisplaySettings}
|
---|
78 | */
|
---|
79 | public TileSourceDisplaySettings() {
|
---|
80 | this(new String[] {PREFERENCE_PREFIX});
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Create a new {@link TileSourceDisplaySettings}
|
---|
85 | * @param preferencePrefix The additional prefix to scan for preferences.
|
---|
86 | */
|
---|
87 | public TileSourceDisplaySettings(String preferencePrefix) {
|
---|
88 | this(PREFERENCE_PREFIX, preferencePrefix);
|
---|
89 | }
|
---|
90 |
|
---|
91 | private TileSourceDisplaySettings(String... prefixes) {
|
---|
92 | autoZoom = getProperty(prefixes, "default_autozoom");
|
---|
93 | autoLoad = getProperty(prefixes, "default_autoload");
|
---|
94 | showErrors = getProperty(prefixes, "default_showerrors");
|
---|
95 | }
|
---|
96 |
|
---|
97 | private static boolean getProperty(String[] prefixes, String name) {
|
---|
98 | // iterate through all values to force the preferences to receive the default value.
|
---|
99 | // we only support a default value of true.
|
---|
100 | boolean value = true;
|
---|
101 | for (String p : prefixes) {
|
---|
102 | String key = p + "." + name;
|
---|
103 | boolean currentValue = Main.pref.getBoolean(key, true);
|
---|
104 | if (!Main.pref.get(key).isEmpty()) {
|
---|
105 | value = currentValue;
|
---|
106 | }
|
---|
107 | }
|
---|
108 | return value;
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Let the layer zoom automatically if the user zooms in
|
---|
113 | * @return auto zoom
|
---|
114 | */
|
---|
115 | public boolean isAutoZoom() {
|
---|
116 | return autoZoom;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Sets the auto zoom property
|
---|
121 | * @param autoZoom {@code true} to let the layer zoom automatically if the user zooms in
|
---|
122 | * @see #isAutoZoom()
|
---|
123 | * @see #AUTO_ZOOM
|
---|
124 | */
|
---|
125 | public void setAutoZoom(boolean autoZoom) {
|
---|
126 | this.autoZoom = autoZoom;
|
---|
127 | fireSettingsChange(AUTO_ZOOM);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Gets if the layer should automatically load new tiles.
|
---|
132 | * @return <code>true</code> if it should
|
---|
133 | */
|
---|
134 | public boolean isAutoLoad() {
|
---|
135 | return autoLoad;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Sets the auto load property
|
---|
140 | * @param autoLoad {@code true} if the layer should automatically load new tiles
|
---|
141 | * @see #isAutoLoad()
|
---|
142 | * @see #AUTO_LOAD
|
---|
143 | */
|
---|
144 | public void setAutoLoad(boolean autoLoad) {
|
---|
145 | this.autoLoad = autoLoad;
|
---|
146 | fireSettingsChange(AUTO_LOAD);
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * If the layer should display the errors it encountered while loading the tiles.
|
---|
151 | * @return <code>true</code> to show errors.
|
---|
152 | */
|
---|
153 | public boolean isShowErrors() {
|
---|
154 | return showErrors;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Sets the show errors property. Fires a change event.
|
---|
159 | * @param showErrors {@code true} if the layer should display the errors it encountered while loading the tiles
|
---|
160 | * @see #isShowErrors()
|
---|
161 | * @see #SHOW_ERRORS
|
---|
162 | */
|
---|
163 | public void setShowErrors(boolean showErrors) {
|
---|
164 | this.showErrors = showErrors;
|
---|
165 | fireSettingsChange(SHOW_ERRORS);
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Gets the displacement in x (east) direction
|
---|
170 | * @return The displacement.
|
---|
171 | * @since 10571
|
---|
172 | */
|
---|
173 | public double getDx() {
|
---|
174 | return displacement.east();
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Gets the displacement in y (north) direction
|
---|
179 | * @return The displacement.
|
---|
180 | * @since 10571
|
---|
181 | */
|
---|
182 | public double getDy() {
|
---|
183 | return displacement.north();
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Gets the displacement of the image
|
---|
188 | * @return The displacement.
|
---|
189 | * @since 10571
|
---|
190 | */
|
---|
191 | public EastNorth getDisplacement() {
|
---|
192 | return displacement;
|
---|
193 | }
|
---|
194 |
|
---|
195 | /**
|
---|
196 | * Sets an offset bookmark to use.
|
---|
197 | *
|
---|
198 | * @param offsetBookmark the offset bookmark, may be null
|
---|
199 | */
|
---|
200 | public void setOffsetBookmark(OffsetBookmark offsetBookmark) {
|
---|
201 | this.offsetBookmark = offsetBookmark;
|
---|
202 | if (offsetBookmark == null) {
|
---|
203 | setDisplacement(EastNorth.ZERO);
|
---|
204 | } else {
|
---|
205 | setDisplacement(offsetBookmark.getDisplacement(Main.getProjection()));
|
---|
206 | }
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Gets the offset bookmark in use.
|
---|
211 | * @return the offset bookmark, may be null
|
---|
212 | */
|
---|
213 | public OffsetBookmark getOffsetBookmark() {
|
---|
214 | return this.offsetBookmark;
|
---|
215 | }
|
---|
216 |
|
---|
217 | private void setDisplacement(EastNorth displacement) {
|
---|
218 | CheckParameterUtil.ensureValidCoordinates(displacement, "displacement");
|
---|
219 | this.displacement = displacement;
|
---|
220 | fireSettingsChange(DISPLACEMENT);
|
---|
221 | }
|
---|
222 |
|
---|
223 | /**
|
---|
224 | * Notifies all listeners that the paint settings have changed
|
---|
225 | * @param changedSetting The setting name
|
---|
226 | */
|
---|
227 | private void fireSettingsChange(String changedSetting) {
|
---|
228 | DisplaySettingsChangeEvent e = new DisplaySettingsChangeEvent(changedSetting);
|
---|
229 | for (DisplaySettingsChangeListener l : listeners) {
|
---|
230 | l.displaySettingsChanged(e);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | /**
|
---|
235 | * Add a listener that listens to display settings changes.
|
---|
236 | * @param l The listener
|
---|
237 | */
|
---|
238 | public void addSettingsChangeListener(DisplaySettingsChangeListener l) {
|
---|
239 | listeners.add(l);
|
---|
240 | }
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Remove a listener that listens to display settings changes.
|
---|
244 | * @param l The listener
|
---|
245 | */
|
---|
246 | public void removeSettingsChangeListener(DisplaySettingsChangeListener l) {
|
---|
247 | listeners.remove(l);
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Stores the current settings object to the given hashmap.
|
---|
252 | * The offset data is not stored and needs to be handled separately.
|
---|
253 | * @see #applyFromPropertiesMap(Map)
|
---|
254 | * @see OffsetBookmark#toPropertiesMap()
|
---|
255 | */
|
---|
256 | @Override
|
---|
257 | public Map<String, String> toPropertiesMap() {
|
---|
258 | Map<String, String> data = new HashMap<>();
|
---|
259 | data.put(AUTO_LOAD, Boolean.toString(autoLoad));
|
---|
260 | data.put(AUTO_ZOOM, Boolean.toString(autoZoom));
|
---|
261 | data.put(SHOW_ERRORS, Boolean.toString(showErrors));
|
---|
262 | return data;
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Load the settings from the given data instance.
|
---|
267 | * The offset data is not loaded and needs to be handled separately.
|
---|
268 | * @param data The data
|
---|
269 | * @see #toPropertiesMap()
|
---|
270 | * @see OffsetBookmark#fromPropertiesMap(java.util.Map)
|
---|
271 | */
|
---|
272 | @Override
|
---|
273 | public void applyFromPropertiesMap(Map<String, String> data) {
|
---|
274 | try {
|
---|
275 | String doAutoLoad = data.get(AUTO_LOAD);
|
---|
276 | if (doAutoLoad != null) {
|
---|
277 | setAutoLoad(Boolean.parseBoolean(doAutoLoad));
|
---|
278 | }
|
---|
279 |
|
---|
280 | String doAutoZoom = data.get(AUTO_ZOOM);
|
---|
281 | if (doAutoZoom != null) {
|
---|
282 | setAutoZoom(Boolean.parseBoolean(doAutoZoom));
|
---|
283 | }
|
---|
284 |
|
---|
285 | String doShowErrors = data.get(SHOW_ERRORS);
|
---|
286 | if (doShowErrors != null) {
|
---|
287 | setShowErrors(Boolean.parseBoolean(doShowErrors));
|
---|
288 | }
|
---|
289 | } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
|
---|
290 | throw BugReport.intercept(e).put("data", data);
|
---|
291 | }
|
---|
292 | }
|
---|
293 |
|
---|
294 | @Override
|
---|
295 | public int hashCode() {
|
---|
296 | final int prime = 31;
|
---|
297 | int result = 1;
|
---|
298 | result = prime * result + (autoLoad ? 1231 : 1237);
|
---|
299 | result = prime * result + (autoZoom ? 1231 : 1237);
|
---|
300 | result = prime * result + (showErrors ? 1231 : 1237);
|
---|
301 | return result;
|
---|
302 | }
|
---|
303 |
|
---|
304 | @Override
|
---|
305 | public boolean equals(Object obj) {
|
---|
306 | if (this == obj)
|
---|
307 | return true;
|
---|
308 | if (obj == null || getClass() != obj.getClass())
|
---|
309 | return false;
|
---|
310 | TileSourceDisplaySettings other = (TileSourceDisplaySettings) obj;
|
---|
311 | return autoLoad == other.autoLoad
|
---|
312 | && autoZoom == other.autoZoom
|
---|
313 | && showErrors == other.showErrors;
|
---|
314 | }
|
---|
315 |
|
---|
316 | @Override
|
---|
317 | public String toString() {
|
---|
318 | return "TileSourceDisplaySettings [autoZoom=" + autoZoom + ", autoLoad=" + autoLoad + ", showErrors="
|
---|
319 | + showErrors + ']';
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * A listener that listens to changes to the {@link TileSourceDisplaySettings} object.
|
---|
324 | * @author Michael Zangl
|
---|
325 | * @since 10600 (functional interface)
|
---|
326 | */
|
---|
327 | @FunctionalInterface
|
---|
328 | public interface DisplaySettingsChangeListener {
|
---|
329 | /**
|
---|
330 | * Called whenever the display settings have changed.
|
---|
331 | * @param e The change event.
|
---|
332 | */
|
---|
333 | void displaySettingsChanged(DisplaySettingsChangeEvent e);
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * An event that is created whenever the display settings change.
|
---|
338 | * @author Michael Zangl
|
---|
339 | */
|
---|
340 | public static final class DisplaySettingsChangeEvent {
|
---|
341 | private final String changedSetting;
|
---|
342 |
|
---|
343 | DisplaySettingsChangeEvent(String changedSetting) {
|
---|
344 | this.changedSetting = changedSetting;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /**
|
---|
348 | * Gets the setting that was changed
|
---|
349 | * @return The name of the changed setting.
|
---|
350 | */
|
---|
351 | public String getChangedSetting() {
|
---|
352 | return changedSetting;
|
---|
353 | }
|
---|
354 |
|
---|
355 | @Override
|
---|
356 | public String toString() {
|
---|
357 | return "DisplaySettingsChangeEvent [changedSetting=" + changedSetting + ']';
|
---|
358 | }
|
---|
359 | }
|
---|
360 | }
|
---|