Ignore:
Timestamp:
2013-03-18T21:58:17+01:00 (12 years ago)
Author:
zverik
Message:

some updates to iodb plugin

File:
1 copied

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/imagery_offset_db/src/iodb/DeprecateOffsetAction.java

    r29361 r29371  
    22
    33import java.awt.event.ActionEvent;
    4 import java.awt.event.KeyEvent;
    5 import java.io.IOException;
    6 import java.io.InputStream;
    74import java.io.UnsupportedEncodingException;
    85import 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;
     6import javax.swing.AbstractAction;
     7import javax.swing.JOptionPane;
    178import 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;
     9import org.openstreetmap.josm.gui.JosmUserIdentityManager;
    2610import static org.openstreetmap.josm.tools.I18n.tr;
    27 import org.openstreetmap.josm.tools.Shortcut;
    28 import org.xml.sax.SAXException;
     11import org.openstreetmap.josm.tools.ImageProvider;
    2912
    3013/**
     
    3316 * @author zverik
    3417 */
    35 public class GetImageryOffsetAction extends JosmAction {
     18public class DeprecateOffsetAction extends AbstractAction {
     19    private ImageryOffsetBase offset;
    3620   
    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();
     21    public DeprecateOffsetAction( ImageryOffsetBase offset ) {
     22        super(tr("Deprecate Offset"));
     23        putValue(SMALL_ICON, ImageProvider.get("dialogs", "delete"));
     24        this.offset = offset;
     25        setEnabled(offset != null && !offset.isDeprecated());
    4326    }
    4427
    4528    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 )
     29        if( Main.map == null || Main.map.mapView == null || !Main.map.isVisible() )
    5130            return;
    5231       
    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();
     32        if( JOptionPane.showConfirmDialog(Main.parent,
     33                tr("Warning: deprecation is irreversible"), // todo: expand
     34                ImageryOffsetTools.DIALOG_TITLE, JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) != JOptionPane.YES_OPTION ) {
    6035            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
     36        }
     37        deprecateOffset(offset);
     38    }
     39
     40    public static void deprecateOffset( ImageryOffsetBase offset ) {
     41        String userName = JosmUserIdentityManager.getInstance().getUserName();
     42        if( userName == null ) {
     43            JOptionPane.showMessageDialog(Main.parent, tr("To store imagery offsets you must be a registered OSM user."), ImageryOffsetTools.DIALOG_TITLE, JOptionPane.ERROR_MESSAGE);
     44            return;
     45        }
     46
     47        String message = "Please enter the reason why you mark this "
     48                + (offset instanceof ImageryOffset ? "imagery offset" : "calibraion object") + " as deprecated:";
     49        String reason = null;
     50        boolean iterated = false;
     51        while( reason == null ) {
     52            reason = JOptionPane.showInputDialog(Main.parent, message, ImageryOffsetTools.DIALOG_TITLE, JOptionPane.PLAIN_MESSAGE);
     53            if( reason == null || reason.length() == 0 ) {
     54                return;
     55            }
     56            if( reason.length() < 3 || reason.length() > 200 ) {
     57                reason = null;
     58                if( !iterated ) {
     59                    message = message + "\n" + tr("Reason text should be 3 to 200 letters long.");
     60                    iterated = true;
     61                }
    7162            }
    7263        }
    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;
     64       
    7965        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;
     66            String query = "deprecate?id=" + offset.getId()
     67                + "&author=" + URLEncoder.encode(userName, "UTF8")
     68                + "&reason=" + URLEncoder.encode(reason, "UTF8");
     69            SimpleOffsetQueryTask depTask = new SimpleOffsetQueryTask(query, tr("Notifying the server of the deprecation..."));
     70            Main.worker.submit(depTask);
     71        } catch( UnsupportedEncodingException ex ) {
     72            // WTF
    13573        }
    13674    }
Note: See TracChangeset for help on using the changeset viewer.