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

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

see #15182 - code refactoring to avoid dependence on GUI packages from Preferences

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