- Timestamp:
- 2009-10-11T16:33:29+02:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 1 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/Main.java
r2224 r2272 52 52 import org.openstreetmap.josm.gui.layer.OsmDataLayer.CommandQueueListener; 53 53 import org.openstreetmap.josm.gui.preferences.MapPaintPreference; 54 import org.openstreetmap.josm.gui.preferences.ProjectionPreference; 54 55 import org.openstreetmap.josm.gui.preferences.TaggingPresetPreference; 55 56 import org.openstreetmap.josm.gui.preferences.ToolbarPreferences; … … 282 283 }; 283 284 284 static public void setProjection(String name)285 {286 Bounds b = (map != null && map.mapView != null) ? map.mapView.getRealBounds() : null;287 Projection oldProj = Main.proj;288 try {289 Main.proj = (Projection)Class.forName(name).newInstance();290 } catch (final Exception e) {291 JOptionPane.showMessageDialog(292 Main.parent,293 tr("The projection {0} could not be activated. Using Mercator", name),294 tr("Error"),295 JOptionPane.ERROR_MESSAGE296 );297 Main.proj = new Mercator();298 }299 if(!Main.proj.equals(oldProj))300 {301 if(b != null) {302 map.mapView.zoomTo(b);303 /* TODO - remove layers with fixed projection */304 }305 }306 }307 308 285 /** 309 286 * Should be called before the main constructor to setup some parameter stuff … … 311 288 */ 312 289 public static void preConstructorInit(Map<String, Collection<String>> args) { 313 setProjection(Main.pref.get("projection", Mercator.class.getName()));290 ProjectionPreference.setProjection(); 314 291 315 292 try { -
trunk/src/org/openstreetmap/josm/data/projection/UTM.java
r2114 r2272 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.awt.GridBagLayout; 7 import java.util.Collection; 8 import java.util.Collections; 9 10 import javax.swing.JComboBox; 11 import javax.swing.JLabel; 12 import javax.swing.JPanel; 13 14 import org.openstreetmap.josm.Main; 6 15 import org.openstreetmap.josm.data.Bounds; 7 16 import org.openstreetmap.josm.data.coor.EastNorth; 8 17 import org.openstreetmap.josm.data.coor.LatLon; 18 import org.openstreetmap.josm.tools.GBC; 9 19 10 20 /** … … 14 24 * code based on JavaScript from Chuck Taylor 15 25 */ 16 public class UTM implements Projection { 26 public class UTM implements Projection, ProjectionSubPrefs { 27 28 private int zone = 33; 17 29 18 30 final private double UTMScaleFactor = 0.9996; … … 336 348 337 349 @Override public String toString() { 338 return tr("UTM Zone {0}", getzone()); 339 } 340 341 /* TODO - support all UTM's not only zone 33 */ 350 return tr("UTM"); 351 } 352 342 353 public int getzone() 343 354 { 344 return 33;355 return zone; 345 356 } 346 357 347 358 public String toCode() { 348 return "EPSG: 325833";359 return "EPSG:"+ (325800 + getzone()); 349 360 } 350 361 351 362 public String getCacheDirectoryName() { 352 return "epsg 325833";363 return "epsg"+ (325800 + getzone()); 353 364 } 354 365 … … 364 375 return 10; 365 376 } 377 378 private JPanel prefpanel = null; 379 private JComboBox prefcb = null; 380 public JPanel getPreferencePanel() { 381 if(prefpanel != null) 382 return prefpanel; 383 384 prefcb = new JComboBox(); 385 for(int i = 1; i <= 60; i++) { 386 prefcb.addItem(i); 387 } 388 389 prefcb.setSelectedIndex(zone - 1); 390 prefpanel = new JPanel(new GridBagLayout()); 391 prefpanel.add(new JLabel(tr("UTM Zone")), GBC.std().insets(5,5,0,5)); 392 prefpanel.add(GBC.glue(1, 0), GBC.std().fill(GBC.HORIZONTAL)); 393 prefpanel.add(prefcb, GBC.eop().fill(GBC.HORIZONTAL)); 394 prefpanel.add(GBC.glue(1, 1), GBC.eol().fill(GBC.BOTH)); 395 return prefpanel; 396 } 397 398 public Collection<String> getPreferences() { 399 if(prefcb == null) 400 return null; 401 int zone = prefcb.getSelectedIndex() + 1; 402 return Collections.singleton(Integer.toString(zone)); 403 } 404 405 public void destroyCachedPanel() { 406 prefpanel = null; 407 prefcb = null; 408 } 409 410 public void setPreferences(Collection<String> args) 411 { 412 /* TODO: parse args instead of fixed value */ 413 zone = 33; 414 } 415 416 public Collection<String> getPreferencesFromCode(String code) 417 { 418 /* TODO: implement */ 419 return null; 420 } 366 421 } -
trunk/src/org/openstreetmap/josm/gui/preferences/ProjectionPreference.java
r2017 r2272 5 5 6 6 import java.awt.GridBagLayout; 7 import java.awt.event.ActionEvent; 8 import java.awt.event.ActionListener; 9 10 import java.util.Collection; 7 11 8 12 import javax.swing.BorderFactory; 9 import javax.swing.Box;10 13 import javax.swing.JComboBox; 11 14 import javax.swing.JLabel; 15 import javax.swing.JOptionPane; 12 16 import javax.swing.JPanel; 13 17 import javax.swing.JScrollPane; 14 18 15 19 import org.openstreetmap.josm.Main; 20 import org.openstreetmap.josm.data.Bounds; 16 21 import org.openstreetmap.josm.data.coor.CoordinateFormat; 17 22 import org.openstreetmap.josm.data.projection.Mercator; 18 23 import org.openstreetmap.josm.data.projection.Projection; 24 import org.openstreetmap.josm.data.projection.ProjectionSubPrefs; 19 25 import org.openstreetmap.josm.tools.GBC; 20 26 … … 31 37 */ 32 38 private JComboBox projectionCombo = new JComboBox(Projection.allProjections); 39 40 /** 41 * Combobox with all coordinate display possibilities 42 */ 33 43 private JComboBox coordinatesCombo = new JComboBox(CoordinateFormat.values()); 34 44 45 /** 46 * This variable holds the JPanel with the projection's preferences. If the 47 * selected projection does not implement this, it will be set to an empty 48 * Panel. 49 */ 50 private JPanel projSubPrefPanel; 51 52 private JLabel projectionCode = new JLabel(); 53 private JLabel bounds = new JLabel(); 54 55 /** 56 * This is the panel holding all projection preferences 57 */ 58 private JPanel projPanel = new JPanel(); 59 60 /** 61 * The GridBagConstraints for the Panel containing the ProjectionSubPrefs. 62 * This is required twice in the code, creating it here keeps both occurrences 63 * in sync 64 */ 65 static private GBC projSubPrefPanelGBC = GBC.eol().fill(GBC.BOTH).insets(20,5,5,5); 66 35 67 public void addGui(PreferenceDialog gui) { 36 37 for (int i = 0; i < projectionCombo.getItemCount(); ++i) { 38 if (projectionCombo.getItemAt(i).getClass().getName().equals(Main.pref.get("projection", Mercator.class.getName()))) { 39 projectionCombo.setSelectedIndex(i); 40 break; 41 } 42 } 68 clearSubProjPrefs(); 69 setupProjectionCombo(); 43 70 44 71 for (int i = 0; i < coordinatesCombo.getItemCount(); ++i) { … … 49 76 } 50 77 51 JPanel projPanel = new JPanel();52 78 projPanel.setBorder(BorderFactory.createEmptyBorder( 0, 0, 0, 0 )); 53 79 projPanel.setLayout(new GridBagLayout()); … … 58 84 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL)); 59 85 projPanel.add(projectionCombo, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5)); 60 projPanel.add(Box.createVerticalGlue(), GBC.eol().fill(GBC.BOTH)); 86 projPanel.add(new JLabel(tr("Projection code")), GBC.std().insets(25,5,0,5)); 87 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL)); 88 projPanel.add(projectionCode, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5)); 89 projPanel.add(new JLabel(tr("Bounds")), GBC.std().insets(25,5,0,5)); 90 projPanel.add(GBC.glue(5,0), GBC.std().fill(GBC.HORIZONTAL)); 91 projPanel.add(bounds, GBC.eop().fill(GBC.HORIZONTAL).insets(0,5,5,5)); 92 projPanel.add(projSubPrefPanel, projSubPrefPanelGBC); 61 93 JScrollPane scrollpane = new JScrollPane(projPanel); 62 94 gui.mapcontent.addTab(tr("Map Projection"), scrollpane); 95 96 projectionCode.setText(Main.proj.toCode()); 97 Bounds b = Main.proj.getWorldBoundsLatLon(); 98 CoordinateFormat cf = CoordinateFormat.getDefaultFormat(); 99 bounds.setText(b.min.latToString(cf)+"; "+b.min.lonToString(cf)+" : "+b.max.latToString(cf)+"; "+b.max.lonToString(cf)); 100 /* TODO: Fix bugs, refresh code line and world bounds, fix design (e.g. add border around sub-prefs-stuff */ 63 101 } 64 102 65 103 public boolean ok() { 66 String projname = projectionCombo.getSelectedItem().getClass().getName(); 104 Projection proj = (Projection) projectionCombo.getSelectedItem(); 105 106 String projname = proj.getClass().getName(); 107 Collection<String> prefs = null; 108 if(projHasPrefs(proj)) 109 prefs = ((ProjectionSubPrefs) proj).getPreferences(); 110 67 111 if(Main.pref.put("projection", projname)) { 68 Main.setProjection(projname); 69 } 112 setProjection(projname, prefs); 113 } 114 70 115 if(Main.pref.put("coordinates", 71 116 ((CoordinateFormat)coordinatesCombo.getSelectedItem()).name())) { 72 117 CoordinateFormat.setCoordinateFormat((CoordinateFormat)coordinatesCombo.getSelectedItem()); 73 118 } 119 120 // We get the change to remove these panels on closing the preferences 121 // dialog, so take it. TODO: Make this work always, even when canceling 122 // the dialog 123 clearSubProjPrefs(); 124 74 125 return false; 75 126 } 127 128 /** 129 * Finds out if the given projection implements the ProjectionPreference 130 * interface 131 * @param proj 132 * @return 133 */ 134 @SuppressWarnings("unchecked") 135 static private boolean projHasPrefs(Projection proj) { 136 Class[] ifaces = proj.getClass().getInterfaces(); 137 for(int i = 0; i < ifaces.length; i++) { 138 if(ifaces[i].getSimpleName().equals("ProjectionSubPrefs")) 139 return true; 140 } 141 return false; 142 } 143 144 static public void setProjection() 145 { 146 setProjection(Main.pref.get("projection", Mercator.class.getName()), 147 Main.pref.getCollection("projection.sub", null)); 148 } 149 150 static public void setProjection(String name, Collection<String> coll) 151 { 152 Bounds b = (Main.map != null && Main.map.mapView != null) ? Main.map.mapView.getRealBounds() : null; 153 Projection oldProj = Main.proj; 154 155 try { 156 Main.proj = (Projection)Class.forName(name).newInstance(); 157 } catch (final Exception e) { 158 JOptionPane.showMessageDialog( 159 Main.parent, 160 tr("The projection {0} could not be activated. Using Mercator", name), 161 tr("Error"), 162 JOptionPane.ERROR_MESSAGE 163 ); 164 coll = null; 165 Main.proj = new Mercator(); 166 } 167 if(!Main.proj.equals(oldProj) && b != null) 168 { 169 Main.map.mapView.zoomTo(b); 170 /* TODO - remove layers with fixed projection */ 171 } 172 Main.pref.putCollection("projection.sub", coll); 173 if(coll != null && projHasPrefs(Main.proj)) 174 ((ProjectionSubPrefs) Main.proj).setPreferences(coll); 175 } 176 177 /** 178 * Handles all the work related to update the projection-specific 179 * preferences 180 * @param proj 181 */ 182 private void selectedProjectionChanged(Projection proj) { 183 if(!projHasPrefs(proj)) { 184 projSubPrefPanel = new JPanel(); 185 } else { 186 ProjectionSubPrefs projPref = (ProjectionSubPrefs) proj; 187 projSubPrefPanel = projPref.getPreferencePanel(); 188 } 189 190 // Don't try to update if we're still starting up 191 int size = projPanel.getComponentCount(); 192 if(size < 1) 193 return; 194 195 // Replace old panel with new one 196 projPanel.remove(size - 1); 197 projPanel.add(projSubPrefPanel, projSubPrefPanelGBC); 198 projPanel.revalidate(); 199 } 200 201 /** 202 * Sets up projection combobox with default values and action listener 203 */ 204 private void setupProjectionCombo() { 205 for (int i = 0; i < projectionCombo.getItemCount(); ++i) { 206 Projection proj = (Projection)projectionCombo.getItemAt(i); 207 if (proj.getClass().getName().equals(Main.pref.get("projection", Mercator.class.getName()))) { 208 projectionCombo.setSelectedIndex(i); 209 selectedProjectionChanged(proj); 210 break; 211 } 212 } 213 214 projectionCombo.addActionListener(new ActionListener() { 215 public void actionPerformed(ActionEvent e) { 216 JComboBox cb = (JComboBox)e.getSource(); 217 Projection proj = (Projection)cb.getSelectedItem(); 218 selectedProjectionChanged(proj); 219 } 220 }); 221 } 222 223 /** 224 * Method to clean up the preference panels made by each projection. This 225 * requires them to be regenerated when the prefs dialog is opened again, 226 * but this also makes them react to changes to their preferences from the 227 * outside 228 */ 229 static private void clearSubProjPrefs() { 230 for(Projection proj : Projection.allProjections) { 231 if(projHasPrefs(proj)) { 232 ((ProjectionSubPrefs) proj).destroyCachedPanel(); 233 } 234 } 235 } 76 236 }
Note:
See TracChangeset
for help on using the changeset viewer.