001 // License: GPL. For details, see LICENSE file. 002 package org.openstreetmap.josm.gui.preferences.projection; 003 004 import static org.openstreetmap.josm.tools.I18n.tr; 005 006 import java.awt.GridBagLayout; 007 import java.awt.event.ActionListener; 008 import java.util.ArrayList; 009 import java.util.Arrays; 010 import java.util.Collection; 011 import java.util.List; 012 013 import javax.swing.ButtonGroup; 014 import javax.swing.JLabel; 015 import javax.swing.JPanel; 016 import javax.swing.JRadioButton; 017 018 import org.openstreetmap.josm.data.projection.Projection; 019 import org.openstreetmap.josm.data.projection.UTM; 020 import org.openstreetmap.josm.tools.GBC; 021 022 public class UTMProjectionChoice extends ListProjectionChoice implements Alias { 023 024 private static final UTM.Hemisphere DEFAULT_HEMISPHERE = UTM.Hemisphere.North; 025 026 private UTM.Hemisphere hemisphere; 027 028 private final static List<String> cbEntries = new ArrayList<String>(); 029 static { 030 for (int i = 1; i <= 60; i++) { 031 cbEntries.add(Integer.toString(i)); 032 } 033 } 034 035 public UTMProjectionChoice() { 036 super("core:utm", tr("UTM"), cbEntries.toArray(), tr("UTM Zone")); 037 } 038 039 private class UTMPanel extends CBPanel { 040 041 public JRadioButton north, south; 042 043 public UTMPanel(Object[] entries, int initialIndex, String label, ActionListener listener) { 044 super(entries, initialIndex, label, listener); 045 046 //Hemisphere 047 north = new JRadioButton(); 048 north.setSelected(hemisphere == UTM.Hemisphere.North); 049 south = new JRadioButton(); 050 south.setSelected(hemisphere == UTM.Hemisphere.South); 051 052 ButtonGroup group = new ButtonGroup(); 053 group.add(north); 054 group.add(south); 055 056 JPanel bPanel = new JPanel(); 057 bPanel.setLayout(new GridBagLayout()); 058 059 bPanel.add(new JLabel(tr("North")), GBC.std().insets(5, 5, 0, 5)); 060 bPanel.add(north, GBC.std().fill(GBC.HORIZONTAL)); 061 bPanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL)); 062 bPanel.add(new JLabel(tr("South")), GBC.std().insets(5, 5, 0, 5)); 063 bPanel.add(south, GBC.std().fill(GBC.HORIZONTAL)); 064 bPanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH)); 065 066 this.add(new JLabel(tr("Hemisphere")), GBC.std().insets(5,5,0,5)); 067 this.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL)); 068 this.add(bPanel, GBC.eop().fill(GBC.HORIZONTAL)); 069 this.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH)); 070 071 if (listener != null) { 072 north.addActionListener(listener); 073 south.addActionListener(listener); 074 } 075 } 076 } 077 078 @Override 079 public JPanel getPreferencePanel(ActionListener listener) { 080 return new UTMPanel(entries, index, label, listener); 081 } 082 083 @Override 084 public Projection getProjection() { 085 return new UTM(index + 1, hemisphere); 086 } 087 088 @Override 089 public Collection<String> getPreferences(JPanel panel) { 090 UTMPanel p = (UTMPanel) panel; 091 int index = p.prefcb.getSelectedIndex(); 092 UTM.Hemisphere hemisphere = p.south.isSelected()?UTM.Hemisphere.South:UTM.Hemisphere.North; 093 return Arrays.asList(indexToZone(index), hemisphere.toString()); 094 } 095 096 @Override 097 public String[] allCodes() { 098 ArrayList<String> projections = new ArrayList<String>(60*4); 099 for (int zone = 1;zone <= 60; zone++) { 100 for (UTM.Hemisphere hemisphere : UTM.Hemisphere.values()) { 101 projections.add("EPSG:" + (32600 + zone + (hemisphere == UTM.Hemisphere.South?100:0))); 102 } 103 } 104 return projections.toArray(new String[0]); 105 } 106 107 @Override 108 public Collection<String> getPreferencesFromCode(String code) { 109 110 if (code.startsWith("EPSG:326") || code.startsWith("EPSG:327")) { 111 try { 112 UTM.Hemisphere hemisphere = code.charAt(7)=='6'?UTM.Hemisphere.North:UTM.Hemisphere.South; 113 String zonestring = code.substring(8); 114 int zoneval = Integer.parseInt(zonestring); 115 if(zoneval > 0 && zoneval <= 60) 116 return Arrays.asList(zonestring, hemisphere.toString()); 117 } catch(NumberFormatException e) {} 118 } 119 return null; 120 } 121 122 @Override 123 public void setPreferences(Collection<String> args) { 124 super.setPreferences(args); 125 UTM.Hemisphere hemisphere = DEFAULT_HEMISPHERE; 126 127 if(args != null) { 128 String[] array = args.toArray(new String[0]); 129 130 if (array.length > 1) { 131 hemisphere = UTM.Hemisphere.valueOf(array[1]); 132 } 133 } 134 this.hemisphere = hemisphere; 135 } 136 137 @Override 138 protected String indexToZone(int index) { 139 return Integer.toString(index + 1); 140 } 141 142 @Override 143 protected int zoneToIndex(String zone) { 144 try { 145 return Integer.parseInt(zone) - 1; 146 } catch(NumberFormatException e) {} 147 return defaultIndex; 148 } 149 150 @Override 151 public String getAlias() { 152 return UTM.class.getName(); 153 } 154 155 }