| 1 | // License: GPL. Copyright 2007 by Immanuel Scholz and others
|
|---|
| 2 | package org.openstreetmap.josm.gui.preferences;
|
|---|
| 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.Arrays;
|
|---|
| 9 | import java.util.Collection;
|
|---|
| 10 | import java.util.List;
|
|---|
| 11 | import java.util.TreeSet;
|
|---|
| 12 |
|
|---|
| 13 | import javax.swing.BorderFactory;
|
|---|
| 14 | import javax.swing.JCheckBox;
|
|---|
| 15 | import javax.swing.JPanel;
|
|---|
| 16 | import javax.swing.event.ChangeEvent;
|
|---|
| 17 | import javax.swing.event.ChangeListener;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.josm.Main;
|
|---|
| 20 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
|---|
| 21 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
|---|
| 22 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 23 | import org.openstreetmap.josm.tools.Predicate;
|
|---|
| 24 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 25 |
|
|---|
| 26 | public class MapPaintPreference implements PreferenceSetting {
|
|---|
| 27 | private SourceEditor sources;
|
|---|
| 28 | private JCheckBox enableIconDefault;
|
|---|
| 29 |
|
|---|
| 30 | public static class Factory implements PreferenceSettingFactory {
|
|---|
| 31 | public PreferenceSetting createPreferenceSetting() {
|
|---|
| 32 | return new MapPaintPreference();
|
|---|
| 33 | }
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public void addGui(final PreferenceTabbedPane gui) {
|
|---|
| 37 | enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
|
|---|
| 38 | Main.pref.getBoolean("mappaint.icon.enable-defaults", true));
|
|---|
| 39 |
|
|---|
| 40 | sources = new MapPaintSourceEditor();
|
|---|
| 41 |
|
|---|
| 42 | final JPanel panel = new JPanel(new GridBagLayout());
|
|---|
| 43 | panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
|---|
| 44 |
|
|---|
| 45 | panel.add(sources, GBC.eol().fill(GBC.BOTH));
|
|---|
| 46 | panel.add(enableIconDefault, GBC.eol().insets(11,2,5,0));
|
|---|
| 47 |
|
|---|
| 48 | gui.mapcontent.addTab(tr("Map Paint Styles"), panel);
|
|---|
| 49 |
|
|---|
| 50 | // this defers loading of style sources to the first time the tab
|
|---|
| 51 | // with the map paint preferences is selected by the user
|
|---|
| 52 | //
|
|---|
| 53 | gui.mapcontent.addChangeListener(
|
|---|
| 54 | new ChangeListener() {
|
|---|
| 55 | public void stateChanged(ChangeEvent e) {
|
|---|
| 56 | if (gui.mapcontent.getSelectedComponent() == panel) {
|
|---|
| 57 | sources.initiallyLoadAvailableSources();
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | );
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | class MapPaintSourceEditor extends SourceEditor {
|
|---|
| 65 |
|
|---|
| 66 | final private String iconpref = "mappaint.icon.sources";
|
|---|
| 67 |
|
|---|
| 68 | public MapPaintSourceEditor() {
|
|---|
| 69 | super(true, "http://josm.openstreetmap.de/styles");
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | @Override
|
|---|
| 73 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
|---|
| 74 | return MapPaintPrefMigration.INSTANCE.get();
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | @Override
|
|---|
| 78 | public boolean finish() {
|
|---|
| 79 | List<SourceEntry> activeStyles = activeSourcesModel.getSources();
|
|---|
| 80 |
|
|---|
| 81 | boolean changed = MapPaintPrefMigration.INSTANCE.put(activeStyles);
|
|---|
| 82 |
|
|---|
| 83 | if (tblIconPaths != null) {
|
|---|
| 84 | List<String> iconPaths = iconPathsModel.getIconPaths();
|
|---|
| 85 |
|
|---|
| 86 | if (!iconPaths.isEmpty()) {
|
|---|
| 87 | if (Main.pref.putCollection(iconpref, iconPaths)) {
|
|---|
| 88 | changed = true;
|
|---|
| 89 | }
|
|---|
| 90 | } else if (Main.pref.putCollection(iconpref, null)) {
|
|---|
| 91 | changed = true;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | return changed;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public Collection<ExtendedSourceEntry> getDefault() {
|
|---|
| 99 | return MapPaintPrefMigration.INSTANCE.getDefault();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | @Override
|
|---|
| 103 | public Collection<String> getInitialIconPathsList() {
|
|---|
| 104 | return Main.pref.getCollection(iconpref, null);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | @Override
|
|---|
| 108 | public String getStr(I18nString ident) {
|
|---|
| 109 | switch (ident) {
|
|---|
| 110 | case AVAILABLE_SOURCES:
|
|---|
| 111 | return tr("Available styles:");
|
|---|
| 112 | case ACTIVE_SOURCES:
|
|---|
| 113 | return tr("Active styles:");
|
|---|
| 114 | case NEW_SOURCE_ENTRY_TOOLTIP:
|
|---|
| 115 | return tr("Add a new style by entering filename or URL");
|
|---|
| 116 | case NEW_SOURCE_ENTRY:
|
|---|
| 117 | return tr("New style entry:");
|
|---|
| 118 | case REMOVE_SOURCE_TOOLTIP:
|
|---|
| 119 | return tr("Remove the selected styles from the list of active styles");
|
|---|
| 120 | case EDIT_SOURCE_TOOLTIP:
|
|---|
| 121 | return tr("Edit the filename or URL for the selected active style");
|
|---|
| 122 | case ACTIVATE_TOOLTIP:
|
|---|
| 123 | return tr("Add the selected available styles to the list of active styles");
|
|---|
| 124 | case RELOAD_ALL_AVAILABLE:
|
|---|
| 125 | return marktr("Reloads the list of available styles from ''{0}''");
|
|---|
| 126 | case LOADING_SOURCES_FROM:
|
|---|
| 127 | return marktr("Loading style sources from ''{0}''");
|
|---|
| 128 | case FAILED_TO_LOAD_SOURCES_FROM:
|
|---|
| 129 | return marktr("<html>Failed to load the list of style sources from<br>"
|
|---|
| 130 | + "''{0}''.<br>"
|
|---|
| 131 | + "<br>"
|
|---|
| 132 | + "Details (untranslated):<br>{1}</html>");
|
|---|
| 133 | case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
|
|---|
| 134 | return "/Preferences/Styles#FailedToLoadStyleSources";
|
|---|
| 135 | case ILLEGAL_FORMAT_OF_ENTRY:
|
|---|
| 136 | return marktr("Warning: illegal format of entry in style list ''{0}''. Got ''{1}''");
|
|---|
| 137 | default: throw new AssertionError();
|
|---|
| 138 | }
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | public boolean ok() {
|
|---|
| 144 | boolean reload = Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected());
|
|---|
| 145 | reload |= sources.finish();
|
|---|
| 146 | if (reload) {
|
|---|
| 147 | MapPaintStyles.readFromPreferences();
|
|---|
| 148 | }
|
|---|
| 149 | if (Main.isDisplayingMapView())
|
|---|
| 150 | {
|
|---|
| 151 | MapPaintStyles.getStyles().clearCached();
|
|---|
| 152 | }
|
|---|
| 153 | return false;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | /**
|
|---|
| 157 | * Initialize the styles
|
|---|
| 158 | */
|
|---|
| 159 | public static void initialize() {
|
|---|
| 160 | MapPaintStyles.readFromPreferences();
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | public static class MapPaintPrefMigration extends SourceEditor.SourcePrefMigration {
|
|---|
| 164 |
|
|---|
| 165 | public final static MapPaintPrefMigration INSTANCE = new MapPaintPrefMigration();
|
|---|
| 166 |
|
|---|
| 167 | public MapPaintPrefMigration() {
|
|---|
| 168 | super("mappaint.style.sources",
|
|---|
| 169 | "mappaint.style.enable-defaults",
|
|---|
| 170 | "mappaint.style.sources-list");
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | @Override
|
|---|
| 174 | public List<SourceEntry> get() {
|
|---|
| 175 | List<SourceEntry> ls = super.get();
|
|---|
| 176 | if (adapt_elemstyles_xml(ls)) {
|
|---|
| 177 | put(ls);
|
|---|
| 178 | }
|
|---|
| 179 | if (insertNewDefaults(ls)) {
|
|---|
| 180 | put(ls);
|
|---|
| 181 | }
|
|---|
| 182 | return ls;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | /**
|
|---|
| 186 | * The internal path of elemstyles.xml has changed, this
|
|---|
| 187 | * can be removed when a few months have passed.
|
|---|
| 188 | */
|
|---|
| 189 | private boolean adapt_elemstyles_xml(List<SourceEntry> ls) {
|
|---|
| 190 | boolean changed = false;
|
|---|
| 191 | for (SourceEntry se : ls) {
|
|---|
| 192 | if (se.url.equals("resource://data/elemstyles.xml")) {
|
|---|
| 193 | se.url = "resource://styles/standard/elemstyles.xml";
|
|---|
| 194 | changed = true;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 | return changed;
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | /**
|
|---|
| 201 | * If the selection of default styles changes in future releases, add
|
|---|
| 202 | * the new entries to the user-configured list. Remember the known URLs,
|
|---|
| 203 | * so an item that was deleted explicitly is not added again.
|
|---|
| 204 | */
|
|---|
| 205 | private boolean insertNewDefaults(List<SourceEntry> list) {
|
|---|
| 206 | boolean changed = false;
|
|---|
| 207 |
|
|---|
| 208 | Collection<String> knownDefaults = new TreeSet<String>(Main.pref.getCollection("mappaint.style.known-defaults"));
|
|---|
| 209 |
|
|---|
| 210 | Collection<ExtendedSourceEntry> defaults = getDefault();
|
|---|
| 211 | int insertionIdx = 0;
|
|---|
| 212 | for (final SourceEntry def : defaults) {
|
|---|
| 213 | int i = Utils.indexOf(list,
|
|---|
| 214 | new Predicate<SourceEntry>() {
|
|---|
| 215 | @Override
|
|---|
| 216 | public boolean evaluate(SourceEntry se) {
|
|---|
| 217 | return Utils.equal(def.url, se.url);
|
|---|
| 218 | }
|
|---|
| 219 | });
|
|---|
| 220 | if (i == -1 && !knownDefaults.contains(def.url)) {
|
|---|
| 221 | list.add(insertionIdx, def);
|
|---|
| 222 | insertionIdx++;
|
|---|
| 223 | changed = true;
|
|---|
| 224 | } else {
|
|---|
| 225 | if (i >= insertionIdx) {
|
|---|
| 226 | insertionIdx = i + 1;
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | for (SourceEntry def : defaults) {
|
|---|
| 232 | knownDefaults.add(def.url);
|
|---|
| 233 | }
|
|---|
| 234 | Main.pref.putCollection("mappaint.style.known-defaults", knownDefaults);
|
|---|
| 235 |
|
|---|
| 236 | return changed;
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | @Override
|
|---|
| 240 | public Collection<ExtendedSourceEntry> getDefault() {
|
|---|
| 241 | ExtendedSourceEntry defJOSM = new ExtendedSourceEntry("elemstyles.xml", "resource://styles/standard/elemstyles.xml");
|
|---|
| 242 | defJOSM.active = true;
|
|---|
| 243 | defJOSM.name = "standard";
|
|---|
| 244 | defJOSM.title = tr("JOSM Internal Style");
|
|---|
| 245 | defJOSM.description = tr("Internal style to be used as base for runtime switchable overlay styles");
|
|---|
| 246 | ExtendedSourceEntry defPL2 = new ExtendedSourceEntry("potlatch2.mapcss", "resource://styles/standard/potlatch2.mapcss");
|
|---|
| 247 | defPL2.active = false;
|
|---|
| 248 | defPL2.name = "standard";
|
|---|
| 249 | defPL2.title = tr("Potlatch 2");
|
|---|
| 250 | defPL2.description = tr("the main Potlatch 2 style");
|
|---|
| 251 |
|
|---|
| 252 | return Arrays.asList(new ExtendedSourceEntry[] { defJOSM, defPL2 });
|
|---|
| 253 | }
|
|---|
| 254 |
|
|---|
| 255 | @Override
|
|---|
| 256 | public Collection<String> serialize(SourceEntry entry) {
|
|---|
| 257 | return Arrays.asList(new String[] {entry.url, entry.name, entry.title, Boolean.toString(entry.active)});
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | @Override
|
|---|
| 261 | public SourceEntry deserialize(List<String> entryStr) {
|
|---|
| 262 | if (entryStr.size() < 4)
|
|---|
| 263 | return null;
|
|---|
| 264 | String url = entryStr.get(0);
|
|---|
| 265 | String name = entryStr.get(1);
|
|---|
| 266 | String shortdescription = entryStr.get(2);
|
|---|
| 267 | boolean active = Boolean.parseBoolean(entryStr.get(3));
|
|---|
| 268 | return new SourceEntry(url, name, shortdescription, active);
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 | }
|
|---|