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

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

Experimental mapcss support. All *.java files in the gui/mappaint/mapcss/parser folder are generated from the javacc source file MapCSSParser.jj in the same folder. The generated code sums up to 2700 lines, there is no further build dependency.

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