Changeset 3246 in josm for trunk


Ignore:
Timestamp:
2010-05-15T10:13:14+02:00 (9 years ago)
Author:
jttt
Message:

Add simple wrapper for preferences, add projection changed event (will be used later to replace CachedLatLon)

Location:
trunk/src/org/openstreetmap/josm
Files:
6 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/preferences/ProjectionPreference.java

    r3127 r3246  
    88import java.awt.event.ActionListener;
    99import java.util.Collection;
     10import java.util.concurrent.CopyOnWriteArrayList;
    1011
    1112import javax.swing.BorderFactory;
     
    1920import org.openstreetmap.josm.data.Bounds;
    2021import org.openstreetmap.josm.data.coor.CoordinateFormat;
     22import org.openstreetmap.josm.data.preferences.CollectionProperty;
     23import org.openstreetmap.josm.data.preferences.ParametrizedCollectionProperty;
     24import org.openstreetmap.josm.data.preferences.StringProperty;
    2125import org.openstreetmap.josm.data.projection.Mercator;
    2226import org.openstreetmap.josm.data.projection.Projection;
     
    3236    }
    3337
     38    public interface ProjectionChangedListener {
     39        void projectionChanged();
     40    }
     41
     42    private static final StringProperty PROP_PROJECTION = new StringProperty("projection", Mercator.class.getName());
     43    private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
     44    private static final CollectionProperty PROP_SUB_PROJECTION = new CollectionProperty("projection.sub", null);
     45    private static final ParametrizedCollectionProperty PROP_PROJECTION_SUBPROJECTION = new ParametrizedCollectionProperty(null) {
     46        @Override
     47        protected String getKey(String... params) {
     48            String name = params[0];
     49            String sname = name.substring(name.lastIndexOf(".")+1);
     50            return "projection.sub."+sname;
     51        }
     52    };
     53
     54    //TODO This is not nice place for a listener code but probably only Dataset will want to listen for projection changes so it's acceptable
     55    private static CopyOnWriteArrayList<ProjectionChangedListener> listeners = new CopyOnWriteArrayList<ProjectionChangedListener>();
     56
     57    public static void addProjectionChangedListener(ProjectionChangedListener listener) {
     58        listeners.addIfAbsent(listener);
     59    }
     60
     61    public static void removeProjectionChangedListener(ProjectionChangedListener listener) {
     62        listeners.remove(listener);
     63    }
     64
     65    private static void fireProjectionChanged() {
     66        for (ProjectionChangedListener listener: listeners) {
     67            listener.projectionChanged();
     68        }
     69    }
     70
     71
    3472    /**
    3573     * Combobox with all projections available
     
    68106
    69107        for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
    70             if (((CoordinateFormat)coordinatesCombo.getItemAt(i)).name().equals(Main.pref.get("coordinates"))) {
     108            if (((CoordinateFormat)coordinatesCombo.getItemAt(i)).name().equals(PROP_COORDINATES.get())) {
    71109                coordinatesCombo.setSelectedIndex(i);
    72110                break;
     
    113151        }
    114152
    115         Main.pref.put("projection", projname);
     153        PROP_PROJECTION.put(projname);
    116154        setProjection(projname, prefs);
    117155
    118         if(Main.pref.put("coordinates",
    119                 ((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) {
     156        if(PROP_COORDINATES.put(((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) {
    120157            CoordinateFormat.setCoordinateFormat((CoordinateFormat)coordinatesCombo.getSelectedItem());
    121158        }
     
    126163    static public void setProjection()
    127164    {
    128         setProjection(Main.pref.get("projection", Mercator.class.getName()),
    129                 Main.pref.getCollection("projection.sub", null));
     165        setProjection(PROP_PROJECTION.get(), PROP_SUB_PROJECTION.get());
    130166    }
    131167
     
    148184            name = Main.proj.getClass().getName();
    149185        }
    150         Main.pref.putCollection("projection.sub", coll);
    151         String sname = name.substring(name.lastIndexOf(".")+1);
    152         Main.pref.putCollection("projection.sub."+sname, coll);
     186        PROP_SUB_PROJECTION.put(coll);
     187        PROP_PROJECTION_SUBPROJECTION.put(coll, name);
    153188        if(Main.proj instanceof ProjectionSubPrefs) {
    154189            ((ProjectionSubPrefs) Main.proj).setPreferences(coll);
    155190        }
     191        fireProjectionChanged(); // This should be probably called from the if bellow, but hashCode condition doesn't look sure enough
    156192        if(b != null && (!Main.proj.getClass().getName().equals(oldProj.getClass().getName()) || Main.proj.hashCode() != oldProj.hashCode()))
    157193        {
     
    212248            Projection proj = (Projection)projectionCombo.getItemAt(i);
    213249            String name = proj.getClass().getName();
    214             String sname = name.substring(name.lastIndexOf(".")+1);
    215250            if(proj instanceof ProjectionSubPrefs) {
    216                 ((ProjectionSubPrefs) proj).setPreferences(Main.pref.getCollection("projection.sub."+sname, null));
    217             }
    218             if (name.equals(Main.pref.get("projection", Mercator.class.getName()))) {
     251                ((ProjectionSubPrefs) proj).setPreferences(PROP_PROJECTION_SUBPROJECTION.get(name));
     252            }
     253            if (name.equals(PROP_PROJECTION.get())) {
    219254                projectionCombo.setSelectedIndex(i);
    220255                selectedProjectionChanged(proj);
Note: See TracChangeset for help on using the changeset viewer.