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

Last change on this file since 4936 was 4936, checked in by bastiK, 12 years ago

remove old migration code; keep class structure for next migration step Array -> ListOfStructs

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