Changeset 74 in josm


Ignore:
Timestamp:
Mar 28, 2006 11:37:37 PM (7 years ago)
Author:
imi
Message:

changed Preferences system to more flexible one.

Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • .classpath

    r58 r74  
    77        <classpathentry kind="lib" path="lib/jdom.jar"/> 
    88        <classpathentry sourcepath="JUNIT_SRC_HOME/junitsrc.zip" kind="var" path="JUNIT_HOME/junit.jar"/> 
    9         <classpathentry sourcepath="C:/projects/MinML2.release" kind="lib" path="lib/MinML2.jar"/> 
     9        <classpathentry kind="lib" path="lib/MinML2.jar"/> 
    1010        <classpathentry kind="output" path="bin"/> 
    1111</classpath> 
  • src/org/openstreetmap/josm/Main.java

    r73 r74  
    99import java.awt.event.WindowEvent; 
    1010import java.io.File; 
     11import java.io.IOException; 
    1112import java.util.Arrays; 
    1213import java.util.Iterator; 
     
    3334import org.openstreetmap.josm.actions.UploadAction; 
    3435import org.openstreetmap.josm.data.Preferences; 
    35 import org.openstreetmap.josm.data.Preferences.PreferencesException; 
    3636import org.openstreetmap.josm.data.osm.DataSet; 
    3737import org.openstreetmap.josm.data.projection.Projection; 
     
    186186                }); 
    187187                setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); 
    188                  
    189                 proj = pref.getProjection(); 
    190188        } 
    191189 
     
    219217                String errMsg = null; 
    220218                try { 
    221                         if (arguments.remove("--reset-preferences")) 
     219                        if (arguments.remove("--reset-preferences")) { 
     220                                pref.resetToDefault(); 
    222221                                pref.save(); 
    223                         else 
     222                        } else 
    224223                                pref.load(); 
    225                 } catch (PreferencesException e1) { 
     224                } catch (RuntimeException x) { 
     225                        //TODO: Temporary code to update user preferences. 
     226                        if (x.getMessage().equals("old version")) { 
     227                                int answer = JOptionPane.showConfirmDialog( 
     228                                                null,  
     229                                                "The preferences - file format has changed.\nThe settings will be reset to default.", 
     230                                                "Information", 
     231                                                JOptionPane.OK_CANCEL_OPTION); 
     232                                if (answer == JOptionPane.CANCEL_OPTION) 
     233                                        System.exit(0); 
     234                                pref.resetToDefault(); 
     235                                try { 
     236                                        pref.save(); 
     237                                } catch (IOException e) { 
     238                                        e.printStackTrace(); 
     239                                        errMsg = "Preferences could not be loaded. Reverting to default."; 
     240                                } 
     241                        } 
     242                } catch (IOException e1) { 
    226243                        e1.printStackTrace(); 
    227244                        errMsg = "Preferences could not be loaded. Write default preference file to '"+Preferences.getPreferencesDir()+"preferences'."; 
     245                        pref.resetToDefault(); 
    228246                        try { 
    229247                                pref.save(); 
    230                         } catch (PreferencesException e) { 
     248                        } catch (IOException e) { 
    231249                                e.printStackTrace(); 
    232250                                errMsg = "Preferences could not be loaded. Reverting to default."; 
     
    235253                if (errMsg != null) 
    236254                        JOptionPane.showMessageDialog(null, errMsg); 
    237                  
     255 
    238256                try { 
    239                         UIManager.setLookAndFeel(pref.laf.getClassName()); 
     257                        proj = (Projection)Class.forName(pref.get("projection")).newInstance(); 
     258                } catch (Exception e) { 
     259                        e.printStackTrace(); 
     260                        JOptionPane.showMessageDialog(null, "The projection could not be initialized. Aborting."); 
     261                        System.exit(1); 
     262                } 
     263                 
     264                try { 
     265                        UIManager.setLookAndFeel(pref.get("laf")); 
    240266                } catch (Exception e) { 
    241267                        e.printStackTrace(); 
  • src/org/openstreetmap/josm/actions/DownloadAction.java

    r73 r74  
    3131import org.openstreetmap.josm.Main; 
    3232import org.openstreetmap.josm.data.Bounds; 
    33 import org.openstreetmap.josm.data.Preferences.PreferencesException; 
    3433import org.openstreetmap.josm.data.coor.LatLon; 
    3534import org.openstreetmap.josm.data.osm.DataSet; 
     
    7877        public void actionPerformed(ActionEvent e) { 
    7978                 
     79                String osmDataServer = Main.pref.get("osmDataServer"); 
    8080                //TODO: Remove this in later versions (temporary only) 
    81                 if (Main.pref.osmDataServer.endsWith("/0.2") || Main.pref.osmDataServer.endsWith("/0.2/")) { 
     81                if (osmDataServer.endsWith("/0.2") || osmDataServer.endsWith("/0.2/")) { 
    8282                        int answer = JOptionPane.showConfirmDialog(Main.main,  
    8383                                        "You seem to have an outdated server entry in your preferences.\n" + 
     
    8989                        if (answer != JOptionPane.YES_OPTION) 
    9090                                return; 
    91                         int cutPos = Main.pref.osmDataServer.endsWith("/0.2") ? 4 : 5; 
    92                         Main.pref.osmDataServer = Main.pref.osmDataServer.substring(0, Main.pref.osmDataServer.length()-cutPos); 
     91                        int cutPos = osmDataServer.endsWith("/0.2") ? 4 : 5; 
     92                        Main.pref.put("osmDataServer", osmDataServer.substring(0, osmDataServer.length()-cutPos)); 
    9393                        try { 
    9494                                Main.pref.save(); 
    95                         } catch (PreferencesException x) { 
     95                        } catch (IOException x) { 
    9696                                x.printStackTrace(); 
    97                                 JOptionPane.showMessageDialog(Main.main, "Could not save the preferences chane:\n" + 
    98                                                 x.getMessage()); 
     97                                JOptionPane.showMessageDialog(Main.main, "Could not save the preferences change:\n" + x.getMessage()); 
    9998                        } 
    10099                } 
  • src/org/openstreetmap/josm/actions/UploadAction.java

    r71 r74  
    55import java.awt.event.InputEvent; 
    66import java.awt.event.KeyEvent; 
     7import java.io.IOException; 
    78import java.util.Collection; 
    89import java.util.LinkedList; 
     
    1718import org.jdom.JDOMException; 
    1819import org.openstreetmap.josm.Main; 
    19 import org.openstreetmap.josm.data.Preferences.PreferencesException; 
    2020import org.openstreetmap.josm.data.osm.OsmPrimitive; 
    2121import org.openstreetmap.josm.gui.OsmPrimitivRenderer; 
     
    4040        public void actionPerformed(ActionEvent e) { 
    4141                 
     42                String osmDataServer = Main.pref.get("osmDataServer"); 
    4243                //TODO: Remove this in later versions (temporary only) 
    43                 if (Main.pref.osmDataServer.endsWith("/0.2") || Main.pref.osmDataServer.endsWith("/0.2/")) { 
     44                if (osmDataServer.endsWith("/0.2") || osmDataServer.endsWith("/0.2/")) { 
    4445                        int answer = JOptionPane.showConfirmDialog(Main.main,  
    4546                                        "You seem to have an outdated server entry in your preferences.\n" + 
     
    5152                        if (answer != JOptionPane.YES_OPTION) 
    5253                                return; 
    53                         int cutPos = Main.pref.osmDataServer.endsWith("/0.2") ? 4 : 5; 
    54                         Main.pref.osmDataServer = Main.pref.osmDataServer.substring(0, Main.pref.osmDataServer.length()-cutPos); 
     54                        int cutPos = osmDataServer.endsWith("/0.2") ? 4 : 5; 
     55                        Main.pref.put("osmDataServer", osmDataServer.substring(0, osmDataServer.length()-cutPos)); 
    5556                        try { 
    5657                                Main.pref.save(); 
    57                         } catch (PreferencesException x) { 
     58                        } catch (IOException x) { 
    5859                                x.printStackTrace(); 
    59                                 JOptionPane.showMessageDialog(Main.main, "Could not save the preferences chane:\n" + 
    60                                                 x.getMessage()); 
     60                                JOptionPane.showMessageDialog(Main.main, "Could not save the preferences change:\n" + x.getMessage()); 
    6161                        } 
    6262                } 
  • src/org/openstreetmap/josm/data/projection/Projection.java

    r71 r74  
    1515        public static double MAX_LON = 180; 
    1616        public static final double MAX_SERVER_PRECISION = 1e12; 
     17 
     18        /** 
     19         * List of all available Projections. 
     20         */ 
     21        public static final Projection[] allProjections = new Projection[]{ 
     22                new Epsg4263(), 
     23                new Mercator() 
     24        }; 
    1725         
    1826        /** 
  • src/org/openstreetmap/josm/gui/MapView.java

    r73 r74  
    66import java.awt.event.ComponentAdapter; 
    77import java.awt.event.ComponentEvent; 
    8 import java.beans.PropertyChangeEvent; 
    9 import java.beans.PropertyChangeListener; 
    108import java.util.ArrayList; 
    119import java.util.Collection; 
     
    4038 * @author imi 
    4139 */ 
    42 public class MapView extends NavigatableComponent implements ChangeListener, PropertyChangeListener { 
     40public class MapView extends NavigatableComponent implements ChangeListener { 
    4341 
    4442        /** 
     
    8583                        } 
    8684                }); 
    87  
    88                 // initialize the movement listener 
    8985                new MapMover(this); 
    90  
    91                 // initialize the projection 
    9286                addLayer(layer); 
    93                 Main.pref.addPropertyChangeListener(this); 
    9487        } 
    9588 
     
    340333                recalculateCenterScale(); 
    341334        } 
    342  
    343         /** 
    344          * Change to the new projection. Recalculate the dataset and zoom, if autoZoom 
    345          * is active. 
    346          * @param oldProjection The old projection. Unregister from this. 
    347          * @param newProjection The new projection. Register as state change listener. 
    348          */ 
    349         public void propertyChange(PropertyChangeEvent evt) { 
    350                 if (evt.getPropertyName().equals("projection")) 
    351                         stateChanged(new ChangeEvent(this)); 
    352         } 
    353335} 
  • src/org/openstreetmap/josm/gui/PreferenceDialog.java

    r73 r74  
    77import java.awt.event.ActionEvent; 
    88import java.awt.event.ActionListener; 
     9import java.io.IOException; 
    910 
    1011import javax.swing.AbstractAction; 
     
    2829 
    2930import org.openstreetmap.josm.Main; 
    30 import org.openstreetmap.josm.data.Preferences; 
    31 import org.openstreetmap.josm.data.Preferences.PreferencesException; 
    3231import org.openstreetmap.josm.data.projection.Projection; 
    3332import org.openstreetmap.josm.tools.GBC; 
     
    5150                } 
    5251                public void actionPerformed(ActionEvent e) { 
    53                         Main.pref.laf = (LookAndFeelInfo)lafCombo.getSelectedItem(); 
    54                         Main.pref.setProjection((Projection)projectionCombo.getSelectedItem()); 
    55                         Main.pref.osmDataServer = osmDataServer.getText(); 
    56                         Main.pref.osmDataUsername = osmDataUsername.getText(); 
    57                         Main.pref.osmDataPassword = String.valueOf(osmDataPassword.getPassword()); 
    58                         if (Main.pref.osmDataPassword == "") 
    59                                 Main.pref.osmDataPassword = null; 
    60                         Main.pref.csvImportString = csvImportString.getText(); 
    61                         Main.pref.setDrawRawGpsLines(drawRawGpsLines.isSelected()); 
    62                         Main.pref.setForceRawGpsLines(forceRawGpsLines.isSelected()); 
     52                        Main.pref.put("laf", ((LookAndFeelInfo)lafCombo.getSelectedItem()).getClassName()); 
     53                        Main.pref.put("projection", projectionCombo.getSelectedItem().getClass().getName()); 
     54                        Main.pref.put("osmDataServer", osmDataServer.getText()); 
     55                        Main.pref.put("osmDataUsername", osmDataUsername.getText()); 
     56                        String pwd = String.valueOf(osmDataPassword.getPassword()); 
     57                        if (pwd.equals("")) 
     58                                pwd = null; 
     59                        Main.pref.put("osmDataPassword", pwd); 
     60                        Main.pref.put("csvImportString", csvImportString.getText()); 
     61                        Main.pref.put("drawRawGpsLines", drawRawGpsLines.isSelected()); 
     62                        Main.pref.put("forceRawGpsLines", forceRawGpsLines.isSelected()); 
    6363                        try { 
    6464                                Main.pref.save(); 
    65                         } catch (PreferencesException x) { 
     65                        } catch (IOException x) { 
    6666                                x.printStackTrace(); 
    6767                                JOptionPane.showMessageDialog(PreferenceDialog.this, "Could not save preferences:\n"+x.getMessage()); 
     
    9898         * Combobox with all projections available 
    9999         */ 
    100         JComboBox projectionCombo = new JComboBox(Preferences.allProjections); 
     100        JComboBox projectionCombo = new JComboBox(Projection.allProjections); 
    101101        /** 
    102102         * The main tab panel. 
     
    140140 
    141141                // look and feel combo box 
     142                lafCombo.setSelectedItem(Main.pref.get("laf")); 
    142143                final ListCellRenderer oldRenderer = lafCombo.getRenderer(); 
    143144                lafCombo.setRenderer(new DefaultListCellRenderer(){ 
    144                         @Override 
    145                         public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
     145                        @Override public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 
    146146                                return oldRenderer.getListCellRendererComponent(list, ((LookAndFeelInfo)value).getName(), index, isSelected, cellHasFocus); 
    147                         }}); 
    148                 lafCombo.setSelectedItem(Main.pref.laf); 
     147                        } 
     148                }); 
    149149                lafCombo.addActionListener(new ActionListener(){ 
    150150                        public void actionPerformed(ActionEvent e) { 
    151151                                requiresRestart = true; 
    152                         }}); 
     152                        } 
     153                }); 
    153154 
    154155                // projection combo box 
    155156                for (int i = 0; i < projectionCombo.getItemCount(); ++i) { 
    156                         if (projectionCombo.getItemAt(i).getClass().equals(Main.pref.getProjection().getClass())) { 
     157                        if (projectionCombo.getItemAt(i).toString().equals(Main.pref.get("projection"))) { 
    157158                                projectionCombo.setSelectedIndex(i); 
    158159                                break; 
     
    185186                                "Other example: \"lat,lon\" will just read lat/lon values comma seperated.</html>"); 
    186187                drawRawGpsLines.setToolTipText("If your gps device draw to few lines, select this to draw lines along your way."); 
    187                 drawRawGpsLines.setSelected(Main.pref.isDrawRawGpsLines()); 
     188                drawRawGpsLines.setSelected(Main.pref.getBoolean("drawRawGpsLines")); 
    188189                forceRawGpsLines.setToolTipText("Force drawing of lines if the imported data contain no line information."); 
    189                 forceRawGpsLines.setSelected(Main.pref.isForceRawGpsLines()); 
     190                forceRawGpsLines.setSelected(Main.pref.getBoolean("forceRawGpsLines")); 
    190191                forceRawGpsLines.setEnabled(drawRawGpsLines.isSelected()); 
    191192 
    192                 osmDataServer.setText(Main.pref.osmDataServer); 
    193                 osmDataUsername.setText(Main.pref.osmDataUsername); 
    194                 osmDataPassword.setText(Main.pref.osmDataPassword); 
    195                 csvImportString.setText(Main.pref.csvImportString); 
     193                osmDataServer.setText(Main.pref.get("osmDataServer")); 
     194                osmDataUsername.setText(Main.pref.get("osmDataUsername")); 
     195                osmDataPassword.setText(Main.pref.get("osmDataPassword")); 
     196                csvImportString.setText(Main.pref.get("csvImportString")); 
    196197 
    197198                // Display tab 
  • src/org/openstreetmap/josm/gui/layer/OsmDataLayer.java

    r71 r74  
    22 
    33import java.awt.Graphics; 
    4 import java.beans.PropertyChangeEvent; 
    5 import java.beans.PropertyChangeListener; 
    64import java.util.Collection; 
    75import java.util.HashSet; 
     
    8179                this.data = data; 
    8280                this.fromDisk = fromDisk; 
    83                 Main.pref.addPropertyChangeListener(new PropertyChangeListener() { 
    84                         public void propertyChange(PropertyChangeEvent evt) { 
    85                                 if (evt.getPropertyName().equals("projection")) 
    86                                         for (Node n : OsmDataLayer.this.data.nodes) 
    87                                                 ((Projection)evt.getNewValue()).latlon2eastNorth(n.coor); 
    88                         } 
    89                 }); 
    9081        } 
    9182 
  • src/org/openstreetmap/josm/gui/layer/RawGpsDataLayer.java

    r73 r74  
    44import java.awt.Graphics; 
    55import java.awt.Point; 
    6 import java.beans.PropertyChangeEvent; 
    7 import java.beans.PropertyChangeListener; 
    86import java.util.Collection; 
    97import java.util.LinkedList; 
     
    1210 
    1311import org.openstreetmap.josm.Main; 
     12import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener; 
     13import org.openstreetmap.josm.data.coor.EastNorth; 
    1414import org.openstreetmap.josm.data.coor.LatLon; 
    15 import org.openstreetmap.josm.data.coor.EastNorth; 
    1615import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor; 
    1716import org.openstreetmap.josm.data.projection.Projection; 
     
    3938                this.data = data; 
    4039                 
    41                 Main.pref.addPropertyChangeListener(new PropertyChangeListener(){ 
    42                         public void propertyChange(PropertyChangeEvent evt) { 
     40                Main.pref.addPreferenceChangedListener(new PreferenceChangedListener(){ 
     41                        public void preferenceChanged(String key, String newValue) { 
    4342                                if (Main.main.getMapFrame() == null) { 
    44                                         Main.pref.removePropertyChangeListener(this); 
     43                                        Main.pref.removePreferenceChangedListener(this); 
    4544                                        return; 
    4645                                } 
    47                                 if (evt.getPropertyName().equals("drawRawGpsLines") || 
    48                                                 evt.getPropertyName().equals("forceRawGpsLines")) 
     46                                if (key.equals("drawRawGpsLines") || key.equals("forceRawGpsLines")) 
    4947                                        Main.main.getMapFrame().repaint(); 
    5048                        } 
     
    6765                Point old = null; 
    6866                for (Collection<EastNorth> c : eastNorth) { 
    69                         if (!Main.pref.isForceRawGpsLines()) 
     67                        if (!Main.pref.getBoolean("forceRawGpsLines")) 
    7068                                old = null; 
    7169                        for (EastNorth eastNorth : c) { 
    7270                                Point screen = mv.getPoint(eastNorth); 
    73                                 if (Main.pref.isDrawRawGpsLines() && old != null) 
     71                                if (Main.pref.getBoolean("drawRawGpsLines") && old != null) 
    7472                                        g.drawLine(old.x, old.y, screen.x, screen.y); 
    7573                                else 
  • src/org/openstreetmap/josm/io/OsmConnection.java

    r71 r74  
    3939                @Override 
    4040                protected PasswordAuthentication getPasswordAuthentication() { 
    41                         String username = Main.pref.osmDataUsername; 
    42                         String password = Main.pref.osmDataPassword; 
    43                         if (passwordtried || "".equals(username) || password == null || "".equals(password)) { 
     41                        String username = Main.pref.get("osmDataUsername"); 
     42                        String password = Main.pref.get("osmDataPassword"); 
     43                        if (passwordtried || username.equals("") || password.equals("")) { 
    4444                                JPanel p = new JPanel(new GridBagLayout()); 
    4545                                p.add(new JLabel("Username"), GBC.std().insets(0,0,10,0)); 
    46                                 JTextField usernameField = new JTextField("".equals(username) ? "" : username, 20); 
     46                                JTextField usernameField = new JTextField(username, 20); 
    4747                                p.add(usernameField, GBC.eol()); 
    4848                                p.add(new JLabel("Password"), GBC.std().insets(0,0,10,0)); 
    49                                 JPasswordField passwordField = new JPasswordField(password == null ? "" : password, 20); 
     49                                JPasswordField passwordField = new JPasswordField(password, 20); 
    5050                                p.add(passwordField, GBC.eol()); 
    5151                                JLabel warning = new JLabel("Warning: The password is transferred unencrypted."); 
     
    5959                                username = usernameField.getText(); 
    6060                                password = String.valueOf(passwordField.getPassword()); 
    61                                 if ("".equals(username)) 
     61                                if (username.equals("")) 
    6262                                        return null; 
    6363                        } 
  • src/org/openstreetmap/josm/io/OsmServerReader.java

    r73 r74  
    4848         */ 
    4949        public Collection<Collection<LatLon>> parseRawGps() throws IOException, JDOMException { 
    50                 String url = Main.pref.osmDataServer+"/0.3/trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page="; 
     50                String url = Main.pref.get("osmDataServer")+"/0.3/trackpoints?bbox="+lon1+","+lat1+","+lon2+","+lat2+"&page="; 
    5151                Collection<Collection<LatLon>> data = new LinkedList<Collection<LatLon>>(); 
    5252                Collection<LatLon> list = new LinkedList<LatLon>(); 
     
    8080         */ 
    8181        public DataSet parseOsm() throws SAXException, IOException { 
    82                 Reader r = getReader(Main.pref.osmDataServer+"/0.3/map?bbox="+lon1+","+lat1+","+lon2+","+lat2); 
     82                Reader r = getReader(Main.pref.get("osmDataServer")+"/0.3/map?bbox="+lon1+","+lat1+","+lon2+","+lat2); 
    8383                if (r == null) 
    8484                        return null; 
  • src/org/openstreetmap/josm/io/OsmServerWriter.java

    r66 r74  
    136136                        OsmPrimitive osm, boolean addBody) { 
    137137                try { 
    138                         URL url = new URL(Main.pref.osmDataServer + "/0.3/" + urlSuffix + "/" + osm.id); 
     138                        URL url = new URL(Main.pref.get("osmDataServer") + "/0.3/" + urlSuffix + "/" + osm.id); 
    139139                        System.out.println("upload to: "+url); 
    140140                        HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
  • src/org/openstreetmap/josm/io/RawCsvReader.java

    r71 r74  
    3030        public Collection<LatLon> parse() throws JDOMException, IOException { 
    3131                Collection<LatLon> data = new LinkedList<LatLon>(); 
    32                 String formatStr = Main.pref.csvImportString; 
     32                String formatStr = Main.pref.get("csvImportString"); 
    3333                if (formatStr == null) 
    3434                        formatStr = in.readLine(); 
     
    5252                // test for completness 
    5353                if (!format.contains("lat") || !format.contains("lon")) { 
    54                         if (Main.pref.csvImportString != null) 
    55                                 throw new JDOMException("Format string is incomplete. Need at least 'lat' and 'lon' specification"); 
    56                         throw new JDOMException("Format string in data is incomplete or not found. Try setting an manual format string in Preferences."); 
     54                        if (Main.pref.get("csvImportString").equals("")) 
     55                                throw new JDOMException("Format string in data is incomplete or not found. Try setting an manual format string in Preferences."); 
     56                        throw new JDOMException("Format string is incomplete. Need at least 'lat' and 'lon' specification"); 
    5757                } 
    5858                 
     
    7171                                                st.nextToken(); 
    7272                                        else 
    73                                                 throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.csvImportString == null ? " Maybe add an format string in preferences." : "")); 
     73                                                throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csvImportString").equals("") ? " Maybe add an format string in preferences." : "")); 
    7474                                } 
    7575                                data.add(new LatLon(lat, lon)); 
  • src/org/openstreetmap/josm/tools/XmlWriter.java

    r72 r74  
    3131         * @return The standard XML1.0 header. Encoding is utf-8 
    3232         */ 
    33         public static Object header() { 
     33        public static String header() { 
    3434                return "<?xml version='1.0' encoding='UTF-8'?>"; 
    3535        } 
Note: See TracChangeset for help on using the changeset viewer.