| 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.Collections;
|
|---|
| 11 | import java.util.List;
|
|---|
| 12 | import java.util.TreeSet;
|
|---|
| 13 |
|
|---|
| 14 | import javax.swing.BorderFactory;
|
|---|
| 15 | import javax.swing.JCheckBox;
|
|---|
| 16 | import javax.swing.JComboBox;
|
|---|
| 17 | import javax.swing.JLabel;
|
|---|
| 18 | import javax.swing.JPanel;
|
|---|
| 19 | import javax.swing.event.ChangeEvent;
|
|---|
| 20 | import javax.swing.event.ChangeListener;
|
|---|
| 21 |
|
|---|
| 22 | import org.openstreetmap.josm.Main;
|
|---|
| 23 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
|---|
| 24 | import org.openstreetmap.josm.gui.preferences.SourceEditor.ExtendedSourceEntry;
|
|---|
| 25 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 26 |
|
|---|
| 27 | public class MapPaintPreference implements PreferenceSetting {
|
|---|
| 28 | private SourceEditor sources;
|
|---|
| 29 | private JCheckBox enableIconDefault;
|
|---|
| 30 |
|
|---|
| 31 | public static class Factory implements PreferenceSettingFactory {
|
|---|
| 32 | public PreferenceSetting createPreferenceSetting() {
|
|---|
| 33 | return new MapPaintPreference();
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | public void addGui(final PreferenceTabbedPane gui) {
|
|---|
| 38 | enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
|
|---|
| 39 | Main.pref.getBoolean("mappaint.icon.enable-defaults", true));
|
|---|
| 40 |
|
|---|
| 41 | sources = new MapPaintSourceEditor();
|
|---|
| 42 |
|
|---|
| 43 | final JPanel panel = new JPanel(new GridBagLayout());
|
|---|
| 44 | panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
|---|
| 45 |
|
|---|
| 46 | panel.add(sources, GBC.eol().fill(GBC.BOTH));
|
|---|
| 47 | panel.add(enableIconDefault, GBC.eol().insets(11,2,5,0));
|
|---|
| 48 |
|
|---|
| 49 | gui.mapcontent.addTab(tr("Map Paint Styles"), panel);
|
|---|
| 50 |
|
|---|
| 51 | // this defers loading of style sources to the first time the tab
|
|---|
| 52 | // with the map paint preferences is selected by the user
|
|---|
| 53 | //
|
|---|
| 54 | gui.mapcontent.addChangeListener(
|
|---|
| 55 | new ChangeListener() {
|
|---|
| 56 | public void stateChanged(ChangeEvent e) {
|
|---|
| 57 | if (gui.mapcontent.getSelectedComponent() == panel) {
|
|---|
| 58 | sources.initiallyLoadAvailableSources();
|
|---|
| 59 | }
|
|---|
| 60 | }
|
|---|
| 61 | }
|
|---|
| 62 | );
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | class MapPaintSourceEditor extends SourceEditor {
|
|---|
| 66 |
|
|---|
| 67 | final private String iconpref = "mappaint.icon.sources";
|
|---|
| 68 |
|
|---|
| 69 | public MapPaintSourceEditor() {
|
|---|
| 70 | super("http://josm.openstreetmap.de/styles");
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | @Override
|
|---|
| 74 | public Collection<? extends SourceEntry> getInitialSourcesList() {
|
|---|
| 75 | return (new MapPaintPrefMigration()).get();
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | @Override
|
|---|
| 79 | public boolean finish() {
|
|---|
| 80 | List<SourceEntry> activeStyles = activeSourcesModel.getSources();
|
|---|
| 81 |
|
|---|
| 82 | boolean changed = (new MapPaintPrefMigration()).put(activeStyles);
|
|---|
| 83 |
|
|---|
| 84 | if (tblIconPaths != null) {
|
|---|
| 85 | List<String> iconPaths = iconPathsModel.getIconPaths();
|
|---|
| 86 |
|
|---|
| 87 | if (!iconPaths.isEmpty()) {
|
|---|
| 88 | if (Main.pref.putCollection(iconpref, iconPaths)) {
|
|---|
| 89 | changed = true;
|
|---|
| 90 | }
|
|---|
| 91 | } else if (Main.pref.putCollection(iconpref, null)) {
|
|---|
| 92 | changed = true;
|
|---|
| 93 | }
|
|---|
| 94 | }
|
|---|
| 95 | return changed;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | @Override
|
|---|
| 99 | public Collection<ExtendedSourceEntry> getDefault() {
|
|---|
| 100 | return (new MapPaintPrefMigration()).getDefault();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | @Override
|
|---|
| 104 | public Collection<String> getInitialIconPathsList() {
|
|---|
| 105 | return Main.pref.getCollection(iconpref, null);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | @Override
|
|---|
| 109 | public String getStr(I18nString ident) {
|
|---|
| 110 | switch (ident) {
|
|---|
| 111 | case AVAILABLE_SOURCES:
|
|---|
| 112 | return tr("Available styles:");
|
|---|
| 113 | case ACTIVE_SOURCES:
|
|---|
| 114 | return tr("Active styles:");
|
|---|
| 115 | case NEW_SOURCE_ENTRY_TOOLTIP:
|
|---|
| 116 | return tr("Add a new style by entering filename or URL");
|
|---|
| 117 | case NEW_SOURCE_ENTRY:
|
|---|
| 118 | return tr("New style entry:");
|
|---|
| 119 | case REMOVE_SOURCE_TOOLTIP:
|
|---|
| 120 | return tr("Remove the selected styles from the list of active styles");
|
|---|
| 121 | case EDIT_SOURCE_TOOLTIP:
|
|---|
| 122 | return tr("Edit the filename or URL for the selected active style");
|
|---|
| 123 | case ACTIVATE_TOOLTIP:
|
|---|
| 124 | return tr("Add the selected available styles to the list of active styles");
|
|---|
| 125 | case RELOAD_ALL_AVAILABLE:
|
|---|
| 126 | return marktr("Reloads the list of available styles from ''{0}''");
|
|---|
| 127 | case LOADING_SOURCES_FROM:
|
|---|
| 128 | return marktr("Loading style sources from ''{0}''");
|
|---|
| 129 | case FAILED_TO_LOAD_SOURCES_FROM:
|
|---|
| 130 | return marktr("<html>Failed to load the list of style sources from<br>"
|
|---|
| 131 | + "''{0}''.<br>"
|
|---|
| 132 | + "<br>"
|
|---|
| 133 | + "Details (untranslated):<br>{1}</html>");
|
|---|
| 134 | case FAILED_TO_LOAD_SOURCES_FROM_HELP_TOPIC:
|
|---|
| 135 | return "/Preferences/Styles#FailedToLoadStyleSources";
|
|---|
| 136 | case ILLEGAL_FORMAT_OF_ENTRY:
|
|---|
| 137 | return marktr("Warning: illegal format of entry in style list ''{0}''. Got ''{1}''");
|
|---|
| 138 | default: throw new AssertionError();
|
|---|
| 139 | }
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | public boolean ok() {
|
|---|
| 145 | Boolean restart = false;
|
|---|
| 146 | if(Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected())) {
|
|---|
| 147 | restart = true;
|
|---|
| 148 | }
|
|---|
| 149 | if(sources.finish()) {
|
|---|
| 150 | restart = true;
|
|---|
| 151 | }
|
|---|
| 152 | if(Main.isDisplayingMapView())
|
|---|
| 153 | {
|
|---|
| 154 | MapPaintStyles.getStyles().clearCached();
|
|---|
| 155 | }
|
|---|
| 156 | return restart;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | /**
|
|---|
| 160 | * Initialize the styles
|
|---|
| 161 | */
|
|---|
| 162 | public static void initialize() {
|
|---|
| 163 | MapPaintStyles.readFromPreferences();
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | public static class MapPaintPrefMigration extends SourceEditor.SourcePrefMigration {
|
|---|
| 167 |
|
|---|
| 168 | public MapPaintPrefMigration() {
|
|---|
| 169 | super("mappaint.style.sources",
|
|---|
| 170 | "mappaint.style.enable-defaults",
|
|---|
| 171 | "mappaint.style.sources-list");
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | @Override
|
|---|
| 175 | public Collection<ExtendedSourceEntry> getDefault() {
|
|---|
| 176 | ExtendedSourceEntry i = new ExtendedSourceEntry("elemstyles.xml", "resource://data/elemstyles.xml");
|
|---|
| 177 | i.name = "standard";
|
|---|
| 178 | i.shortdescription = tr("Internal Style");
|
|---|
| 179 | i.description = tr("Internal style to be used as base for runtime switchable overlay styles");
|
|---|
| 180 | return Collections.singletonList(i);
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | @Override
|
|---|
| 184 | public Collection<String> serialize(SourceEntry entry) {
|
|---|
| 185 | return Arrays.asList(new String[] {entry.url, entry.name, entry.shortdescription, Boolean.toString(entry.active)});
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | @Override
|
|---|
| 189 | public SourceEntry deserialize(List<String> entryStr) {
|
|---|
| 190 | if (entryStr.size() < 4)
|
|---|
| 191 | return null;
|
|---|
| 192 | String url = entryStr.get(0);
|
|---|
| 193 | String name = entryStr.get(1);
|
|---|
| 194 | String shortdescription = entryStr.get(2);
|
|---|
| 195 | boolean active = Boolean.parseBoolean(entryStr.get(3));
|
|---|
| 196 | return new SourceEntry(url, name, shortdescription, active);
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|