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