| 1 | package org.openstreetmap.josm.data;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.File;
|
|---|
| 4 | import java.io.FileReader;
|
|---|
| 5 | import java.io.FileWriter;
|
|---|
| 6 |
|
|---|
| 7 | import javax.swing.UIManager;
|
|---|
| 8 | import javax.swing.UIManager.LookAndFeelInfo;
|
|---|
| 9 |
|
|---|
| 10 | import org.jdom.Element;
|
|---|
| 11 | import org.jdom.input.SAXBuilder;
|
|---|
| 12 | import org.jdom.output.Format;
|
|---|
| 13 | import org.jdom.output.XMLOutputter;
|
|---|
| 14 | import org.openstreetmap.josm.data.projection.LatitudeLongitude;
|
|---|
| 15 | import org.openstreetmap.josm.data.projection.Projection;
|
|---|
| 16 | import org.openstreetmap.josm.data.projection.UTM;
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * This class holds all preferences for JOSM.
|
|---|
| 21 | *
|
|---|
| 22 | * @author imi
|
|---|
| 23 | */
|
|---|
| 24 | public class Preferences {
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | * The look and feel. Classname of the look and feel class to use.
|
|---|
| 28 | */
|
|---|
| 29 | public LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0];
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * The convertor used to translate lat/lon points to screen points.
|
|---|
| 33 | */
|
|---|
| 34 | public Projection projection = new UTM();
|
|---|
| 35 |
|
|---|
| 36 | /**
|
|---|
| 37 | * The monitor geometry in meter per pixel. (How big is 1 pixel in meters)
|
|---|
| 38 | * Example: 17" Sony flatscreen in 1280x1024 mode: 0.000264 ppm
|
|---|
| 39 | *
|
|---|
| 40 | * Remember: ppm = dpi/25400
|
|---|
| 41 | */
|
|---|
| 42 | public double ppm = 0.000264;
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 | public static final Projection[] allProjections = new Projection[]{
|
|---|
| 46 | new UTM(),
|
|---|
| 47 | new LatitudeLongitude()
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | /**
|
|---|
| 51 | * Return the location of the preferences file
|
|---|
| 52 | */
|
|---|
| 53 | public static String getPreferencesFile() {
|
|---|
| 54 | return System.getProperty("user.home")+"/.josm-preferences";
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | /**
|
|---|
| 58 | * Exception thrown in case of any loading/saving error (including parse errors).
|
|---|
| 59 | * @author imi
|
|---|
| 60 | */
|
|---|
| 61 | public static class PreferencesException extends Exception {
|
|---|
| 62 | public PreferencesException(String message, Throwable cause) {
|
|---|
| 63 | super(message, cause);
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 | /**
|
|---|
| 67 | * Load from disk.
|
|---|
| 68 | * @throws PreferencesException Any loading error (parse errors as well)
|
|---|
| 69 | */
|
|---|
| 70 | public void load() throws PreferencesException {
|
|---|
| 71 | File file = new File(System.getProperty("user.home")+"/.josm-preferences");
|
|---|
| 72 | Element root;
|
|---|
| 73 | try {
|
|---|
| 74 | root = new SAXBuilder().build(new FileReader(file)).getRootElement();
|
|---|
| 75 |
|
|---|
| 76 | // laf
|
|---|
| 77 | String lafClassName = root.getChildText("laf");
|
|---|
| 78 | for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels())
|
|---|
| 79 | if (lafInfo.getClassName().equals(lafClassName)) {
|
|---|
| 80 | laf = lafInfo;
|
|---|
| 81 | break;
|
|---|
| 82 | }
|
|---|
| 83 | if (laf == null)
|
|---|
| 84 | throw new PreferencesException("Look and Feel not found.", null);
|
|---|
| 85 |
|
|---|
| 86 | projection = (Projection)Class.forName(root.getChildText("projection")).newInstance();
|
|---|
| 87 | } catch (Exception e) {
|
|---|
| 88 | if (e instanceof PreferencesException)
|
|---|
| 89 | throw (PreferencesException)e;
|
|---|
| 90 | throw new PreferencesException("Could not load preferences", e);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | }
|
|---|
| 94 | /**
|
|---|
| 95 | * Save to disk.
|
|---|
| 96 | * @throws PreferencesException Any saving error (exceeding disk space, etc..)
|
|---|
| 97 | */
|
|---|
| 98 | @SuppressWarnings("unchecked")
|
|---|
| 99 | public void save() throws PreferencesException {
|
|---|
| 100 | Element root = new Element("josm-settings");
|
|---|
| 101 |
|
|---|
| 102 | root.getChildren().add(new Element("laf").setText(laf.getClassName()));
|
|---|
| 103 | root.getChildren().add(new Element("projection").setText(projection.getClass().getName()));
|
|---|
| 104 |
|
|---|
| 105 | try {
|
|---|
| 106 | final FileWriter file = new FileWriter(getPreferencesFile());
|
|---|
| 107 | new XMLOutputter(Format.getPrettyFormat()).output(root, file);
|
|---|
| 108 | file.close();
|
|---|
| 109 | } catch (Exception e) {
|
|---|
| 110 | throw new PreferencesException("Could not write preferences", e);
|
|---|
| 111 | }
|
|---|
| 112 | }
|
|---|
| 113 | }
|
|---|