Changeset 16 in josm for src/org/openstreetmap/josm/data
- Timestamp:
- 2005-10-08T17:14:54+02:00 (19 years ago)
- Location:
- src/org/openstreetmap/josm/data
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/data/Preferences.java
r15 r16 1 1 package org.openstreetmap.josm.data; 2 2 3 import java.beans.PropertyChangeEvent;4 import java.beans.PropertyChangeListener;5 3 import java.io.File; 6 4 import java.io.FileReader; 7 5 import java.io.FileWriter; 6 import java.util.Collection; 8 7 import java.util.LinkedList; 9 8 import java.util.List; … … 28 27 public class Preferences { 29 28 30 31 29 /** 32 30 * The look and feel. Classname of the look and feel class to use. 33 31 */ 34 private LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0]; 32 public LookAndFeelInfo laf = UIManager.getInstalledLookAndFeels()[0]; 33 35 34 /** 36 35 * The convertor used to translate lat/lon points to screen points. 37 36 */ 38 37 private Projection projection = new UTM(); 38 39 39 40 /** 40 41 * Whether nodes on the same place should be considered identical. 41 42 */ 42 private boolean mergeNodes = true; 43 public boolean mergeNodes = true; 44 45 43 46 47 /** 48 * List of all available Projections. 49 */ 50 public static final Projection[] allProjections = new Projection[]{ 51 new UTM(), 52 new LatitudeLongitude() 53 }; 44 54 45 55 /** 56 * Return the location of the preferences file 57 */ 58 public static String getPreferencesFile() { 59 return System.getProperty("user.home")+"/.josm-preferences"; 60 } 61 46 62 /** 47 63 * Exception thrown in case of any loading/saving error (including parse errors). … … 53 69 } 54 70 } 55 56 /**57 * List of all available Projections.58 */59 public static final Projection[] allProjections = new Projection[]{60 new UTM(),61 new LatitudeLongitude()62 };63 64 65 66 67 // listener stuff68 69 /**70 * The event listener list71 */72 private List<PropertyChangeListener> listener = new LinkedList<PropertyChangeListener>();73 /**74 * If <code>listener != null</code>, add it to the listener list.75 */76 public void addPropertyChangeListener(PropertyChangeListener listener) {77 if (listener != null)78 this.listener.add(listener);79 }80 /**81 * If <code>listener != null</code>, remove it from the listener list.82 */83 public void removePropertyChangeListener(PropertyChangeListener listener) {84 if (listener != null)85 this.listener.remove(listener);86 }87 /**88 * Fires an event that the property has changed.89 */90 private void firePropertyChanged(String propName, Object oldValue, Object newValue) {91 PropertyChangeEvent event = null;92 for (PropertyChangeListener l : listener) {93 if (event == null)94 event = new PropertyChangeEvent(this, propName, oldValue, newValue);95 l.propertyChange(event);96 }97 }98 99 100 101 /**102 * Return the location of the preferences file103 */104 public static String getPreferencesFile() {105 return System.getProperty("user.home")+"/.josm-preferences";106 }107 108 71 /** 109 72 * Load from disk. … … 120 83 for (LookAndFeelInfo lafInfo : UIManager.getInstalledLookAndFeels()) 121 84 if (lafInfo.getClassName().equals(lafClassName)) { 122 setLaf(lafInfo);85 laf = lafInfo; 123 86 break; 124 87 } 125 if ( getLaf()== null)88 if (laf == null) 126 89 throw new PreferencesException("Look and Feel not found.", null); 127 128 // setprojection90 91 // projection 129 92 Class<?> projectionClass = Class.forName(root.getChildText("projection")); 130 93 projection = allProjections[0]; // defaults to UTM … … 136 99 } 137 100 138 setMergeNodes(root.getChild("mergeNodes") != null);101 mergeNodes = root.getChild("mergeNodes") != null; 139 102 } catch (Exception e) { 140 103 if (e instanceof PreferencesException) … … 153 116 154 117 List children = root.getChildren(); 155 children.add(new Element("laf").setText( getLaf().getClassName()));118 children.add(new Element("laf").setText(laf.getClassName())); 156 119 children.add(new Element("projection").setText(getProjection().getClass().getName())); 157 if ( isMergeNodes())120 if (mergeNodes) 158 121 children.add(new Element("mergeNodes")); 159 122 … … 167 130 } 168 131 169 // getter / setter 170 132 133 // projection change listener stuff 134 135 /** 136 * This interface notifies any interested about changes in the projection 137 * @author imi 138 */ 139 public interface ProjectionChangeListener { 140 void projectionChanged(Projection oldProjection, Projection newProjection); 141 } 142 /** 143 * The list of all listeners to projection changes. 144 */ 145 private Collection<ProjectionChangeListener> listener = new LinkedList<ProjectionChangeListener>(); 146 /** 147 * Add a listener of projection changes to the list of listeners. 148 * @param listener The listerner to add. 149 */ 150 public void addProjectionChangeListener(ProjectionChangeListener listener) { 151 if (listener != null) 152 this.listener.add(listener); 153 } 154 /** 155 * Remove the listener from the list. 156 */ 157 public void removeProjectionChangeListener(ProjectionChangeListener listener) { 158 this.listener.remove(listener); 159 } 160 /** 161 * Set the projection and fire an event to all ProjectionChangeListener 162 * @param projection The new Projection. 163 */ 171 164 public void setProjection(Projection projection) { 172 165 Projection old = this.projection; 173 166 this.projection = projection; 174 firePropertyChanged("projection", old, projection); 167 if (old != projection) 168 for (ProjectionChangeListener l : listener) 169 l.projectionChanged(old, projection); 175 170 } 171 /** 172 * Get the current projection. 173 * @return The current projection set. 174 */ 176 175 public Projection getProjection() { 177 176 return projection; 178 177 } 179 public void setMergeNodes(boolean mergeNodes) {180 boolean old = this.mergeNodes;181 this.mergeNodes = mergeNodes;182 firePropertyChanged("mergeNodes", old, mergeNodes);183 }184 public boolean isMergeNodes() {185 return mergeNodes;186 }187 public void setLaf(LookAndFeelInfo laf) {188 LookAndFeelInfo old = this.laf;189 this.laf = laf;190 firePropertyChanged("laf", old, laf);191 }192 public LookAndFeelInfo getLaf() {193 return laf;194 }195 178 } -
src/org/openstreetmap/josm/data/osm/DataSet.java
r15 r16 113 113 Node first = nodes.iterator().next(); 114 114 Bounds b = new Bounds(first.coor.clone(), first.coor.clone()); 115 for (Node n: nodes)115 for (Node w : nodes) 116 116 { 117 if (Double.isNaN( n.coor.x) || Double.isNaN(n.coor.y))117 if (Double.isNaN(w.coor.x) || Double.isNaN(w.coor.y)) 118 118 return null; 119 if ( n.coor.x < b.min.x)120 b.min.x = n.coor.x;121 if ( n.coor.y < b.min.y)122 b.min.y = n.coor.y;123 if ( n.coor.x > b.max.x)124 b.max.x = n.coor.x;125 if ( n.coor.y > b.max.y)126 b.max.y = n.coor.y;119 if (w.coor.x < b.min.x) 120 b.min.x = w.coor.x; 121 if (w.coor.y < b.min.y) 122 b.min.y = w.coor.y; 123 if (w.coor.x > b.max.x) 124 b.max.x = w.coor.x; 125 if (w.coor.y > b.max.y) 126 b.max.y = w.coor.y; 127 127 } 128 128 return b; -
src/org/openstreetmap/josm/data/projection/LatitudeLongitude.java
r1 r16 1 1 package org.openstreetmap.josm.data.projection; 2 2 3 import javax.swing.J Panel;3 import javax.swing.JComponent; 4 4 5 5 import org.openstreetmap.josm.data.GeoPoint; … … 30 30 31 31 @Override 32 public String description() {33 return "Use lat/lon values directly.";32 public JComponent getConfigurationPanel() { 33 return null; 34 34 } 35 35 36 36 @Override 37 public JPanel getConfigurationPanel() { 38 return null; 37 public void commitConfigurationPanel() { 39 38 } 40 39 } -
src/org/openstreetmap/josm/data/projection/Projection.java
r15 r16 47 47 abstract public String toString(); 48 48 49 /**50 * Describe the projection converter. Give examples, where it is best to use51 * and maybe a reference link to more information about the converter.52 */53 abstract public String description();54 55 56 57 49 // miscellous functions 58 50 59 51 /** 60 52 * If the projection supports any configuration, this function return 61 * the configuration panel. If no configuration needed, return null. 53 * the configuration panel. If no configuration needed, 54 * return <code>null</code>. 62 55 * 63 * The items on the configuration panel should update the configuration 64 * directly, so the changes are instantly visible on screen. 56 * The items on the configuration panel should not update the configuration 57 * directly, but remember changed settings so a call to commitConfigurationPanel 58 * can set them. 59 * 60 * This function also rolls back all changes to the configuration panel interna 61 * components. 65 62 */ 66 63 abstract public JComponent getConfigurationPanel(); 64 /** 65 * Commits any changes from components created by addToConfigurationPanel. 66 * The projection should now obtain the new settings. If any setting has 67 * changed, the implementation have to call to fireStateChanged to inform 68 * the listeners. 69 */ 70 abstract public void commitConfigurationPanel(); 67 71 68 72 /** -
src/org/openstreetmap/josm/data/projection/UTM.java
r15 r16 1 1 package org.openstreetmap.josm.data.projection; 2 2 3 import java.awt.Font; 4 import java.awt.GridBagLayout; 3 5 import java.awt.event.ActionEvent; 4 6 import java.awt.event.ActionListener; 5 7 6 import javax.swing.BorderFactory; 7 import javax.swing.Box; 8 import javax.swing.JButton; 8 9 import javax.swing.JComboBox; 9 10 import javax.swing.JComponent; 10 11 import javax.swing.JLabel; 12 import javax.swing.JOptionPane; 13 import javax.swing.JPanel; 11 14 import javax.swing.JSpinner; 12 15 import javax.swing.SpinnerNumberModel; 13 import javax.swing.border.Border;14 import javax.swing.event.ChangeEvent;15 import javax.swing.event.ChangeListener;16 16 17 17 import org.openstreetmap.josm.data.Bounds; 18 18 import org.openstreetmap.josm.data.GeoPoint; 19 19 import org.openstreetmap.josm.data.osm.DataSet; 20 import org.openstreetmap.josm.gui.GBC; 21 import org.openstreetmap.josm.gui.Main; 20 22 21 23 /** … … 93 95 protected Ellipsoid ellipsoid = allEllipsoids[allEllipsoids.length-1]; 94 96 97 /** 98 * Combobox with all ellipsoids for the configuration panel 99 */ 100 private JComboBox ellipsoidCombo = new JComboBox(allEllipsoids); 101 /** 102 * Spinner with all possible zones for the configuration panel 103 */ 104 private JSpinner zoneSpinner = new JSpinner(new SpinnerNumberModel(1,1,60,1)); 105 /** 106 * Hemisphere combo for the configuration panel 107 */ 108 private JComboBox hemisphereCombo = new JComboBox(Hemisphere.values()); 109 95 110 96 111 @Override … … 168 183 } 169 184 170 @Override 171 public String description() { 172 return "UTM projection ported from Ben Gimpert's ruby port.\n" + 173 "http://www.openstreetmap.org/websvn/filedetails.php?repname=" + 174 "OpenStreetMap&path=%2Futils%2Ftiger_import%2Ftiger%2Futm.rb"; 175 } 176 185 /** 186 * Helper class for the zone detection 187 * @author imi 188 */ 189 private class ZoneData { 190 int zone = 0; 191 Hemisphere hemisphere = Hemisphere.north; 192 } 193 /** 194 * Try to autodetect the zone and hemisphere from the dataset. 195 * @param dataSet The dataset to extrakt zone information from. 196 * @return The zone data extrakted from the dataset. 197 */ 198 private ZoneData autoDetect(DataSet dataSet) { 199 ZoneData zd = new ZoneData(); 200 201 Bounds b = dataSet.getBoundsLatLon(); 202 if (b == null) 203 return zd; 204 GeoPoint center = b.centerLatLon(); 205 double lat = center.lat; 206 double lon = center.lon; 207 // make sure the longitude is between -180.00 .. 179.9 208 double long_temp = (lon + 180) - (Math.floor((lon + 180) / 360) * 360) - 180; 209 210 zd.zone = (int)((long_temp + 180) / 6) + 1; 211 if ((lat >= 56.0) && (lat < 64.0) && (long_temp >= 3.0) && (long_temp < 12.0)) 212 zd.zone = 32; 213 // special zones for Svalbard 214 if ((lat >= 72.0) && (lat < 84.0)) 215 { 216 if ((long_temp >= 0.0) && (long_temp < 9.0)) 217 zd.zone = 31; 218 else if ((long_temp >= 9.0) && (long_temp < 21.0)) 219 zd.zone = 33; 220 else if ((long_temp >= 21.0) && (long_temp < 33.0)) 221 zd.zone = 35; 222 else if ((long_temp >= 33.0) && (long_temp < 42.0)) 223 zd.zone = 37; 224 } 225 zd.hemisphere = lat > 0 ? Hemisphere.north : Hemisphere.south; 226 return zd; 227 } 228 177 229 /** 178 230 * If the zone is not already set, calculate it from this dataset. … … 184 236 public void init(DataSet dataSet) { 185 237 if (zone == 0) { 186 Bounds b = dataSet.getBoundsLatLon(); 187 if (b == null) 188 return; 189 GeoPoint center = b.centerLatLon(); 190 double lat = center.lat; 191 double lon = center.lon; 192 // make sure the longitude is between -180.00 .. 179.9 193 double long_temp = (lon + 180) - (Math.floor((lon + 180) / 360) * 360) - 180; 194 195 zone = (int)((long_temp + 180) / 6) + 1; 196 if ((lat >= 56.0) && (lat < 64.0) && (long_temp >= 3.0) && (long_temp < 12.0)) 197 zone = 32; 198 // special zones for Svalbard 199 if ((lat >= 72.0) && (lat < 84.0)) 200 { 201 if ((long_temp >= 0.0) && (long_temp < 9.0)) 202 zone = 31; 203 else if ((long_temp >= 9.0) && (long_temp < 21.0)) 204 zone = 33; 205 else if ((long_temp >= 21.0) && (long_temp < 33.0)) 206 zone = 35; 207 else if ((long_temp >= 33.0) && (long_temp < 42.0)) 208 zone = 37; 209 } 210 hemisphere = lat > 0 ? Hemisphere.north : Hemisphere.south; 238 ZoneData zd = autoDetect(dataSet); 239 zone = zd.zone; 240 hemisphere = zd.hemisphere; 211 241 } 212 242 } … … 214 244 @Override 215 245 public JComponent getConfigurationPanel() { 216 Border border = BorderFactory.createEmptyBorder(5,0,0,0);217 Box panel = Box.createVerticalBox();218 246 JPanel panel = new JPanel(new GridBagLayout()); 247 GBC gbc = GBC.std().insets(0,0,5,0); 248 219 249 // ellipsoid 220 Box ellipsoidPanel = Box.createHorizontalBox(); 221 ellipsoidPanel.add(new JLabel("Ellipsoid")); 222 final JComboBox ellipsoidCombo = new JComboBox(allEllipsoids); 223 ellipsoidPanel.add(ellipsoidCombo); 250 panel.add(new JLabel("Ellipsoid"), gbc); 251 panel.add(ellipsoidCombo, GBC.eol()); 224 252 ellipsoidCombo.setSelectedItem(ellipsoid); 225 ellipsoidCombo.addActionListener(new ActionListener(){ 253 254 // zone 255 panel.add(new JLabel("Zone"), gbc); 256 panel.add(zoneSpinner, GBC.eol().insets(0,5,0,5)); 257 if (zone != 0) 258 zoneSpinner.setValue(zone); 259 260 // hemisphere 261 panel.add(new JLabel("Hemisphere"), gbc); 262 panel.add(hemisphereCombo, GBC.eop()); 263 hemisphereCombo.setSelectedItem(hemisphere); 264 265 // Autodetect 266 JButton autoDetect = new JButton("Detect"); 267 autoDetect.addActionListener(new ActionListener(){ 226 268 public void actionPerformed(ActionEvent e) { 227 ellipsoid = (Ellipsoid)ellipsoidCombo.getSelectedItem(); 228 fireStateChanged(); 269 if (Main.main.getMapFrame() != null) { 270 DataSet ds = Main.main.getMapFrame().mapView.dataSet; 271 ZoneData zd = autoDetect(ds); 272 if (zd.zone == 0) 273 JOptionPane.showMessageDialog(Main.main, "Autodetection failed. Maybe the data set contain too few information."); 274 else { 275 zoneSpinner.setValue(zd.zone); 276 hemisphereCombo.setSelectedItem(zd.hemisphere); 277 } 278 } else { 279 JOptionPane.showMessageDialog(Main.main, "No data loaded. Please open a data set first."); 280 } 229 281 } 230 282 }); 231 ellipsoidPanel.setBorder(border); 232 panel.add(ellipsoidPanel); 233 234 // zone 235 Box zonePanel = Box.createHorizontalBox(); 236 zonePanel.add(new JLabel("Zone")); 237 final JSpinner zoneSpinner = new JSpinner(new SpinnerNumberModel(zone,1,60,1)); 238 zonePanel.add(zoneSpinner); 239 zoneSpinner.addChangeListener(new ChangeListener(){ 240 public void stateChanged(ChangeEvent e) { 241 zone = (Integer)zoneSpinner.getValue(); 242 fireStateChanged(); 243 } 244 }); 245 zonePanel.setBorder(border); 246 panel.add(zonePanel); 247 248 // hemisphere 249 Box hemispherePanel = Box.createHorizontalBox(); 250 hemispherePanel.add(new JLabel("Hemisphere")); 251 final JComboBox hemisphereCombo = new JComboBox(Hemisphere.values()); 252 hemispherePanel.add(hemisphereCombo); 253 hemisphereCombo.setSelectedItem(hemisphere); 254 hemisphereCombo.addActionListener(new ActionListener(){ 255 public void actionPerformed(ActionEvent e) { 256 hemisphere = (Hemisphere)hemisphereCombo.getSelectedItem(); 257 fireStateChanged(); 258 } 259 }); 260 hemispherePanel.setBorder(border); 261 panel.add(hemispherePanel); 262 283 JLabel descLabel = new JLabel("Autodetect parameter based on loaded data"); 284 descLabel.setFont(descLabel.getFont().deriveFont(Font.ITALIC)); 285 panel.add(descLabel, GBC.eol().fill(GBC.HORIZONTAL)); 286 panel.add(autoDetect, GBC.eol().anchor(GBC.CENTER)); 287 263 288 return panel; 264 289 } 290 291 @Override 292 public void commitConfigurationPanel() { 293 ellipsoid = (Ellipsoid)ellipsoidCombo.getSelectedItem(); 294 zone = (Integer)zoneSpinner.getValue(); 295 hemisphere = (Hemisphere)hemisphereCombo.getSelectedItem(); 296 fireStateChanged(); 297 } 265 298 }
Note:
See TracChangeset
for help on using the changeset viewer.