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

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

Sonar/FindBugs - various bugfixes / violation fixes

  • Property svn:eol-style set to native
File size: 10.5 KB
Line 
1// License: GPL. Copyright 2007 by Immanuel Scholz and others
2package org.openstreetmap.josm.gui.preferences.map;
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.HashMap;
12import java.util.List;
13import java.util.Map;
14import java.util.TreeSet;
15
16import javax.swing.BorderFactory;
17import javax.swing.JCheckBox;
18import javax.swing.JPanel;
19import javax.swing.event.ChangeEvent;
20import javax.swing.event.ChangeListener;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
24import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
25import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
26import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
27import org.openstreetmap.josm.gui.preferences.SourceEditor;
28import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
29import org.openstreetmap.josm.gui.preferences.SourceEntry;
30import org.openstreetmap.josm.gui.preferences.SourceProvider;
31import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
32import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
33import org.openstreetmap.josm.tools.GBC;
34import org.openstreetmap.josm.tools.Predicate;
35import org.openstreetmap.josm.tools.Utils;
36
37public 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 {
174 MapPaintStyles.getStyles().clearCached();
175 }
176 return false;
177 }
178
179 /**
180 * Initialize the styles
181 */
182 public static void initialize() {
183 MapPaintStyles.readFromPreferences();
184 }
185
186 public static class MapPaintPrefHelper extends SourceEditor.SourcePrefHelper {
187
188 public final static MapPaintPrefHelper INSTANCE = new MapPaintPrefHelper();
189
190 public MapPaintPrefHelper() {
191 super("mappaint.style.entries", "mappaint.style.sources-list");
192 }
193
194 @Override
195 public List<SourceEntry> get() {
196 List<SourceEntry> ls = super.get();
197 if (insertNewDefaults(ls)) {
198 put(ls);
199 }
200 return ls;
201 }
202
203 /**
204 * If the selection of default styles changes in future releases, add
205 * the new entries to the user-configured list. Remember the known URLs,
206 * so an item that was deleted explicitly is not added again.
207 */
208 private boolean insertNewDefaults(List<SourceEntry> list) {
209 boolean changed = false;
210
211 Collection<String> knownDefaults = new TreeSet<String>(Main.pref.getCollection("mappaint.style.known-defaults"));
212
213 Collection<ExtendedSourceEntry> defaults = getDefault();
214 int insertionIdx = 0;
215 for (final SourceEntry def : defaults) {
216 int i = Utils.indexOf(list,
217 new Predicate<SourceEntry>() {
218 @Override
219 public boolean evaluate(SourceEntry se) {
220 return Utils.equal(def.url, se.url);
221 }
222 });
223 if (i == -1 && !knownDefaults.contains(def.url)) {
224 list.add(insertionIdx, def);
225 insertionIdx++;
226 changed = true;
227 } else {
228 if (i >= insertionIdx) {
229 insertionIdx = i + 1;
230 }
231 }
232 }
233
234 for (SourceEntry def : defaults) {
235 knownDefaults.add(def.url);
236 }
237 Main.pref.putCollection("mappaint.style.known-defaults", knownDefaults);
238
239 return changed;
240 }
241
242 @Override
243 public Collection<ExtendedSourceEntry> getDefault() {
244 ExtendedSourceEntry defJOSM = new ExtendedSourceEntry("elemstyles.xml", "resource://styles/standard/elemstyles.xml");
245 defJOSM.active = true;
246 defJOSM.name = "standard";
247 defJOSM.title = tr("JOSM Internal Style");
248 defJOSM.description = tr("Internal style to be used as base for runtime switchable overlay styles");
249 ExtendedSourceEntry defPL2 = new ExtendedSourceEntry("potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
250 defPL2.active = false;
251 defPL2.name = "standard";
252 defPL2.title = tr("Potlatch 2");
253 defPL2.description = tr("the main Potlatch 2 style");
254
255 return Arrays.asList(new ExtendedSourceEntry[] { defJOSM, defPL2 });
256 }
257
258 @Override
259 public Map<String, String> serialize(SourceEntry entry) {
260 Map<String, String> res = new HashMap<String, String>();
261 res.put("url", entry.url);
262 res.put("title", entry.title == null ? "" : entry.title);
263 res.put("active", Boolean.toString(entry.active));
264 if (entry.name != null) {
265 res.put("ptoken", entry.name);
266 }
267 return res;
268 }
269
270 @Override
271 public SourceEntry deserialize(Map<String, String> s) {
272 return new SourceEntry(s.get("url"), s.get("ptoken"), s.get("title"), Boolean.parseBoolean(s.get("active")));
273 }
274 }
275
276 @Override
277 public boolean isExpert() {
278 return false;
279 }
280
281 @Override
282 public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
283 return gui.getMapPreference();
284 }
285}
Note: See TracBrowser for help on using the repository browser.