Ignore:
Timestamp:
07.03.2010 18:48:56 (2 years ago)
Author:
Gubaer
Message:

Improved widget for selecting a bounding box based on a grid of OSM tiles (see Haiti Lessons Learned?).
Also moving the various widgets for selecting a bounding box into a new package org.openstreetmap.josm.gui.bbox. Going to need them shortly in the Changeset Query Dialog and in an upcoming XAPI plugin.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/download/TileSelection.java

    r3083 r3094  
    44import static org.openstreetmap.josm.tools.I18n.tr; 
    55 
    6 import java.awt.Dimension; 
    7 import java.awt.GridBagLayout; 
    8 import java.awt.event.FocusAdapter; 
    9 import java.awt.event.FocusEvent; 
    10 import java.awt.event.FocusListener; 
    11  
    12 import javax.swing.JLabel; 
    13 import javax.swing.JPanel; 
    14 import javax.swing.JSpinner; 
    15 import javax.swing.JTextField; 
    16 import javax.swing.SpinnerNumberModel; 
     6import java.beans.PropertyChangeEvent; 
     7import java.beans.PropertyChangeListener; 
    178 
    189import org.openstreetmap.josm.data.Bounds; 
    19 import org.openstreetmap.josm.data.coor.LatLon; 
    20 import org.openstreetmap.josm.tools.GBC; 
     10import org.openstreetmap.josm.gui.bbox.BBoxChooser; 
     11import org.openstreetmap.josm.gui.bbox.TileSelectionBBoxChooser; 
    2112/** 
    2213 * Tile selector. 
     
    2718 * 
    2819 */ 
    29 public class TileSelection implements DownloadSelection { 
     20public class TileSelection implements DownloadSelection, PropertyChangeListener { 
    3021 
    31     private JTextField tileX0 = new JTextField(7); 
    32     private JTextField tileY0 = new JTextField(7); 
    33     private JTextField tileX1 = new JTextField(7); 
    34     private JTextField tileY1 = new JTextField(7); 
    35     private JSpinner tileZ = new JSpinner(new SpinnerNumberModel(12, 10, 18, 1)); 
     22    private TileSelectionBBoxChooser chooser; 
     23    private DownloadDialog parent; 
     24 
     25    protected void build() { 
     26        chooser = new TileSelectionBBoxChooser(); 
     27        chooser.addPropertyChangeListener(this); 
     28    } 
     29 
     30    public TileSelection() { 
     31        build(); 
     32    } 
    3633 
    3734    public void addGui(final DownloadDialog gui) { 
    38  
    39         JPanel smpanel = new JPanel(new GridBagLayout()); 
    40         smpanel.add(new JLabel(tr("zoom level")), GBC.std().insets(0,0,10,0)); 
    41         smpanel.add(new JLabel(tr("x from")), GBC.std().insets(10,0,5,0)); 
    42         smpanel.add(tileX0, GBC.std()); 
    43         smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 
    44         smpanel.add(tileX1, GBC.eol()); 
    45         smpanel.add(tileZ, GBC.std().insets(0,0,10,0)); 
    46         smpanel.add(new JLabel(tr("y from")), GBC.std().insets(10,0,5,0)); 
    47         smpanel.add(tileY0, GBC.std()); 
    48         smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 
    49         smpanel.add(tileY1, GBC.eol()); 
    50  
    51         final FocusListener dialogUpdater = new FocusAdapter() { 
    52             @Override public void focusLost(FocusEvent e) { 
    53                 try { 
    54                     int zoomlvl = (Integer) tileZ.getValue(); 
    55                     int fromx = Integer.parseInt(tileX0.getText()); 
    56                     int tox = fromx; 
    57                     if (tileX1.getText().length()>0) { 
    58                         tox = Integer.parseInt(tileX1.getText()); 
    59                     } 
    60                     if (tox<fromx) { int i = fromx; fromx=tox; tox=i; } 
    61  
    62                     int fromy = Integer.parseInt(tileY0.getText()); 
    63                     int toy = fromy; 
    64                     if (tileY1.getText().length()>0) { 
    65                         toy = Integer.parseInt(tileY1.getText()); 
    66                     } 
    67                     if (toy<fromy) { int i = fromy; fromy=toy; toy=i; } 
    68  
    69                     Bounds b = new Bounds( 
    70                             new LatLon(tileYToLat(zoomlvl, toy + 1), tileXToLon(zoomlvl, fromx)), 
    71                             new LatLon(tileYToLat(zoomlvl, fromy), tileXToLon(zoomlvl, tox + 1)) 
    72                             ); 
    73                     gui.boundingBoxChanged(b, TileSelection.this); 
    74                     //repaint(); 
    75                 } catch (NumberFormatException x) { 
    76                     // ignore 
    77                 } 
    78             } 
    79         }; 
    80  
    81         for (JTextField f : new JTextField[] { tileX0, tileX1, tileY0, tileY1 }) { 
    82             f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height)); 
    83             f.addFocusListener(dialogUpdater); 
    84         } 
    85  
    86         gui.addDownloadAreaSelector(smpanel, tr("Tile Numbers")); 
     35        gui.addDownloadAreaSelector(chooser, tr("Tile Numbers")); 
     36        parent = gui; 
    8737    } 
    8838 
    8939    public void setDownloadArea(Bounds area) { 
    90         if (area == null) 
    91             return; 
    92         int z = ((Integer) tileZ.getValue()).intValue(); 
    93         tileX0.setText(Integer.toString(lonToTileX(z, area.getMin().lon()))); 
    94         tileX1.setText(Integer.toString(lonToTileX(z, area.getMax().lon()-.00001))); 
    95         tileY0.setText(Integer.toString(latToTileY(z, area.getMax().lat()-.00001))); 
    96         tileY1.setText(Integer.toString(latToTileY(z, area.getMin().lat()))); 
     40        chooser.setBoundingBox(area); 
    9741    } 
    9842 
    99     public static int latToTileY(int zoom, double lat) { 
    100         if ((zoom < 3) || (zoom > 18)) return -1; 
    101         double l = lat / 180 * Math.PI; 
    102         double pf = Math.log(Math.tan(l) + (1/Math.cos(l))); 
    103         return (int) ((1<<(zoom-1)) * (Math.PI - pf) / Math.PI); 
    104     } 
    105  
    106     public static int lonToTileX(int zoom, double lon) { 
    107         if ((zoom < 3) || (zoom > 18)) return -1; 
    108         return (int) ((1<<(zoom-3)) * (lon + 180.0) / 45.0); 
    109     } 
    110  
    111     public static double tileYToLat(int zoom, int y) { 
    112         if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 
    113         return Math.atan(Math.sinh(Math.PI - (Math.PI*y / (1<<(zoom-1))))) * 180 / Math.PI; 
    114     } 
    115  
    116     public static double tileXToLon(int zoom, int x) { 
    117         if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 
    118         return x * 45.0 / (1<<(zoom-3)) - 180.0; 
    119  
     43    @Override 
     44    public void propertyChange(PropertyChangeEvent evt) { 
     45        if (evt.getPropertyName().equals(BBoxChooser.BBOX_PROP)) { 
     46            Bounds bbox = (Bounds)evt.getNewValue(); 
     47            parent.boundingBoxChanged(bbox, this); 
     48        } 
    12049    } 
    12150} 
Note: See TracChangeset for help on using the changeset viewer.