Changeset 1493 in josm for trunk/src


Ignore:
Timestamp:
2009-03-17T18:50:36+01:00 (15 years ago)
Author:
stoecker
Message:

fix #2309 - Exception might get lost when ExecutorService is used

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java

    r1465 r1493  
    1414
    1515import javax.swing.JOptionPane;
     16import javax.swing.SwingUtilities;
    1617
    1718import org.openstreetmap.josm.Main;
     
    3031    private boolean closeDialogCalled = false;
    3132    private boolean cancelled = false;
     33    private boolean ignoreException;
    3234
    3335    private final String title;
     36   
     37    /**
     38     * Create the runnable object with a given message for the user.
     39     */   
     40    public PleaseWaitRunnable(String title) {
     41        this(title, false);
     42    }
    3443
    3544    /**
    3645     * Create the runnable object with a given message for the user.
     46     * @param title Message for user
     47     * @param ignoreException If true, exception will be propaged to calling code. If false then
     48     * exception will be thrown directly in EDT. When this runnable is executed using executor framework
     49     * then use false unless you read result of task (because exception will get lost if you don't)
    3750     */
    38     public PleaseWaitRunnable(String title) {
     51    public PleaseWaitRunnable(String title, boolean ignoreException) {
    3952        this.title = title;
     53        this.ignoreException = ignoreException;
    4054        Main.pleaseWaitDlg.cancel.addActionListener(new ActionListener(){
    4155            public void actionPerformed(ActionEvent e) {
     
    6175    public final void run() {
    6276        try {
    63             if (cancelled)
    64                 return; // since realRun isn't executed, do not call to finish
     77            try {
     78                if (cancelled)
     79                    return; // since realRun isn't executed, do not call to finish
    6580
    66             // reset dialog state
    67             Main.pleaseWaitDlg.setTitle(title);
    68             Main.pleaseWaitDlg.cancel.setEnabled(true);
    69             Main.pleaseWaitDlg.setCustomText("");
    70             errorMessage = null;
    71             closeDialogCalled = false;
     81                // reset dialog state
     82                Main.pleaseWaitDlg.setTitle(title);
     83                Main.pleaseWaitDlg.cancel.setEnabled(true);
     84                Main.pleaseWaitDlg.setCustomText("");
     85                errorMessage = null;
     86                closeDialogCalled = false;
    7287
    73             // show the dialog
    74             synchronized (this) {
    75                 EventQueue.invokeLater(new Runnable() {
     88                // show the dialog
     89                synchronized (this) {
     90                    EventQueue.invokeLater(new Runnable() {
     91                        public void run() {
     92                            synchronized (PleaseWaitRunnable.this) {
     93                                PleaseWaitRunnable.this.notifyAll();
     94                            }
     95                            Main.pleaseWaitDlg.setVisible(true);
     96                        }
     97                    });
     98                    try {wait();} catch (InterruptedException e) {}
     99                }
     100
     101                realRun();
     102            } catch (SAXException x) {
     103                x.printStackTrace();
     104                errorMessage = tr("Error while parsing")+": "+x.getMessage();
     105            } catch (FileNotFoundException x) {
     106                x.printStackTrace();
     107                errorMessage = tr("File not found")+": "+x.getMessage();
     108            } catch (IOException x) {
     109                x.printStackTrace();
     110                errorMessage = x.getMessage();
     111            } finally {
     112                closeDialog();
     113            }
     114        } catch (final Throwable e) {
     115            if (!ignoreException) {
     116                // Exception has to thrown in EDT to be shown to user
     117                SwingUtilities.invokeLater(new Runnable() {
    76118                    public void run() {
    77                         synchronized (PleaseWaitRunnable.this) {
    78                             PleaseWaitRunnable.this.notifyAll();
    79                         }
    80                         Main.pleaseWaitDlg.setVisible(true);
    81                     }
     119                        throw new RuntimeException(e);
     120                    }               
    82121                });
    83                 try {wait();} catch (InterruptedException e) {}
    84122            }
    85 
    86             realRun();
    87         } catch (SAXException x) {
    88             x.printStackTrace();
    89             errorMessage = tr("Error while parsing")+": "+x.getMessage();
    90         } catch (FileNotFoundException x) {
    91             x.printStackTrace();
    92             errorMessage = tr("File not found")+": "+x.getMessage();
    93         } catch (IOException x) {
    94             x.printStackTrace();
    95             errorMessage = x.getMessage();
    96         } finally {
    97             closeDialog();
    98123        }
    99124    }
Note: See TracChangeset for help on using the changeset viewer.