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

Last change on this file since 3869 was 3869, checked in by bastiK, 14 years ago

mappaint styles configuration: If the selection of default styles changes in future releases, add the new entries to the user-configured list. Remember the known URLs, so an item that was deleted explicitly is not added again.

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