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

Last change on this file since 12846 was 12846, checked in by bastiK, 7 years ago

see #15229 - use Config.getPref() wherever possible

  • Property svn:eol-style set to native
File size: 7.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
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.Collection;
10import java.util.List;
11
12import javax.swing.BorderFactory;
13import javax.swing.JCheckBox;
14import javax.swing.JPanel;
15
16import org.openstreetmap.josm.Main;
17import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
18import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
19import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
20import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
21import org.openstreetmap.josm.data.preferences.sources.SourceType;
22import org.openstreetmap.josm.gui.MainApplication;
23import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
24import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
25import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
26import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
27import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
28import org.openstreetmap.josm.gui.preferences.SourceEditor;
29import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
30import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
31import org.openstreetmap.josm.spi.preferences.Config;
32import org.openstreetmap.josm.tools.GBC;
33import org.openstreetmap.josm.tools.Logging;
34
35/**
36 * Preference settings for map paint styles.
37 */
38public class MapPaintPreference implements SubPreferenceSetting {
39 private SourceEditor sources;
40 private JCheckBox enableIconDefault;
41
42 private static final List<SourceProvider> styleSourceProviders = new ArrayList<>();
43
44 /**
45 * Registers a new additional style source provider.
46 * @param provider The style source provider
47 * @return {@code true}, if the provider has been added, {@code false} otherwise
48 */
49 public static boolean registerSourceProvider(SourceProvider provider) {
50 if (provider != null)
51 return styleSourceProviders.add(provider);
52 return false;
53 }
54
55 /**
56 * Factory used to create a new {@code MapPaintPreference}.
57 */
58 public static class Factory implements PreferenceSettingFactory {
59 @Override
60 public PreferenceSetting createPreferenceSetting() {
61 return new MapPaintPreference();
62 }
63 }
64
65 @Override
66 public void addGui(PreferenceTabbedPane gui) {
67 enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
68 Config.getPref().getBoolean("mappaint.icon.enable-defaults", true));
69
70 sources = new MapPaintSourceEditor();
71
72 final JPanel panel = new JPanel(new GridBagLayout());
73 panel.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
74
75 panel.add(sources, GBC.eol().fill(GBC.BOTH));
76 panel.add(enableIconDefault, GBC.eol().insets(11, 2, 5, 0));
77
78 final MapPreference mapPref = gui.getMapPreference();
79 mapPref.addSubTab(this, tr("Map Paint Styles"), panel);
80 sources.deferLoading(mapPref, panel);
81 }
82
83 static class MapPaintSourceEditor extends SourceEditor {
84
85 private static final String ICONPREF = "mappaint.icon.sources";
86
87 MapPaintSourceEditor() {
88 super(SourceType.MAP_PAINT_STYLE, Main.getJOSMWebsite()+"/styles", styleSourceProviders, true);
89 }
90
91 @Override
92 public Collection<? extends SourceEntry> getInitialSourcesList() {
93 return MapPaintPrefHelper.INSTANCE.get();
94 }
95
96 @Override
97 public boolean finish() {
98 return doFinish(MapPaintPrefHelper.INSTANCE, ICONPREF);
99 }
100
101 @Override
102 public Collection<ExtendedSourceEntry> getDefault() {
103 return MapPaintPrefHelper.INSTANCE.getDefault();
104 }
105
106 @Override
107 public Collection<String> getInitialIconPathsList() {
108 return Config.getPref().getList(ICONPREF, null);
109 }
110
111 @Override
112 public String getStr(I18nString ident) {
113 switch (ident) {
114 case AVAILABLE_SOURCES:
115 return tr("Available styles:");
116 case ACTIVE_SOURCES:
117 return tr("Active styles:");
118 case NEW_SOURCE_ENTRY_TOOLTIP:
119 return tr("Add a new style by entering filename or URL");
120 case NEW_SOURCE_ENTRY:
121 return tr("New style entry:");
122 case REMOVE_SOURCE_TOOLTIP:
123 return tr("Remove the selected styles from the list of active styles");
124 case EDIT_SOURCE_TOOLTIP:
125 return tr("Edit the filename or URL for the selected active style");
126 case ACTIVATE_TOOLTIP:
127 return tr("Add the selected available styles to the list of active styles");
128 case RELOAD_ALL_AVAILABLE:
129 return marktr("Reloads the list of available styles from ''{0}''");
130 case LOADING_SOURCES_FROM:
131 return marktr("Loading style sources from ''{0}''");
132 case FAILED_TO_LOAD_SOURCES_FROM:
133 return marktr("<html>Failed to load the list of style sources from<br>"
134 + "''{0}''.<br>"
135 + "<br>"
136 + "Details (untranslated):<br>{1}</html>");
137 case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
138 return "/Preferences/Styles#FailedToLoadStyleSources";
139 case ILLEGAL_FORMAT_OF_ENTRY:
140 return marktr("Warning: illegal format of entry in style list ''{0}''. Got ''{1}''");
141 default: throw new AssertionError();
142 }
143 }
144
145 @Override
146 protected String getTitleForSourceEntry(SourceEntry entry) {
147 final String title = getTitleFromSourceEntry(entry);
148 return title != null ? title : super.getTitleForSourceEntry(entry);
149 }
150 }
151
152 /**
153 * Returns title from a source entry.
154 * @param entry source entry
155 * @return title
156 * @see MapCSSStyleSource#title
157 */
158 public static String getTitleFromSourceEntry(SourceEntry entry) {
159 try {
160 final MapCSSStyleSource css = new MapCSSStyleSource(entry);
161 css.loadStyleSource();
162 if (css.title != null && !css.title.isEmpty()) {
163 return css.title;
164 }
165 } catch (RuntimeException ignore) { // NOPMD
166 Logging.debug(ignore);
167 }
168 return null;
169 }
170
171 @Override
172 public boolean ok() {
173 boolean reload = Config.getPref().putBoolean("mappaint.icon.enable-defaults", enableIconDefault.isSelected());
174 reload |= sources.finish();
175 if (reload) {
176 MapPaintStyles.readFromPreferences();
177 }
178 if (MainApplication.isDisplayingMapView()) {
179 MapPaintStyles.getStyles().clearCached();
180 }
181 return false;
182 }
183
184 /**
185 * Initialize the styles
186 */
187 public static void initialize() {
188 MapPaintStyles.readFromPreferences();
189 }
190
191 @Override
192 public boolean isExpert() {
193 return false;
194 }
195
196 @Override
197 public TabPreferenceSetting getTabPreferenceSetting(final PreferenceTabbedPane gui) {
198 return gui.getMapPreference();
199 }
200}
Note: See TracBrowser for help on using the repository browser.