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.util.ArrayList;
|
---|
9 | import java.util.Arrays;
|
---|
10 | import java.util.Collection;
|
---|
11 | import java.util.HashMap;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.TreeSet;
|
---|
15 |
|
---|
16 | import javax.swing.BorderFactory;
|
---|
17 | import javax.swing.JCheckBox;
|
---|
18 | import javax.swing.JPanel;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
22 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
23 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.SourceEntry;
|
---|
28 | import org.openstreetmap.josm.gui.preferences.SourceProvider;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.SourceType;
|
---|
30 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
31 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
32 | import org.openstreetmap.josm.tools.GBC;
|
---|
33 | import org.openstreetmap.josm.tools.Predicate;
|
---|
34 | import org.openstreetmap.josm.tools.Utils;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Preference settings for map paint styles.
|
---|
38 | */
|
---|
39 | public class MapPaintPreference implements SubPreferenceSetting {
|
---|
40 | private SourceEditor sources;
|
---|
41 | private JCheckBox enableIconDefault;
|
---|
42 |
|
---|
43 | private static final List<SourceProvider> styleSourceProviders = new ArrayList<SourceProvider>();
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Registers a new additional style source provider.
|
---|
47 | * @param provider The style source provider
|
---|
48 | * @return {@code true}, if the provider has been added, {@code false} otherwise
|
---|
49 | */
|
---|
50 | public static boolean registerSourceProvider(SourceProvider provider) {
|
---|
51 | if (provider != null)
|
---|
52 | return styleSourceProviders.add(provider);
|
---|
53 | return false;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Factory used to create a new {@code MapPaintPreference}.
|
---|
58 | */
|
---|
59 | public static class Factory implements PreferenceSettingFactory {
|
---|
60 | @Override
|
---|
61 | public PreferenceSetting createPreferenceSetting() {
|
---|
62 | return new MapPaintPreference();
|
---|
63 | }
|
---|
64 | }
|
---|
65 |
|
---|
66 | @Override
|
---|
67 | public void addGui(PreferenceTabbedPane gui) {
|
---|
68 | enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
|
---|
69 | Main.pref.getBoolean("mappaint.icon.enable-defaults", true));
|
---|
70 |
|
---|
71 | sources = new MapPaintSourceEditor();
|
---|
72 |
|
---|
73 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
74 | panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
75 |
|
---|
76 | panel.add(sources, GBC.eol().fill(GBC.BOTH));
|
---|
77 | panel.add(enableIconDefault, GBC.eol().insets(11,2,5,0));
|
---|
78 |
|
---|
79 | final MapPreference mapPref = gui.getMapPreference();
|
---|
80 | mapPref.addSubTab(this, tr("Map Paint Styles"), panel);
|
---|
81 | sources.deferLoading(mapPref, panel);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static class MapPaintSourceEditor extends SourceEditor {
|
---|
85 |
|
---|
86 | private static final String iconpref = "mappaint.icon.sources";
|
---|
87 |
|
---|
88 | public MapPaintSourceEditor() {
|
---|
89 | super(SourceType.MAP_PAINT_STYLE, Main.JOSM_WEBSITE+"/styles", styleSourceProviders, true);
|
---|
90 | }
|
---|
91 |
|
---|
92 | @Override
|
---|
93 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
---|
94 | return MapPaintPrefHelper.INSTANCE.get();
|
---|
95 | }
|
---|
96 |
|
---|
97 | @Override
|
---|
98 | public boolean finish() {
|
---|
99 | List<SourceEntry> activeStyles = activeSourcesModel.getSources();
|
---|
100 |
|
---|
101 | boolean changed = MapPaintPrefHelper.INSTANCE.put(activeStyles);
|
---|
102 |
|
---|
103 | if (tblIconPaths != null) {
|
---|
104 | List<String> iconPaths = iconPathsModel.getIconPaths();
|
---|
105 |
|
---|
106 | if (!iconPaths.isEmpty()) {
|
---|
107 | if (Main.pref.putCollection(iconpref, iconPaths)) {
|
---|
108 | changed = true;
|
---|
109 | }
|
---|
110 | } else if (Main.pref.putCollection(iconpref, null)) {
|
---|
111 | changed = true;
|
---|
112 | }
|
---|
113 | }
|
---|
114 | return changed;
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
119 | return MapPaintPrefHelper.INSTANCE.getDefault();
|
---|
120 | }
|
---|
121 |
|
---|
122 | @Override
|
---|
123 | public Collection<String> getInitialIconPathsList() {
|
---|
124 | return Main.pref.getCollection(iconpref, null);
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Override
|
---|
128 | public String getStr(I18nString ident) {
|
---|
129 | switch (ident) {
|
---|
130 | case AVAILABLE_SOURCES:
|
---|
131 | return tr("Available styles:");
|
---|
132 | case ACTIVE_SOURCES:
|
---|
133 | return tr("Active styles:");
|
---|
134 | case NEW_SOURCE_ENTRY_TOOLTIP:
|
---|
135 | return tr("Add a new style by entering filename or URL");
|
---|
136 | case NEW_SOURCE_ENTRY:
|
---|
137 | return tr("New style entry:");
|
---|
138 | case REMOVE_SOURCE_TOOLTIP:
|
---|
139 | return tr("Remove the selected styles from the list of active styles");
|
---|
140 | case EDIT_SOURCE_TOOLTIP:
|
---|
141 | return tr("Edit the filename or URL for the selected active style");
|
---|
142 | case ACTIVATE_TOOLTIP:
|
---|
143 | return tr("Add the selected available styles to the list of active styles");
|
---|
144 | case RELOAD_ALL_AVAILABLE:
|
---|
145 | return marktr("Reloads the list of available styles from ''{0}''");
|
---|
146 | case LOADING_SOURCES_FROM:
|
---|
147 | return marktr("Loading style sources from ''{0}''");
|
---|
148 | case FAILED_TO_LOAD_SOURCES_FROM:
|
---|
149 | return marktr("<html>Failed to load the list of style sources from<br>"
|
---|
150 | + "''{0}''.<br>"
|
---|
151 | + "<br>"
|
---|
152 | + "Details (untranslated):<br>{1}</html>");
|
---|
153 | case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
|
---|
154 | return "/Preferences/Styles#FailedToLoadStyleSources";
|
---|
155 | case ILLEGAL_FORMAT_OF_ENTRY:
|
---|
156 | return marktr("Warning: illegal format of entry in style list ''{0}''. Got ''{1}''");
|
---|
157 | default: throw new AssertionError();
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | @Override
|
---|
164 | public boolean ok() {
|
---|
165 | boolean reload = Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected());
|
---|
166 | reload |= sources.finish();
|
---|
167 | if (reload) {
|
---|
168 | MapPaintStyles.readFromPreferences();
|
---|
169 | }
|
---|
170 | if (Main.isDisplayingMapView()) {
|
---|
171 | MapPaintStyles.getStyles().clearCached();
|
---|
172 | }
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 |
|
---|
176 | /**
|
---|
177 | * Initialize the styles
|
---|
178 | */
|
---|
179 | public static void initialize() {
|
---|
180 | MapPaintStyles.readFromPreferences();
|
---|
181 | }
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * Helper class for map paint styles preferences.
|
---|
185 | */
|
---|
186 | public static class MapPaintPrefHelper extends SourceEditor.SourcePrefHelper {
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * The unique instance.
|
---|
190 | */
|
---|
191 | public final static MapPaintPrefHelper INSTANCE = new MapPaintPrefHelper();
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * Constructs a new {@code MapPaintPrefHelper}.
|
---|
195 | */
|
---|
196 | public MapPaintPrefHelper() {
|
---|
197 | super("mappaint.style.entries");
|
---|
198 | }
|
---|
199 |
|
---|
200 | @Override
|
---|
201 | public List<SourceEntry> get() {
|
---|
202 | List<SourceEntry> ls = super.get();
|
---|
203 | if (insertNewDefaults(ls)) {
|
---|
204 | put(ls);
|
---|
205 | }
|
---|
206 | return ls;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * If the selection of default styles changes in future releases, add
|
---|
211 | * the new entries to the user-configured list. Remember the known URLs,
|
---|
212 | * so an item that was deleted explicitly is not added again.
|
---|
213 | */
|
---|
214 | private boolean insertNewDefaults(List<SourceEntry> list) {
|
---|
215 | boolean changed = false;
|
---|
216 |
|
---|
217 | Collection<String> knownDefaults = new TreeSet<String>(Main.pref.getCollection("mappaint.style.known-defaults"));
|
---|
218 |
|
---|
219 | Collection<ExtendedSourceEntry> defaults = getDefault();
|
---|
220 | int insertionIdx = 0;
|
---|
221 | for (final SourceEntry def : defaults) {
|
---|
222 | int i = Utils.indexOf(list,
|
---|
223 | new Predicate<SourceEntry>() {
|
---|
224 | @Override
|
---|
225 | public boolean evaluate(SourceEntry se) {
|
---|
226 | return Utils.equal(def.url, se.url);
|
---|
227 | }
|
---|
228 | });
|
---|
229 | if (i == -1 && !knownDefaults.contains(def.url)) {
|
---|
230 | list.add(insertionIdx, def);
|
---|
231 | insertionIdx++;
|
---|
232 | changed = true;
|
---|
233 | } else {
|
---|
234 | if (i >= insertionIdx) {
|
---|
235 | insertionIdx = i + 1;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | for (SourceEntry def : defaults) {
|
---|
241 | knownDefaults.add(def.url);
|
---|
242 | }
|
---|
243 | Main.pref.putCollection("mappaint.style.known-defaults", knownDefaults);
|
---|
244 |
|
---|
245 | return changed;
|
---|
246 | }
|
---|
247 |
|
---|
248 | @Override
|
---|
249 | public Collection<ExtendedSourceEntry> getDefault() {
|
---|
250 | ExtendedSourceEntry defJOSM = new ExtendedSourceEntry("elemstyles.xml", "resource://styles/standard/elemstyles.xml");
|
---|
251 | defJOSM.active = true;
|
---|
252 | defJOSM.name = "standard";
|
---|
253 | defJOSM.title = tr("JOSM Internal Style");
|
---|
254 | defJOSM.description = tr("Internal style to be used as base for runtime switchable overlay styles");
|
---|
255 | ExtendedSourceEntry defPL2 = new ExtendedSourceEntry("potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
|
---|
256 | defPL2.active = false;
|
---|
257 | defPL2.name = "standard";
|
---|
258 | defPL2.title = tr("Potlatch 2");
|
---|
259 | defPL2.description = tr("the main Potlatch 2 style");
|
---|
260 |
|
---|
261 | return Arrays.asList(new ExtendedSourceEntry[] { defJOSM, defPL2 });
|
---|
262 | }
|
---|
263 |
|
---|
264 | @Override
|
---|
265 | public Map<String, String> serialize(SourceEntry entry) {
|
---|
266 | Map<String, String> res = new HashMap<String, String>();
|
---|
267 | res.put("url", entry.url);
|
---|
268 | res.put("title", entry.title == null ? "" : entry.title);
|
---|
269 | res.put("active", Boolean.toString(entry.active));
|
---|
270 | if (entry.name != null) {
|
---|
271 | res.put("ptoken", entry.name);
|
---|
272 | }
|
---|
273 | return res;
|
---|
274 | }
|
---|
275 |
|
---|
276 | @Override
|
---|
277 | public SourceEntry deserialize(Map<String, String> s) {
|
---|
278 | return new SourceEntry(s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
|
---|
279 | }
|
---|
280 | }
|
---|
281 |
|
---|
282 | @Override
|
---|
283 | public boolean isExpert() {
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 |
|
---|
287 | @Override
|
---|
288 | public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
|
---|
289 | return gui.getMapPreference();
|
---|
290 | }
|
---|
291 | }
|
---|