Changeset 1169 in josm for trunk/src/org/openstreetmap/josm/gui/download
- Timestamp:
- 2008-12-23T15:07:05+01:00 (15 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/gui/download
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/gui/download/BookmarkSelection.java
r733 r1169 27 27 /** 28 28 * Bookmark selector. 29 * 29 * 30 30 * Provides selection, creation and deletion of bookmarks. 31 31 * Extracted from old DownloadAction. 32 * 32 * 33 33 * @author Frederik Ramm <frederik@remote.org> 34 34 * … … 36 36 public class BookmarkSelection implements DownloadSelection { 37 37 38 private Preferences.Bookmark tempBookmark = null; 39 private BookmarkList bookmarks; 40 41 public void addGui(final DownloadDialog gui) { 42 43 JPanel dlg = new JPanel(new GridBagLayout()); 44 gui.tabpane.addTab(tr("Bookmarks"), dlg); 38 private Preferences.Bookmark tempBookmark = null; 39 private BookmarkList bookmarks; 45 40 46 bookmarks = new BookmarkList(); 47 48 /* add a handler for "double click" mouse events */ 49 MouseListener mouseListener = new MouseAdapter() { 50 @Override public void mouseClicked(MouseEvent e) { 51 if (e.getClickCount() == 2) { 52 //int index = bookmarks.locationToIndex(e.getPoint()); 53 gui.closeDownloadDialog(true); 54 } 55 } 56 }; 57 bookmarks.addMouseListener(mouseListener); 58 59 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 60 public void valueChanged(ListSelectionEvent e) { 61 Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue(); 62 if (b != null) { 63 gui.minlat = b.latlon[0]; 64 gui.minlon = b.latlon[1]; 65 gui.maxlat = b.latlon[2]; 66 gui.maxlon = b.latlon[3]; 67 gui.boundingBoxChanged(BookmarkSelection.this); 68 } 69 } 70 }); 71 //wc.addListMarker(bookmarks); 72 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill()); 41 public void addGui(final DownloadDialog gui) { 73 42 74 JPanel buttons = new JPanel(new GridLayout(1,2)); 75 JButton add = new JButton(tr("Add")); 76 add.addActionListener(new ActionListener(){ 77 public void actionPerformed(ActionEvent e) { 78 79 if (tempBookmark == null) { 80 JOptionPane.showMessageDialog(Main.parent, tr("Please enter the desired coordinates first.")); 81 return; 82 } 83 tempBookmark.name = JOptionPane.showInputDialog(Main.parent,tr("Please enter a name for the location.")); 84 if (tempBookmark.name != null && !tempBookmark.name.equals("")) { 85 ((DefaultListModel)bookmarks.getModel()).addElement(tempBookmark); 86 bookmarks.save(); 87 } 88 } 89 }); 90 buttons.add(add); 91 JButton remove = new JButton(tr("Remove")); 92 remove.addActionListener(new ActionListener(){ 93 public void actionPerformed(ActionEvent e) { 94 Object sel = bookmarks.getSelectedValue(); 95 if (sel == null) { 96 JOptionPane.showMessageDialog(Main.parent,tr("Select a bookmark first.")); 97 return; 98 } 99 ((DefaultListModel)bookmarks.getModel()).removeElement(sel); 100 bookmarks.save(); 101 } 102 }); 103 buttons.add(remove); 104 dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL)); 105 } 43 JPanel dlg = new JPanel(new GridBagLayout()); 44 gui.tabpane.addTab(tr("Bookmarks"), dlg); 106 45 107 public void boundingBoxChanged(DownloadDialog gui) { 108 tempBookmark = new Preferences.Bookmark(); 109 tempBookmark.latlon[0] = gui.minlat; 110 tempBookmark.latlon[1] = gui.minlon; 111 tempBookmark.latlon[2] = gui.maxlat; 112 tempBookmark.latlon[3] = gui.maxlon; 113 bookmarks.clearSelection(); 114 } 46 bookmarks = new BookmarkList(); 47 48 /* add a handler for "double click" mouse events */ 49 MouseListener mouseListener = new MouseAdapter() { 50 @Override public void mouseClicked(MouseEvent e) { 51 if (e.getClickCount() == 2) { 52 //int index = bookmarks.locationToIndex(e.getPoint()); 53 gui.closeDownloadDialog(true); 54 } 55 } 56 }; 57 bookmarks.addMouseListener(mouseListener); 58 59 bookmarks.getSelectionModel().addListSelectionListener(new ListSelectionListener() { 60 public void valueChanged(ListSelectionEvent e) { 61 Preferences.Bookmark b = (Preferences.Bookmark)bookmarks.getSelectedValue(); 62 if (b != null) { 63 gui.minlat = b.latlon[0]; 64 gui.minlon = b.latlon[1]; 65 gui.maxlat = b.latlon[2]; 66 gui.maxlon = b.latlon[3]; 67 gui.boundingBoxChanged(BookmarkSelection.this); 68 } 69 } 70 }); 71 //wc.addListMarker(bookmarks); 72 dlg.add(new JScrollPane(bookmarks), GBC.eol().fill()); 73 74 JPanel buttons = new JPanel(new GridLayout(1,2)); 75 JButton add = new JButton(tr("Add")); 76 add.addActionListener(new ActionListener(){ 77 public void actionPerformed(ActionEvent e) { 78 79 if (tempBookmark == null) { 80 JOptionPane.showMessageDialog(Main.parent, tr("Please enter the desired coordinates first.")); 81 return; 82 } 83 tempBookmark.name = JOptionPane.showInputDialog(Main.parent,tr("Please enter a name for the location.")); 84 if (tempBookmark.name != null && !tempBookmark.name.equals("")) { 85 ((DefaultListModel)bookmarks.getModel()).addElement(tempBookmark); 86 bookmarks.save(); 87 } 88 } 89 }); 90 buttons.add(add); 91 JButton remove = new JButton(tr("Remove")); 92 remove.addActionListener(new ActionListener(){ 93 public void actionPerformed(ActionEvent e) { 94 Object sel = bookmarks.getSelectedValue(); 95 if (sel == null) { 96 JOptionPane.showMessageDialog(Main.parent,tr("Select a bookmark first.")); 97 return; 98 } 99 ((DefaultListModel)bookmarks.getModel()).removeElement(sel); 100 bookmarks.save(); 101 } 102 }); 103 buttons.add(remove); 104 dlg.add(buttons, GBC.eop().fill(GBC.HORIZONTAL)); 105 } 106 107 public void boundingBoxChanged(DownloadDialog gui) { 108 tempBookmark = new Preferences.Bookmark(); 109 tempBookmark.latlon[0] = gui.minlat; 110 tempBookmark.latlon[1] = gui.minlon; 111 tempBookmark.latlon[2] = gui.maxlat; 112 tempBookmark.latlon[3] = gui.maxlon; 113 bookmarks.clearSelection(); 114 } 115 115 116 116 -
trunk/src/org/openstreetmap/josm/gui/download/BoundingBoxSelection.java
r1064 r1169 26 26 /** 27 27 * Bounding box selector. 28 * 28 * 29 29 * Provides max/min lat/lon input fields as well as the "URL from www.openstreetmap.org" text field. 30 * 30 * 31 31 * @author Frederik Ramm <frederik@remote.org> 32 32 * … … 34 34 public class BoundingBoxSelection implements DownloadSelection { 35 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 gui.minlat = minlat; gui.minlon = minlon; 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 } 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 for (JTextField f : latlon) 150 151 152 153 private void updateUrl(DownloadDialog gui) { 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 36 private JTextField[] latlon = new JTextField[] { 37 new JTextField(11), 38 new JTextField(11), 39 new JTextField(11), 40 new JTextField(11) }; 41 final JTextArea osmUrl = new JTextArea(); 42 final JTextArea showUrl = new JTextArea(); 43 String noteUrl = tr("You can paste an URL here to download the area."); 44 45 public void addGui(final DownloadDialog gui) { 46 47 JPanel dlg = new JPanel(new GridBagLayout()); 48 osmUrl.setText(noteUrl); 49 50 final FocusListener dialogUpdater = new FocusAdapter() { 51 @Override public void focusLost(FocusEvent e) { 52 SwingUtilities.invokeLater(new Runnable() { 53 public void run() { 54 try { 55 double minlat = Double.parseDouble(latlon[0].getText()); 56 double minlon = Double.parseDouble(latlon[1].getText()); 57 double maxlat = Double.parseDouble(latlon[2].getText()); 58 double maxlon = Double.parseDouble(latlon[3].getText()); 59 if (minlat != gui.minlat || minlon != gui.minlon || maxlat != gui.maxlat || maxlon != gui.maxlon) { 60 gui.minlat = minlat; gui.minlon = minlon; 61 gui.maxlat = maxlat; gui.maxlon = maxlon; 62 gui.boundingBoxChanged(BoundingBoxSelection.this); 63 } 64 } catch (NumberFormatException x) { 65 // ignore 66 } 67 updateUrl(gui); 68 } 69 }); 70 } 71 }; 72 73 for (JTextField f : latlon) { 74 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height)); 75 f.addFocusListener(dialogUpdater); 76 } 77 class osmUrlRefresher implements DocumentListener { 78 public void changedUpdate(DocumentEvent e) { dowork(); } 79 public void insertUpdate(DocumentEvent e) { dowork(); } 80 public void removeUpdate(DocumentEvent e) { dowork(); } 81 private void dowork() { 82 Bounds b = osmurl2bounds(osmUrl.getText()); 83 if (b != null) { 84 gui.minlon = b.min.lon(); 85 gui.minlat = b.min.lat(); 86 gui.maxlon = b.max.lon(); 87 gui.maxlat = b.max.lat(); 88 gui.boundingBoxChanged(BoundingBoxSelection.this); 89 updateBboxFields(gui); 90 updateUrl(gui); 91 } 92 } 93 } 94 95 osmUrl.getDocument().addDocumentListener(new osmUrlRefresher()); 96 97 // select content on receiving focus. this seems to be the default in the 98 // windows look+feel but not for others. needs invokeLater to avoid strange 99 // side effects that will cancel out the newly made selection otherwise. 100 osmUrl.addFocusListener(new FocusAdapter() { 101 @Override public void focusGained(FocusEvent e) { 102 SwingUtilities.invokeLater(new Runnable() { 103 public void run() { 104 osmUrl.selectAll(); 105 } 106 }); 107 } 108 }); 109 osmUrl.setLineWrap(true); 110 osmUrl.setBorder(latlon[0].getBorder()); 111 112 dlg.add(new JLabel(tr("min lat")), GBC.std().insets(10,20,5,0)); 113 dlg.add(latlon[0], GBC.std().insets(0,20,0,0)); 114 dlg.add(new JLabel(tr("min lon")), GBC.std().insets(10,20,5,0)); 115 dlg.add(latlon[1], GBC.eol().insets(0,20,0,0)); 116 dlg.add(new JLabel(tr("max lat")), GBC.std().insets(10,0,5,0)); 117 dlg.add(latlon[2], GBC.std()); 118 dlg.add(new JLabel(tr("max lon")), GBC.std().insets(10,0,5,0)); 119 dlg.add(latlon[3], GBC.eol()); 120 121 dlg.add(new JLabel(tr("URL from www.openstreetmap.org")), GBC.eol().insets(10,20,5,0)); 122 dlg.add(osmUrl, GBC.eop().insets(10,0,5,0).fill()); 123 dlg.add(showUrl, GBC.eop().insets(10,0,5,5)); 124 showUrl.setEditable(false); 125 showUrl.setBackground(dlg.getBackground()); 126 showUrl.addFocusListener(new FocusAdapter(){ 127 @Override 128 public void focusGained(FocusEvent e) { 129 showUrl.selectAll(); 130 } 131 }); 132 133 gui.tabpane.addTab(tr("Bounding Box"), dlg); 134 } 135 136 /** 137 * Called when bounding box is changed by one of the other download dialog tabs. 138 */ 139 public void boundingBoxChanged(DownloadDialog gui) { 140 updateBboxFields(gui); 141 updateUrl(gui); 142 } 143 144 private void updateBboxFields(DownloadDialog gui) { 145 latlon[0].setText(Double.toString(gui.minlat)); 146 latlon[1].setText(Double.toString(gui.minlon)); 147 latlon[2].setText(Double.toString(gui.maxlat)); 148 latlon[3].setText(Double.toString(gui.maxlon)); 149 for (JTextField f : latlon) 150 f.setCaretPosition(0); 151 } 152 153 private void updateUrl(DownloadDialog gui) { 154 double lat = (gui.minlat + gui.maxlat)/2; 155 double lon = (gui.minlon + gui.maxlon)/2; 156 // convert to mercator (for calculation of zoom only) 157 double latMin = Math.log(Math.tan(Math.PI/4.0+gui.minlat/180.0*Math.PI/2.0))*180.0/Math.PI; 158 double latMax = Math.log(Math.tan(Math.PI/4.0+gui.maxlat/180.0*Math.PI/2.0))*180.0/Math.PI; 159 double size = Math.max(Math.abs(latMax-latMin), Math.abs(gui.maxlon-gui.minlon)); 160 int zoom = 0; 161 while (zoom <= 20) { 162 if (size >= 180) 163 break; 164 size *= 2; 165 zoom++; 166 } 167 showUrl.setText("http://www.openstreetmap.org/index.html?mlat="+lat+"&mlon="+lon+"&zoom="+zoom); 168 } 169 170 public static Bounds osmurl2bounds(String url) { 171 int i = url.indexOf('?'); 172 if (i == -1) 173 return null; 174 String[] args = url.substring(i+1).split("&"); 175 HashMap<String, String> map = new HashMap<String, String>(); 176 for (String arg : args) { 177 int eq = arg.indexOf('='); 178 if (eq != -1) { 179 map.put(arg.substring(0, eq), arg.substring(eq + 1)); 180 } 181 } 182 183 Bounds b = null; 184 try { 185 if (map.containsKey("bbox")) { 186 String bbox[] = map.get("bbox").split(","); 187 b = new Bounds( 188 new LatLon(Double.parseDouble(bbox[1]), Double.parseDouble(bbox[0])), 189 new LatLon(Double.parseDouble(bbox[3]), Double.parseDouble(bbox[2]))); 190 191 } else { 192 double size = 180.0 / Math.pow(2, Integer.parseInt(map.get("zoom"))); 193 b = new Bounds( 194 new LatLon(parseDouble(map, "lat") - size/2, parseDouble(map, "lon") - size), 195 new LatLon(parseDouble(map, "lat") + size/2, parseDouble(map, "lon") + size)); 196 } 197 } catch (NumberFormatException x) { 198 } catch (NullPointerException x) { 199 } 200 return b; 201 } 202 203 private static double parseDouble(HashMap<String, String> map, String key) { 204 if (map.containsKey(key)) 205 return Double.parseDouble(map.get(key)); 206 return Double.parseDouble(map.get("m"+key)); 207 207 } 208 208 } -
trunk/src/org/openstreetmap/josm/gui/download/DownloadDialog.java
r1147 r1169 36 36 public class DownloadDialog extends JPanel { 37 37 38 39 38 // the JOptionPane that contains this dialog. required for the closeDialog() method. 39 private JOptionPane optionPane; 40 40 41 42 43 44 45 41 public interface DownloadTask { 42 /** 43 * Execute the download. 44 */ 45 void download(DownloadAction action, double minlat, double minlon, double maxlat, double maxlon); 46 46 void loadUrl(boolean newLayer, String url); 47 48 49 50 51 52 53 54 55 56 47 /** 48 * @return The checkbox presented to the user 49 */ 50 JCheckBox getCheckBox(); 51 /** 52 * @return The name of the preferences suffix to use for storing the 53 * selection state. 54 */ 55 String getPreferencesSuffix(); 56 } 57 57 58 59 60 61 62 63 58 /** 59 * The list of download tasks. First entry should be the osm data entry 60 * and the second the gps entry. After that, plugins can register additional 61 * download possibilities. 62 */ 63 public final List<DownloadTask> downloadTasks = new ArrayList<DownloadTask>(5); 64 64 65 66 67 68 65 public final List<DownloadSelection> downloadSelections = new ArrayList<DownloadSelection>(); 66 public final JTabbedPane tabpane = new JTabbedPane(); 67 public final JCheckBox newLayer; 68 public final JLabel sizeCheck = new JLabel(); 69 69 70 71 72 73 70 public double minlon; 71 public double minlat; 72 public double maxlon; 73 public double maxlat; 74 74 75 75 76 77 76 public DownloadDialog() { 77 setLayout(new GridBagLayout()); 78 78 79 80 79 downloadTasks.add(new DownloadOsmTask()); 80 downloadTasks.add(new DownloadGpsTask()); 81 81 82 83 84 85 86 87 88 89 90 82 // adding the download tasks 83 add(new JLabel(tr("Data Sources and Types")), GBC.eol().insets(0,5,0,0)); 84 for (DownloadTask task : downloadTasks) { 85 add(task.getCheckBox(), GBC.eol().insets(20,0,0,0)); 86 // don't override defaults, if we (initially) don't have any preferences 87 if(Main.pref.hasKey("download."+task.getPreferencesSuffix())) { 88 task.getCheckBox().setSelected(Main.pref.getBoolean("download."+task.getPreferencesSuffix())); 89 } 90 } 91 91 92 93 94 95 96 92 // predefined download selections 93 downloadSelections.add(new BoundingBoxSelection()); 94 downloadSelections.add(new TileSelection()); 95 downloadSelections.add(new BookmarkSelection()); 96 downloadSelections.add(new WorldChooser()); 97 97 98 99 100 101 98 // add selections from plugins 99 for (PluginProxy p : Main.plugins) { 100 p.addDownloadSelection(downloadSelections); 101 } 102 102 103 104 105 106 107 108 103 // now everybody may add their tab to the tabbed pane 104 // (not done right away to allow plugins to remove one of 105 // the default selectors!) 106 for (DownloadSelection s : downloadSelections) { 107 s.addGui(this); 108 } 109 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 110 if (Main.map != null) { 111 MapView mv = Main.map.mapView; 112 minlon = mv.getLatLon(0, mv.getHeight()).lon(); 113 minlat = mv.getLatLon(0, mv.getHeight()).lat(); 114 maxlon = mv.getLatLon(mv.getWidth(), 0).lon(); 115 maxlat = mv.getLatLon(mv.getWidth(), 0).lat(); 116 boundingBoxChanged(null); 117 } 118 else if (Main.pref.hasKey("osm-download.bounds")) { 119 // read the bounding box from the preferences 120 try { 121 String bounds[] = Main.pref.get("osm-download.bounds").split(";"); 122 minlat = Double.parseDouble(bounds[0]); 123 minlon = Double.parseDouble(bounds[1]); 124 maxlat = Double.parseDouble(bounds[2]); 125 maxlon = Double.parseDouble(bounds[3]); 126 boundingBoxChanged(null); 127 } 128 catch (Exception e) { 129 e.printStackTrace(); 130 } 131 } 132 132 133 134 133 newLayer = new JCheckBox(tr("Download as new layer"), Main.pref.getBoolean("download.newlayer", false)); 134 add(newLayer, GBC.eol().insets(0,5,0,0)); 135 135 136 137 136 add(new JLabel(tr("Download Area")), GBC.eol().insets(0,5,0,0)); 137 add(tabpane, GBC.eol().fill()); 138 138 139 140 141 142 143 139 try { 140 tabpane.setSelectedIndex(Integer.parseInt(Main.pref.get("download.tab", "0"))); 141 } catch (Exception ex) { 142 Main.pref.put("download.tab", "0"); 143 } 144 144 145 146 147 148 145 Font labelFont = sizeCheck.getFont(); 146 sizeCheck.setFont(labelFont.deriveFont(Font.PLAIN, labelFont.getSize())); 147 add(sizeCheck, GBC.eop().insets(0,5,5,10)); 148 } 149 149 150 151 152 153 154 155 156 157 158 150 private void updateSizeCheck() { 151 if ((maxlon-minlon)*(maxlat-minlat) > Main.pref.getDouble("osm-server.max-request-area", 0.25)) { 152 sizeCheck.setText(tr("Download area too large; will probably be rejected by server")); 153 sizeCheck.setForeground(Color.red); 154 } else { 155 sizeCheck.setText(tr("Download area ok, size probably acceptable to server")); 156 sizeCheck.setForeground(Color.darkGray); 157 } 158 } 159 159 160 161 162 163 164 165 166 167 168 169 170 171 172 160 /** 161 * Distributes a "bounding box changed" from one DownloadSelection 162 * object to the others, so they may update or clear their input 163 * fields. 164 * 165 * @param eventSource - the DownloadSelection object that fired this notification. 166 */ 167 public void boundingBoxChanged(DownloadSelection eventSource) { 168 for (DownloadSelection s : downloadSelections) { 169 if (s != eventSource) s.boundingBoxChanged(this); 170 } 171 updateSizeCheck(); 172 } 173 173 174 175 176 177 178 179 174 /* 175 * Returns currently selected tab. 176 */ 177 public int getSelectedTab() { 178 return tabpane.getSelectedIndex(); 179 } 180 180 181 182 183 184 185 186 187 188 189 181 /** 182 * Closes the download dialog. This is intended to be called by one of 183 * the various download area selection "plugins". 184 * 185 * @param download true to download selected data, false to cancel download 186 */ 187 public void closeDownloadDialog(boolean download) { 188 optionPane.setValue(download ? JOptionPane.OK_OPTION : JOptionPane.CANCEL_OPTION); 189 } 190 190 191 192 193 194 195 196 197 191 /** 192 * Has to be called after this dialog has been added to a JOptionPane. 193 * @param optionPane 194 */ 195 public void setOptionPane(JOptionPane optionPane) { 196 this.optionPane = optionPane; 197 } 198 198 } -
trunk/src/org/openstreetmap/josm/gui/download/DownloadSelection.java
r627 r1169 4 4 5 5 public interface DownloadSelection { 6 7 * Add the GUI elements to the dialog. 8 9 6 /** 7 * Add the GUI elements to the dialog. 8 */ 9 void addGui(DownloadDialog gui); 10 10 11 /** 12 13 14 15 16 11 /** 12 * Update or clear display when a selection is made through another 13 * DownloadSelection object 14 */ 15 void boundingBoxChanged(DownloadDialog gui); 16 17 17 } -
trunk/src/org/openstreetmap/josm/gui/download/TileSelection.java
r979 r1169 19 19 /** 20 20 * Tile selector. 21 * 21 * 22 22 * Provides a tile coordinate input field. 23 * 23 * 24 24 * @author Frederik Ramm <frederik@remote.org> 25 25 * … … 27 27 public class TileSelection implements DownloadSelection { 28 28 29 private JTextField tileX0 = new JTextField(7); 30 private JTextField tileY0 = new JTextField(7); 31 private JTextField tileX1 = new JTextField(7); 32 private JTextField tileY1 = new JTextField(7); 33 private JSpinner tileZ = new JSpinner(new SpinnerNumberModel(12, 10, 18, 1)); 34 35 public void addGui(final DownloadDialog gui) { 29 private JTextField tileX0 = new JTextField(7); 30 private JTextField tileY0 = new JTextField(7); 31 private JTextField tileX1 = new JTextField(7); 32 private JTextField tileY1 = new JTextField(7); 33 private JSpinner tileZ = new JSpinner(new SpinnerNumberModel(12, 10, 18, 1)); 36 34 37 JPanel smpanel = new JPanel(new GridBagLayout()); 38 smpanel.add(new JLabel(tr("zoom level")), GBC.std().insets(0,0,10,0)); 39 smpanel.add(new JLabel(tr("x from")), GBC.std().insets(10,0,5,0)); 40 smpanel.add(tileX0, GBC.std()); 41 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 42 smpanel.add(tileX1, GBC.eol()); 43 smpanel.add(tileZ, GBC.std().insets(0,0,10,0)); 44 smpanel.add(new JLabel(tr("y from")), GBC.std().insets(10,0,5,0)); 45 smpanel.add(tileY0, GBC.std()); 46 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 47 smpanel.add(tileY1, GBC.eol()); 35 public void addGui(final DownloadDialog gui) { 48 36 49 final FocusListener dialogUpdater = new FocusAdapter() { 50 @Override public void focusLost(FocusEvent e) { 51 try { 52 int zoomlvl = (Integer) tileZ.getValue(); 53 int fromx = Integer.parseInt(tileX0.getText()); 54 int tox = fromx; 55 if (tileX1.getText().length()>0) { 56 tox = Integer.parseInt(tileX1.getText()); 57 } 58 if (tox<fromx) { int i = fromx; fromx=tox; tox=i; } 37 JPanel smpanel = new JPanel(new GridBagLayout()); 38 smpanel.add(new JLabel(tr("zoom level")), GBC.std().insets(0,0,10,0)); 39 smpanel.add(new JLabel(tr("x from")), GBC.std().insets(10,0,5,0)); 40 smpanel.add(tileX0, GBC.std()); 41 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 42 smpanel.add(tileX1, GBC.eol()); 43 smpanel.add(tileZ, GBC.std().insets(0,0,10,0)); 44 smpanel.add(new JLabel(tr("y from")), GBC.std().insets(10,0,5,0)); 45 smpanel.add(tileY0, GBC.std()); 46 smpanel.add(new JLabel(tr("to")), GBC.std().insets(10,0,5,0)); 47 smpanel.add(tileY1, GBC.eol()); 59 48 60 int fromy = Integer.parseInt(tileY0.getText()); 61 int toy = fromy; 62 if (tileY1.getText().length()>0) { 63 toy = Integer.parseInt(tileY1.getText()); 64 } 65 if (toy<fromy) { int i = fromy; fromy=toy; toy=i; } 49 final FocusListener dialogUpdater = new FocusAdapter() { 50 @Override public void focusLost(FocusEvent e) { 51 try { 52 int zoomlvl = (Integer) tileZ.getValue(); 53 int fromx = Integer.parseInt(tileX0.getText()); 54 int tox = fromx; 55 if (tileX1.getText().length()>0) { 56 tox = Integer.parseInt(tileX1.getText()); 57 } 58 if (tox<fromx) { int i = fromx; fromx=tox; tox=i; } 66 59 67 gui.minlat = tileYToLat(zoomlvl, toy+1); 68 gui.minlon = tileXToLon(zoomlvl, fromx); 69 gui.maxlat = tileYToLat(zoomlvl, fromy); 70 gui.maxlon = tileXToLon(zoomlvl, tox+1); 71 gui.boundingBoxChanged(TileSelection.this); 72 //repaint(); 73 } catch (NumberFormatException x) { 74 // ignore 75 } 76 } 77 }; 78 79 for (JTextField f : new JTextField[] { tileX0, tileX1, tileY0, tileY1 }) { 80 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height)); 81 f.addFocusListener(dialogUpdater); 82 } 60 int fromy = Integer.parseInt(tileY0.getText()); 61 int toy = fromy; 62 if (tileY1.getText().length()>0) { 63 toy = Integer.parseInt(tileY1.getText()); 64 } 65 if (toy<fromy) { int i = fromy; fromy=toy; toy=i; } 83 66 84 gui.tabpane.addTab(tr("Tile Numbers"), smpanel); 85 } 86 87 /** 88 * Called when bounding box is changed by one of the other download dialog tabs. 89 */ 90 public void boundingBoxChanged(DownloadDialog gui) { 91 updateBboxFields(gui); 92 } 93 94 private void updateBboxFields(DownloadDialog gui) { 95 int z = ((Integer) tileZ.getValue()).intValue(); 96 tileX0.setText(Integer.toString(lonToTileX(z, gui.minlon))); 97 tileX1.setText(Integer.toString(lonToTileX(z, gui.maxlon-.00001))); 98 tileY0.setText(Integer.toString(latToTileY(z, gui.maxlat-.00001))); 99 tileY1.setText(Integer.toString(latToTileY(z, gui.minlat))); 100 } 101 102 public static int latToTileY(int zoom, double lat) { 103 if ((zoom < 3) || (zoom > 18)) return -1; 104 double l = lat / 180 * Math.PI; 105 double pf = Math.log(Math.tan(l) + (1/Math.cos(l))); 106 return (int) ((1<<(zoom-1)) * (Math.PI - pf) / Math.PI); 107 } 67 gui.minlat = tileYToLat(zoomlvl, toy+1); 68 gui.minlon = tileXToLon(zoomlvl, fromx); 69 gui.maxlat = tileYToLat(zoomlvl, fromy); 70 gui.maxlon = tileXToLon(zoomlvl, tox+1); 71 gui.boundingBoxChanged(TileSelection.this); 72 //repaint(); 73 } catch (NumberFormatException x) { 74 // ignore 75 } 76 } 77 }; 108 78 109 public static int lonToTileX(int zoom, double lon) {110 if ((zoom < 3) || (zoom > 18)) return -1; 111 return (int) ((1<<(zoom-3)) * (lon + 180.0) / 45.0);112 79 for (JTextField f : new JTextField[] { tileX0, tileX1, tileY0, tileY1 }) { 80 f.setMinimumSize(new Dimension(100,new JTextField().getMinimumSize().height)); 81 f.addFocusListener(dialogUpdater); 82 } 113 83 114 public static double tileYToLat(int zoom, int y) { 115 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 116 return Math.atan(Math.sinh(Math.PI - (Math.PI*y / (1<<(zoom-1))))) * 180 / Math.PI; 117 } 84 gui.tabpane.addTab(tr("Tile Numbers"), smpanel); 85 } 118 86 119 public static double tileXToLon(int zoom, int x) { 120 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 121 return x * 45.0 / (1<<(zoom-3)) - 180.0; 87 /** 88 * Called when bounding box is changed by one of the other download dialog tabs. 89 */ 90 public void boundingBoxChanged(DownloadDialog gui) { 91 updateBboxFields(gui); 92 } 122 93 123 } 94 private void updateBboxFields(DownloadDialog gui) { 95 int z = ((Integer) tileZ.getValue()).intValue(); 96 tileX0.setText(Integer.toString(lonToTileX(z, gui.minlon))); 97 tileX1.setText(Integer.toString(lonToTileX(z, gui.maxlon-.00001))); 98 tileY0.setText(Integer.toString(latToTileY(z, gui.maxlat-.00001))); 99 tileY1.setText(Integer.toString(latToTileY(z, gui.minlat))); 100 } 101 102 public static int latToTileY(int zoom, double lat) { 103 if ((zoom < 3) || (zoom > 18)) return -1; 104 double l = lat / 180 * Math.PI; 105 double pf = Math.log(Math.tan(l) + (1/Math.cos(l))); 106 return (int) ((1<<(zoom-1)) * (Math.PI - pf) / Math.PI); 107 } 108 109 public static int lonToTileX(int zoom, double lon) { 110 if ((zoom < 3) || (zoom > 18)) return -1; 111 return (int) ((1<<(zoom-3)) * (lon + 180.0) / 45.0); 112 } 113 114 public static double tileYToLat(int zoom, int y) { 115 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 116 return Math.atan(Math.sinh(Math.PI - (Math.PI*y / (1<<(zoom-1))))) * 180 / Math.PI; 117 } 118 119 public static double tileXToLon(int zoom, int x) { 120 if ((zoom < 3) || (zoom > 18)) return Double.MIN_VALUE; 121 return x * 45.0 / (1<<(zoom-3)) - 180.0; 122 123 } 124 124 } -
trunk/src/org/openstreetmap/josm/gui/download/WorldChooser.java
r733 r1169 38 38 public class WorldChooser extends NavigatableComponent implements DownloadSelection { 39 39 40 41 42 43 40 /** 41 * The world as picture. 42 */ 43 private ImageIcon world; 44 44 45 46 47 48 45 /** 46 * Maximum scale level 47 */ 48 private double scaleMax; 49 49 50 51 52 53 50 /** 51 * Mark this rectangle (lat/lon values) when painting. 52 */ 53 private EastNorth markerMin, markerMax; 54 54 55 55 private Projection projection; 56 56 57 58 59 60 61 62 63 64 57 /** 58 * Create the chooser component. 59 */ 60 public WorldChooser() { 61 URL path = Main.class.getResource("/images/world.jpg"); 62 world = new ImageIcon(path); 63 center = new EastNorth(world.getIconWidth()/2, world.getIconHeight()/2); 64 setPreferredSize(new Dimension(400, 200)); 65 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 66 projection = new Projection() { 67 public EastNorth latlon2eastNorth(LatLon p) { 68 return new EastNorth( 69 (p.lon()+180) / 360 * world.getIconWidth(), 70 (p.lat()+90) / 180 * world.getIconHeight()); 71 } 72 public LatLon eastNorth2latlon(EastNorth p) { 73 return new LatLon( 74 p.north()*180/world.getIconHeight() - 90, 75 p.east()*360/world.getIconWidth() - 180); 76 } 77 @Override public String toString() { 78 return "WorldChooser"; 79 } 80 80 public String getCacheDirectoryName() { 81 81 throw new UnsupportedOperationException(); 82 82 } 83 84 83 public double scaleFactor() { 84 return 1.0 / world.getIconWidth(); 85 85 } 86 86 87 87 }; 88 88 89 90 91 89 MapScaler scaler = new MapScaler(this, projection); 90 add(scaler); 91 scaler.setLocation(10,10); 92 92 93 setMinimumSize(new Dimension(350, 350/2)); 94 } 95 96 public void addGui(final DownloadDialog gui) { 97 JPanel temp = new JPanel(); 98 temp.setLayout(new BorderLayout()); 99 temp.add(this, BorderLayout.CENTER); 100 temp.add(new JLabel(tr("You can use the mouse or Ctrl+Arrow keys/./ to zoom and pan.")), BorderLayout.SOUTH); 101 gui.tabpane.add(temp, tr("Map")); 102 new MapMover(this, temp); 103 SelectionEnded selListener = new SelectionEnded(){ 104 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) { 105 markerMin = getEastNorth(r.x, r.y+r.height); 106 markerMax = getEastNorth(r.x+r.width, r.y); 107 LatLon min = getProjection().eastNorth2latlon(markerMin); 108 LatLon max = getProjection().eastNorth2latlon(markerMax); 109 gui.minlat = min.lat(); 110 gui.minlon = min.lon(); 111 gui.maxlat = max.lat(); 112 gui.maxlon = max.lon(); 113 gui.boundingBoxChanged(WorldChooser.this); 114 repaint(); 115 } 116 public void addPropertyChangeListener(PropertyChangeListener listener) {} 117 public void removePropertyChangeListener(PropertyChangeListener listener) {} 118 }; 119 SelectionManager sm = new SelectionManager(selListener, false, this); 120 sm.register(this); 93 setMinimumSize(new Dimension(350, 350/2)); 121 94 } 122 95 123 public void boundingBoxChanged(DownloadDialog gui) { 124 markerMin = getProjection().latlon2eastNorth(new LatLon(gui.minlat, gui.minlon)); 125 markerMax = getProjection().latlon2eastNorth(new LatLon(gui.maxlat, gui.maxlon)); 126 repaint(); 127 } 96 public void addGui(final DownloadDialog gui) { 97 JPanel temp = new JPanel(); 98 temp.setLayout(new BorderLayout()); 99 temp.add(this, BorderLayout.CENTER); 100 temp.add(new JLabel(tr("You can use the mouse or Ctrl+Arrow keys/./ to zoom and pan.")), BorderLayout.SOUTH); 101 gui.tabpane.add(temp, tr("Map")); 102 new MapMover(this, temp); 103 SelectionEnded selListener = new SelectionEnded(){ 104 public void selectionEnded(Rectangle r, boolean alt, boolean shift, boolean ctrl) { 105 markerMin = getEastNorth(r.x, r.y+r.height); 106 markerMax = getEastNorth(r.x+r.width, r.y); 107 LatLon min = getProjection().eastNorth2latlon(markerMin); 108 LatLon max = getProjection().eastNorth2latlon(markerMax); 109 gui.minlat = min.lat(); 110 gui.minlon = min.lon(); 111 gui.maxlat = max.lat(); 112 gui.maxlon = max.lon(); 113 gui.boundingBoxChanged(WorldChooser.this); 114 repaint(); 115 } 116 public void addPropertyChangeListener(PropertyChangeListener listener) {} 117 public void removePropertyChangeListener(PropertyChangeListener listener) {} 118 }; 119 SelectionManager sm = new SelectionManager(selListener, false, this); 120 sm.register(this); 121 } 128 122 129 /** 130 * Set the scale as well as the preferred size. 131 */ 132 @Override public void setPreferredSize(Dimension preferredSize) { 133 super.setPreferredSize(preferredSize); 134 scale = world.getIconWidth()/preferredSize.getWidth(); 135 scaleMax = scale; 136 } 123 public void boundingBoxChanged(DownloadDialog gui) { 124 markerMin = getProjection().latlon2eastNorth(new LatLon(gui.minlat, gui.minlon)); 125 markerMax = getProjection().latlon2eastNorth(new LatLon(gui.maxlat, gui.maxlon)); 126 repaint(); 127 } 137 128 138 /** 139 * Draw the current selected region. 140 */ 141 @Override public void paint(Graphics g) { 142 EastNorth tl = getEastNorth(0,0); 143 EastNorth br = getEastNorth(getWidth(),getHeight()); 144 g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.east(),(int)tl.north(),(int)br.east(),(int)br.north(), null); 129 /** 130 * Set the scale as well as the preferred size. 131 */ 132 @Override public void setPreferredSize(Dimension preferredSize) { 133 super.setPreferredSize(preferredSize); 134 scale = world.getIconWidth()/preferredSize.getWidth(); 135 scaleMax = scale; 136 } 145 137 146 // draw marker rect 147 if (markerMin != null && markerMax != null) { 148 Point p1 = getPoint(markerMin); 149 Point p2 = getPoint(markerMax); 150 double x = Math.min(p1.x, p2.x); 151 double y = Math.min(p1.y, p2.y); 152 double w = Math.max(p1.x, p2.x) - x; 153 double h = Math.max(p1.y, p2.y) - y; 154 if (w < 1) 155 w = 1; 156 if (h < 1) 157 h = 1; 158 g.setColor(Color.YELLOW); 159 g.drawRect((int)x, (int)y, (int)w, (int)h); 160 } 161 super.paint(g); 162 } 138 /** 139 * Draw the current selected region. 140 */ 141 @Override public void paint(Graphics g) { 142 EastNorth tl = getEastNorth(0,0); 143 EastNorth br = getEastNorth(getWidth(),getHeight()); 144 g.drawImage(world.getImage(),0,0,getWidth(),getHeight(),(int)tl.east(),(int)tl.north(),(int)br.east(),(int)br.north(), null); 163 145 164 @Override public void zoomTo(EastNorth newCenter, double scale) { 165 if (getWidth() != 0 && scale > scaleMax) { 166 scale = scaleMax; 167 newCenter = center; 168 } 169 super.zoomTo(newCenter, scale); 170 } 146 // draw marker rect 147 if (markerMin != null && markerMax != null) { 148 Point p1 = getPoint(markerMin); 149 Point p2 = getPoint(markerMax); 150 double x = Math.min(p1.x, p2.x); 151 double y = Math.min(p1.y, p2.y); 152 double w = Math.max(p1.x, p2.x) - x; 153 double h = Math.max(p1.y, p2.y) - y; 154 if (w < 1) 155 w = 1; 156 if (h < 1) 157 h = 1; 158 g.setColor(Color.YELLOW); 159 g.drawRect((int)x, (int)y, (int)w, (int)h); 160 } 161 super.paint(g); 162 } 171 163 172 /** 173 * Always use our image projection mode. 174 */ 175 @Override protected Projection getProjection() { 176 return projection; 177 } 164 @Override public void zoomTo(EastNorth newCenter, double scale) { 165 if (getWidth() != 0 && scale > scaleMax) { 166 scale = scaleMax; 167 newCenter = center; 168 } 169 super.zoomTo(newCenter, scale); 170 } 171 172 /** 173 * Always use our image projection mode. 174 */ 175 @Override protected Projection getProjection() { 176 return projection; 177 } 178 178 }
Note:
See TracChangeset
for help on using the changeset viewer.