1 | package iodb;
|
---|
2 |
|
---|
3 | import java.awt.event.ActionEvent;
|
---|
4 | import java.awt.event.KeyEvent;
|
---|
5 | import java.io.InputStream;
|
---|
6 | import java.io.UnsupportedEncodingException;
|
---|
7 | import java.net.*;
|
---|
8 | import java.util.*;
|
---|
9 | import javax.swing.JOptionPane;
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
13 | import org.openstreetmap.josm.data.projection.Projection;
|
---|
14 | import org.openstreetmap.josm.gui.layer.ImageryLayer;
|
---|
15 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
16 | import 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 | */
|
---|
23 | public 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 | }
|
---|