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