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

Last change on this file since 12117 was 12117, checked in by Don-vip, 7 years ago

see #14734 - fix PMD warning

  • Property svn:eol-style set to native
File size: 11.3 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 * @param data The map to store the settings to.
251 * @see #loadFrom(Map)
252 */
253 public void storeTo(Map<String, String> data) {
254 data.put(AUTO_LOAD, Boolean.toString(autoLoad));
255 data.put(AUTO_ZOOM, Boolean.toString(autoZoom));
256 data.put(SHOW_ERRORS, Boolean.toString(showErrors));
257 data.put("dx", String.valueOf(getDx()));
258 data.put("dy", String.valueOf(getDy()));
259 }
260
261 /**
262 * Load the settings from the given data instance.
263 * @param data The data
264 * @see #storeTo(Map)
265 */
266 public void loadFrom(Map<String, String> data) {
267 try {
268 String doAutoLoad = data.get(AUTO_LOAD);
269 if (doAutoLoad != null) {
270 setAutoLoad(Boolean.parseBoolean(doAutoLoad));
271 }
272
273 String doAutoZoom = data.get(AUTO_ZOOM);
274 if (doAutoZoom != null) {
275 setAutoZoom(Boolean.parseBoolean(doAutoZoom));
276 }
277
278 String doShowErrors = data.get(SHOW_ERRORS);
279 if (doShowErrors != null) {
280 setShowErrors(Boolean.parseBoolean(doShowErrors));
281 }
282
283 String dx = data.get("dx");
284 String dy = data.get("dy");
285 if (dx != null && dy != null) {
286 setDisplacement(new EastNorth(Double.parseDouble(dx), Double.parseDouble(dy)));
287 }
288 } catch (JosmRuntimeException | IllegalArgumentException | IllegalStateException e) {
289 throw BugReport.intercept(e).put("data", data);
290 }
291 }
292
293 @Override
294 public int hashCode() {
295 final int prime = 31;
296 int result = 1;
297 result = prime * result + (autoLoad ? 1231 : 1237);
298 result = prime * result + (autoZoom ? 1231 : 1237);
299 result = prime * result + (showErrors ? 1231 : 1237);
300 return result;
301 }
302
303 @Override
304 public boolean equals(Object obj) {
305 if (this == obj)
306 return true;
307 if (obj == null || getClass() != obj.getClass())
308 return false;
309 TileSourceDisplaySettings other = (TileSourceDisplaySettings) obj;
310 return autoLoad == other.autoLoad
311 && autoZoom == other.autoZoom
312 && showErrors == other.showErrors;
313 }
314
315 @Override
316 public String toString() {
317 return "TileSourceDisplaySettings [autoZoom=" + autoZoom + ", autoLoad=" + autoLoad + ", showErrors="
318 + showErrors + ']';
319 }
320
321 /**
322 * A listener that listens to changes to the {@link TileSourceDisplaySettings} object.
323 * @author Michael Zangl
324 * @since 10600 (functional interface)
325 */
326 @FunctionalInterface
327 public interface DisplaySettingsChangeListener {
328 /**
329 * Called whenever the display settings have changed.
330 * @param e The change event.
331 */
332 void displaySettingsChanged(DisplaySettingsChangeEvent e);
333 }
334
335 /**
336 * An event that is created whenever the display settings change.
337 * @author Michael Zangl
338 */
339 public static final class DisplaySettingsChangeEvent {
340 private final String changedSetting;
341
342 DisplaySettingsChangeEvent(String changedSetting) {
343 this.changedSetting = changedSetting;
344 }
345
346 /**
347 * Gets the setting that was changed
348 * @return The name of the changed setting.
349 */
350 public String getChangedSetting() {
351 return changedSetting;
352 }
353
354 @Override
355 public String toString() {
356 return "DisplaySettingsChangeEvent [changedSetting=" + changedSetting + ']';
357 }
358 }
359}
Note: See TracBrowser for help on using the repository browser.