Changeset 29371 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetInfoAction.java
- Timestamp:
- 2013-03-18T21:58:17+01:00 (11 years ago)
- File:
-
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/OffsetInfoAction.java
r29361 r29371 2 2 3 3 import java.awt.event.ActionEvent; 4 import java.awt.event.KeyEvent; 5 import java.io.IOException; 6 import java.io.InputStream; 7 import java.io.UnsupportedEncodingException; 8 import java.net.*; 9 import java.util.ArrayList; 10 import java.util.Collections; 11 import java.util.HashMap; 12 import java.util.List; 13 import java.util.concurrent.Future; 14 import java.util.logging.Level; 15 import java.util.logging.Logger; 16 import org.openstreetmap.gui.jmapviewer.tilesources.AbstractTMSTileSource; 4 import javax.swing.AbstractAction; 5 import javax.swing.JOptionPane; 17 6 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.actions.JosmAction;19 import org.openstreetmap.josm.data.coor.LatLon;20 import org.openstreetmap.josm.data.projection.Projection;21 import org.openstreetmap.josm.gui.MapView;22 import org.openstreetmap.josm.gui.PleaseWaitRunnable;23 import org.openstreetmap.josm.gui.layer.ImageryLayer;24 import org.openstreetmap.josm.gui.progress.ProgressMonitor;25 import org.openstreetmap.josm.io.OsmTransferException;26 7 import static org.openstreetmap.josm.tools.I18n.tr; 27 import org.openstreetmap.josm.tools.Shortcut; 28 import org.xml.sax.SAXException; 8 import org.openstreetmap.josm.tools.ImageProvider; 29 9 30 10 /** … … 33 13 * @author zverik 34 14 */ 35 public class GetImageryOffsetAction extends JosmAction { 15 public class OffsetInfoAction extends AbstractAction { 16 private ImageryOffsetBase offset; 36 17 37 private List<ImageryOffsetBase> offsets; 38 39 public GetImageryOffsetAction() { 40 super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"), 41 Shortcut.registerShortcut("imageryoffset:get", tr("Imagery: {0}", tr("Get Imagery Offset...")), KeyEvent.VK_I, Shortcut.ALT+Shortcut.CTRL), true); 42 offsets = Collections.emptyList(); 18 public OffsetInfoAction( ImageryOffsetBase offset ) { 19 super(tr("Offset Information")); 20 putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete")); 21 this.offset = offset; 22 setEnabled(offset != null); 43 23 } 44 24 45 25 public void actionPerformed(ActionEvent e) { 46 Projection proj = Main.map.mapView.getProjection(); 47 LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter()); 48 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 49 String imagery = ImageryOffsetTools.getImageryID(layer); 50 if( imagery == null ) 51 return; 52 53 List<ImageryOffsetBase> offsets = download(center, imagery); // todo: async 54 /*DownloadOffsets download = new DownloadOffsets(); 55 Future<?> future = Main.worker.submit(download); 56 try { 57 future.get(); 58 } catch( Exception ex ) { 59 ex.printStackTrace(); 60 return; 61 }*/ 62 63 // todo: show a dialog for selecting one of the offsets (without "update" flag) 64 ImageryOffsetBase offset = new OffsetDialog(offsets).showDialog(); 65 if( offset != null ) { 66 // todo: use the chosen offset 67 if( offset instanceof ImageryOffset ) { 68 ImageryOffsetTools.applyLayerOffset(layer, (ImageryOffset)offset); 69 } else if( offset instanceof CalibrationObject ) { 70 // todo: select object 71 } 72 } 73 } 74 75 private List<ImageryOffsetBase> download( LatLon center, String imagery ) { 76 String base = Main.pref.get("iodb.server.url", "http://offsets.textual.ru/"); 77 String query = "get?lat=" + center.getX() + "&lon=" + center.getY(); 78 List<ImageryOffsetBase> result = null; 79 try { 80 query = query + "&imagery=" + URLEncoder.encode(imagery, "utf-8"); 81 URL url = new URL(base + query); 82 System.out.println("url=" + url); 83 HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 84 connection.connect(); 85 int retCode = connection.getResponseCode(); 86 InputStream inp = connection.getInputStream(); 87 if( inp != null ) { 88 result = new IODBReader(inp).parse(); 89 System.out.println("result.size() = " + result.size()); 90 } 91 connection.disconnect(); 92 } catch( MalformedURLException ex ) { 93 // ? 94 } catch( UnsupportedEncodingException e ) { 95 // do nothing. WTF is that? 96 } catch( IOException e ) { 97 e.printStackTrace(); 98 // ? 99 } catch( SAXException e ) { 100 e.printStackTrace(); 101 // ? 102 } 103 if( result == null ) 104 result = new ArrayList<ImageryOffsetBase>(); 105 return result; 106 } 107 108 class DownloadOffsets extends PleaseWaitRunnable { 109 110 private boolean cancelled; 111 112 public DownloadOffsets() { 113 super(tr("Downloading calibration data")); 114 cancelled = false; 115 } 116 117 @Override 118 protected void realRun() throws SAXException, IOException, OsmTransferException { 119 // todo: open httpconnection to server and read xml 120 if( cancelled ) 121 return; 122 123 } 124 125 @Override 126 protected void finish() { 127 if( cancelled ) 128 return; 129 // todo: parse xml and return an array of ImageryOffsetBase 130 } 131 132 @Override 133 protected void cancel() { 134 cancelled = true; 135 } 26 JOptionPane.showMessageDialog(Main.parent, "TODO", ImageryOffsetTools.DIALOG_TITLE, JOptionPane.PLAIN_MESSAGE); 136 27 } 137 28 }
Note:
See TracChangeset
for help on using the changeset viewer.