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

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

PMD - Strict Exceptions

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