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

Last change on this file since 3797 was 3797, checked in by bastiK, 13 years ago

rename some stuff: StyleSourceEditor is generic editor for both MapPaint styles andd Tagging Presets

  • Property svn:eol-style set to native
File size: 8.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.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.data.osm.OsmPrimitive;
24import org.openstreetmap.josm.gui.layer.OsmDataLayer;
25import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
26import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
27import org.openstreetmap.josm.tools.GBC;
28
29public class MapPaintPreference implements PreferenceSetting {
30 private SourceEditor sources;
31 private JCheckBox enableIconDefault;
32 private JCheckBox enableDefault;
33 private JComboBox styleCombo = new JComboBox();
34
35 public static class Factory implements PreferenceSettingFactory {
36 public PreferenceSetting createPreferenceSetting() {
37 return new MapPaintPreference();
38 }
39 }
40
41 public void addGui(final PreferenceTabbedPane gui) {
42 enableDefault = new JCheckBox(tr("Enable built-in defaults"),
43 Main.pref.getBoolean("mappaint.style.enable-defaults", true));
44 enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
45 Main.pref.getBoolean("mappaint.icon.enable-defaults", true));
46
47 sources = new MapPaintSourceEditor();
48
49 Collection<String> styles = new TreeSet<String>(MapPaintStyles.getStyles().getStyleNames());
50 String defstyle = Main.pref.get("mappaint.style", "standard");
51 styles.add(defstyle);
52 for(String style : styles) {
53 styleCombo.addItem(style);
54 }
55
56 styleCombo.setEditable(true);
57 for (int i = 0; i < styleCombo.getItemCount(); ++i) {
58 if (((String)styleCombo.getItemAt(i)).equals(defstyle)) {
59 styleCombo.setSelectedIndex(i);
60 break;
61 }
62 }
63
64 final JPanel panel = new JPanel(new GridBagLayout());
65 panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
66
67 panel.add(new JLabel(tr("Used style")), GBC.std().insets(5,5,0,5));
68 panel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
69 panel.add(styleCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,0,5,0));
70
71 panel.add(sources, GBC.eol().fill(GBC.BOTH));
72 panel.add(enableIconDefault, GBC.eol().insets(11,2,5,0));
73
74 gui.mapcontent.addTab(tr("Map Paint Styles"), panel);
75
76 // this defers loading of style sources to the first time the tab
77 // with the map paint preferences is selected by the user
78 //
79 gui.mapcontent.addChangeListener(
80 new ChangeListener() {
81 public void stateChanged(ChangeEvent e) {
82 if (gui.mapcontent.getSelectedComponent() == panel) {
83 sources.initiallyLoadAvailableSources();
84 }
85 }
86 }
87 );
88 }
89
90 class MapPaintSourceEditor extends SourceEditor {
91
92 final private String iconpref = "mappaint.icon.sources";
93
94 public MapPaintSourceEditor() {
95 super("http://josm.openstreetmap.de/styles");
96 }
97
98 @Override
99 public Collection<? extends SourceEntry> getInitialSourcesList() {
100 return (new MapPaintPrefMigration()).get();
101 }
102
103 @Override
104 public boolean finish() {
105 List<SourceEntry> activeStyles = activeSourcesModel.getSources();
106
107 boolean changed = (new MapPaintPrefMigration()).put(activeStyles);
108
109 if (tblIconPaths != null) {
110 List<String> iconPaths = iconPathsModel.getIconPaths();
111
112 if (!iconPaths.isEmpty()) {
113 if (Main.pref.putCollection(iconpref, iconPaths)) {
114 changed = true;
115 }
116 } else if (Main.pref.putCollection(iconpref, null)) {
117 changed = true;
118 }
119 }
120 return changed;
121 }
122
123 @Override
124 public Collection<ExtendedSourceEntry> getDefault() {
125 return (new MapPaintPrefMigration()).getDefault();
126 }
127
128 @Override
129 public Collection<String> getInitialIconPathsList() {
130 return Main.pref.getCollection(iconpref, null);
131 }
132
133 @Override
134 public String getStr(I18nString ident) {
135 switch (ident) {
136 case AVAILABLE_SOURCES:
137 return tr("Available styles:");
138 case ACTIVE_SOURCES:
139 return tr("Active styles:");
140 case NEW_SOURCE_ENTRY_TOOLTIP:
141 return tr("Add a new style by entering filename or URL");
142 case NEW_SOURCE_ENTRY:
143 return tr("New style entry:");
144 case REMOVE_SOURCE_TOOLTIP:
145 return tr("Remove the selected styles from the list of active styles");
146 case EDIT_SOURCE_TOOLTIP:
147 return tr("Edit the filename or URL for the selected active style");
148 case ACTIVATE_TOOLTIP:
149 return tr("Add the selected available styles to the list of active styles");
150 case RELOAD_ALL_AVAILABLE:
151 return marktr("Reloads the list of available styles from ''{0}''");
152 case LOADING_SOURCES_FROM:
153 return marktr("Loading style sources from ''{0}''");
154 case FAILED_TO_LOAD_SOURCES_FROM:
155 return marktr("<html>Failed to load the list of style sources from<br>"
156 + "''{0}''.<br>"
157 + "<br>"
158 + "Details (untranslated):<br>{1}</html>");
159 case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
160 return "/Preferences/Styles#FailedToLoadStyleSources";
161 case ILLEGAL_FORMAT_OF_ENTRY:
162 return marktr("Warning: illegal format of entry in style list ''{0}''. Got ''{1}''");
163 default: throw new AssertionError();
164 }
165 }
166
167 }
168
169 public boolean ok() {
170 Boolean restart = Main.pref.put("mappaint.style.enable-defaults", enableDefault.isSelected());
171 if(Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected())) {
172 restart = true;
173 }
174 if(sources.finish()) {
175 restart = true;
176 }
177 if(Main.pref.put("mappaint.style", styleCombo.getEditor().getItem().toString())
178 && Main.isDisplayingMapView())
179 {
180 for(OsmDataLayer l : Main.map.mapView.getLayersOfType(OsmDataLayer.class))
181 {
182 for(OsmPrimitive osm : l.data.allPrimitives())
183 {
184 osm.clearCached();
185 }
186 }
187 }
188 return restart;
189 }
190
191 /**
192 * Initialize the styles
193 */
194 public static void initialize() {
195 MapPaintStyles.readFromPreferences();
196 }
197
198 public static class MapPaintPrefMigration extends SourceEditor.SourcePrefMigration {
199
200 public MapPaintPrefMigration() {
201 super("mappaint.style.sources",
202 "mappaint.style.enable-defaults",
203 "mappaint.style.sources-list");
204 }
205
206 @Override
207 public Collection<ExtendedSourceEntry> getDefault() {
208 ExtendedSourceEntry i = new ExtendedSourceEntry("elemstyles.xml", "resource://data/elemstyles.xml");
209 i.name = "standard";
210 i.shortdescription = tr("Internal Style");
211 i.description = tr("Internal style to be used as base for runtime switchable overlay styles");
212 return Collections.singletonList(i);
213 }
214
215 @Override
216 public Collection<String> serialize(SourceEntry entry) {
217 return Arrays.asList(new String[] {entry.url, entry.name, entry.shortdescription, Boolean.toString(entry.active)});
218 }
219
220 @Override
221 public SourceEntry deserialize(List<String> entryStr) {
222 if (entryStr.size() < 4)
223 return null;
224 String url = entryStr.get(0);
225 String name = entryStr.get(1);
226 String shortdescription = entryStr.get(2);
227 boolean active = Boolean.parseBoolean(entryStr.get(3));
228 return new SourceEntry(url, name, shortdescription, active);
229 }
230 }
231}
Note: See TracBrowser for help on using the repository browser.