Changeset 32528 in osm for applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
- Timestamp:
- 2016-07-02T03:55:03+02:00 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/imagery_offset_db/src/iodb/GetImageryOffsetAction.java
r30808 r32528 1 // License: WTFPL. For details, see LICENSE file. 1 2 package iodb; 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 2 5 3 6 import java.awt.event.ActionEvent; … … 5 8 import java.io.InputStream; 6 9 import java.io.UnsupportedEncodingException; 7 import java.net.*; 8 import java.util.*; 10 import java.net.URLEncoder; 11 import java.util.List; 12 9 13 import javax.swing.Action; 10 14 import javax.swing.Icon; 11 15 import javax.swing.JOptionPane; 16 12 17 import org.openstreetmap.josm.Main; 13 18 import org.openstreetmap.josm.actions.JosmAction; … … 16 21 import org.openstreetmap.josm.data.projection.Projection; 17 22 import org.openstreetmap.josm.gui.layer.ImageryLayer; 18 import static org.openstreetmap.josm.tools.I18n.tr;19 23 import org.openstreetmap.josm.tools.ImageProvider; 20 24 import org.openstreetmap.josm.tools.Shortcut; … … 22 26 /** 23 27 * Download a list of imagery offsets for the current position, let user choose which one to use. 24 * 28 * 25 29 * @author Zverik 26 30 * @license WTFPL … … 29 33 private Icon iconOffsetOk; 30 34 private Icon iconOffsetBad; 31 35 32 36 /** 33 37 * Initialize the action. Sets "Ctrl+Alt+I" shortcut: the only shortcut in this plugin. … … 37 41 super(tr("Get Imagery Offset..."), "getoffset", tr("Download offsets for current imagery from a server"), 38 42 Shortcut.registerShortcut("imageryoffset:get", tr("Imagery: {0}", tr("Get Imagery Offset...")), 39 KeyEvent.VK_I, Shortcut.ALT_CTRL), true); 43 KeyEvent.VK_I, Shortcut.ALT_CTRL), true); 40 44 iconOffsetOk = new ImageProvider("getoffset").setSize(ImageProvider.ImageSizes.MENU).get(); 41 45 iconOffsetBad = new ImageProvider("getoffsetnow").setSize(ImageProvider.ImageSizes.MENU).get(); … … 46 50 * The action just executes {@link DownloadOffsetsTask}. 47 51 */ 52 @Override 48 53 public void actionPerformed(ActionEvent e) { 49 if (Main.map == null || Main.map.mapView == null || !Main.map.isVisible())54 if (Main.map == null || Main.map.mapView == null || !Main.map.isVisible()) 50 55 return; 51 56 Projection proj = Main.map.mapView.getProjection(); … … 53 58 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 54 59 String imagery = ImageryOffsetTools.getImageryID(layer); 55 if (imagery == null)60 if (imagery == null) 56 61 return; 57 62 58 63 DownloadOffsetsTask download = new DownloadOffsetsTask(center, layer, imagery); 59 64 Main.worker.submit(download); … … 67 72 protected void updateEnabledState() { 68 73 boolean state = true; 69 if (Main.map == null || Main.map.mapView == null || !Main.map.isVisible())74 if (Main.map == null || Main.map.mapView == null || !Main.map.isVisible()) 70 75 state = false; 71 76 ImageryLayer layer = ImageryOffsetTools.getTopImageryLayer(); 72 if (ImageryOffsetTools.getImageryID(layer) == null)77 if (ImageryOffsetTools.getImageryID(layer) == null) 73 78 state = false; 74 79 setEnabled(state); 75 80 } 76 81 77 82 /** 78 83 * Display a dialog for choosing between offsets. If there are no offsets in … … 80 85 * @param offsets List of offset objects to choose from. 81 86 */ 82 private void showOffsetDialog( List<ImageryOffsetBase> offsets) {83 if (offsets.isEmpty()) {87 private void showOffsetDialog(List<ImageryOffsetBase> offsets) { 88 if (offsets.isEmpty()) { 84 89 JOptionPane.showMessageDialog(Main.parent, 85 90 tr("No data for this region. Please adjust imagery layer and upload an offset."), … … 88 93 } 89 94 OffsetDialog offsetDialog = new OffsetDialog(offsets); 90 if (offsetDialog.showDialog() != null)95 if (offsetDialog.showDialog() != null) 91 96 offsetDialog.applyOffset(); 92 97 } … … 95 100 * Update action icon based on an offset state. 96 101 */ 97 public void offsetStateChanged( boolean isOffsetGood ) { 102 @Override 103 public void offsetStateChanged(boolean isOffsetGood) { 98 104 putValue(Action.SMALL_ICON, isOffsetGood ? iconOffsetOk : iconOffsetBad); 99 105 } … … 122 128 * @param imagery Imagery ID for the layer. 123 129 */ 124 publicDownloadOffsetsTask(LatLon center, ImageryLayer layer, String imagery) {130 DownloadOffsetsTask(LatLon center, ImageryLayer layer, String imagery) { 125 131 super(null, tr("Loading imagery offsets...")); 126 132 try { 127 133 String query = "get?lat=" + center.latToString(CoordinateFormat.DECIMAL_DEGREES) 128 + "&lon=" + center.lonToString(CoordinateFormat.DECIMAL_DEGREES)129 + "&imagery=" + URLEncoder.encode(imagery, "UTF8");134 + "&lon=" + center.lonToString(CoordinateFormat.DECIMAL_DEGREES) 135 + "&imagery=" + URLEncoder.encode(imagery, "UTF8"); 130 136 int radius = Main.pref.getInteger("iodb.radius", -1); 131 if (radius > 0)137 if (radius > 0) 132 138 query = query + "&radius=" + radius; 133 139 setQuery(query); 134 } catch (UnsupportedEncodingException e) {140 } catch (UnsupportedEncodingException e) { 135 141 throw new IllegalArgumentException(e); 136 142 } … … 142 148 @Override 143 149 protected void afterFinish() { 144 if (!cancelled && offsets != null)150 if (!cancelled && offsets != null) 145 151 showOffsetDialog(offsets); 146 152 } 147 153 148 154 /** 149 155 * Parses the response with {@link IODBReader}. … … 152 158 */ 153 159 @Override 154 protected void processResponse( InputStream inp) throws UploadException {160 protected void processResponse(InputStream inp) throws UploadException { 155 161 offsets = null; 156 162 try { 157 163 offsets = new IODBReader(inp).parse(); 158 } catch (Exception e) {164 } catch (Exception e) { 159 165 throw new UploadException(tr("Error processing XML response: {0}", e.getMessage())); 160 166 }
Note:
See TracChangeset
for help on using the changeset viewer.
