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.tools.GBC; 019 020 public class UTMProjectionChoice extends ListProjectionChoice { 021 022 public enum Hemisphere { North, South } 023 024 private static final Hemisphere DEFAULT_HEMISPHERE = Hemisphere.North; 025 026 private 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(tr("UTM"), "core: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 == Hemisphere.North); 049 south = new JRadioButton(); 050 south.setSelected(hemisphere == 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 String getCurrentCode() { 085 int zone = index + 1; 086 int code = 32600 + zone + (hemisphere == Hemisphere.South ? 100 : 0); 087 return "EPSG:" + Integer.toString(code); 088 } 089 090 @Override 091 public String getProjectionName() { 092 return tr("UTM"); 093 } 094 095 096 @Override 097 public Collection<String> getPreferences(JPanel panel) { 098 UTMPanel p = (UTMPanel) panel; 099 int index = p.prefcb.getSelectedIndex(); 100 Hemisphere hemisphere = p.south.isSelected()?Hemisphere.South:Hemisphere.North; 101 return Arrays.asList(indexToZone(index), hemisphere.toString()); 102 } 103 104 @Override 105 public String[] allCodes() { 106 ArrayList<String> projections = new ArrayList<String>(60*4); 107 for (int zone = 1;zone <= 60; zone++) { 108 for (Hemisphere hemisphere : Hemisphere.values()) { 109 projections.add("EPSG:" + (32600 + zone + (hemisphere == Hemisphere.South?100:0))); 110 } 111 } 112 return projections.toArray(new String[0]); 113 } 114 115 @Override 116 public Collection<String> getPreferencesFromCode(String code) { 117 118 if (code.startsWith("EPSG:326") || code.startsWith("EPSG:327")) { 119 try { 120 Hemisphere hemisphere = code.charAt(7)=='6'?Hemisphere.North:Hemisphere.South; 121 String zonestring = code.substring(8); 122 int zoneval = Integer.parseInt(zonestring); 123 if(zoneval > 0 && zoneval <= 60) 124 return Arrays.asList(zonestring, hemisphere.toString()); 125 } catch(NumberFormatException e) {} 126 } 127 return null; 128 } 129 130 @Override 131 public void setPreferences(Collection<String> args) { 132 super.setPreferences(args); 133 Hemisphere hemisphere = DEFAULT_HEMISPHERE; 134 135 if(args != null) { 136 String[] array = args.toArray(new String[0]); 137 138 if (array.length > 1) { 139 hemisphere = Hemisphere.valueOf(array[1]); 140 } 141 } 142 this.hemisphere = hemisphere; 143 } 144 145 @Override 146 protected String indexToZone(int index) { 147 return Integer.toString(index + 1); 148 } 149 150 @Override 151 protected int zoneToIndex(String zone) { 152 try { 153 return Integer.parseInt(zone) - 1; 154 } catch(NumberFormatException e) {} 155 return defaultIndex; 156 } 157 158 }