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