source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/ImagerySettingsMigration.java@ 3719

Last change on this file since 3719 was 3719, checked in by bastiK, 13 years ago

added missing license information

File size: 9.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.preferences;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.awt.Component;
7import java.awt.GridBagLayout;
8import java.util.Collection;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.Map;
12
13import javax.swing.ButtonGroup;
14import javax.swing.JLabel;
15import javax.swing.JOptionPane;
16import javax.swing.JPanel;
17import javax.swing.JRadioButton;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.imagery.ImageryInfo;
21import org.openstreetmap.josm.data.imagery.ImageryLayerInfo;
22import org.openstreetmap.josm.tools.GBC;
23
24/** Migration of WMSPlugin and SlippyMap settings */
25class ImagerySettingsMigration {
26 enum PropertyMerge {
27 USE_OLD,
28 USE_NEW,
29 THROW_ON_CONFLICT
30 }
31 static boolean wmsLayersConflict;
32 static boolean wmsSettingsConflict;
33 static boolean tmsSettingsConflict;
34
35 private PropertyMerge mergeModel = PropertyMerge.THROW_ON_CONFLICT;
36
37 static class SettingsConflictException extends Exception {
38 }
39
40 private void migrateProperty(String oldProp, String newProp)
41 throws SettingsConflictException {
42 String oldValue = Main.pref.get(oldProp, null);
43 if (oldValue == null) return;
44 if (mergeModel == PropertyMerge.THROW_ON_CONFLICT) {
45 String newValue = Main.pref.get(newProp, null);
46 if (newValue != null && !oldValue.equals(newValue)) {
47 System.out.println(tr("Imagery settings migration: conflict when moving property {0} -> {1}",
48 oldProp, newProp));
49 throw new SettingsConflictException();
50 }
51 }
52 if (mergeModel != PropertyMerge.USE_NEW) {
53 Main.pref.put(newProp, oldValue);
54 }
55 Main.pref.put(oldProp, null);
56 }
57
58 private void migrateArray(String oldProp, String newProp)
59 throws SettingsConflictException {
60 Collection<Collection<String>> oldValue = Main.pref.getArray(oldProp, null);
61 if (oldValue == null) return;
62 if (mergeModel == PropertyMerge.THROW_ON_CONFLICT) {
63 Collection<Collection<String>> newValue = Main.pref.getArray(newProp, null);
64 if (newValue != null) {
65 System.out.println(tr("Imagery settings migration: conflict when moving array {0} -> {1}",
66 oldProp, newProp));
67 throw new SettingsConflictException();
68 }
69 }
70 if (mergeModel != PropertyMerge.USE_NEW) {
71 Main.pref.putArray(newProp, oldValue);
72 }
73 Main.pref.putArray(oldProp, null);
74 }
75
76 private void migrateWMSPlugin() {
77 try {
78 Main.pref.put("wmslayers.default", null);
79 migrateProperty("imagery.remotecontrol", "remotecontrol.permission.imagery");
80 migrateProperty("wmsplugin.remotecontrol", "remotecontrol.permission.imagery");
81 migrateProperty("wmsplugin.alpha_channel", "imagery.wms.alpha_channel");
82 migrateProperty("wmsplugin.browser", "imagery.wms.browser");
83 migrateProperty("wmsplugin.user_agent", "imagery.wms.user_agent");
84 migrateProperty("wmsplugin.timeout.connect", "imagery.wms.timeout.connect");
85 migrateProperty("wmsplugin.timeout.read", "imagery.wms.timeout.read");
86 migrateProperty("wmsplugin.simultaneousConnections", "imagery.wms.simultaneousConnections");
87 migrateProperty("wmsplugin.overlap", "imagery.wms.overlap");
88 migrateProperty("wmsplugin.overlapEast", "imagery.wms.overlapEast");
89 migrateProperty("wmsplugin.overlapNorth", "imagery.wms.overlapNorth");
90 Map<String, String> unknownProps = Main.pref.getAllPrefix("wmsplugin");
91 if (!unknownProps.isEmpty()) {
92 System.out.println(tr("There are {0} unknown WMSPlugin settings", unknownProps.size()));
93 }
94 } catch (SettingsConflictException e) {
95 wmsSettingsConflict = true;
96 }
97 }
98
99 private void migrateSlippyMapPlugin() {
100 try {
101 Main.pref.put("slippymap.tile_source", null);
102 Main.pref.put("slippymap.last_zoom_lvl", null);
103 migrateProperty("slippymap.draw_debug", "imagery.tms.draw_debug");
104 migrateProperty("slippymap.autoload_tiles", "imagery.tms.autoload");
105 migrateProperty("slippymap.autozoom", "imagery.tms.autozoom");
106 migrateProperty("slippymap.min_zoom_lvl", "imagery.tms.min_zoom_lvl");
107 migrateProperty("slippymap.max_zoom_lvl", "imagery.tms.max_zoom_lvl");
108 if (Main.pref.get("slippymap.fade_background_100", null) == null) {
109 try {
110 Main.pref.putInteger("slippymap.fade_background_100", (int)Math.round(
111 Double.valueOf(Main.pref.get("slippymap.fade_background", "0"))*100.0));
112 } catch (NumberFormatException e) {
113 }
114 }
115 Main.pref.put("slippymap.fade_background", null);
116 migrateProperty("slippymap.fade_background_100", "imagery.fade_amount");
117 Map<String, String> unknownProps = Main.pref.getAllPrefix("slippymap");
118 if (!unknownProps.isEmpty()) {
119 System.out.println(tr("There are {0} unknown slippymap plugin settings", unknownProps.size()));
120 }
121 } catch (SettingsConflictException e) {
122 tmsSettingsConflict = true;
123 }
124 }
125
126 private void mergeWMSLayers() {
127 ImageryLayerInfo layerInfo = ImageryLayerInfo.instance;
128 HashSet<String> existingUrls = new HashSet<String>();
129 for (ImageryInfo info : layerInfo.getLayers()) {
130 existingUrls.add(info.getFullURL());
131 }
132 for(Collection<String> c : Main.pref.getArray("wmslayers",
133 Collections.<Collection<String>>emptySet())) {
134 ImageryInfo info = new ImageryInfo(c);
135 if (!existingUrls.contains(info.getFullURL())) {
136 layerInfo.add(info);
137 }
138 }
139 layerInfo.save();
140 Main.pref.putArray("wmslayers", null);
141 }
142
143 public void migrateSettings() {
144 mergeModel = PropertyMerge.THROW_ON_CONFLICT;
145 try {
146 migrateArray("wmslayers", "imagery.layers");
147 } catch (SettingsConflictException e) {
148 wmsLayersConflict = true;
149 }
150 migrateWMSPlugin();
151 migrateSlippyMapPlugin();
152 }
153
154 public boolean hasConflicts() {
155 return wmsLayersConflict || wmsSettingsConflict || tmsSettingsConflict;
156 }
157
158 JRadioButton wlKeepImagery = new JRadioButton(tr("Keep current list"));
159 JRadioButton wlUseWMS = new JRadioButton(tr("Overwrite with WMSPlugin list"));
160 JRadioButton wlMerge = new JRadioButton(tr("Merge"));
161
162 JRadioButton wsKeepImagery = new JRadioButton(tr("Keep current settings"));
163 JRadioButton wsUseWMS = new JRadioButton(tr("Overwrite with WMSPlugin settings"));
164
165 JRadioButton tsKeepImagery = new JRadioButton(tr("Keep current settings"));
166 JRadioButton tsUseSlippyMap = new JRadioButton(tr("Overwrite with SlippyMap settings"));
167
168 public void settingsMigrationDialog(Component parent) {
169 JPanel p = new JPanel(new GridBagLayout());
170
171 if (wmsLayersConflict) {
172 p.add(new JLabel(tr("WMS layer list:")), GBC.eol());
173 ButtonGroup g = new ButtonGroup();
174 g.add(wlKeepImagery);
175 g.add(wlUseWMS);
176 g.add(wlMerge);
177 wlMerge.setSelected(true);
178
179 p.add(wlKeepImagery, GBC.eol());
180 p.add(wlUseWMS, GBC.eol());
181 p.add(wlMerge, GBC.eop());
182 }
183
184 if (wmsSettingsConflict) {
185 p.add(new JLabel(tr("WMSPlugin settings:")), GBC.eol());
186 ButtonGroup g = new ButtonGroup();
187 g.add(wsKeepImagery);
188 g.add(wsUseWMS);
189 wsKeepImagery.setSelected(true);
190
191 p.add(wsKeepImagery, GBC.eol());
192 p.add(wsUseWMS, GBC.eop());
193 }
194
195 if (tmsSettingsConflict) {
196 p.add(new JLabel(tr("SlippyMap settings:")), GBC.eol());
197 ButtonGroup g = new ButtonGroup();
198 g.add(tsKeepImagery);
199 g.add(tsUseSlippyMap);
200 tsKeepImagery.setSelected(true);
201
202 p.add(tsKeepImagery, GBC.eol());
203 p.add(tsUseSlippyMap, GBC.eop());
204 }
205 int answer = JOptionPane.showConfirmDialog(
206 parent, p,
207 tr("Imagery settings migration"),
208 JOptionPane.OK_CANCEL_OPTION);
209 if (answer != JOptionPane.OK_OPTION) return;
210 try {
211 if (wlMerge.isSelected()) {
212 mergeWMSLayers();
213 } else {
214 mergeModel = wlKeepImagery.isSelected() ? PropertyMerge.USE_NEW : PropertyMerge.USE_OLD;
215 migrateArray("wmslayers", "imagery.layers");
216 }
217 wmsLayersConflict = false;
218 mergeModel = wsKeepImagery.isSelected() ? PropertyMerge.USE_NEW : PropertyMerge.USE_OLD;
219 migrateWMSPlugin();
220 wmsSettingsConflict = false;
221 mergeModel = tsKeepImagery.isSelected() ? PropertyMerge.USE_NEW : PropertyMerge.USE_OLD;
222 migrateSlippyMapPlugin();
223 tmsSettingsConflict = false;
224 } catch (SettingsConflictException e) {
225 JOptionPane.showMessageDialog(
226 parent, tr("Warning: unexpected settings conflict"),
227 tr("Imagery settings migration"),
228 JOptionPane.OK_OPTION);
229 }
230 }
231}
Note: See TracBrowser for help on using the repository browser.