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.
File:
1 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}
Note: See TracChangeset for help on using the changeset viewer.