Changeset 81 in josm


Ignore:
Timestamp:
2006-04-05T00:31:57+02:00 (18 years ago)
Author:
imi
Message:
  • fixed Exception when downloading empty data and auto-open dialogs
  • fixed Exception when download finished before "Please Wait" appeared
  • added new selection modificator options to search dialog
  • added upload of timestamp
Location:
src/org/openstreetmap/josm
Files:
12 edited

Legend:

Unmodified
Added
Removed
  • src/org/openstreetmap/josm/Main.java

    r80 r81  
    1414import java.util.LinkedList;
    1515import java.util.StringTokenizer;
     16import java.util.concurrent.Executor;
     17import java.util.concurrent.Executors;
    1618
    1719import javax.swing.Action;
     
    5860        public static Main main;
    5961
     62        /**
     63         * The worker thread slave. This is for executing all long and intensive
     64         * calculations. The executed runnables are guaranteed to be executed seperatly
     65         * and sequenciel.
     66         */
     67        public static Executor worker = Executors.newSingleThreadExecutor();
     68       
     69       
    6070        public static Projection proj;
    6171
  • src/org/openstreetmap/josm/actions/DownloadAction.java

    r79 r81  
    1010import java.awt.event.KeyEvent;
    1111import java.awt.event.KeyListener;
    12 import java.io.FileNotFoundException;
    1312import java.io.IOException;
     13import java.util.Collection;
    1414import java.util.HashMap;
    1515import java.util.Map;
     
    4141import org.openstreetmap.josm.gui.layer.OsmDataLayer;
    4242import org.openstreetmap.josm.gui.layer.RawGpsDataLayer;
     43import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
    4344import org.openstreetmap.josm.io.OsmServerReader;
    4445import org.openstreetmap.josm.tools.GBC;
     
    5556public class DownloadAction extends JosmAction {
    5657
    57     /**
     58        /**
     59         * Open the download dialog and download the data.
     60         * Run in the worker thread.
     61         */
     62    private final class DownloadOsmTask extends PleaseWaitRunnable {
     63            private final OsmServerReader reader;
     64                private DataSet dataSet;
     65
     66            private DownloadOsmTask(OsmServerReader reader) {
     67                    super("Downloading data");
     68                    this.reader = reader;
     69            }
     70
     71            @Override
     72            public void realRun() throws IOException, SAXException {
     73                dataSet = reader.parseOsm();
     74                if (dataSet.nodes.isEmpty())
     75                        JOptionPane.showMessageDialog(Main.main, "No data imported.");
     76            }
     77
     78                @Override
     79        protected void finish() {
     80                        if (dataSet == null)
     81                                return; // user cancelled download or error occoured
     82                        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;
     94                private Collection<Collection<GpsPoint>> rawData;
     95
     96            private DownloadGpsTask(OsmServerReader reader) {
     97                    super("Downloading GPS data");
     98                    this.reader = reader;
     99            }
     100
     101            @Override
     102            public void realRun() throws IOException, JDOMException {
     103                rawData = reader.parseRawGps();
     104            }
     105
     106                @Override
     107        protected void finish() {
     108                        if (rawData == null)
     109                                return;
     110                        String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText();
     111                        Layer layer = new RawGpsDataLayer(rawData, name);
     112                if (Main.main.getMapFrame() == null)
     113                        Main.main.setMapFrame(new MapFrame(layer));
     114                else
     115                        Main.main.getMapFrame().mapView.addLayer(layer);
     116                }
     117           
     118    }
     119   
     120   
     121        /**
    58122     * minlat, minlon, maxlat, maxlon
    59123     */
     
    231295               
    232296                // Finally: the dialog
    233                 while(true) {
     297                Bookmark b;
     298                do {
    234299                        int r = JOptionPane.showConfirmDialog(Main.main, dlg, "Choose an area",
    235300                                        JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
    236301                        if (r != JOptionPane.OK_OPTION)
    237302                                return;
    238                         if (startDownload())
    239                                 break;
    240                 }
    241         }
    242 
    243         /**
    244          * Read the values from the edit fields and from the download.
    245          * @return <code>true</code> for a success, <code>false</code> redisplay
    246          */
    247         private boolean startDownload() {
    248                 Bookmark b = readBookmark();
    249                 if (b == null) {
    250                         JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
    251                         return false;
    252                 }
     303                        b = readBookmark();
     304                        if (b == null)
     305                                JOptionPane.showMessageDialog(Main.main, "Please enter the desired coordinates or click on a bookmark.");
     306                } while (b == null);
    253307
    254308                double minlon = b.latlon[0];
     
    257311                double maxlat = b.latlon[3];
    258312                download(rawGps.isSelected(), minlon, minlat, maxlon, maxlat);
    259                 return true;
    260         }
    261        
     313        }
     314
    262315        /**
    263316         * Read a bookmark from the current set edit fields. If one of the fields is
     
    325378         * Do the download for the given area.
    326379         */
    327         public void download(final boolean rawGps, double minlat, double minlon, double maxlat, double maxlon) {
    328                 final OsmServerReader osmReader = new OsmServerReader(minlat, minlon, maxlat, maxlon);
    329                 new Thread(new PleaseWaitRunnable("Downloading data"){
    330                         @Override
    331                         public void realRun() {
    332                                 try {
    333                                         String name = latlon[0].getText() + " " + latlon[1].getText() + " x " + latlon[2].getText() + " " + latlon[3].getText();
    334 
    335                                         Layer layer = null;
    336                                         if (rawGps) {
    337                                                 layer = new RawGpsDataLayer(osmReader.parseRawGps(), name);
    338                                         } else {
    339                                                 DataSet dataSet = osmReader.parseOsm();
    340                                                 if (dataSet == null)
    341                                                         return; // user cancelled download
    342                                                 if (dataSet.nodes.isEmpty()) {
    343                                                         closeDialog();
    344                                                         JOptionPane.showMessageDialog(Main.main,
    345                                                         "No data imported.");
    346                                                 }
    347 
    348                                                 layer = new OsmDataLayer(dataSet, "Data Layer", false);
    349                                         }
    350 
    351                                         if (Main.main.getMapFrame() == null)
    352                                                 Main.main.setMapFrame(new MapFrame(layer));
    353                                         else
    354                                                 Main.main.getMapFrame().mapView.addLayer(layer);
    355                                 } catch (SAXException x) {
    356                                         closeDialog();
    357                                         x.printStackTrace();
    358                                         JOptionPane.showMessageDialog(Main.main, "Error while parsing: "+x.getMessage());
    359                                 } catch (JDOMException x) {
    360                                         closeDialog();
    361                                         x.printStackTrace();
    362                                         JOptionPane.showMessageDialog(Main.main, "Error while parsing: "+x.getMessage());
    363                                 } catch (FileNotFoundException x) {
    364                                         closeDialog();
    365                                         x.printStackTrace();
    366                                         JOptionPane.showMessageDialog(Main.main, "URL not found: " + x.getMessage());
    367                                 } catch (IOException x) {
    368                                         closeDialog();
    369                                         x.printStackTrace();
    370                                         JOptionPane.showMessageDialog(Main.main, x.getMessage());
    371                                 }
    372                         }
    373                 }).start();
     380        public void download(boolean rawGps, double minlat, double minlon, double maxlat, double maxlon) {
     381                OsmServerReader reader = new OsmServerReader(minlat, minlon, maxlat, maxlon);
     382                PleaseWaitRunnable task = rawGps ? new DownloadGpsTask(reader) : new DownloadOsmTask(reader);
     383                Main.worker.execute(task);
     384                task.pleaseWaitDlg.setVisible(true);
    374385        }
    375386}
  • src/org/openstreetmap/josm/actions/JosmAction.java

    r71 r81  
    11package org.openstreetmap.josm.actions;
     2
     3import java.awt.EventQueue;
     4import java.io.FileNotFoundException;
     5import java.io.IOException;
    26
    37import javax.swing.AbstractAction;
     
    610import javax.swing.JDialog;
    711import javax.swing.JLabel;
     12import javax.swing.JOptionPane;
    813import javax.swing.KeyStroke;
    9 import javax.swing.SwingUtilities;
    1014
     15import org.jdom.JDOMException;
    1116import org.openstreetmap.josm.Main;
    1217import org.openstreetmap.josm.tools.ImageProvider;
     18import org.xml.sax.SAXException;
    1319
    1420/**
     
    2531         */
    2632        protected abstract class PleaseWaitRunnable implements Runnable {
    27                 private String msg;
    28                 private JDialog pleaseWaitDlg;
     33                public final JDialog pleaseWaitDlg;
     34                private String errorMessage;
    2935                /**
    3036                 * Create the runnable object with a given message for the user.
    3137                 */
    32                 PleaseWaitRunnable(String msg) {
    33                         this.msg = msg;
    34                 }
    35                 public final void run() {
     38                public PleaseWaitRunnable(String msg) {
    3639                        pleaseWaitDlg = new JDialog(Main.main, true);
    3740                        pleaseWaitDlg.setUndecorated(true);
     
    4245                        pleaseWaitDlg.getContentPane().add(l);
    4346                        pleaseWaitDlg.pack();
    44                         pleaseWaitDlg.setLocation(Main.main.getX()+Main.main.getWidth()/2-pleaseWaitDlg.getWidth()/2,
    45                                         Main.main.getY()+Main.main.getHeight()/2-pleaseWaitDlg.getHeight()/2);
     47                        pleaseWaitDlg.setLocationRelativeTo(Main.main);
    4648                        pleaseWaitDlg.setResizable(false);
    47                         SwingUtilities.invokeLater(new Runnable(){
    48                                 public void run() {
    49                                         pleaseWaitDlg.setVisible(true);
    50                                 }
    51                         });
     49                }
     50                public final void run() {
    5251                        try {
    5352                                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();
    5465                        } finally {
    5566                                closeDialog();
    5667                        }
    5768                }
    58                 public abstract void realRun();
     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                 */
    5983                public void closeDialog() {
    60                         pleaseWaitDlg.setVisible(false);
    61                         pleaseWaitDlg.dispose();
     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                        });
    6293                }
    6394        }
  • src/org/openstreetmap/josm/actions/UploadAction.java

    r77 r81  
    7676                all.addAll(delete);
    7777               
    78                 new Thread(new PleaseWaitRunnable("Uploading data"){
    79                         @Override
    80                         public void realRun() {
    81                                 try {
    82                                         server.uploadOsm(all);
    83                                 } catch (JDOMException x) {
    84                                         closeDialog();
    85                                         x.printStackTrace();
    86                                         JOptionPane.showMessageDialog(Main.main, x.getMessage());
    87                                 }
    88                                 Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
    89                         }
    90                 }).start();
     78                PleaseWaitRunnable uploadTask = new PleaseWaitRunnable("Uploading data"){
     79                                @Override
     80                                protected void realRun() throws JDOMException {
     81                                        server.uploadOsm(all);
     82                                }
     83                                @Override
     84                    protected void finish() {
     85                                        Main.main.getMapFrame().mapView.editLayer().cleanData(server.processed, !add.isEmpty());
     86                    }
     87                               
     88                        };
     89                Main.worker.execute(uploadTask);
     90                uploadTask.pleaseWaitDlg.setVisible(true);
    9191        }
    9292
  • src/org/openstreetmap/josm/data/SelectionTracker.java

    r22 r81  
    9191                        listeners.remove(listener);
    9292        }
    93        
     93
    9494        /**
    9595         * Remember to fire an selection changed event. A call to this will not fire
  • src/org/openstreetmap/josm/data/osm/OsmPrimitive.java

    r68 r81  
    33import java.util.Collection;
    44import java.util.Collections;
     5import java.util.Date;
    56import java.util.HashMap;
    67import java.util.Map;
     
    6263
    6364        /**
     65         * Time of last modification to this object. This is not set by JOSM but
     66         * read from the server and delivered back to the server unmodified. It is
     67         * used to check against edit conflicts.
     68         */
     69        public Date lastModified = null;
     70
     71        /**
    6472         * Implementation of the visitor scheme. Subclases have to call the correct
    6573         * visitor function.
     
    190198                deleted = osm.deleted;
    191199                selected = osm.selected;
     200                lastModified = osm.lastModified;
    192201        }
    193202}
  • src/org/openstreetmap/josm/gui/PreferenceDialog.java

    r79 r81  
    335335                setModal(true);
    336336                pack();
    337                 Dimension s = Main.main.getSize();
    338                 setLocation(Main.main.getX()+s.width/2-getWidth()/2, Main.main.getY()+s.height/2-getHeight()/2);
     337                setLocationRelativeTo(Main.main);
    339338        }
    340339
  • src/org/openstreetmap/josm/gui/dialogs/SelectionListDialog.java

    r79 r81  
    33import java.awt.BorderLayout;
    44import java.awt.Dimension;
     5import java.awt.GridBagLayout;
    56import java.awt.GridLayout;
    67import java.awt.event.ActionEvent;
     
    1112import java.util.Collection;
    1213
     14import javax.swing.ButtonGroup;
    1315import javax.swing.DefaultListModel;
    1416import javax.swing.JButton;
     
    1719import javax.swing.JOptionPane;
    1820import javax.swing.JPanel;
     21import javax.swing.JRadioButton;
    1922import javax.swing.JScrollPane;
     23import javax.swing.JTextField;
    2024import javax.swing.ListSelectionModel;
    2125
     
    2529import org.openstreetmap.josm.gui.MapFrame;
    2630import org.openstreetmap.josm.gui.OsmPrimitivRenderer;
     31import org.openstreetmap.josm.tools.GBC;
    2732import org.openstreetmap.josm.tools.ImageProvider;
    2833import org.openstreetmap.josm.tools.SearchCompiler;
     
    8287                        private String lastSearch = "";
    8388                        public void actionPerformed(ActionEvent e) {
    84                                 JLabel l = new JLabel("Please enter a search string.");
    85                                 l.setToolTipText("<html>Fulltext search.<ul>" +
     89                                JLabel label = new JLabel("Please enter a search string.");
     90                                final JTextField input = new JTextField(lastSearch);
     91                                input.setToolTipText("<html>Fulltext search.<ul>" +
    8692                                                "<li><code>Baker Street</code>  - 'Baker' and 'Street' in any key or name.</li>" +
    8793                                                "<li><code>\"Baker Street\"</code>  - 'Baker Street' in any key or name.</li>" +
     
    9096                                                "<li><code>foot:</code>  - key=foot set to any value." +
    9197                                                "</ul></html>");
    92                                 lastSearch = (String)JOptionPane.showInputDialog(Main.main,l,"Search",JOptionPane.INFORMATION_MESSAGE,null,null,lastSearch);
    93                                 if (lastSearch == null)
     98
     99                                JRadioButton replace = new JRadioButton("replace selection", true);
     100                                JRadioButton add = new JRadioButton("add to selection", false);
     101                                JRadioButton remove = new JRadioButton("remove from selection", false);
     102                                ButtonGroup bg = new ButtonGroup();
     103                                bg.add(replace);
     104                                bg.add(add);
     105                                bg.add(remove);
     106
     107                                JPanel p = new JPanel(new GridBagLayout());
     108                                p.add(label, GBC.eop());
     109                                p.add(input, GBC.eop().fill(GBC.HORIZONTAL));
     110                                p.add(replace, GBC.eol());
     111                                p.add(add, GBC.eol());
     112                                p.add(remove, GBC.eol());
     113                                JOptionPane pane = new JOptionPane(p, JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_CANCEL_OPTION, null){
     114                                        @Override public void selectInitialValue() {
     115                                                input.requestFocusInWindow();
     116                    }
     117                                };
     118                                pane.createDialog(Main.main, "Search").setVisible(true);
     119                                System.out.println(pane.getValue());
     120                                if (!Integer.valueOf(JOptionPane.OK_OPTION).equals(pane.getValue()))
    94121                                        return;
     122                                lastSearch = input.getText();
    95123                                SearchCompiler.Match matcher = SearchCompiler.compile(lastSearch);
    96                                 for (OsmPrimitive osm : Main.main.ds.allNonDeletedPrimitives())
    97                                         osm.setSelected(matcher.match(osm));
     124                                for (OsmPrimitive osm : Main.main.ds.allNonDeletedPrimitives()) {
     125                                        if (replace.isSelected())
     126                                                osm.setSelected(matcher.match(osm));
     127                                        else if (add.isSelected() && !osm.isSelected() && matcher.match(osm))
     128                                                osm.setSelected(true);
     129                                        else if (remove.isSelected() && osm.isSelected() && matcher.match(osm))
     130                                                osm.setSelected(false);
     131                                }
    98132                                selectionChanged(Main.main.ds.getSelected());
    99133                                Main.main.getMapFrame().repaint();
  • src/org/openstreetmap/josm/gui/dialogs/ToggleDialog.java

    r80 r81  
    4444                        }
    4545                };
    46                
    4746                setLayout(new BorderLayout());
    4847                add(new JLabel(title), BorderLayout.NORTH);
  • src/org/openstreetmap/josm/io/OsmReader.java

    r79 r81  
    33import java.io.IOException;
    44import java.io.Reader;
     5import java.text.ParseException;
     6import java.text.SimpleDateFormat;
    57import java.util.HashMap;
    68import java.util.Map;
     
    8284                                nodes.put(n.id, n);
    8385                        } else if (qName.equals("segment")) {
    84                                 current = new LineSegment(
    85                                                 nodes.get(getLong(atts, "from")),
    86                                                 nodes.get(getLong(atts, "to")));
     86                                Node from = nodes.get(getLong(atts, "from"));
     87                                Node to = nodes.get(getLong(atts, "to"));
     88                                if (from == null || to == null)
     89                                        throw new SAXException("Segment "+atts.getValue("id")+" is missing its nodes.");
     90                                current = new LineSegment(from, to);
    8791                                readCommon(atts);
    8892                                lineSegments.put(current.id, (LineSegment)current);
     
    103107                                        ((Way)current).segments.add(ls);
    104108                                }
    105                         } else if (qName.equals("tag")) {
     109                        } else if (qName.equals("tag"))
    106110                                current.put(atts.getValue("k"), atts.getValue("v"));
    107                         }
    108111                } catch (NumberFormatException x) {
    109112            x.printStackTrace(); // SAXException does not chain correctly
     
    129132                if (current.id == 0)
    130133                        throw new SAXException("Illegal object with id=0");
     134               
     135                String time = atts.getValue("timestamp");
     136                if (time != null && time.length() != 0) {
     137                        try {
     138                    current.lastModified = SimpleDateFormat.getDateTimeInstance().parse(time);
     139            } catch (ParseException e) {
     140                    e.printStackTrace();
     141                    throw new SAXException("Couldn't read time format '"+time+"'.");
     142            }
     143                }
     144               
    131145                String action = atts.getValue("action");
    132146                if ("delete".equals(action))
  • src/org/openstreetmap/josm/io/OsmWriter.java

    r72 r81  
    33import java.io.PrintWriter;
    44import java.io.Writer;
     5import java.text.SimpleDateFormat;
    56import java.util.HashMap;
    67import java.util.Map.Entry;
     
    140141                                out.print(" action='"+action+"'");
    141142                }
     143                if (osm.lastModified != null) {
     144                        String time = SimpleDateFormat.getDateTimeInstance().format(osm.lastModified);
     145                        out.print(" timestamp='"+time+"'");
     146                }
    142147        }
    143148}
  • src/org/openstreetmap/josm/tools/TileCache.java

    r80 r81  
    174174                currentlyLoading.add(tileId*256+zoom);
    175175                // load from network
    176                 new Thread(){
    177                     @Override
     176                Main.worker.execute(new Runnable(){
    178177                    public void run() {
    179178                        try {
     
    195194                        }       
    196195                    }
    197                 }.start();
     196                });
    198197            }
    199198        } else {
Note: See TracChangeset for help on using the changeset viewer.