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)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.