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