Changeset 5234 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2012-05-12T18:23:08+02:00 (12 years ago)
Author:
bastiK
Message:

separate preference gui from core projection code (there may be bugs)

Location:
trunk/src/org/openstreetmap/josm
Files:
13 added
2 deleted
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/Main.java

    r5226 r5234  
    816816        CheckParameterUtil.ensureParameterNotNull(p);
    817817        Projection oldValue = proj;
     818        Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
    818819        proj = p;
    819         fireProjectionChanged(oldValue, proj);
     820        fireProjectionChanged(oldValue, proj, b);
    820821    }
    821822
     
    827828    private static final ArrayList<WeakReference<ProjectionChangeListener>> listeners = new ArrayList<WeakReference<ProjectionChangeListener>>();
    828829
    829     private static void fireProjectionChanged(Projection oldValue, Projection newValue) {
     830    private static void fireProjectionChanged(Projection oldValue, Projection newValue, Bounds oldBounds) {
    830831        if (newValue == null ^ oldValue == null
    831832                || (newValue != null && oldValue != null && !Utils.equal(newValue.toCode(), oldValue.toCode()))) {
     
    843844                }
    844845            }
    845             if (newValue != null) {
    846                 Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
    847                 if (b != null){
    848                     Main.map.mapView.zoomTo(b);
    849                 }
     846            if (newValue != null && oldBounds != null) {
     847                Main.map.mapView.zoomTo(oldBounds);
    850848            }
    851849            /* TODO - remove layers with fixed projection */
  • trunk/src/org/openstreetmap/josm/data/projection/CustomProjection.java

    r5228 r5234  
    4040     * null means fall back mode (Mercator)
    4141     */
    42     protected String pref = null;
     42    protected String pref;
    4343    protected Bounds bounds;
    4444
     
    8787            this.key = key;
    8888            this.hasValue = hasValue;
     89        }
     90    }
     91
     92    public CustomProjection() {
     93        this.pref = null;
     94    }
     95
     96    public CustomProjection(String pref) {
     97        try {
     98            this.pref = pref;
     99            update(pref);
     100        } catch (ProjectionConfigurationException ex) {
     101            try {
     102                update(null);
     103            } catch (ProjectionConfigurationException ex1) {
     104                throw new RuntimeException();
     105            }
    89106        }
    90107    }
  • trunk/src/org/openstreetmap/josm/data/projection/GaussKrueger.java

    r5226 r5234  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.GridBagLayout;
    7 import java.awt.event.ActionListener;
    8 import java.util.Collection;
    9 import java.util.Collections;
    10 
    11 import javax.swing.JComboBox;
    12 import javax.swing.JLabel;
    13 import javax.swing.JPanel;
    145
    156import org.openstreetmap.josm.data.Bounds;
     
    1910import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    2011import org.openstreetmap.josm.data.projection.proj.TransverseMercator;
    21 import org.openstreetmap.josm.tools.GBC;
    2212
    23 public class GaussKrueger extends AbstractProjection implements ProjectionSubPrefs {
     13public class GaussKrueger extends AbstractProjection {
    2414
    2515    public static final int DEFAULT_ZONE = 2;
    26     private int zone;
     16    private final int zone;
    2717
    2818    private static Bounds[] bounds = {
     
    3323    };
    3424
    35     private static String[] zones = { "2", "3", "4", "5" };
    36 
    3725    public GaussKrueger() {
    3826        this(DEFAULT_ZONE);
     
    4028
    4129    public GaussKrueger(int zone) {
    42         updateParameters(zone);
    43     }
    44 
    45     private void updateParameters(int zone) {
     30        if (zone < 2 || zone > 5)
     31            throw new IllegalArgumentException();
    4632        this.zone = zone;
    4733        ellps = Ellipsoid.Bessel1841;
     
    6349    @Override
    6450    public String toString() {
    65         return tr("Gau\u00DF-Kr\u00FCger");
     51        return tr("Gau\u00DF-Kr\u00FCger Zone {0}", zone);
    6652    }
    6753
     
    8167    }
    8268
    83     @Override
    84     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    85         JComboBox prefcb = new JComboBox(zones);
    86 
    87         prefcb.setSelectedIndex(zone-2);
    88         p.setLayout(new GridBagLayout());
    89         p.add(new JLabel(tr("GK Zone")), GBC.std().insets(5,5,0,5));
    90         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    91         /* Note: we use component position 2 below to find this again */
    92         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
    93         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    94 
    95         if (listener != null) {
    96             prefcb.addActionListener(listener);
    97         }
    98     }
    99 
    100     @Override
    101     public Collection<String> getPreferences(JPanel p) {
    102         Object prefcb = p.getComponent(2);
    103         if(!(prefcb instanceof JComboBox))
    104             return null;
    105         int zone = ((JComboBox)prefcb).getSelectedIndex();
    106         return Collections.singleton(Integer.toString(zone+2));
    107     }
    108 
    109     @Override
    110     public void setPreferences(Collection<String> args) {
    111         int zone = DEFAULT_ZONE;
    112         if (args != null) {
    113             try {
    114                 for(String s : args)
    115                 {
    116                     zone = Integer.parseInt(s);
    117                     if(zone < 2 || zone > 5) {
    118                         zone = DEFAULT_ZONE;
    119                     }
    120                     break;
    121                 }
    122             } catch(NumberFormatException e) {}
    123         }
    124         updateParameters(zone);
    125     }
    126 
    127     @Override
    128     public String[] allCodes() {
    129         String[] zones = new String[4];
    130         for (int zone = 2; zone <= 5; zone++) {
    131             zones[zone-2] = "EPSG:" + (31464 + zone);
    132         }
    133         return zones;
    134     }
    135 
    136     @Override
    137     public Collection<String> getPreferencesFromCode(String code)
    138     {
    139         //zone 2 = EPSG:31466 up to zone 5 = EPSG:31469
    140         for (int zone = 2; zone <= 5; zone++) {
    141             String epsg = "EPSG:" + (31464 + zone);
    142             if (epsg.equals(code))
    143                 return Collections.singleton(String.valueOf(zone));
    144         }
    145         return null;
    146     }
    147 
    14869}
  • trunk/src/org/openstreetmap/josm/data/projection/Lambert.java

    r5230 r5234  
    3030 * @author Pieren
    3131 */
    32 public class Lambert extends AbstractProjection implements ProjectionSubPrefs {
     32public class Lambert extends AbstractProjection {
    3333
    3434    /**
     
    9292
    9393    public Lambert() {
    94         updateParameters(DEFAULT_ZONE);
     94        this(DEFAULT_ZONE);
    9595    }
    9696
    97     private void updateParameters(final int layoutZone) {
     97    public Lambert(final int layoutZone) {
    9898        this.layoutZone = layoutZone;
    9999        ellps = Ellipsoid.clarkeIGN;
     
    151151    }
    152152
    153     public static String[] lambert4zones = {
    154         tr("{0} ({1} to {2} degrees)", 1,"51.30","48.15"),
    155         tr("{0} ({1} to {2} degrees)", 2,"48.15","45.45"),
    156         tr("{0} ({1} to {2} degrees)", 3,"45.45","42.76"),
    157         tr("{0} (Corsica)", 4)
    158     };
    159 
    160     @Override
    161     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    162         JComboBox prefcb = new JComboBox(lambert4zones);
    163 
    164         prefcb.setSelectedIndex(layoutZone);
    165         p.setLayout(new GridBagLayout());
    166         p.add(new JLabel(tr("Lambert CC Zone")), GBC.std().insets(5,5,0,5));
    167         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    168         /* Note: we use component position 2 below to find this again */
    169         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
    170         p.add(new JLabel(ImageProvider.get("data/projection", "Departements_Lambert4Zones.png")), GBC.eol().fill(GBC.HORIZONTAL));
    171         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    172 
    173         if (listener != null) {
    174             prefcb.addActionListener(listener);
    175         }
    176     }
    177 
    178     @Override
    179     public Collection<String> getPreferences(JPanel p) {
    180         Object prefcb = p.getComponent(2);
    181         if(!(prefcb instanceof JComboBox))
    182             return null;
    183         layoutZone = ((JComboBox)prefcb).getSelectedIndex();
    184         return Collections.singleton(Integer.toString(layoutZone+1));
    185     }
    186 
    187     @Override
    188     public void setPreferences(Collection<String> args) {
    189         int layoutZone = DEFAULT_ZONE;
    190         if (args != null) {
    191             try {
    192                 for(String s : args)
    193                 {
    194                     layoutZone = Integer.parseInt(s)-1;
    195                     if(layoutZone < 0 || layoutZone > 3) {
    196                         layoutZone = DEFAULT_ZONE;
    197                     }
    198                     break;
    199                 }
    200             } catch(NumberFormatException e) {}
    201         }
    202         updateParameters(layoutZone);
    203     }
    204 
    205     @Override
    206     public String[] allCodes() {
    207         String[] zones = new String[4];
    208         for (int zone = 0; zone < 4; zone++) {
    209             zones[zone] = "EPSG:"+(27561+zone);
    210         }
    211         return zones;
    212     }
    213 
    214     @Override
    215     public Collection<String> getPreferencesFromCode(String code) {
    216         if (code.startsWith("EPSG:2756") && code.length() == 10) {
    217             try {
    218                 String zonestring = code.substring(9);
    219                 int zoneval = Integer.parseInt(zonestring);
    220                 if(zoneval >= 1 && zoneval <= 4)
    221                     return Collections.singleton(zonestring);
    222             } catch(NumberFormatException e) {}
    223         }
    224         return null;
    225     }
    226 
    227153}
  • trunk/src/org/openstreetmap/josm/data/projection/LambertCC9Zones.java

    r5066 r5234  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.GridBagLayout;
    7 import java.awt.event.ActionListener;
    8 import java.util.Collection;
    9 import java.util.Collections;
    10 
    11 import javax.swing.JComboBox;
    12 import javax.swing.JLabel;
    13 import javax.swing.JPanel;
    145
    156import org.openstreetmap.josm.data.Bounds;
     
    189import org.openstreetmap.josm.data.projection.proj.LambertConformalConic;
    1910import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    20 import org.openstreetmap.josm.tools.GBC;
    21 import org.openstreetmap.josm.tools.ImageProvider;
    2211
    2312/**
     
    2716 *
    2817 */
    29 public class LambertCC9Zones extends AbstractProjection implements ProjectionSubPrefs {
     18public class LambertCC9Zones extends AbstractProjection {
    3019
    3120    /**
     
    4029    public static final int DEFAULT_ZONE = 0;
    4130
    42     private int layoutZone = DEFAULT_ZONE;
     31    private final int layoutZone;
    4332
    4433    public LambertCC9Zones() {
     
    4635    }
    4736
    48     public LambertCC9Zones(int layoutZone) {
    49         updateParameters(layoutZone);
    50     }
    51 
    52     public void updateParameters(final int layoutZone) {
     37    public LambertCC9Zones(final int layoutZone) {
    5338        ellps = Ellipsoid.GRS80;
    5439        datum = GRS80Datum.INSTANCE;
     
    11297    }
    11398
    114     private static String[] lambert9zones = {
    115         tr("{0} ({1} to {2} degrees)", 1,41,43),
    116         tr("{0} ({1} to {2} degrees)", 2,42,44),
    117         tr("{0} ({1} to {2} degrees)", 3,43,45),
    118         tr("{0} ({1} to {2} degrees)", 4,44,46),
    119         tr("{0} ({1} to {2} degrees)", 5,45,47),
    120         tr("{0} ({1} to {2} degrees)", 6,46,48),
    121         tr("{0} ({1} to {2} degrees)", 7,47,49),
    122         tr("{0} ({1} to {2} degrees)", 8,48,50),
    123         tr("{0} ({1} to {2} degrees)", 9,49,51)
    124     };
    125 
    126     @Override
    127     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    128         JComboBox prefcb = new JComboBox(lambert9zones);
    129 
    130         prefcb.setSelectedIndex(layoutZone);
    131         p.setLayout(new GridBagLayout());
    132         p.add(new JLabel(tr("Lambert CC Zone")), GBC.std().insets(5,5,0,5));
    133         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    134         /* Note: we use component position 2 below to find this again */
    135         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
    136         p.add(new JLabel(ImageProvider.get("data/projection", "LambertCC9Zones.png")), GBC.eol().fill(GBC.HORIZONTAL));
    137         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    138 
    139         if (listener != null) {
    140             prefcb.addActionListener(listener);
    141         }
    142     }
    143 
    144     @Override
    145     public Collection<String> getPreferences(JPanel p) {
    146         Object prefcb = p.getComponent(2);
    147         if(!(prefcb instanceof JComboBox))
    148             return null;
    149         int layoutZone = ((JComboBox)prefcb).getSelectedIndex();
    150         return Collections.singleton(Integer.toString(layoutZone+1));
    151     }
    152 
    153     @Override
    154     public void setPreferences(Collection<String> args) {
    155         int layoutZone = DEFAULT_ZONE;
    156         if (args != null) {
    157             try {
    158                 for(String s : args)
    159                 {
    160                     layoutZone = Integer.parseInt(s)-1;
    161                     if(layoutZone < 0 || layoutZone > 8) {
    162                         layoutZone = DEFAULT_ZONE;
    163                     }
    164                     break;
    165                 }
    166             } catch(NumberFormatException e) {}
    167         }
    168         updateParameters(layoutZone);
    169     }
    170 
    171     @Override
    172     public String[] allCodes() {
    173         String[] zones = new String[9];
    174         for (int zone = 0; zone < 9; zone++) {
    175             zones[zone] = "EPSG:" + (3942 + zone);
    176         }
    177         return zones;
    178     }
    179 
    180     @Override
    181     public Collection<String> getPreferencesFromCode(String code)
    182     {
    183         //zone 1=CC42=EPSG:3942 up to zone 9=CC50=EPSG:3950
    184         if (code.startsWith("EPSG:39") && code.length() == 9) {
    185             try {
    186                 String zonestring = code.substring(5,9);
    187                 int zoneval = Integer.parseInt(zonestring)-3942;
    188                 if(zoneval >= 0 && zoneval <= 8)
    189                     return Collections.singleton(String.valueOf(zoneval+1));
    190             } catch(NumberFormatException ex) {}
    191         }
    192         return null;
    193     }
    19499}
  • trunk/src/org/openstreetmap/josm/data/projection/ProjectionInfo.java

    r3872 r5234  
    11// License: GPL. For details, see LICENSE file.
    22package org.openstreetmap.josm.data.projection;
    3 
    4 import static org.openstreetmap.josm.tools.I18n.tr;
    53
    64import java.util.Collection;
    75import java.util.HashMap;
    86
     7import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
     8import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
     9
    910public class ProjectionInfo {
     11    private static HashMap<String, ProjectionChoice> allCodesPC;
    1012    private static HashMap<String, Projection> allCodes;
    11 
    12     private static ProjectionSubPrefs recreateProj(ProjectionSubPrefs proj) {
    13         try {
    14             return proj.getClass().newInstance();
    15         } catch (Exception e) {
    16             throw new IllegalStateException(
    17                     tr("Cannot instantiate projection ''{0}''", proj.getClass().toString()), e);
    18         }
    19     }
    2013
    2114    static {
    2215        allCodes = new HashMap<String, Projection>();
    23         for (Projection proj : Projections.getProjections()) {
    24             if (proj instanceof ProjectionSubPrefs) {
    25                 ProjectionSubPrefs projSub = recreateProj((ProjectionSubPrefs)proj);
    26                 for (String code : projSub.allCodes()) {
    27                     allCodes.put(code, projSub);
    28                 }
    29             } else {
    30                 allCodes.put(proj.toCode(), proj);
     16        for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
     17            for (String code : pc.allCodes()) {
     18                allCodesPC.put(code, pc);
    3119            }
    3220        }
     
    3422
    3523    public static Projection getProjectionByCode(String code) {
    36         Projection proj = allCodes.get(code);
    37         if (proj == null) return null;
    38         if (code.equals(proj.toCode())) return proj;
    39         if (!(proj instanceof ProjectionSubPrefs))
    40             throw new IllegalStateException(tr(
    41                     "Projection code mismatch in ''{0}'': toCode() returns ''{1}'', expected ''{2}''.",
    42                     proj.getClass().toString(), proj.toCode(), code));
    43         ProjectionSubPrefs projSub = recreateProj((ProjectionSubPrefs)proj);
    44         Collection<String> prefs = projSub.getPreferencesFromCode(code);
    45         if (prefs != null) {
    46             projSub.setPreferences(prefs);
    47         }
    48         if (!code.equals(projSub.toCode()))
    49             throw new IllegalStateException(tr(
    50                     "Bad implementation of ''{0}'' projection class: cannot set preferences to match code ''{1}''.",
    51                     projSub.getClass().toString(), code));
    52         allCodes.put(code, projSub);
    53         return projSub;
     24        Projection p = allCodes.get(code);
     25        if (p != null) return p;
     26        ProjectionChoice pc = allCodesPC.get(code);
     27        if (pc == null) return null;
     28        Collection<String> pref = pc.getPreferencesFromCode(code);
     29        pc.setPreferences(pref);
     30        p = pc.getProjection();
     31        allCodes.put(code, p);
     32        return p;
    5433    }
    5534}
  • trunk/src/org/openstreetmap/josm/data/projection/Projections.java

    r5228 r5234  
    66import java.io.InputStream;
    77import java.io.InputStreamReader;
    8 import java.util.ArrayList;
    9 import java.util.Arrays;
    108import java.util.HashMap;
    119import java.util.Map;
     
    3331 */
    3432public class Projections {
    35     /**
    36      * List of all available projections.
    37      */
    38     private static ArrayList<Projection> allProjections =
    39         new ArrayList<Projection>(Arrays.asList(new Projection[] {
    40                 // global projections
    41                 new Epsg4326(),
    42                 new Mercator(),
    43                 new UTM(),
    44                 // regional - alphabetical order by country code
    45                 new BelgianLambert1972(),   // BE
    46                 new BelgianLambert2008(),   // BE
    47                 new SwissGrid(),            // CH
    48                 new GaussKrueger(),         // DE
    49                 new LambertEST(),           // EE
    50                 new Lambert(),              // FR
    51                 new Lambert93(),            // FR
    52                 new LambertCC9Zones(),      // FR
    53                 new UTM_France_DOM(),       // FR
    54                 new TransverseMercatorLV(), // LV
    55                 new Puwg(),                 // PL
    56                 new Epsg3008(),             // SE
    57                 new CustomProjectionPrefGui()
    58         }));
    59 
    60     public static ArrayList<Projection> getProjections() {
    61         return allProjections;
    62     }
    63 
    64     /**
    65      * Adds a new projection to the list of known projections.
    66      *
    67      * For Plugins authors: make sure your plugin is an early plugin, i.e. put
    68      * Plugin-Early=true in your Manifest.
    69      */
    70     public static void addProjection(Projection proj) {
    71         allProjections.add(proj);
    72     }
    7333
    7434    public static EastNorth project(LatLon ll) {
  • trunk/src/org/openstreetmap/josm/data/projection/Puwg.java

    r5066 r5234  
    44
    55import static org.openstreetmap.josm.tools.I18n.tr;
    6 
    7 import java.awt.GridBagLayout;
    8 import java.awt.event.ActionListener;
    9 import java.util.Collection;
    10 import java.util.Collections;
    11 
    12 import javax.swing.JComboBox;
    13 import javax.swing.JLabel;
    14 import javax.swing.JPanel;
    156
    167import org.openstreetmap.josm.data.Bounds;
     
    189import org.openstreetmap.josm.data.projection.datum.GRS80Datum;
    1910import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    20 import org.openstreetmap.josm.tools.GBC;
    2111
    2212/**
     
    2616 * @author steelman
    2717 */
    28 public class Puwg extends AbstractProjection implements ProjectionSubPrefs {
     18public class Puwg extends AbstractProjection {
    2919
    3020    public static final int DEFAULT_ZONE = 0;
    3121
    32     private int zone;
    33 
    34     static PuwgData[] Zones = new PuwgData[] {
     22    private final int zone;
     23
     24    static public PuwgData[] Zones = new PuwgData[] {
    3525        new Epsg2180(),
    3626        new Epsg2176(),
     
    5343        }
    5444        datum = GRS80Datum.INSTANCE;
    55         updateParameters(zone);
    56     }
    57 
    58     public void updateParameters(int zone) {
    5945        this.zone = zone;
    6046        PuwgData z = Zones[zone];
     
    9076    }
    9177
    92     @Override
    93     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    94         JComboBox prefcb = new JComboBox(Puwg.Zones);
    95 
    96         prefcb.setSelectedIndex(zone);
    97         p.setLayout(new GridBagLayout());
    98         p.add(new JLabel(tr("PUWG Zone")), GBC.std().insets(5,5,0,5));
    99         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    100         /* Note: we use component position 2 below to find this again */
    101         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
    102         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    103 
    104         if (listener != null) {
    105             prefcb.addActionListener(listener);
    106         }
    107     }
    108 
    109     @Override
    110     public Collection<String> getPreferences(JPanel p) {
    111         Object prefcb = p.getComponent(2);
    112         if(!(prefcb instanceof JComboBox))
    113             return null;
    114         int zone = ((JComboBox)prefcb).getSelectedIndex();
    115         return Collections.singleton((Puwg.Zones[zone]).toCode());
    116     }
    117 
    118     @Override
    119     public String[] allCodes() {
    120         String[] zones = new String[Zones.length];
    121         for (int zone = 0; zone < Zones.length; zone++) {
    122             zones[zone] = Zones[zone].toCode();
    123         }
    124         return zones;
    125     }
    126 
    127     @Override
    128     public Collection<String> getPreferencesFromCode(String code) {
    129         for (PuwgData p : Puwg.Zones) {
    130             if (code.equals(p.toCode()))
    131                 return Collections.singleton(code);
    132         }
    133         return null;
    134     }
    135 
    136     @Override
    137     public void setPreferences(Collection<String> args) {
    138         int z = DEFAULT_ZONE;
    139         if (args != null) {
    140             try {
    141                 for (String s : args) {
    142                     for (int i=0; i < Zones.length; ++i)
    143                         if (s.equals(Zones[i].toCode())) {
    144                             z = i;
    145                             break;
    146                         }
    147                     break;
    148                 }
    149             } catch (NullPointerException e) {}
    150         }
    151         updateParameters(z);
     78    public interface PuwgData {
     79        double getPuwgCentralMeridianDeg();
     80        double getPuwgCentralMeridian();
     81        double getPuwgFalseEasting();
     82        double getPuwgFalseNorthing();
     83        double getPuwgScaleFactor();
     84
     85        // Projection methods
     86        Integer getEpsgCode();
     87        String toCode();
     88        String getCacheDirectoryName();
     89        Bounds getWorldBoundsLatLon();
     90    }
     91
     92    public static class Epsg2180 implements PuwgData {
     93
     94        private static final double Epsg2180FalseEasting = 500000.0; /* y */
     95        private static final double Epsg2180FalseNorthing = -5300000.0; /* x */
     96        private static final double Epsg2180ScaleFactor = 0.9993;
     97        private static final double Epsg2180CentralMeridian = 19.0;
     98
     99        @Override public String toString() {
     100            return tr("PUWG 1992 (Poland)");
     101        }
     102
     103        @Override
     104        public Integer getEpsgCode() {
     105            return 2180;
     106        }
     107
     108        @Override
     109        public String toCode() {
     110            return "EPSG:" + getEpsgCode();
     111        }
     112
     113        @Override
     114        public String getCacheDirectoryName() {
     115            return "epsg2180";
     116        }
     117
     118        @Override
     119        public Bounds getWorldBoundsLatLon()
     120        {
     121            return new Bounds(
     122                    new LatLon(49.00, 14.12),
     123                    new LatLon(54.84, 24.15));
     124        }
     125
     126        @Override public double getPuwgCentralMeridianDeg() { return Epsg2180CentralMeridian; }
     127        @Override public double getPuwgCentralMeridian() { return Math.toRadians(Epsg2180CentralMeridian); }
     128        @Override public double getPuwgFalseEasting() { return Epsg2180FalseEasting; }
     129        @Override public double getPuwgFalseNorthing() { return Epsg2180FalseNorthing; }
     130        @Override public double getPuwgScaleFactor() { return Epsg2180ScaleFactor; }
     131    }
     132
     133    abstract static class Puwg2000 implements PuwgData {
     134
     135        private static final double PuwgFalseEasting = 500000.0;
     136        private static final double PuwgFalseNorthing = 0;
     137        private static final double PuwgScaleFactor = 0.999923;
     138        //final private double[] Puwg2000CentralMeridian = {15.0, 18.0, 21.0, 24.0};
     139        final private Integer[] Puwg2000Code = { 2176,  2177, 2178, 2179 };
     140        final private String[] Puwg2000CDName = { "epsg2176",  "epsg2177", "epsg2178", "epsg2179" };
     141
     142        @Override public String toString() {
     143            return tr("PUWG 2000 Zone {0} (Poland)", Integer.toString(getZone()));
     144        }
     145
     146        @Override
     147        public Integer getEpsgCode() {
     148            return Puwg2000Code[getZoneIndex()];
     149        }
     150
     151        @Override
     152        public String toCode() {
     153            return "EPSG:" + getEpsgCode();
     154        }
     155
     156        @Override
     157        public String getCacheDirectoryName() {
     158            return Puwg2000CDName[getZoneIndex()];
     159        }
     160
     161        @Override
     162        public Bounds getWorldBoundsLatLon()
     163        {
     164            return new Bounds(
     165                    new LatLon(49.00, (3 * getZone()) - 1.5),
     166                    new LatLon(54.84, (3 * getZone()) + 1.5));
     167        }
     168
     169        @Override public double getPuwgCentralMeridianDeg() { return getZone() * 3.0; }
     170        @Override public double getPuwgCentralMeridian() { return Math.toRadians(getZone() * 3.0); }
     171        @Override public double getPuwgFalseNorthing() { return PuwgFalseNorthing;}
     172        @Override public double getPuwgFalseEasting() { return 1e6 * getZone() + PuwgFalseEasting; }
     173        @Override public double getPuwgScaleFactor() { return PuwgScaleFactor; }
     174        public abstract int getZone();
     175
     176        public int getZoneIndex() { return getZone() - 5; }
     177
     178    }
     179
     180    public static class Epsg2176 extends Puwg2000 {
     181        private static final int PuwgZone = 5;
     182
     183        @Override
     184        public int getZone() { return PuwgZone; }
     185    }
     186
     187    public static class Epsg2177 extends Puwg2000 {
     188        private static final int PuwgZone = 6;
     189
     190        @Override
     191        public int getZone() { return PuwgZone; }
     192    }
     193
     194    public static class Epsg2178 extends Puwg2000 {
     195        private static final int PuwgZone = 7;
     196
     197        @Override
     198        public int getZone() { return PuwgZone; }
     199    }
     200
     201    public static class Epsg2179 extends Puwg2000 {
     202        private static final int PuwgZone = 8;
     203
     204        @Override
     205        public int getZone() { return PuwgZone; }
    152206    }
    153207}
    154 
    155 interface PuwgData {
    156     double getPuwgCentralMeridianDeg();
    157     double getPuwgCentralMeridian();
    158     double getPuwgFalseEasting();
    159     double getPuwgFalseNorthing();
    160     double getPuwgScaleFactor();
    161 
    162     // Projection methods
    163     Integer getEpsgCode();
    164     String toCode();
    165     String getCacheDirectoryName();
    166     Bounds getWorldBoundsLatLon();
    167 }
    168 
    169 class Epsg2180 implements PuwgData {
    170 
    171     private static final double Epsg2180FalseEasting = 500000.0; /* y */
    172     private static final double Epsg2180FalseNorthing = -5300000.0; /* x */
    173     private static final double Epsg2180ScaleFactor = 0.9993;
    174     private static final double Epsg2180CentralMeridian = 19.0;
    175 
    176     @Override public String toString() {
    177         return tr("PUWG 1992 (Poland)");
    178     }
    179 
    180     @Override
    181     public Integer getEpsgCode() {
    182         return 2180;
    183     }
    184 
    185     @Override
    186     public String toCode() {
    187         return "EPSG:" + getEpsgCode();
    188     }
    189 
    190     @Override
    191     public String getCacheDirectoryName() {
    192         return "epsg2180";
    193     }
    194 
    195     @Override
    196     public Bounds getWorldBoundsLatLon()
    197     {
    198         return new Bounds(
    199                 new LatLon(49.00, 14.12),
    200                 new LatLon(54.84, 24.15));
    201     }
    202 
    203     @Override public double getPuwgCentralMeridianDeg() { return Epsg2180CentralMeridian; }
    204     @Override public double getPuwgCentralMeridian() { return Math.toRadians(Epsg2180CentralMeridian); }
    205     @Override public double getPuwgFalseEasting() { return Epsg2180FalseEasting; }
    206     @Override public double getPuwgFalseNorthing() { return Epsg2180FalseNorthing; }
    207     @Override public double getPuwgScaleFactor() { return Epsg2180ScaleFactor; }
    208 }
    209 
    210 abstract class Puwg2000 implements PuwgData {
    211 
    212     private static final double PuwgFalseEasting = 500000.0;
    213     private static final double PuwgFalseNorthing = 0;
    214     private static final double PuwgScaleFactor = 0.999923;
    215     //final private double[] Puwg2000CentralMeridian = {15.0, 18.0, 21.0, 24.0};
    216     final private Integer[] Puwg2000Code = { 2176,  2177, 2178, 2179 };
    217     final private String[] Puwg2000CDName = { "epsg2176",  "epsg2177", "epsg2178", "epsg2179" };
    218 
    219     @Override public String toString() {
    220         return tr("PUWG 2000 Zone {0} (Poland)", Integer.toString(getZone()));
    221     }
    222 
    223     @Override
    224     public Integer getEpsgCode() {
    225         return Puwg2000Code[getZoneIndex()];
    226     }
    227 
    228     @Override
    229     public String toCode() {
    230         return "EPSG:" + getEpsgCode();
    231     }
    232 
    233     @Override
    234     public String getCacheDirectoryName() {
    235         return Puwg2000CDName[getZoneIndex()];
    236     }
    237 
    238     @Override
    239     public Bounds getWorldBoundsLatLon()
    240     {
    241         return new Bounds(
    242                 new LatLon(49.00, (3 * getZone()) - 1.5),
    243                 new LatLon(54.84, (3 * getZone()) + 1.5));
    244     }
    245 
    246     @Override public double getPuwgCentralMeridianDeg() { return getZone() * 3.0; }
    247     @Override public double getPuwgCentralMeridian() { return Math.toRadians(getZone() * 3.0); }
    248     @Override public double getPuwgFalseNorthing() { return PuwgFalseNorthing;}
    249     @Override public double getPuwgFalseEasting() { return 1e6 * getZone() + PuwgFalseEasting; }
    250     @Override public double getPuwgScaleFactor() { return PuwgScaleFactor; }
    251     public abstract int getZone();
    252 
    253     public int getZoneIndex() { return getZone() - 5; }
    254 
    255 }
    256 
    257 class Epsg2176 extends Puwg2000 {
    258     private static final int PuwgZone = 5;
    259 
    260     @Override
    261     public int getZone() { return PuwgZone; }
    262 }
    263 
    264 class Epsg2177 extends Puwg2000 {
    265     private static final int PuwgZone = 6;
    266 
    267     @Override
    268     public int getZone() { return PuwgZone; }
    269 }
    270 
    271 class Epsg2178 extends Puwg2000 {
    272     private static final int PuwgZone = 7;
    273 
    274     @Override
    275     public int getZone() { return PuwgZone; }
    276 }
    277 
    278 class Epsg2179 extends Puwg2000 {
    279     private static final int PuwgZone = 8;
    280 
    281     @Override
    282     public int getZone() { return PuwgZone; }
    283 }
  • trunk/src/org/openstreetmap/josm/data/projection/SwissGrid.java

    r5066 r5234  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.event.ActionListener;
    7 import java.util.Collection;
    8 import java.util.Collections;
    9 
    10 import javax.swing.Box;
    11 import javax.swing.JPanel;
    125
    136import org.openstreetmap.josm.data.Bounds;
     
    169import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    1710import org.openstreetmap.josm.data.projection.proj.SwissObliqueMercator;
    18 import org.openstreetmap.josm.gui.widgets.HtmlPanel;
    19 import org.openstreetmap.josm.tools.GBC;
    2011
    2112/**
     
    3122 * ProjectionSubPrefs to show a warning that the grid file correction is not done.
    3223 */
    33 public class SwissGrid extends AbstractProjection implements ProjectionSubPrefs {
     24public class SwissGrid extends AbstractProjection {
    3425
    3526    public SwissGrid() {
     
    7566    }
    7667
    77     @Override
    78     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    79         p.add(new HtmlPanel(tr("<i>CH1903 / LV03 (without local corrections)</i>")), GBC.eol().fill(GBC.HORIZONTAL));
    80         p.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH));
    81     }
    82 
    83     @Override
    84     public void setPreferences(Collection<String> args) {
    85     }
    86 
    87     @Override
    88     public Collection<String> getPreferences(JPanel p) {
    89         return Collections.singletonList("CH1903");
    90     }
    91 
    92     @Override
    93     public String[] allCodes() {
    94         return new String[] { "EPSG:21781" };
    95     }
    96 
    97     @Override
    98     public Collection<String> getPreferencesFromCode(String code) {
    99         if ("EPSG:21781".equals(code))
    100             return Collections.singletonList("CH1903");
    101         return null;
    102     }
    10368}
  • trunk/src/org/openstreetmap/josm/data/projection/UTM.java

    r5232 r5234  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
    6 import java.awt.GridBagLayout;
    7 import java.awt.event.ActionListener;
    8 import java.util.ArrayList;
    9 import java.util.Arrays;
    10 import java.util.Collection;
    11 
    12 import javax.swing.ButtonGroup;
    13 import javax.swing.JCheckBox;
    14 import javax.swing.JComboBox;
    15 import javax.swing.JLabel;
    16 import javax.swing.JPanel;
    17 import javax.swing.JRadioButton;
    18 
    196import org.openstreetmap.josm.data.Bounds;
    207import org.openstreetmap.josm.data.coor.LatLon;
    218import org.openstreetmap.josm.data.projection.datum.WGS84Datum;
    229import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    23 import org.openstreetmap.josm.tools.GBC;
    2410
    2511/**
     
    2915 *
    3016 */
    31 public class UTM extends AbstractProjection implements ProjectionSubPrefs {
     17public class UTM extends AbstractProjection {
    3218
    3319    private static final int DEFAULT_ZONE = 30;
     
    5642        }
    5743        datum = WGS84Datum.INSTANCE;
    58         updateParameters(zone, hemisphere, offset);
    59     }
    60 
    61     public void updateParameters(int zone, Hemisphere hemisphere, boolean offset) {
    6244        this.zone = zone;
    6345        this.hemisphere = hemisphere;
     
    125107    }
    126108
    127     @Override
    128     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    129         //Zone
    130         JComboBox zonecb = new JComboBox();
    131         for(int i = 1; i <= 60; i++) {
    132             zonecb.addItem(i);
    133         }
    134 
    135         zonecb.setSelectedIndex(zone - 1);
    136         p.setLayout(new GridBagLayout());
    137         p.add(new JLabel(tr("UTM Zone")), GBC.std().insets(5,5,0,5));
    138         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    139         /* Note: we use component position 2 below to find this again */
    140         p.add(zonecb, GBC.eop().fill(GBC.HORIZONTAL));
    141         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    142 
    143         //Hemisphere
    144         JRadioButton north = new JRadioButton();
    145         north.setSelected(hemisphere == Hemisphere.North);
    146         JRadioButton south = new JRadioButton();
    147         south.setSelected(hemisphere == Hemisphere.South);
    148 
    149         ButtonGroup group = new ButtonGroup();
    150         group.add(north);
    151         group.add(south);
    152 
    153         JPanel bPanel = new JPanel();
    154         bPanel.setLayout(new GridBagLayout());
    155 
    156         bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5));
    157         bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL));
    158         bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    159         bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5));
    160         bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL));
    161         bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    162 
    163         p.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5,5,0,5));
    164         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    165         p.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL));
    166         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    167 
    168         //Offset
    169         JCheckBox offsetBox = new JCheckBox();
    170         offsetBox.setSelected(offset);
    171 
    172         p.add(new JLabel(tr("Offset 3.000.000m east")), GBC.std().insets(5,5,0,5));
    173         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    174         /* Note: we use component position 2 below to find this again */
    175         p.add(offsetBox, GBC.eop().fill(GBC.HORIZONTAL));
    176         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    177 
    178         if (listener != null) {
    179             north.addActionListener(listener);
    180             south.addActionListener(listener);
    181             zonecb.addActionListener(listener);
    182             offsetBox.addActionListener(listener);
    183         }
    184     }
    185 
    186     @Override
    187     public Collection<String> getPreferences(JPanel p) {
    188         int zone = DEFAULT_ZONE;
    189         Hemisphere hemisphere = DEFAULT_HEMISPHERE;
    190         boolean offset = false;
    191 
    192         Object zonecb = p.getComponent(2);
    193         if (zonecb instanceof JComboBox) {
    194             zone = ((JComboBox)zonecb).getSelectedIndex() + 1;
    195         }
    196 
    197         Object bPanel = p.getComponent(6);
    198         if (bPanel instanceof JPanel) {
    199             Object south = ((JPanel)bPanel).getComponent(4);
    200             if (south instanceof JRadioButton) {
    201                 hemisphere = ((JRadioButton)south).isSelected()?Hemisphere.South:Hemisphere.North;
    202             }
    203         }
    204 
    205         Object offsetBox = p.getComponent(10);
    206         if (offsetBox instanceof JCheckBox) {
    207             offset = ((JCheckBox) offsetBox).isSelected();
    208         }
    209 
    210         return Arrays.asList(Integer.toString(zone), hemisphere.toString(), (offset?"offset":"standard"));
    211     }
    212 
    213     @Override
    214     public void setPreferences(Collection<String> args) {
    215         int zone = DEFAULT_ZONE;
    216         Hemisphere hemisphere = DEFAULT_HEMISPHERE;
    217         boolean offset = false;
    218 
    219         if(args != null)
    220         {
    221             String[] array = args.toArray(new String[0]);
    222             try {
    223                 zone = Integer.parseInt(array[0]);
    224                 if(zone <= 0 || zone > 60) {
    225                     zone = DEFAULT_ZONE;
    226                 }
    227             } catch(NumberFormatException e) {}
    228 
    229             if (array.length > 1) {
    230                 hemisphere = Hemisphere.valueOf(array[1]);
    231             }
    232 
    233             if (array.length > 2) {
    234                 offset = array[2].equals("offset");
    235             }
    236         }
    237         updateParameters(zone, hemisphere, offset);
    238     }
    239 
    240     @Override
    241     public String[] allCodes() {
    242         ArrayList<String> projections = new ArrayList<String>(60*4);
    243         for (int zone = 1;zone <= 60; zone++) {
    244             for (boolean offset : new boolean[] { false, true }) {
    245                 for (Hemisphere hemisphere : Hemisphere.values()) {
    246                     projections.add("EPSG:" + ((offset?325800:32600) + zone + (hemisphere == Hemisphere.South?100:0)));
    247                 }
    248             }
    249         }
    250         return projections.toArray(new String[0]);
    251     }
    252 
    253     @Override
    254     public Collection<String> getPreferencesFromCode(String code) {
    255 
    256         boolean offset = code.startsWith("EPSG:3258") || code.startsWith("EPSG:3259");
    257 
    258         if(code.startsWith("EPSG:326") || code.startsWith("EPSG:327") || offset)
    259         {
    260             try {
    261                 Hemisphere hemisphere;
    262                 String zonestring;
    263                 if (offset) {
    264                     hemisphere = code.charAt(8)=='8'?Hemisphere.North:Hemisphere.South;
    265                     zonestring = code.substring(9);
    266                 } else {
    267                     hemisphere = code.charAt(7)=='6'?Hemisphere.North:Hemisphere.South;
    268                     zonestring = code.substring(8);
    269                 }
    270 
    271                 int zoneval = Integer.parseInt(zonestring);
    272                 if(zoneval > 0 && zoneval <= 60)
    273                     return Arrays.asList(zonestring, hemisphere.toString(), (offset?"offset":"standard"));
    274             } catch(NumberFormatException e) {}
    275         }
    276         return null;
    277     }
    278109}
  • trunk/src/org/openstreetmap/josm/data/projection/UTM_France_DOM.java

    r5067 r5234  
    33
    44import static org.openstreetmap.josm.tools.I18n.tr;
    5 
    6 import java.awt.GridBagLayout;
    7 import java.awt.event.ActionListener;
    8 import java.util.Collection;
    9 import java.util.Collections;
    10 
    11 import javax.swing.JComboBox;
    12 import javax.swing.JLabel;
    13 import javax.swing.JPanel;
    145
    156import org.openstreetmap.josm.data.Bounds;
     
    2011import org.openstreetmap.josm.data.projection.datum.ThreeParameterDatum;
    2112import org.openstreetmap.josm.data.projection.proj.ProjParameters;
    22 import org.openstreetmap.josm.tools.GBC;
    2313
    2414/**
     
    2717 *
    2818 */
    29 public class UTM_France_DOM extends AbstractProjection implements ProjectionSubPrefs {
    30 
    31     private final static String FortMarigotName = tr("Guadeloupe Fort-Marigot 1949");
    32     private final static String SainteAnneName = tr("Guadeloupe Ste-Anne 1948");
    33     private final static String MartiniqueName = tr("Martinique Fort Desaix 1952");
    34     private final static String Reunion92Name = tr("Reunion RGR92");
    35     private final static String Guyane92Name = tr("Guyane RGFG95");
    36     private final static String[] utmGeodesicsNames = { FortMarigotName, SainteAnneName, MartiniqueName, Reunion92Name, Guyane92Name};
     19public class UTM_France_DOM extends AbstractProjection {
    3720
    3821    private final static Bounds FortMarigotBounds = new Bounds( new LatLon(17.6,-63.25), new LatLon(18.5,-62.5));
     
    4831    private final static Integer ReunionEPSG = 2975;
    4932    private final static Integer GuyaneEPSG = 2972;
    50     private final static Integer[] utmEPSGs = { FortMarigotEPSG, SainteAnneEPSG, MartiniqueEPSG, ReunionEPSG, GuyaneEPSG };
     33    public final static Integer[] utmEPSGs = { FortMarigotEPSG, SainteAnneEPSG, MartiniqueEPSG, ReunionEPSG, GuyaneEPSG };
    5134
    5235    private final static Datum FortMarigotDatum = new ThreeParameterDatum("FortMarigot Datum", null, Ellipsoid.hayford, 136.596, 248.148, -429.789);
     
    7457
    7558    public UTM_France_DOM() {
    76         updateParameters(DEFAULT_GEODESIC);
     59        this(DEFAULT_GEODESIC);
    7760    }
    7861
    79     public void updateParameters(int currentGeodesic) {
     62    public UTM_France_DOM(int currentGeodesic) {
    8063        this.currentGeodesic = currentGeodesic;
    8164        datum = utmDatums[currentGeodesic];
     
    124107    }
    125108
    126     @Override
    127     public void setupPreferencePanel(JPanel p, ActionListener listener) {
    128         JComboBox prefcb = new JComboBox(utmGeodesicsNames);
    129 
    130         prefcb.setSelectedIndex(currentGeodesic);
    131         p.setLayout(new GridBagLayout());
    132         p.add(new JLabel(tr("UTM Geodesic system")), GBC.std().insets(5,5,0,5));
    133         p.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL));
    134         p.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL));
    135         p.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH));
    136         if (listener != null) {
    137             prefcb.addActionListener(listener);
    138         }
    139     }
    140 
    141     @Override
    142     public Collection<String> getPreferences(JPanel p) {
    143         Object prefcb = p.getComponent(2);
    144         if(!(prefcb instanceof JComboBox))
    145             return null;
    146         currentGeodesic = ((JComboBox)prefcb).getSelectedIndex();
    147         return Collections.singleton(Integer.toString(currentGeodesic+1));
    148     }
    149 
    150     @Override
    151     public String[] allCodes() {
    152         String[] res = new String[utmEPSGs.length];
    153         for (int i=0; i<utmEPSGs.length; ++i) {
    154             res[i] = "EPSG:"+utmEPSGs[i];
    155         }
    156         return res;
    157     }
    158 
    159     @Override
    160     public Collection<String> getPreferencesFromCode(String code) {
    161         for (int i=0; i < utmEPSGs.length; i++ )
    162             if (("EPSG:"+utmEPSGs[i]).equals(code))
    163                 return Collections.singleton(Integer.toString(i+1));
    164         return null;
    165     }
    166 
    167     @Override
    168     public void setPreferences(Collection<String> args) {
    169         int currentGeodesic = DEFAULT_GEODESIC;
    170         if (args != null) {
    171             try {
    172                 for(String s : args)
    173                 {
    174                     currentGeodesic = Integer.parseInt(s)-1;
    175                     if(currentGeodesic < 0 || currentGeodesic >= utmEPSGs.length) {
    176                         currentGeodesic = DEFAULT_GEODESIC;
    177                     }
    178                     break;
    179                 }
    180             } catch(NumberFormatException e) {}
    181         }
    182         updateParameters(currentGeodesic);
    183     }
    184109}
  • trunk/src/org/openstreetmap/josm/gui/preferences/imagery/AddWMSLayerPanel.java

    r4968 r5234  
    5454import org.openstreetmap.josm.data.imagery.ImageryInfo;
    5555import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType;
    56 import org.openstreetmap.josm.data.projection.Projection;
    57 import org.openstreetmap.josm.data.projection.ProjectionSubPrefs;
    58 import org.openstreetmap.josm.data.projection.Projections;
    5956import org.openstreetmap.josm.gui.bbox.SlippyMapBBoxChooser;
    6057import org.openstreetmap.josm.gui.layer.TMSLayer;
     58import org.openstreetmap.josm.gui.preferences.projection.ProjectionChoice;
     59import org.openstreetmap.josm.gui.preferences.projection.ProjectionPreference;
    6160import org.openstreetmap.josm.io.UTFInputStreamReader;
    6261import org.openstreetmap.josm.tools.GBC;
     
    492491
    493492    private boolean isProjSupported(String crs) {
    494         for (Projection proj : Projections.getProjections()) {
    495             if (proj instanceof ProjectionSubPrefs) {
    496                 if (((ProjectionSubPrefs) proj).getPreferencesFromCode(crs) == null)
    497                     return true;
    498             } else {
    499                 if (proj.toCode().equals(crs))
    500                     return true;
    501             }
     493        for (ProjectionChoice pc : ProjectionPreference.getProjectionChoices()) {
     494            if (pc.getPreferencesFromCode(crs) != null) return true;
    502495        }
    503496        return false;
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/ProjectionPreference.java

    r5226 r5234  
    1010import java.util.ArrayList;
    1111import java.util.Collection;
     12import java.util.Collections;
     13import java.util.HashMap;
     14import java.util.List;
     15import java.util.Map;
    1216
    1317import javax.swing.BorderFactory;
     
    2327import org.openstreetmap.josm.data.coor.CoordinateFormat;
    2428import org.openstreetmap.josm.data.preferences.CollectionProperty;
    25 import org.openstreetmap.josm.data.preferences.ParametrizedCollectionProperty;
    2629import org.openstreetmap.josm.data.preferences.StringProperty;
     30import org.openstreetmap.josm.data.projection.BelgianLambert1972;
     31import org.openstreetmap.josm.data.projection.BelgianLambert2008;
     32import org.openstreetmap.josm.data.projection.Epsg3008;
     33import org.openstreetmap.josm.data.projection.Epsg4326;
     34import org.openstreetmap.josm.data.projection.Lambert93;
     35import org.openstreetmap.josm.data.projection.LambertEST;
    2736import org.openstreetmap.josm.data.projection.Mercator;
    2837import org.openstreetmap.josm.data.projection.Projection;
    29 import org.openstreetmap.josm.data.projection.ProjectionSubPrefs;
    30 import org.openstreetmap.josm.data.projection.Projections;
     38import org.openstreetmap.josm.data.projection.TransverseMercatorLV;
    3139import org.openstreetmap.josm.gui.NavigatableComponent;
    3240import org.openstreetmap.josm.gui.preferences.PreferenceSetting;
     
    3543import org.openstreetmap.josm.gui.preferences.SubPreferenceSetting;
    3644import org.openstreetmap.josm.gui.preferences.TabPreferenceSetting;
    37 import org.openstreetmap.josm.plugins.PluginHandler;
    3845import org.openstreetmap.josm.tools.GBC;
    3946
     
    4653    }
    4754
    48     private static final StringProperty PROP_PROJECTION = new StringProperty("projection", Mercator.class.getName());
     55    private static List<ProjectionChoice> projectionChoices = new ArrayList<ProjectionChoice>();
     56    private static Map<String, ProjectionChoice> projectionChoicesById = new HashMap<String, ProjectionChoice>();
     57    private static Map<String, String> aliasNormalizer = new HashMap<String, String>();
     58
     59    public static ProjectionChoice mercator = new SingleProjectionChoice("core:mercator", new Mercator());
     60    static {
     61        // global projections
     62        registerProjectionChoice("core:wgs84", new Epsg4326());
     63        registerProjectionChoice(mercator);
     64        registerProjectionChoice(new UTMProjectionChoice());
     65        // regional - alphabetical order by country code
     66        registerProjectionChoice("core:belambert1972", new BelgianLambert1972());   // BE
     67        registerProjectionChoice("core:belambert2008", new BelgianLambert2008());   // BE
     68        registerProjectionChoice(new SwissGridProjectionChoice());                  // CH
     69        registerProjectionChoice(new GaussKruegerProjectionChoice());               // DE
     70        registerProjectionChoice("core:lambertest", new LambertEST());              // EE
     71        registerProjectionChoice(new LambertProjectionChoice());                    // FR
     72        registerProjectionChoice("core:lambert93", new Lambert93());                // FR
     73        registerProjectionChoice(new LambertCC9ZonesProjectionChoice());            // FR
     74        registerProjectionChoice(new UTM_France_DOM_ProjectionChoice());            // FR
     75        registerProjectionChoice("core:tmerclv", new TransverseMercatorLV());       // LV
     76        registerProjectionChoice(new PuwgProjectionChoice());                       // PL
     77        registerProjectionChoice("core:sweref99", new Epsg3008());                  // SE
     78        registerProjectionChoice(new CustomProjectionChoice());
     79    }
     80
     81    public static void registerProjectionChoice(ProjectionChoice c) {
     82        projectionChoices.add(c);
     83        projectionChoicesById.put(c.getId(), c);
     84        aliasNormalizer.put(c.getId(), c.getId());
     85        if (c instanceof Alias) {
     86            String alias = ((Alias) c).getAlias();
     87            projectionChoicesById.put(alias, c);
     88            aliasNormalizer.put(alias, c.getId());
     89        }
     90    }
     91
     92    public static void registerProjectionChoice(String id, Projection projection) {
     93        registerProjectionChoice(new SingleProjectionChoice(id, projection));
     94    }
     95
     96    public static List<ProjectionChoice> getProjectionChoices() {
     97        return Collections.unmodifiableList(projectionChoices);
     98    }
     99
     100    private static final StringProperty PROP_PROJECTION = new StringProperty("projection", mercator.getId());
    49101    private static final StringProperty PROP_COORDINATES = new StringProperty("coordinates", null);
    50102    private static final CollectionProperty PROP_SUB_PROJECTION = new CollectionProperty("projection.sub", null);
    51     private static final ParametrizedCollectionProperty PROP_PROJECTION_SUBPROJECTION = new ParametrizedCollectionProperty(null) {
    52         @Override
    53         protected String getKey(String... params) {
    54             String name = params[0];
    55             String sname = name.substring(name.lastIndexOf(".")+1);
    56             return "projection.sub."+sname;
    57         }
    58     };
    59103    public static final StringProperty PROP_SYSTEM_OF_MEASUREMENT = new StringProperty("system_of_measurement", "Metric");
    60104    private static final String[] unitsValues = (new ArrayList<String>(NavigatableComponent.SYSTEMS_OF_MEASUREMENT.keySet())).toArray(new String[0]);
     
    69113     * Combobox with all projections available
    70114     */
    71     private JComboBox projectionCombo = new JComboBox(Projections.getProjections().toArray());
     115    private JComboBox projectionCombo = new JComboBox(projectionChoices.toArray());
    72116
    73117    /**
     
    104148
    105149    public void addGui(PreferenceTabbedPane gui) {
    106         setupProjectionCombo();
     150        ProjectionChoice pc = setupProjectionCombo();
    107151
    108152        for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) {
     
    131175        projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL));
    132176        projPanel.add(bounds, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5));
    133         projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
    134177        projPanel.add(projSubPrefPanelWrapper, GBC.eol().fill(GBC.HORIZONTAL).insets(20,5,5,5));
    135178
     
    146189        gui.getMapPreference().mapcontent.addTab(tr("Map Projection"), scrollpane);
    147190
    148         updateMeta(Main.getProjection());
    149     }
    150 
    151     private void updateMeta(Projection proj)
    152     {
     191        selectedProjectionChanged(pc);
     192    }
     193
     194    private void updateMeta(ProjectionChoice pc) {
     195        pc.setPreferences(pc.getPreferences(projSubPrefPanel));
     196        Projection proj = pc.getProjection();
    153197        projectionCode.setText(proj.toCode());
    154198        Bounds b = proj.getWorldBoundsLatLon();
    155199        CoordinateFormat cf = CoordinateFormat.getDefaultFormat();
    156200        bounds.setText(b.getMin().latToString(cf)+"; "+b.getMin().lonToString(cf)+" : "+b.getMax().latToString(cf)+"; "+b.getMax().lonToString(cf));
    157         boolean hideCode = proj instanceof SubPrefsOptions && !((SubPrefsOptions) proj).showProjectionCode();
    158         projectionCodeLabel.setVisible(!hideCode);
    159         projectionCodeGlue.setVisible(!hideCode);
    160         projectionCode.setVisible(!hideCode);
    161     }
    162 
     201        boolean showCode = true;
     202        if (pc instanceof SubPrefsOptions) {
     203            showCode = ((SubPrefsOptions) pc).showProjectionCode();
     204        }
     205        projectionCodeLabel.setVisible(showCode);
     206        projectionCodeGlue.setVisible(showCode);
     207        projectionCode.setVisible(showCode);
     208    }
     209
     210    @Override
    163211    public boolean ok() {
    164         Projection proj = (Projection) projectionCombo.getSelectedItem();
    165 
    166         String projname = proj.getClass().getName();
    167         Collection<String> prefs = null;
    168         if(proj instanceof ProjectionSubPrefs) {
    169             prefs = ((ProjectionSubPrefs) proj).getPreferences(projSubPrefPanel);
    170         }
    171 
    172         PROP_PROJECTION.put(projname);
    173         setProjection(projname, prefs);
     212        ProjectionChoice pc = (ProjectionChoice) projectionCombo.getSelectedItem();
     213
     214        String id = pc.getId();
     215        Collection<String> prefs = pc.getPreferences(projSubPrefPanel);
     216
     217        setProjection(id, prefs);
    174218
    175219        if(PROP_COORDINATES.put(((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) {
     
    183227    }
    184228
    185     static public void setProjection()
    186     {
     229    static public void setProjection() {
    187230        setProjection(PROP_PROJECTION.get(), PROP_SUB_PROJECTION.get());
    188231    }
    189232
    190     static public void setProjection(String name, Collection<String> coll)
    191     {
    192         Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null;
    193 
    194         Projection proj = null;
    195         for (ClassLoader cl : PluginHandler.getResourceClassLoaders()) {
    196             try {
    197                 proj = (Projection) Class.forName(name, true, cl).newInstance();
    198             } catch (final Exception e) {
    199             }
    200             if (proj != null) {
    201                 break;
    202             }
    203         }
    204         if (proj == null) {
     233    static public void setProjection(String id, Collection<String> pref) {
     234        ProjectionChoice pc = projectionChoicesById.get(id);
     235
     236        if (pc == null) {
    205237            JOptionPane.showMessageDialog(
    206238                    Main.parent,
    207                     tr("The projection {0} could not be activated. Using Mercator", name),
     239                    tr("The projection {0} could not be activated. Using Mercator", id),
    208240                    tr("Error"),
    209241                    JOptionPane.ERROR_MESSAGE
    210242            );
    211             coll = null;
    212             proj = new Mercator();
    213             name = proj.getClass().getName();
    214             PROP_PROJECTION.put(name);
    215         }
    216         PROP_SUB_PROJECTION.put(coll);
    217         PROP_PROJECTION_SUBPROJECTION.put(coll, name);
    218         if (proj instanceof ProjectionSubPrefs) {
    219             ((ProjectionSubPrefs) proj).setPreferences(coll);
    220         }
    221         Projection oldProj = Main.getProjection();
     243            pref = null;
     244            pc = mercator;
     245        }
     246        id = pc.getId();
     247        PROP_PROJECTION.put(id);
     248        PROP_SUB_PROJECTION.put(pref);
     249        Main.pref.putCollection("projection.sub."+id, pref);
     250        pc.setPreferences(pref);
     251        Projection proj = pc.getProjection();
    222252        Main.setProjection(proj);
    223         if (b != null && (!proj.getClass().getName().equals(oldProj.getClass().getName()) || proj.hashCode() != oldProj.hashCode()))
    224         {
    225             Main.map.mapView.zoomTo(b);
    226             /* TODO - remove layers with fixed projection */
    227         }
    228     }
    229 
    230     private class SBPanel extends JPanel implements ActionListener
    231     {
    232         private ProjectionSubPrefs p;
    233         public SBPanel(ProjectionSubPrefs pr)
    234         {
    235             super();
    236             p = pr;
    237         }
    238 
    239         @Override
    240         public void actionPerformed(ActionEvent e) {
    241             p.setPreferences(p.getPreferences(this));
    242             updateMeta(p);
    243         }
    244253    }
    245254
     
    249258     * @param proj
    250259     */
    251     private void selectedProjectionChanged(Projection proj) {
    252         if(!(proj instanceof ProjectionSubPrefs)) {
    253             projSubPrefPanel = new JPanel();
    254         } else {
    255             ProjectionSubPrefs projPref = (ProjectionSubPrefs) proj;
    256             projSubPrefPanel = new SBPanel(projPref);
    257             projPref.setupPreferencePanel(projSubPrefPanel, (SBPanel)projSubPrefPanel);
    258         }
    259 
     260    private void selectedProjectionChanged(final ProjectionChoice pc) {
    260261        // Don't try to update if we're still starting up
    261262        int size = projPanel.getComponentCount();
     
    263264            return;
    264265
     266        final ActionListener listener = new ActionListener() {
     267            @Override
     268            public void actionPerformed(ActionEvent e) {
     269                updateMeta(pc);
     270            }
     271        };
     272
    265273        // Replace old panel with new one
    266274        projSubPrefPanelWrapper.removeAll();
     275        projSubPrefPanel = pc.getPreferencePanel(listener);
    267276        projSubPrefPanelWrapper.add(projSubPrefPanel, projSubPrefPanelGBC);
    268277        projPanel.revalidate();
    269278        projSubPrefPanel.repaint();
    270         updateMeta(proj);
     279        updateMeta(pc);
    271280    }
    272281
     
    274283     * Sets up projection combobox with default values and action listener
    275284     */
    276     private void setupProjectionCombo() {
    277         boolean found = false;
     285    private ProjectionChoice setupProjectionCombo() {
     286        ProjectionChoice pc = null;
    278287        for (int i = 0; i < projectionCombo.getItemCount(); ++i) {
    279             Projection proj = (Projection)projectionCombo.getItemAt(i);
    280             String name = proj.getClass().getName();
    281             if(proj instanceof ProjectionSubPrefs) {
    282                 ((ProjectionSubPrefs) proj).setPreferences(PROP_PROJECTION_SUBPROJECTION.get(name));
    283             }
    284             if (name.equals(PROP_PROJECTION.get())) {
     288            ProjectionChoice pc1 = (ProjectionChoice) projectionCombo.getItemAt(i);
     289            pc1.setPreferences(getSubprojectionPreference(pc1));
     290            if (pc1.getId().equals(aliasNormalizer.get(PROP_PROJECTION.get()))) {
    285291                projectionCombo.setSelectedIndex(i);
    286                 selectedProjectionChanged(proj);
    287                 found = true;
    288                 break;
    289             }
    290         }
    291         if (!found)
     292                selectedProjectionChanged(pc1);
     293                pc = pc1;
     294            }
     295        }
     296        // If the ProjectionChoice from the preferences is not available, it
     297        // should have been set to Mercator at JOSM start.
     298        if (pc == null)
    292299            throw new RuntimeException("Couldn't find the current projection in the list of available projections!");
    293300
    294301        projectionCombo.addActionListener(new ActionListener() {
    295302            public void actionPerformed(ActionEvent e) {
    296                 JComboBox cb = (JComboBox)e.getSource();
    297                 Projection proj = (Projection)cb.getSelectedItem();
    298                 selectedProjectionChanged(proj);
     303                ProjectionChoice pc = (ProjectionChoice) projectionCombo.getSelectedItem();
     304                selectedProjectionChanged(pc);
    299305            }
    300306        });
     307        return pc;
     308    }
     309
     310    private Collection<String> getSubprojectionPreference(ProjectionChoice pc) {
     311        Collection<String> c1 = Main.pref.getCollection("projection.sub."+pc.getId(), null);
     312        if (c1 != null)
     313            return c1;
     314        if (pc instanceof Alias) {
     315            String alias = ((Alias) pc).getAlias();
     316            String sname = alias.substring(alias.lastIndexOf(".")+1);
     317            return Main.pref.getCollection("projection.sub."+sname, null);
     318        }
     319        return null;
    301320    }
    302321
  • trunk/src/org/openstreetmap/josm/gui/preferences/projection/SubPrefsOptions.java

    r5226 r5234  
    33
    44/**
    5  * ProjectionSubPrefs can implement this interface to set some additional options
     5 * ProjectionChoice can implement this interface to set some additional options
    66 */
    77public interface SubPrefsOptions {
Note: See TracChangeset for help on using the changeset viewer.