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.tr;
|
---|
5 |
|
---|
6 | import java.awt.GridBagLayout;
|
---|
7 | import java.util.Collection;
|
---|
8 | import java.util.TreeSet;
|
---|
9 |
|
---|
10 | import javax.swing.BorderFactory;
|
---|
11 | import javax.swing.JCheckBox;
|
---|
12 | import javax.swing.JComboBox;
|
---|
13 | import javax.swing.JLabel;
|
---|
14 | import javax.swing.JPanel;
|
---|
15 | import javax.swing.event.ChangeEvent;
|
---|
16 | import javax.swing.event.ChangeListener;
|
---|
17 |
|
---|
18 | import org.openstreetmap.josm.Main;
|
---|
19 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
20 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
21 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
22 | import org.openstreetmap.josm.tools.GBC;
|
---|
23 |
|
---|
24 | public class MapPaintPreference implements PreferenceSetting {
|
---|
25 | private StyleSourceEditor sources;
|
---|
26 | private JCheckBox enableIconDefault;
|
---|
27 | private JCheckBox enableDefault;
|
---|
28 | private JComboBox styleCombo = new JComboBox();
|
---|
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 | enableDefault = new JCheckBox(tr("Enable built-in defaults"),
|
---|
38 | Main.pref.getBoolean("mappaint.style.enable-defaults", true));
|
---|
39 | enableIconDefault = new JCheckBox(tr("Enable built-in icon defaults"),
|
---|
40 | Main.pref.getBoolean("mappaint.icon.enable-defaults", true));
|
---|
41 |
|
---|
42 | sources = new StyleSourceEditor("mappaint.style.sources", "mappaint.icon.sources",
|
---|
43 | "http://josm.openstreetmap.de/styles");
|
---|
44 |
|
---|
45 | Collection<String> styles = new TreeSet<String>(MapPaintStyles.getStyles().getStyleNames());
|
---|
46 | String defstyle = Main.pref.get("mappaint.style", "standard");
|
---|
47 | styles.add(defstyle);
|
---|
48 | for(String style : styles) {
|
---|
49 | styleCombo.addItem(style);
|
---|
50 | }
|
---|
51 |
|
---|
52 | styleCombo.setEditable(true);
|
---|
53 | for (int i = 0; i < styleCombo.getItemCount(); ++i) {
|
---|
54 | if (((String)styleCombo.getItemAt(i)).equals(defstyle)) {
|
---|
55 | styleCombo.setSelectedIndex(i);
|
---|
56 | break;
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | final JPanel panel = new JPanel(new GridBagLayout());
|
---|
61 | panel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 ));
|
---|
62 | panel.add(enableDefault, GBC.std().insets(5,5,5,0));
|
---|
63 | panel.add(enableIconDefault, GBC.eol().insets(5,5,5,0));
|
---|
64 |
|
---|
65 | panel.add(new JLabel(tr("Used style")), GBC.std().insets(5,5,0,5));
|
---|
66 | panel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
|
---|
67 | panel.add(styleCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,0,5,0));
|
---|
68 |
|
---|
69 | panel.add(sources, GBC.eol().fill(GBC.BOTH));
|
---|
70 | gui.mapcontent.addTab(tr("Map Paint Styles"), panel);
|
---|
71 |
|
---|
72 | // this defers loading of style sources to the first time the tab
|
---|
73 | // with the map paint preferences is selected by the user
|
---|
74 | //
|
---|
75 | gui.mapcontent.addChangeListener(
|
---|
76 | new ChangeListener() {
|
---|
77 | public void stateChanged(ChangeEvent e) {
|
---|
78 | if (gui.mapcontent.getSelectedComponent() == panel) {
|
---|
79 | sources.initiallyLoadAvailableStyles();
|
---|
80 | }
|
---|
81 | }
|
---|
82 | }
|
---|
83 | );
|
---|
84 | }
|
---|
85 |
|
---|
86 | public boolean ok() {
|
---|
87 | Boolean restart = Main.pref.put("mappaint.style.enable-defaults", enableDefault.isSelected());
|
---|
88 | if(Main.pref.put("mappaint.icon.enable-defaults", enableIconDefault.isSelected())) {
|
---|
89 | restart = true;
|
---|
90 | }
|
---|
91 | if(sources.finish()) {
|
---|
92 | restart = true;
|
---|
93 | }
|
---|
94 | if(Main.pref.put("mappaint.style", styleCombo.getEditor().getItem().toString())
|
---|
95 | && Main.isDisplayingMapView())
|
---|
96 | {
|
---|
97 | for(OsmDataLayer l : Main.map.mapView.getLayersOfType(OsmDataLayer.class))
|
---|
98 | {
|
---|
99 | for(OsmPrimitive osm : l.data.allPrimitives())
|
---|
100 | {
|
---|
101 | osm.clearCached();
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | return restart;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * Initialize the styles
|
---|
110 | */
|
---|
111 | public static void initialize() {
|
---|
112 | MapPaintStyles.readFromPreferences();
|
---|
113 | }
|
---|
114 | }
|
---|