source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/MapPaintPreference.java@ 4839

Last change on this file since 4839 was 4839, checked in by Don-vip, 14 years ago

Allow plugins to register custom map painting styles and tagging presets

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