Changeset 92 in josm for src/org/openstreetmap/josm/actions


Ignore:
Timestamp:
2006-04-22T18:50:21+02:00 (18 years ago)
Author:
imi
Message:
  • added progress bar and cancel button to all down-/uploads
  • added url loading via command line
  • added --selection=... loading of selections via command line
Location:
src/org/openstreetmap/josm/actions
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/actions/DownloadAction.java

    r86 r92  
    1313import java.util.Collection;
    1414import java.util.HashMap;
    15 import java.util.Map;
    1615
    1716import javax.swing.DefaultListModel;
     
    3635import org.openstreetmap.josm.gui.MapFrame;
    3736import org.openstreetmap.josm.gui.MapView;
     37import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3838import org.openstreetmap.josm.gui.WorldChooser;
    3939import org.openstreetmap.josm.gui.BookmarkList.Bookmark;
     
    6060         * Run in the worker thread.
    6161         */
    62     private final class DownloadOsmTask extends PleaseWaitRunnable {
    63             private final OsmServerReader reader;
     62        private final class DownloadOsmTask extends PleaseWaitRunnable {
     63                private final OsmServerReader reader;
    6464                private DataSet dataSet;
    6565
    66             private DownloadOsmTask(OsmServerReader reader) {
    67                     super("Downloading data");
    68                     this.reader = reader;
    69             }
    70 
    71             @Override public void realRun() throws IOException, SAXException {
    72                 dataSet = reader.parseOsm();
    73                 if (dataSet == null)
    74                         return;
    75                 if (dataSet.nodes.isEmpty())
    76                         JOptionPane.showMessageDialog(Main.main, "No data imported.");
    77             }
     66                private DownloadOsmTask(OsmServerReader reader) {
     67                        super("Downloading data");
     68                        this.reader = reader;
     69                        reader.setProgressInformation(currentAction, progress);
     70                }
     71
     72                @Override public void realRun() throws IOException, SAXException {
     73                        dataSet = reader.parseOsm();
     74                        if (dataSet == null)
     75                                return;
     76                        if (dataSet.nodes.isEmpty())
     77                                JOptionPane.showMessageDialog(Main.main, "No data imported.");
     78                }
    7879
    7980                @Override protected void finish() {
     
    8182                                return; // user cancelled download or error occoured
    8283                        Layer layer = new OsmDataLayer(dataSet, "Data Layer", false);
    83                 if (Main.main.getMapFrame() == null)
    84                         Main.main.setMapFrame(new MapFrame(layer));
    85                 else
    86                         Main.main.getMapFrame().mapView.addLayer(layer);
    87                 }
    88            
    89     }
    90 
    91 
    92     private final class DownloadGpsTask extends PleaseWaitRunnable {
    93             private final OsmServerReader reader;
     84                        if (Main.main.getMapFrame() == null)
     85                                Main.main.setMapFrame(new MapFrame(layer));
     86                        else
     87                                Main.main.getMapFrame().mapView.addLayer(layer);
     88                }
     89
     90                @Override protected void cancel() {
     91                        reader.cancel();
     92                }
     93        }
     94
     95
     96        private final class DownloadGpsTask extends PleaseWaitRunnable {
     97                private final OsmServerReader reader;
    9498                private Collection<Collection<GpsPoint>> rawData;
    9599
    96             private DownloadGpsTask(OsmServerReader reader) {
    97                     super("Downloading GPS data");
    98                     this.reader = reader;
    99             }
    100 
    101             @Override public void realRun() throws IOException, JDOMException {
    102                 rawData = reader.parseRawGps();
    103             }
     100                private DownloadGpsTask(OsmServerReader reader) {
     101                        super("Downloading GPS data");
     102                        this.reader = reader;
     103                }
     104
     105                @Override public void realRun() throws IOException, JDOMException {
     106                        rawData = reader.parseRawGps();
     107                }
    104108
    105109                @Override protected void finish() {
     
    108112                        String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText();
    109113                        Layer layer = new RawGpsDataLayer(rawData, name);
    110                 if (Main.main.getMapFrame() == null)
    111                         Main.main.setMapFrame(new MapFrame(layer));
    112                 else
    113                         Main.main.getMapFrame().mapView.addLayer(layer);
    114                 }
    115            
    116     }
    117    
    118    
    119         /**
    120      * minlat, minlon, maxlat, maxlon
    121      */
     114                        if (Main.main.getMapFrame() == null)
     115                                Main.main.setMapFrame(new MapFrame(layer));
     116                        else
     117                                Main.main.getMapFrame().mapView.addLayer(layer);
     118                }
     119
     120                @Override protected void cancel() {
     121                }
     122        }
     123
     124
     125        /**
     126         * minlat, minlon, maxlat, maxlon
     127         */
    122128        JTextField[] latlon = new JTextField[]{
    123129                        new JTextField(9),
     
    136142
    137143        public void actionPerformed(ActionEvent e) {
    138                
    139144                String osmDataServer = Main.pref.get("osm-server.url");
    140145                //TODO: Remove this in later versions (temporary only)
     
    178183                }
    179184                dlg.add(rawGps, GBC.eop());
    180                
     185
    181186                // OSM url edit
    182187                dlg.add(new JLabel("URL from www.openstreetmap.org"), GBC.eol());
     
    221226                                SwingUtilities.invokeLater(new Runnable() {
    222227                                        public void run() {
    223                                                 Map<String, Double> map = readArgs(osmUrl.getText());
    224                                                 try {
    225                                                         double size = 180.0 / Math.pow(2, map.get("zoom"));
    226                                                         Bounds b = new Bounds(
    227                                                                         new LatLon(map.get("lat") - size/2, map.get("lon") - size),
    228                                                                         new LatLon(map.get("lat") + size/2, map.get("lon") + size));
     228                                                Bounds b = osmurl2bounds(osmUrl.getText());
     229                                                if (b != null)
    229230                                                        setEditBounds(b);
    230                                                 } catch (Exception x) { // NPE or IAE
     231                                                else
    231232                                                        for (JTextField f : latlon)
    232233                                                                f.setText("");
    233                                                 }
    234234                                        }
    235235                                });
    236236                        }
    237237                });
    238                
     238
    239239                // Bookmarks
    240240                dlg.add(new JLabel("Bookmarks"), GBC.eol());
     
    289289                wc.setPreferredSize(new Dimension(d.width, d.width/2));
    290290                wc.addInputFields(latlon, osmUrl, osmUrlRefresher);
    291                
     291
    292292                // Finally: the dialog
    293293                Bookmark b;
     
    330330                }
    331331        }
    332        
    333        
    334         /**
    335          * Extrakt URL arguments.
    336          */
    337         private Map<String, Double> readArgs(String s) {
    338                 int i = s.indexOf('?');
     332
     333
     334        public static Bounds osmurl2bounds(String url) {
     335                int i = url.indexOf('?');
    339336                if (i == -1)
    340                         return new HashMap<String, Double>();
    341                 String[] args = s.substring(i+1).split("&");
     337                        return null;
     338                String[] args = url.substring(i+1).split("&");
    342339                HashMap<String, Double> map = new HashMap<String, Double>();
    343340                for (String arg : args) {
     
    350347                        }
    351348                }
    352                 return map;
    353         }
    354        
     349                try {
     350                        double size = 180.0 / Math.pow(2, map.get("zoom"));
     351                        return new Bounds(
     352                                        new LatLon(map.get("lat") - size/2, map.get("lon") - size),
     353                                        new LatLon(map.get("lat") + size/2, map.get("lon") + size));
     354                } catch (Exception x) { // NPE or IAE
     355                        return null;
     356                }
     357        }
     358
    355359        /**
    356360         * Set the four edit fields to the given bounds coordinates.
  • src/org/openstreetmap/josm/actions/JosmAction.java

    r81 r92  
    11package org.openstreetmap.josm.actions;
    22
    3 import java.awt.EventQueue;
    4 import java.io.FileNotFoundException;
    5 import java.io.IOException;
    63
    74import javax.swing.AbstractAction;
    8 import javax.swing.BorderFactory;
    95import javax.swing.JComponent;
    10 import javax.swing.JDialog;
    11 import javax.swing.JLabel;
    12 import javax.swing.JOptionPane;
    136import javax.swing.KeyStroke;
    147
    15 import org.jdom.JDOMException;
    168import org.openstreetmap.josm.Main;
    179import org.openstreetmap.josm.tools.ImageProvider;
    18 import org.xml.sax.SAXException;
    1910
    2011/**
     
    2415abstract public class JosmAction extends AbstractAction {
    2516
    26         /**
    27          * Instanced of this thread will display a "Please Wait" message in middle of JOSM
    28          * to indicate a progress beeing executed.
    29          * 
    30          * @author Imi
    31          */
    32         protected abstract class PleaseWaitRunnable implements Runnable {
    33                 public final JDialog pleaseWaitDlg;
    34                 private String errorMessage;
    35                 /**
    36                  * Create the runnable object with a given message for the user.
    37                  */
    38                 public PleaseWaitRunnable(String msg) {
    39                         pleaseWaitDlg = new JDialog(Main.main, true);
    40                         pleaseWaitDlg.setUndecorated(true);
    41                         JLabel l = new JLabel(msg+". Please Wait.");
    42                         l.setBorder(BorderFactory.createCompoundBorder(
    43                                         BorderFactory.createEtchedBorder(),
    44                                         BorderFactory.createEmptyBorder(20,20,20,20)));
    45                         pleaseWaitDlg.getContentPane().add(l);
    46                         pleaseWaitDlg.pack();
    47                         pleaseWaitDlg.setLocationRelativeTo(Main.main);
    48                         pleaseWaitDlg.setResizable(false);
    49                 }
    50                 public final void run() {
    51                         try {
    52                                 realRun();
    53                 } catch (SAXException x) {
    54                         x.printStackTrace();
    55                         errorMessage = "Error while parsing: "+x.getMessage();
    56                 } catch (JDOMException x) {
    57                         x.printStackTrace();
    58                         errorMessage = "Error while parsing: "+x.getMessage();
    59                 } catch (FileNotFoundException x) {
    60                         x.printStackTrace();
    61                         errorMessage = "URL not found: " + x.getMessage();
    62                 } catch (IOException x) {
    63                         x.printStackTrace();
    64                         errorMessage = x.getMessage();
    65                         } finally {
    66                                 closeDialog();
    67                         }
    68                 }
    69                 /**
    70                  * Called in the worker thread to do the actual work. When any of the
    71                  * exception is thrown, a message box will be displayed and closeDialog
    72                  * is called. finish() is called in any case.
    73                  */
    74                 protected abstract void realRun() throws SAXException, JDOMException, IOException;
    75                 /**
    76                  * Finish up the data work. Is guaranteed to be called if realRun is called.
    77                  * Finish is called in the gui thread just after the dialog disappeared.
    78                  */
    79                 protected void finish() {}
    80                 /**
    81                  * Close the dialog. Usually called from worker thread.
    82                  */
    83                 public void closeDialog() {
    84                         EventQueue.invokeLater(new Runnable(){
    85                                 public void run() {
    86                                         finish();
    87                                         pleaseWaitDlg.setVisible(false);
    88                                         pleaseWaitDlg.dispose();
    89                                         if (errorMessage != null)
    90                                                 JOptionPane.showMessageDialog(Main.main, errorMessage);
    91                 }
    92                         });
    93                 }
    94         }
    95        
    9617        /**
    9718         * Construct the action as menu action entry.
  • src/org/openstreetmap/josm/actions/UploadAction.java

    r90 r92  
    1919import org.openstreetmap.josm.data.osm.OsmPrimitive;
    2020import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     21import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    2122import org.openstreetmap.josm.io.OsmServerWriter;
    2223import org.openstreetmap.josm.tools.GBC;
     
    3839
    3940        public void actionPerformed(ActionEvent e) {
    40                
     41
    4142                String osmDataServer = Main.pref.get("osm-server.url");
    4243                //TODO: Remove this in later versions (temporary only)
     
    5455                        Main.pref.put("osm-server.url", osmDataServer.substring(0, osmDataServer.length()-cutPos));
    5556                }
    56                
     57
    5758                if (!Main.main.getMapFrame().conflictDialog.conflicts.isEmpty()) {
    5859                        JOptionPane.showMessageDialog(Main.main, "There are unresolved conflicts. You have to resolve these first.");
     
    8283                all.addAll(update);
    8384                all.addAll(delete);
    84                
     85
    8586                PleaseWaitRunnable uploadTask = new PleaseWaitRunnable("Uploading data"){
    86                                 @Override protected void realRun() throws JDOMException {
    87                                         server.uploadOsm(all);
    88                                 }
    89                                 @Override protected void finish() {
    90                                         Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
    91                     }
    92                                
    93                         };
     87                        @Override protected void realRun() throws JDOMException {
     88                                server.setProgressInformation(currentAction, progress);
     89                                server.uploadOsm(all);
     90                        }
     91                        @Override protected void finish() {
     92                                Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
     93                        }
     94                        @Override protected void cancel() {
     95                                server.cancel();
     96                        }
     97                };
    9498                Main.worker.execute(uploadTask);
    9599                uploadTask.pleaseWaitDlg.setVisible(true);
Note: See TracChangeset for help on using the changeset viewer.