1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.preferences.map;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.awt.GridBagLayout;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.swing.BorderFactory;
|
---|
13 | import javax.swing.JCheckBox;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.data.preferences.sources.ExtendedSourceEntry;
|
---|
17 | import org.openstreetmap.josm.data.preferences.sources.MapPaintPrefHelper;
|
---|
18 | import org.openstreetmap.josm.data.preferences.sources.SourceEntry;
|
---|
19 | import org.openstreetmap.josm.data.preferences.sources.SourceProvider;
|
---|
20 | import org.openstreetmap.josm.data.preferences.sources.SourceType;
|
---|
21 | import org.openstreetmap.josm.gui.MainApplication;
|
---|
22 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.MapCSSStyleSource;
|
---|
24 | import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
|
---|
25 | import org.openstreetmap.josm.gui.preferences.PreferenceSettingFactory;
|
---|
26 | import org.openstreetmap.josm.gui.preferences.PreferenceTabbedPane;
|
---|
27 | import org.openstreetmap.josm.gui.preferences.SourceEditor;
|
---|
28 | import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
|
---|
29 | import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
|
---|
30 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
31 | import org.openstreetmap.josm.tools.GBC;
|
---|
32 | import org.openstreetmap.josm.tools.Logging;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Preference settings for map paint styles.
|
---|
36 | */
|
---|
37 | public 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 | Config.getPref().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, Config.getUrls().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 Config.getPref().getList(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 = Config.getPref().putBoolean("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 | }
|
---|