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

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

change in build: do not copy elemstyles.xml to data folder, but distribute the entire styles folder like it is done with 'images' and 'data' (toplevel) folders

  • Property svn:eol-style set to native
File size: 8.1 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.JComboBox;
17import javax.swing.JLabel;
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.SourceEditor.ExtendedSourceEntry;
25import org.openstreetmap.josm.tools.GBC;
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 return ls;
181 }
182
183 /**
184 * The internal path of elemstyles.xml has changed, this
185 * can be removed when a few months have passed.
186 */
187 private boolean adapt_elemstyles_xml(List<SourceEntry> ls) {
188 boolean changed = false;
189 for (SourceEntry se : ls) {
190 if (se.url.equals("resource://data/elemstyles.xml")) {
191 se.url = "resource://styles/standard/elemstyles.xml";
192 changed = true;
193 }
194 }
195 return changed;
196 }
197
198 @Override
199 public Collection<ExtendedSourceEntry> getDefault() {
200 ExtendedSourceEntry i = new ExtendedSourceEntry("elemstyles.xml", "resource://styles/standard/elemstyles.xml");
201 i.name = "standard";
202 i.shortdescription = tr("Internal Style");
203 i.description = tr("Internal style to be used as base for runtime switchable overlay styles");
204 return Collections.singletonList(i);
205 }
206
207 @Override
208 public Collection<String> serialize(SourceEntry entry) {
209 return Arrays.asList(new String[] {entry.url, entry.name, entry.shortdescription, Boolean.toString(entry.active)});
210 }
211
212 @Override
213 public SourceEntry deserialize(List<String> entryStr) {
214 if (entryStr.size() < 4)
215 return null;
216 String url = entryStr.get(0);
217 String name = entryStr.get(1);
218 String shortdescription = entryStr.get(2);
219 boolean active = Boolean.parseBoolean(entryStr.get(3));
220 return new SourceEntry(url, name, shortdescription, active);
221 }
222 }
223}
Note: See TracBrowser for help on using the repository browser.