1 | package org.openstreetmap.josm.data;
|
---|
2 |
|
---|
3 | import java.beans.PropertyChangeEvent;
|
---|
4 | import java.beans.PropertyChangeListener;
|
---|
5 | import java.io.File;
|
---|
6 | import java.io.FileReader;
|
---|
7 | import java.io.FileWriter;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.LinkedList;
|
---|
11 | import java.util.List;
|
---|
12 |
|
---|
13 | import javax.swing.UIManager;
|
---|
14 | import javax.swing.UIManager.LookAndFeelInfo;
|
---|
15 |
|
---|
16 | import org.jdom.Element;
|
---|
17 | import org.jdom.input.SAXBuilder;
|
---|
18 | import org.jdom.output.Format;
|
---|
19 | import org.jdom.output.XMLOutputter;
|
---|
20 | import org.openstreetmap.josm.data.projection.Epsg4263;
|
---|
21 | import org.openstreetmap.josm.data.projection.Mercator;
|
---|
22 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
23 |
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * This class holds all preferences for JOSM.
|
---|
27 | *
|
---|
28 | * @author imi
|
---|
29 | */
|
---|
30 | public class Preferences {
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * The look and feel. Classname of the look and feel class to use.
|
---|
34 | */
|
---|
35 | public LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0];
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * The convertor used to translate lat/lon points to screen points.
|
---|
39 | */
|
---|
40 | private Projection projection = new Epsg4263();
|
---|
41 |
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Whether lines should be drawn between track points of raw gps data.
|
---|
45 | */
|
---|
46 | private boolean drawRawGpsLines = false;
|
---|
47 | /**
|
---|
48 | * Force the drawing of lines between raw gps points if there are no
|
---|
49 | * lines in the imported document.
|
---|
50 | */
|
---|
51 | private boolean forceRawGpsLines = false;
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Base URL to the osm data server
|
---|
55 | */
|
---|
56 | public String osmDataServer = "http://www.openstreetmap.org/api";
|
---|
57 | /**
|
---|
58 | * The username to the osm server
|
---|
59 | */
|
---|
60 | public String osmDataUsername = "";
|
---|
61 | /**
|
---|
62 | * The stored password or <code>null</code>, if the password should not be
|
---|
63 | * stored.
|
---|
64 | */
|
---|
65 | public String osmDataPassword = null;
|
---|
66 | /**
|
---|
67 | * The csv input style string or <code>null</code> for auto. The style is a
|
---|
68 | * comma seperated list of identifiers as specified in the tooltip help text
|
---|
69 | * of csvImportString in PreferenceDialog.
|
---|
70 | *
|
---|
71 | * @see org.openstreetmap.josm.gui.PreferenceDialog#csvImportString
|
---|
72 | */
|
---|
73 | public String csvImportString = null;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * List of all available Projections.
|
---|
77 | */
|
---|
78 | public static final Projection[] allProjections = new Projection[]{
|
---|
79 | new Epsg4263(),
|
---|
80 | new Mercator()
|
---|
81 | };
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Return the location of the preferences file
|
---|
85 | */
|
---|
86 | public static String getPreferencesDir() {
|
---|
87 | return System.getProperty("user.home")+"/.josm/";
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Exception thrown in case of any loading/saving error (including parse errors).
|
---|
92 | * @author imi
|
---|
93 | */
|
---|
94 | public static class PreferencesException extends Exception {
|
---|
95 | public PreferencesException(String message, Throwable cause) {
|
---|
96 | super(message, cause);
|
---|
97 | }
|
---|
98 | public PreferencesException(String message) {
|
---|
99 | super(message);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | /**
|
---|
103 | * Load from disk.
|
---|
104 | * @throws PreferencesException Any loading error (parse errors as well)
|
---|
105 | */
|
---|
106 | public void load() throws PreferencesException {
|
---|
107 | File file = new File(getPreferencesDir()+"/preferences");
|
---|
108 | Element root;
|
---|
109 | try {
|
---|
110 | root = new SAXBuilder().build(new FileReader(file)).getRootElement();
|
---|
111 |
|
---|
112 | // laf
|
---|
113 | String lafClassName = root.getChildText("laf");
|
---|
114 | for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels())
|
---|
115 | if (lafInfo.getClassName().equals(lafClassName)) {
|
---|
116 | laf = lafInfo;
|
---|
117 | break;
|
---|
118 | }
|
---|
119 | if (laf == null)
|
---|
120 | throw new PreferencesException("Look and Feel not found.", null);
|
---|
121 |
|
---|
122 | // projection
|
---|
123 | Class<?> projectionClass = Class.forName(root.getChildText("projection"));
|
---|
124 | projection = allProjections[0];
|
---|
125 | for (Projection p : allProjections) {
|
---|
126 | if (p.getClass() == projectionClass) {
|
---|
127 | projection = p;
|
---|
128 | break;
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | Element osmServer = root.getChild("osm-server");
|
---|
133 | if (osmServer != null) {
|
---|
134 | osmDataServer = osmServer.getChildText("url");
|
---|
135 | osmDataUsername = osmServer.getChildText("username");
|
---|
136 | osmDataPassword = osmServer.getChildText("password");
|
---|
137 | csvImportString = osmServer.getChildText("csvImportString");
|
---|
138 | }
|
---|
139 | drawRawGpsLines = root.getChild("drawRawGpsLines") != null;
|
---|
140 | forceRawGpsLines = root.getChild("forceRawGpsLines") != null;
|
---|
141 | } catch (Exception e) {
|
---|
142 | if (e instanceof PreferencesException)
|
---|
143 | throw (PreferencesException)e;
|
---|
144 | throw new PreferencesException("Could not load preferences", e);
|
---|
145 | }
|
---|
146 |
|
---|
147 | }
|
---|
148 | /**
|
---|
149 | * Save to disk.
|
---|
150 | * @throws PreferencesException Any saving error (exceeding disk space, etc..)
|
---|
151 | */
|
---|
152 | @SuppressWarnings("unchecked")
|
---|
153 | public void save() throws PreferencesException {
|
---|
154 | Element root = new Element("josm-settings");
|
---|
155 |
|
---|
156 | List children = root.getChildren();
|
---|
157 | children.add(new Element("laf").setText(laf.getClassName()));
|
---|
158 | children.add(new Element("projection").setText(getProjection().getClass().getName()));
|
---|
159 | if (drawRawGpsLines)
|
---|
160 | children.add(new Element("drawRawGpsLines"));
|
---|
161 | if (forceRawGpsLines)
|
---|
162 | children.add(new Element("forceRawGpsLines"));
|
---|
163 | Element osmServer = new Element("osm-server");
|
---|
164 | osmServer.getChildren().add(new Element("url").setText(osmDataServer));
|
---|
165 | osmServer.getChildren().add(new Element("username").setText(osmDataUsername));
|
---|
166 | osmServer.getChildren().add(new Element("password").setText(osmDataPassword));
|
---|
167 | osmServer.getChildren().add(new Element("csvImportString").setText(csvImportString));
|
---|
168 | children.add(osmServer);
|
---|
169 |
|
---|
170 | try {
|
---|
171 | File prefDir = new File(getPreferencesDir());
|
---|
172 | if (prefDir.exists() && !prefDir.isDirectory())
|
---|
173 | throw new PreferencesException("Preferences directory "+getPreferencesDir()+" is not a directory.");
|
---|
174 | if (!prefDir.exists())
|
---|
175 | prefDir.mkdirs();
|
---|
176 |
|
---|
177 | FileWriter file = new FileWriter(getPreferencesDir()+"/preferences");
|
---|
178 | new XMLOutputter(Format.getPrettyFormat()).output(root, file);
|
---|
179 | file.close();
|
---|
180 | } catch (IOException e) {
|
---|
181 | throw new PreferencesException("Could not write preferences", e);
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | // projection change listener stuff
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * The list of all listeners to projection changes.
|
---|
189 | */
|
---|
190 | private Collection<PropertyChangeListener> listener = new LinkedList<PropertyChangeListener>();
|
---|
191 |
|
---|
192 | /**
|
---|
193 | * Add a listener of projection changes to the list of listeners.
|
---|
194 | * @param listener The listerner to add.
|
---|
195 | */
|
---|
196 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
197 | if (listener != null)
|
---|
198 | this.listener.add(listener);
|
---|
199 | }
|
---|
200 | /**
|
---|
201 | * Remove the listener from the list.
|
---|
202 | */
|
---|
203 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
204 | this.listener.remove(listener);
|
---|
205 | }
|
---|
206 | /**
|
---|
207 | * Fires a PropertyChangeEvent if the old value differs from the new value.
|
---|
208 | */
|
---|
209 | private <T> void firePropertyChanged(String name, T oldValue, T newValue) {
|
---|
210 | if (oldValue == newValue)
|
---|
211 | return;
|
---|
212 | PropertyChangeEvent evt = null;
|
---|
213 | for (PropertyChangeListener l : listener) {
|
---|
214 | if (evt == null)
|
---|
215 | evt = new PropertyChangeEvent(this, name, oldValue, newValue);
|
---|
216 | l.propertyChange(evt);
|
---|
217 | }
|
---|
218 | }
|
---|
219 |
|
---|
220 | // getter / setter
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Set the projection and fire an event to all ProjectionChangeListener
|
---|
224 | * @param projection The new Projection.
|
---|
225 | */
|
---|
226 | public void setProjection(Projection projection) {
|
---|
227 | Projection old = this.projection;
|
---|
228 | this.projection = projection;
|
---|
229 | firePropertyChanged("projection", old, projection);
|
---|
230 | }
|
---|
231 | /**
|
---|
232 | * Get the current projection.
|
---|
233 | * @return The current projection set.
|
---|
234 | */
|
---|
235 | public Projection getProjection() {
|
---|
236 | return projection;
|
---|
237 | }
|
---|
238 | public void setDrawRawGpsLines(boolean drawRawGpsLines) {
|
---|
239 | boolean old = this.drawRawGpsLines;
|
---|
240 | this.drawRawGpsLines = drawRawGpsLines;
|
---|
241 | firePropertyChanged("drawRawGpsLines", old, drawRawGpsLines);
|
---|
242 | }
|
---|
243 | public boolean isDrawRawGpsLines() {
|
---|
244 | return drawRawGpsLines;
|
---|
245 | }
|
---|
246 | public void setForceRawGpsLines(boolean forceRawGpsLines) {
|
---|
247 | boolean old = this.forceRawGpsLines;
|
---|
248 | this.forceRawGpsLines = forceRawGpsLines;
|
---|
249 | firePropertyChanged("forceRawGpsLines", old, forceRawGpsLines);
|
---|
250 | }
|
---|
251 | public boolean isForceRawGpsLines() {
|
---|
252 | return forceRawGpsLines;
|
---|
253 | }
|
---|
254 | }
|
---|