Changeset 16 in josm for src/org/openstreetmap/josm/data


Ignore:
Timestamp:
2005-10-08T17:14:54+02:00 (19 years ago)
Author:
imi
Message:
  • reverted to 14, but kept the global Projection.
  • improved the preference settings for projections.
Location:
src/org/openstreetmap/josm/data
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/data/Preferences.java

    r15 r16  
    11package org.openstreetmap.josm.data;
    22
    3 import java.beans.PropertyChangeEvent;
    4 import java.beans.PropertyChangeListener;
    53import java.io.File;
    64import java.io.FileReader;
    75import java.io.FileWriter;
     6import java.util.Collection;
    87import java.util.LinkedList;
    98import java.util.List;
     
    2827public class Preferences {
    2928
    30        
    3129        /**
    3230         * The look and feel. Classname of the look and feel class to use.
    3331         */
    34         private LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0];
     32        public LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0];
     33
    3534        /**
    3635         * The convertor used to translate lat/lon points to screen points.
    3736         */
    3837        private Projection projection = new UTM();
     38
     39
    3940        /**
    4041         * Whether nodes on the same place should be considered identical.
    4142         */
    42         private boolean mergeNodes = true;
     43        public boolean mergeNodes = true;
     44       
     45       
    4346
     47        /**
     48         * List of all available Projections.
     49         */
     50        public static final Projection[] allProjections = new Projection[]{
     51                new UTM(),
     52                new LatitudeLongitude()
     53        };
    4454
    45 
     55        /**
     56         * Return the location of the preferences file
     57         */
     58        public static String getPreferencesFile() {
     59                return System.getProperty("user.home")+"/.josm-preferences";
     60        }
     61       
    4662        /**
    4763         * Exception thrown in case of any loading/saving error (including parse errors).
     
    5369                }
    5470        }
    55 
    56         /**
    57          * List of all available Projections.
    58          */
    59         public static final Projection[] allProjections = new Projection[]{
    60                 new UTM(),
    61                 new LatitudeLongitude()
    62         };
    63 
    64 
    65 
    66 
    67         // listener stuff
    68        
    69         /**
    70          * The event listener list
    71          */
    72         private List<PropertyChangeListener> listener = new LinkedList<PropertyChangeListener>();
    73         /**
    74          * If <code>listener != null</code>, add it to the listener list.
    75          */
    76         public void addPropertyChangeListener(PropertyChangeListener listener) {
    77                 if (listener != null)
    78                         this.listener.add(listener);
    79         }
    80         /**
    81          * If <code>listener != null</code>, remove it from the listener list.
    82          */
    83         public void removePropertyChangeListener(PropertyChangeListener listener) {
    84                 if (listener != null)
    85                         this.listener.remove(listener);
    86         }
    87         /**
    88          * Fires an event that the property has changed.
    89          */
    90         private void firePropertyChanged(String propName, Object oldValue, Object newValue) {
    91                 PropertyChangeEvent event = null;
    92                 for (PropertyChangeListener l : listener) {
    93                         if (event == null)
    94                                 event = new PropertyChangeEvent(this, propName, oldValue, newValue);
    95                         l.propertyChange(event);
    96                 }
    97         }
    98 
    99        
    100        
    101         /**
    102          * Return the location of the preferences file
    103          */
    104         public static String getPreferencesFile() {
    105                 return System.getProperty("user.home")+"/.josm-preferences";
    106         }
    107        
    10871        /**
    10972         * Load from disk.
     
    12083                        for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels())
    12184                                if (lafInfo.getClassName().equals(lafClassName)) {
    122                                         setLaf(lafInfo);
     85                                        laf = lafInfo;
    12386                                        break;
    12487                                }
    125                         if (getLaf() == null)
     88                        if (laf == null)
    12689                                throw new PreferencesException("Look and Feel not found.", null);
    127 
    128                         // set projection
     90                       
     91                        // projection
    12992                        Class<?> projectionClass = Class.forName(root.getChildText("projection"));
    13093                        projection = allProjections[0]; // defaults to UTM
     
    13699                        }
    137100
    138                         setMergeNodes(root.getChild("mergeNodes") != null);
     101                        mergeNodes = root.getChild("mergeNodes") != null;
    139102                } catch (Exception e) {
    140103                        if (e instanceof PreferencesException)
     
    153116               
    154117                List children = root.getChildren();
    155                 children.add(new Element("laf").setText(getLaf().getClassName()));
     118                children.add(new Element("laf").setText(laf.getClassName()));
    156119                children.add(new Element("projection").setText(getProjection().getClass().getName()));
    157                 if (isMergeNodes())
     120                if (mergeNodes)
    158121                        children.add(new Element("mergeNodes"));
    159122
     
    167130        }
    168131
    169         // getter / setter
    170 
     132       
     133        // projection change listener stuff
     134       
     135        /**
     136         * This interface notifies any interested about changes in the projection
     137         * @author imi
     138         */
     139        public interface ProjectionChangeListener {
     140                void projectionChanged(Projection oldProjection, Projection newProjection);
     141        }
     142        /**
     143         * The list of all listeners to projection changes.
     144         */
     145        private Collection<ProjectionChangeListener> listener = new LinkedList<ProjectionChangeListener>();
     146        /**
     147         * Add a listener of projection changes to the list of listeners.
     148         * @param listener The listerner to add.
     149         */
     150        public void addProjectionChangeListener(ProjectionChangeListener listener) {
     151                if (listener != null)
     152                        this.listener.add(listener);
     153        }
     154        /**
     155         * Remove the listener from the list.
     156         */
     157        public void removeProjectionChangeListener(ProjectionChangeListener listener) {
     158                this.listener.remove(listener);
     159        }
     160        /**
     161         * Set the projection and fire an event to all ProjectionChangeListener
     162         * @param projection The new Projection.
     163         */
    171164        public void setProjection(Projection projection) {
    172165                Projection old = this.projection;
    173166                this.projection = projection;
    174                 firePropertyChanged("projection", old, projection);
     167                if (old != projection)
     168                        for (ProjectionChangeListener l : listener)
     169                                l.projectionChanged(old, projection);
    175170        }
     171        /**
     172         * Get the current projection.
     173         * @return The current projection set.
     174         */
    176175        public Projection getProjection() {
    177176                return projection;
    178177        }
    179         public void setMergeNodes(boolean mergeNodes) {
    180                 boolean old = this.mergeNodes;
    181                 this.mergeNodes = mergeNodes;
    182                 firePropertyChanged("mergeNodes", old, mergeNodes);
    183         }
    184         public boolean isMergeNodes() {
    185                 return mergeNodes;
    186         }
    187         public void setLaf(LookAndFeelInfo laf) {
    188                 LookAndFeelInfo old = this.laf;
    189                 this.laf = laf;
    190                 firePropertyChanged("laf", old, laf);
    191         }
    192         public LookAndFeelInfo getLaf() {
    193                 return laf;
    194         }
    195178}
  • src/org/openstreetmap/josm/data/osm/DataSet.java

    r15 r16  
    113113                Node first = nodes.iterator().next();
    114114                Bounds b = new Bounds(first.coor.clone(), first.coor.clone());
    115                 for (Node n : nodes)
     115                for (Node w : nodes)
    116116                {
    117                         if (Double.isNaN(n.coor.x) || Double.isNaN(n.coor.y))
     117                        if (Double.isNaN(w.coor.x) || Double.isNaN(w.coor.y))
    118118                                return null;
    119                         if (n.coor.x < b.min.x)
    120                                 b.min.x = n.coor.x;
    121                         if (n.coor.y < b.min.y)
    122                                 b.min.y = n.coor.y;
    123                         if (n.coor.x > b.max.x)
    124                                 b.max.x = n.coor.x;
    125                         if (n.coor.y > b.max.y)
    126                                 b.max.y = n.coor.y;
     119                        if (w.coor.x < b.min.x)
     120                                b.min.x = w.coor.x;
     121                        if (w.coor.y < b.min.y)
     122                                b.min.y = w.coor.y;
     123                        if (w.coor.x > b.max.x)
     124                                b.max.x = w.coor.x;
     125                        if (w.coor.y > b.max.y)
     126                                b.max.y = w.coor.y;
    127127                }
    128128                return b;
  • src/org/openstreetmap/josm/data/projection/LatitudeLongitude.java

    r1 r16  
    11package org.openstreetmap.josm.data.projection;
    22
    3 import javax.swing.JPanel;
     3import javax.swing.JComponent;
    44
    55import org.openstreetmap.josm.data.GeoPoint;
     
    3030
    3131        @Override
    32         public String description() {
    33                 return "Use lat/lon values directly.";
     32        public JComponent getConfigurationPanel() {
     33                return null;
    3434        }
    3535
    3636        @Override
    37         public JPanel getConfigurationPanel() {
    38                 return null;
     37        public void commitConfigurationPanel() {
    3938        }
    4039}
  • src/org/openstreetmap/josm/data/projection/Projection.java

    r15 r16  
    4747        abstract public String toString();
    4848       
    49         /**
    50          * Describe the projection converter. Give examples, where it is best to use
    51          * and maybe a reference link to more information about the converter.
    52          */
    53         abstract public String description();
    54 
    55        
    56 
    5749        // miscellous functions
    5850       
    5951        /**
    6052         * If the projection supports any configuration, this function return
    61          * the configuration panel. If no configuration needed, return null.
     53         * the configuration panel. If no configuration needed,
     54         * return <code>null</code>.
    6255         *
    63          * The items on the configuration panel should update the configuration
    64          * directly, so the changes are instantly visible on screen.
     56         * The items on the configuration panel should not update the configuration
     57         * directly, but remember changed settings so a call to commitConfigurationPanel
     58         * can set them.
     59         *
     60         * This function also rolls back all changes to the configuration panel interna
     61         * components.
    6562         */
    6663        abstract public JComponent getConfigurationPanel();
     64        /**
     65         * Commits any changes from components created by addToConfigurationPanel.
     66         * The projection should now obtain the new settings. If any setting has
     67         * changed, the implementation have to call to fireStateChanged to inform
     68         * the listeners.
     69         */
     70        abstract public void commitConfigurationPanel();
    6771
    6872        /**
  • src/org/openstreetmap/josm/data/projection/UTM.java

    r15 r16  
    11package org.openstreetmap.josm.data.projection;
    22
     3import java.awt.Font;
     4import java.awt.GridBagLayout;
    35import java.awt.event.ActionEvent;
    46import java.awt.event.ActionListener;
    57
    6 import javax.swing.BorderFactory;
    7 import javax.swing.Box;
     8import javax.swing.JButton;
    89import javax.swing.JComboBox;
    910import javax.swing.JComponent;
    1011import javax.swing.JLabel;
     12import javax.swing.JOptionPane;
     13import javax.swing.JPanel;
    1114import javax.swing.JSpinner;
    1215import javax.swing.SpinnerNumberModel;
    13 import javax.swing.border.Border;
    14 import javax.swing.event.ChangeEvent;
    15 import javax.swing.event.ChangeListener;
    1616
    1717import org.openstreetmap.josm.data.Bounds;
    1818import org.openstreetmap.josm.data.GeoPoint;
    1919import org.openstreetmap.josm.data.osm.DataSet;
     20import org.openstreetmap.josm.gui.GBC;
     21import org.openstreetmap.josm.gui.Main;
    2022
    2123/**
     
    9395        protected Ellipsoid ellipsoid = allEllipsoids[allEllipsoids.length-1];
    9496
     97        /**
     98         * Combobox with all ellipsoids for the configuration panel
     99         */
     100        private JComboBox ellipsoidCombo = new JComboBox(allEllipsoids);
     101        /**
     102         * Spinner with all possible zones for the configuration panel
     103         */
     104        private JSpinner zoneSpinner = new JSpinner(new SpinnerNumberModel(1,1,60,1));
     105        /**
     106         * Hemisphere combo for the configuration panel
     107         */
     108        private JComboBox hemisphereCombo = new JComboBox(Hemisphere.values());
     109
    95110       
    96111        @Override
     
    168183        }
    169184
    170         @Override
    171         public String description() {
    172                 return "UTM projection ported from Ben Gimpert's ruby port.\n" +
    173                         "http://www.openstreetmap.org/websvn/filedetails.php?repname=" +
    174                         "OpenStreetMap&path=%2Futils%2Ftiger_import%2Ftiger%2Futm.rb";
    175         }
    176 
     185        /**
     186         * Helper class for the zone detection
     187         * @author imi
     188         */
     189        private class ZoneData {
     190                int zone = 0;
     191                Hemisphere hemisphere = Hemisphere.north;
     192        }
     193        /**
     194         * Try to autodetect the zone and hemisphere from the dataset.
     195         * @param dataSet The dataset to extrakt zone information from.
     196         * @return The zone data extrakted from the dataset.
     197         */
     198        private ZoneData autoDetect(DataSet dataSet) {
     199                ZoneData zd = new ZoneData();
     200               
     201                Bounds b = dataSet.getBoundsLatLon();
     202                if (b == null)
     203                        return zd;
     204                GeoPoint center = b.centerLatLon();
     205                double lat = center.lat;
     206                double lon = center.lon;
     207                // make sure the longitude is between -180.00 .. 179.9
     208                double long_temp = (lon + 180) - (Math.floor((lon + 180) / 360) * 360) - 180;
     209               
     210                zd.zone = (int)((long_temp + 180) / 6) + 1;
     211                if ((lat >= 56.0) && (lat < 64.0) && (long_temp >= 3.0) && (long_temp < 12.0))
     212                        zd.zone = 32;
     213                // special zones for Svalbard
     214                if ((lat >= 72.0) && (lat < 84.0))
     215                {
     216                        if ((long_temp >= 0.0) && (long_temp < 9.0))
     217                                zd.zone = 31;
     218                        else if ((long_temp >= 9.0) && (long_temp < 21.0))
     219                                zd.zone = 33;
     220                        else if ((long_temp >= 21.0) && (long_temp < 33.0))
     221                                zd.zone = 35;
     222                        else if ((long_temp >= 33.0) && (long_temp < 42.0))
     223                                zd.zone = 37;
     224                }
     225                zd.hemisphere = lat > 0 ? Hemisphere.north : Hemisphere.south;
     226                return zd;
     227        }
     228       
    177229        /**
    178230         * If the zone is not already set, calculate it from this dataset.
     
    184236        public void init(DataSet dataSet) {
    185237                if (zone == 0) {
    186                         Bounds b = dataSet.getBoundsLatLon();
    187                         if (b == null)
    188                                 return;
    189                         GeoPoint center = b.centerLatLon();
    190                         double lat = center.lat;
    191                         double lon = center.lon;
    192                         // make sure the longitude is between -180.00 .. 179.9
    193                         double long_temp = (lon + 180) - (Math.floor((lon + 180) / 360) * 360) - 180;
    194                        
    195                         zone = (int)((long_temp + 180) / 6) + 1;
    196                         if ((lat >= 56.0) && (lat < 64.0) && (long_temp >= 3.0) && (long_temp < 12.0))
    197                                 zone = 32;
    198                         // special zones for Svalbard
    199                         if ((lat >= 72.0) && (lat < 84.0))
    200                         {
    201                                 if ((long_temp >= 0.0) && (long_temp < 9.0))
    202                                         zone = 31;
    203                                 else if ((long_temp >= 9.0) && (long_temp < 21.0))
    204                                         zone = 33;
    205                                 else if ((long_temp >= 21.0) && (long_temp < 33.0))
    206                                         zone = 35;
    207                                 else if ((long_temp >= 33.0) && (long_temp < 42.0))
    208                                         zone = 37;
    209                         }
    210                         hemisphere = lat > 0 ? Hemisphere.north : Hemisphere.south;
     238                        ZoneData zd = autoDetect(dataSet);
     239                        zone = zd.zone;
     240                        hemisphere = zd.hemisphere;
    211241                }
    212242        }
     
    214244        @Override
    215245        public JComponent getConfigurationPanel() {
    216                 Border border = BorderFactory.createEmptyBorder(5,0,0,0);
    217                 Box panel = Box.createVerticalBox();
    218 
     246                JPanel panel = new JPanel(new GridBagLayout());
     247                GBC gbc = GBC.std().insets(0,0,5,0);
     248               
    219249                // ellipsoid
    220                 Box ellipsoidPanel = Box.createHorizontalBox();
    221                 ellipsoidPanel.add(new JLabel("Ellipsoid"));
    222                 final JComboBox ellipsoidCombo = new JComboBox(allEllipsoids);
    223                 ellipsoidPanel.add(ellipsoidCombo);
     250                panel.add(new JLabel("Ellipsoid"), gbc);
     251                panel.add(ellipsoidCombo, GBC.eol());
    224252                ellipsoidCombo.setSelectedItem(ellipsoid);
    225                 ellipsoidCombo.addActionListener(new ActionListener(){
     253               
     254                // zone
     255                panel.add(new JLabel("Zone"), gbc);
     256                panel.add(zoneSpinner, GBC.eol().insets(0,5,0,5));
     257                if (zone != 0)
     258                        zoneSpinner.setValue(zone);
     259               
     260                // hemisphere
     261                panel.add(new JLabel("Hemisphere"), gbc);
     262                panel.add(hemisphereCombo, GBC.eop());
     263                hemisphereCombo.setSelectedItem(hemisphere);
     264
     265                // Autodetect
     266                JButton autoDetect = new JButton("Detect");
     267                autoDetect.addActionListener(new ActionListener(){
    226268                        public void actionPerformed(ActionEvent e) {
    227                                 ellipsoid = (Ellipsoid)ellipsoidCombo.getSelectedItem();
    228                                 fireStateChanged();
     269                                if (Main.main.getMapFrame() != null) {
     270                                        DataSet ds = Main.main.getMapFrame().mapView.dataSet;
     271                                        ZoneData zd = autoDetect(ds);
     272                                        if (zd.zone == 0)
     273                                                JOptionPane.showMessageDialog(Main.main, "Autodetection failed. Maybe the data set contain too few information.");
     274                                        else {
     275                                                zoneSpinner.setValue(zd.zone);
     276                                                hemisphereCombo.setSelectedItem(zd.hemisphere);
     277                                        }
     278                                } else {
     279                                        JOptionPane.showMessageDialog(Main.main, "No data loaded. Please open a data set first.");
     280                                }
    229281                        }
    230282                });
    231                 ellipsoidPanel.setBorder(border);
    232                 panel.add(ellipsoidPanel);
    233                
    234                 // zone
    235                 Box zonePanel = Box.createHorizontalBox();
    236                 zonePanel.add(new JLabel("Zone"));
    237                 final JSpinner zoneSpinner = new JSpinner(new SpinnerNumberModel(zone,1,60,1));
    238                 zonePanel.add(zoneSpinner);
    239                 zoneSpinner.addChangeListener(new ChangeListener(){
    240                         public void stateChanged(ChangeEvent e) {
    241                                 zone = (Integer)zoneSpinner.getValue();
    242                                 fireStateChanged();
    243                         }
    244                 });
    245                 zonePanel.setBorder(border);
    246                 panel.add(zonePanel);
    247                
    248                 // hemisphere
    249                 Box hemispherePanel = Box.createHorizontalBox();
    250                 hemispherePanel.add(new JLabel("Hemisphere"));
    251                 final JComboBox hemisphereCombo = new JComboBox(Hemisphere.values());
    252                 hemispherePanel.add(hemisphereCombo);
    253                 hemisphereCombo.setSelectedItem(hemisphere);
    254                 hemisphereCombo.addActionListener(new ActionListener(){
    255                         public void actionPerformed(ActionEvent e) {
    256                                 hemisphere = (Hemisphere)hemisphereCombo.getSelectedItem();
    257                                 fireStateChanged();
    258                         }
    259                 });
    260                 hemispherePanel.setBorder(border);
    261                 panel.add(hemispherePanel);
    262 
     283                JLabel descLabel = new JLabel("Autodetect parameter based on loaded data");
     284                descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC));
     285                panel.add(descLabel, GBC.eol().fill(GBC.HORIZONTAL));
     286                panel.add(autoDetect, GBC.eol().anchor(GBC.CENTER));
     287               
    263288                return panel;
    264289        }
     290
     291        @Override
     292        public void commitConfigurationPanel() {
     293                ellipsoid = (Ellipsoid)ellipsoidCombo.getSelectedItem();
     294                zone = (Integer)zoneSpinner.getValue();
     295                hemisphere = (Hemisphere)hemisphereCombo.getSelectedItem();
     296                fireStateChanged();
     297        }
    265298}
Note: See TracChangeset for help on using the changeset viewer.