1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.Collections;
|
---|
12 | import java.util.HashMap;
|
---|
13 | import java.util.List;
|
---|
14 | import java.util.Map;
|
---|
15 |
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JLabel;
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 | import javax.swing.JPanel;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane.ValidationListener;
|
---|
28 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
30 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.SourceProvider;
|
---|
32 | import org.openstreetmap.josm.gui.preferences.SourceType;
|
---|
33 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
34 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
35 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader;
|
---|
36 | import org.openstreetmap.josm.tools.GBC;
|
---|
37 | import org.xml.sax.SAXException;
|
---|
38 | import org.xml.sax.SAXParseException;
|
---|
39 |
|
---|
40 | /**
|
---|
41 | * Preference settings for tagging presets.
|
---|
42 | */
|
---|
43 | public final class TaggingPresetPreference implements SubPreferenceSetting {
|
---|
44 |
|
---|
45 | private final class TaggingPresetValidationListener implements ValidationListener {
|
---|
46 | @Override
|
---|
47 | public boolean validatePreferences() {
|
---|
48 | if (sources.hasActiveSourcesChanged()) {
|
---|
49 | List<Integer> sourcesToRemove = new ArrayList<>();
|
---|
50 | int i = -1;
|
---|
51 | SOURCES:
|
---|
52 | for (SourceEntry source: sources.getActiveSources()) {
|
---|
53 | i++;
|
---|
54 | boolean canLoad = false;
|
---|
55 | try {
|
---|
56 | TaggingPresetReader.readAll(source.url, false);
|
---|
57 | canLoad = true;
|
---|
58 | } catch (IOException e) {
|
---|
59 | Main.warn(tr("Could not read tagging preset source: {0}", source));
|
---|
60 | ExtendedDialog ed = new ExtendedDialog(Main.parent, tr("Error"),
|
---|
61 | new String[] {tr("Yes"), tr("No"), tr("Cancel")});
|
---|
62 | ed.setContent(tr("Could not read tagging preset source: {0}\nDo you want to keep it?", source));
|
---|
63 | switch (ed.showDialog().getValue()) {
|
---|
64 | case 1:
|
---|
65 | continue SOURCES;
|
---|
66 | case 2:
|
---|
67 | sourcesToRemove.add(i);
|
---|
68 | continue SOURCES;
|
---|
69 | default:
|
---|
70 | return false;
|
---|
71 | }
|
---|
72 | } catch (SAXException e) {
|
---|
73 | // We will handle this in step with validation
|
---|
74 | Main.trace(e);
|
---|
75 | }
|
---|
76 |
|
---|
77 | String errorMessage = null;
|
---|
78 |
|
---|
79 | try {
|
---|
80 | TaggingPresetReader.readAll(source.url, true);
|
---|
81 | } catch (IOException e) {
|
---|
82 | // Should not happen, but at least show message
|
---|
83 | String msg = tr("Could not read tagging preset source {0}", source);
|
---|
84 | Main.error(msg);
|
---|
85 | JOptionPane.showMessageDialog(Main.parent, msg);
|
---|
86 | return false;
|
---|
87 | } catch (SAXParseException e) {
|
---|
88 | if (canLoad) {
|
---|
89 | errorMessage = tr("<html>Tagging preset source {0} can be loaded but it contains errors. " +
|
---|
90 | "Do you really want to use it?<br><br><table width=600>Error is: [{1}:{2}] {3}</table></html>",
|
---|
91 | source, e.getLineNumber(), e.getColumnNumber(), e.getMessage());
|
---|
92 | } else {
|
---|
93 | errorMessage = tr("<html>Unable to parse tagging preset source: {0}. " +
|
---|
94 | "Do you really want to use it?<br><br><table width=400>Error is: [{1}:{2}] {3}</table></html>",
|
---|
95 | source, e.getLineNumber(), e.getColumnNumber(), e.getMessage());
|
---|
96 | }
|
---|
97 | } catch (SAXException e) {
|
---|
98 | if (canLoad) {
|
---|
99 | errorMessage = tr("<html>Tagging preset source {0} can be loaded but it contains errors. " +
|
---|
100 | "Do you really want to use it?<br><br><table width=600>Error is: {1}</table></html>",
|
---|
101 | source, e.getMessage());
|
---|
102 | } else {
|
---|
103 | errorMessage = tr("<html>Unable to parse tagging preset source: {0}. " +
|
---|
104 | "Do you really want to use it?<br><br><table width=600>Error is: {1}</table></html>",
|
---|
105 | source, e.getMessage());
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (errorMessage != null) {
|
---|
110 | Main.error(errorMessage);
|
---|
111 | int result = JOptionPane.showConfirmDialog(Main.parent, new JLabel(errorMessage), tr("Error"),
|
---|
112 | JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.ERROR_MESSAGE);
|
---|
113 |
|
---|
114 | switch (result) {
|
---|
115 | case JOptionPane.YES_OPTION:
|
---|
116 | continue SOURCES;
|
---|
117 | case JOptionPane.NO_OPTION:
|
---|
118 | sourcesToRemove.add(i);
|
---|
119 | continue SOURCES;
|
---|
120 | default:
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | sources.removeSources(sourcesToRemove);
|
---|
126 | return true;
|
---|
127 | } else {
|
---|
128 | return true;
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Factory used to create a new {@code TaggingPresetPreference}.
|
---|
135 | */
|
---|
136 | public static class Factory implements PreferenceSettingFactory {
|
---|
137 | @Override
|
---|
138 | public PreferenceSetting createPreferenceSetting() {
|
---|
139 | return new TaggingPresetPreference();
|
---|
140 | }
|
---|
141 | }
|
---|
142 |
|
---|
143 | private TaggingPresetPreference() {
|
---|
144 | super();
|
---|
145 | }
|
---|
146 |
|
---|
147 | private static final List<SourceProvider> presetSourceProviders = new ArrayList<>();
|
---|
148 |
|
---|
149 | private SourceEditor sources;
|
---|
150 | private JCheckBox sortMenu;
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Registers a new additional preset source provider.
|
---|
154 | * @param provider The preset source provider
|
---|
155 | * @return {@code true}, if the provider has been added, {@code false} otherwise
|
---|
156 | */
|
---|
157 | public static boolean registerSourceProvider(SourceProvider provider) {
|
---|
158 | if (provider != null)
|
---|
159 | return presetSourceProviders.add(provider);
|
---|
160 | return false;
|
---|
161 | }
|
---|
162 |
|
---|
163 | private final ValidationListener validationListener = new TaggingPresetValidationListener();
|
---|
164 |
|
---|
165 | @Override
|
---|
166 | public void addGui(PreferenceTabbedPane gui) {
|
---|
167 | sortMenu = new JCheckBox(tr("Sort presets menu alphabetically"),
|
---|
168 | Main.pref.getBoolean("taggingpreset.sortmenu", false));
|
---|
169 |
|
---|
170 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
171 | panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
|
---|
172 | panel.add(sortMenu, GBC.eol().insets(5, 5, 5, 0));
|
---|
173 | sources = new TaggingPresetSourceEditor();
|
---|
174 | panel.add(sources, GBC.eol().fill(GBC.BOTH));
|
---|
175 | final MapPreference mapPref = gui.getMapPreference();
|
---|
176 | mapPref.addSubTab(this, tr("Tagging Presets"), panel);
|
---|
177 | sources.deferLoading(mapPref, panel);
|
---|
178 | gui.addValidationListener(validationListener);
|
---|
179 | }
|
---|
180 |
|
---|
181 | static class TaggingPresetSourceEditor extends SourceEditor {
|
---|
182 |
|
---|
183 | private static final String ICONPREF = "taggingpreset.icon.sources";
|
---|
184 |
|
---|
185 | TaggingPresetSourceEditor() {
|
---|
186 | super(SourceType.TAGGING_PRESET, Main.getJOSMWebsite()+"/presets", presetSourceProviders, true);
|
---|
187 | }
|
---|
188 |
|
---|
189 | @Override
|
---|
190 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
---|
191 | return PresetPrefHelper.INSTANCE.get();
|
---|
192 | }
|
---|
193 |
|
---|
194 | @Override
|
---|
195 | public boolean finish() {
|
---|
196 | return doFinish(PresetPrefHelper.INSTANCE, ICONPREF);
|
---|
197 | }
|
---|
198 |
|
---|
199 | @Override
|
---|
200 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
201 | return PresetPrefHelper.INSTANCE.getDefault();
|
---|
202 | }
|
---|
203 |
|
---|
204 | @Override
|
---|
205 | public Collection<String> getInitialIconPathsList() {
|
---|
206 | return Main.pref.getCollection(ICONPREF, null);
|
---|
207 | }
|
---|
208 |
|
---|
209 | @Override
|
---|
210 | public String getStr(I18nString ident) {
|
---|
211 | switch (ident) {
|
---|
212 | case AVAILABLE_SOURCES:
|
---|
213 | return tr("Available presets:");
|
---|
214 | case ACTIVE_SOURCES:
|
---|
215 | return tr("Active presets:");
|
---|
216 | case NEW_SOURCE_ENTRY_TOOLTIP:
|
---|
217 | return tr("Add a new preset by entering filename or URL");
|
---|
218 | case NEW_SOURCE_ENTRY:
|
---|
219 | return tr("New preset entry:");
|
---|
220 | case REMOVE_SOURCE_TOOLTIP:
|
---|
221 | return tr("Remove the selected presets from the list of active presets");
|
---|
222 | case EDIT_SOURCE_TOOLTIP:
|
---|
223 | return tr("Edit the filename or URL for the selected active preset");
|
---|
224 | case ACTIVATE_TOOLTIP:
|
---|
225 | return tr("Add the selected available presets to the list of active presets");
|
---|
226 | case RELOAD_ALL_AVAILABLE:
|
---|
227 | return marktr("Reloads the list of available presets from ''{0}''");
|
---|
228 | case LOADING_SOURCES_FROM:
|
---|
229 | return marktr("Loading preset sources from ''{0}''");
|
---|
230 | case FAILED_TO_LOAD_SOURCES_FROM:
|
---|
231 | return marktr("<html>Failed to load the list of preset sources from<br>"
|
---|
232 | + "''{0}''.<br>"
|
---|
233 | + "<br>"
|
---|
234 | + "Details (untranslated):<br>{1}</html>");
|
---|
235 | case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
|
---|
236 | return "/Preferences/Presets#FailedToLoadPresetSources";
|
---|
237 | case ILLEGAL_FORMAT_OF_ENTRY:
|
---|
238 | return marktr("Warning: illegal format of entry in preset list ''{0}''. Got ''{1}''");
|
---|
239 | default: throw new AssertionError();
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | @Override
|
---|
245 | public boolean ok() {
|
---|
246 | boolean restart = Main.pref.put("taggingpreset.sortmenu", sortMenu.getSelectedObjects() != null);
|
---|
247 | restart |= sources.finish();
|
---|
248 |
|
---|
249 | return restart;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Helper class for tagging presets preferences.
|
---|
254 | */
|
---|
255 | public static class PresetPrefHelper extends SourceEditor.SourcePrefHelper {
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * The unique instance.
|
---|
259 | */
|
---|
260 | public static final PresetPrefHelper INSTANCE = new PresetPrefHelper();
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Constructs a new {@code PresetPrefHelper}.
|
---|
264 | */
|
---|
265 | public PresetPrefHelper() {
|
---|
266 | super("taggingpreset.entries");
|
---|
267 | }
|
---|
268 |
|
---|
269 | @Override
|
---|
270 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
271 | ExtendedSourceEntry i = new ExtendedSourceEntry("defaultpresets.xml", "resource://data/defaultpresets.xml");
|
---|
272 | i.title = tr("Internal Preset");
|
---|
273 | i.description = tr("The default preset for JOSM");
|
---|
274 | return Collections.singletonList(i);
|
---|
275 | }
|
---|
276 |
|
---|
277 | @Override
|
---|
278 | public Map<String, String> serialize(SourceEntry entry) {
|
---|
279 | Map<String, String> res = new HashMap<>();
|
---|
280 | res.put("url", entry.url);
|
---|
281 | res.put("title", entry.title == null ? "" : entry.title);
|
---|
282 | return res;
|
---|
283 | }
|
---|
284 |
|
---|
285 | @Override
|
---|
286 | public SourceEntry deserialize(Map<String, String> s) {
|
---|
287 | return new SourceEntry(s.get("url"), null, s.get("title"), true);
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | @Override
|
---|
292 | public boolean isExpert() {
|
---|
293 | return false;
|
---|
294 | }
|
---|
295 |
|
---|
296 | @Override
|
---|
297 | public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
|
---|
298 | return gui.getMapPreference();
|
---|
299 | }
|
---|
300 | }
|
---|