source: osm/applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java@ 29376

Last change on this file since 29376 was 29376, checked in by zverik, 11 years ago

another iteration

File size: 4.9 KB
Line 
1package iodb;
2
3import java.awt.event.ActionEvent;
4import java.awt.event.KeyEvent;
5import java.io.InputStream;
6import java.io.UnsupportedEncodingException;
7import java.net.*;
8import java.util.*;
9import javax.swing.JOptionPane;
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.actions.JosmAction;
12import org.openstreetmap.josm.data.coor.LatLon;
13import org.openstreetmap.josm.data.projection.Projection;
14import org.openstreetmap.josm.gui.layer.ImageryLayer;
15import static org.openstreetmap.josm.tools.I18n.tr;
16import org.openstreetmap.josm.tools.Shortcut;
17
18/**
19 * Download a list of imagery offsets for the current position, let user choose which one to use.
20 *
21 * @author zverik
22 */
23public class GetImageryOffsetAction extends JosmAction {
24
25 public GetImageryOffsetAction() {
26 super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"),
27 Shortcut.registerShortcut("imageryoffset:get", tr("Imagery: {0}", tr("Get Imagery Offset...")),
28 KeyEvent.VK_I, Shortcut.ALT_CTRL), true);
29 }
30
31 public void actionPerformed(ActionEvent e) {
32 if( Main.map == null || Main.map.mapView == null || !Main.map.isVisible() )
33 return;
34 Projection proj = Main.map.mapView.getProjection();
35 LatLon center = proj.eastNorth2latlon(Main.map.mapView.getCenter());
36 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
37 String imagery = ImageryOffsetTools.getImageryID(layer);
38 if( imagery == null )
39 return;
40
41 DownloadOffsetsTask download = new DownloadOffsetsTask(center, layer, imagery);
42 Main.worker.submit(download);
43 }
44
45 @Override
46 protected void updateEnabledState() {
47 boolean state = true;
48 if( Main.map == null || Main.map.mapView == null || !Main.map.isVisible() )
49 state = false;
50 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer();
51 if( ImageryOffsetTools.getImageryID(layer) == null )
52 state = false;
53 setEnabled(state);
54 }
55
56 private void showOffsetDialog( List<ImageryOffsetBase> offsets, ImageryLayer layer ) {
57 if( offsets.isEmpty() ) {
58 JOptionPane.showMessageDialog(Main.parent,
59 tr("No data for this region. Please adjust imagery layer and upload an offset."),
60 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
61 return;
62 }
63 final ImageryOffsetBase offset = new OffsetDialog(offsets).showDialog();
64 if( offset != null ) {
65 if( offset instanceof ImageryOffset ) {
66 ImageryOffsetTools.applyLayerOffset(layer, (ImageryOffset)offset);
67 Main.map.repaint();
68 } else if( offset instanceof CalibrationObject ) {
69 CalibrationLayer clayer = new CalibrationLayer((CalibrationObject)offset);
70 Main.map.mapView.addLayer(clayer);
71 clayer.panToCenter();
72 if( !Main.pref.getBoolean("iodb.calibration.message", false) ) {
73 JOptionPane.showMessageDialog(Main.parent, // todo: update text
74 tr("A layer has been added with a calibration geometry. Hide data layers,\n"
75 + "find the corresponding feature on the imagery layer and move it accordingly."),
76 ImageryOffsetTools.DIALOG_TITLE, JOptionPane.INFORMATION_MESSAGE);
77 Main.pref.put("iodb.calibration.message", true);
78 }
79 }
80 }
81 }
82
83 class DownloadOffsetsTask extends SimpleOffsetQueryTask {
84 private ImageryLayer layer;
85 private List<ImageryOffsetBase> offsets;
86
87 public DownloadOffsetsTask( LatLon center, ImageryLayer layer, String imagery ) {
88 super(null, tr("Loading imagery offsets..."));
89 try {
90 String query = "get?lat=" + center.lat() + "&lon=" + center.lon()
91 + "&imagery=" + URLEncoder.encode(imagery, "UTF8");
92 int radius = Main.pref.getInteger("iodb.radius", -1);
93 if( radius > 0 )
94 query = query + "?radius=" + radius;
95 setQuery(query);
96 } catch( UnsupportedEncodingException e ) {
97 throw new IllegalArgumentException(e);
98 }
99 this.layer = layer;
100 }
101
102 @Override
103 protected void afterFinish() {
104 if( !cancelled && offsets != null )
105 showOffsetDialog(offsets, layer);
106 }
107
108 @Override
109 protected void processResponse( InputStream inp ) throws UploadException {
110 offsets = null;
111 try {
112 offsets = new IODBReader(inp).parse();
113 } catch( Exception e ) {
114 throw new UploadException("Error processing XML response: " + e.getMessage());
115 }
116 }
117 }
118}
Note: See TracBrowser for help on using the repository browser.