source: josm/trunk/src/org/openstreetmap/josm/gui/layer/imagery/TileSourceDisplaySettings.java@ 12134

Last change on this file since 12134 was 12134, checked in by bastiK, 7 years ago

see #14773 - improve session export/import for imagery layer offset data

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