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

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

fix #13169 - Extract imagery layer settings to new class (patch by michael2402, modified) - gsoc-core

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