Ticket #2775: plugindownload.patch

File plugindownload.patch, 9.3 KB (added by jttt, 16 years ago)
  • src/org/openstreetmap/josm/actions/PreferencesAction.java

     
    3636     * Launch the preferences dialog.
    3737     */
    3838    public void actionPerformed(ActionEvent e) {
    39         new Thread(this).start();
     39        run();
    4040    }
    4141
    4242    public void run() {
     
    7474
    7575        dlg.setBounds(targetX, targetY, targetWidth, targetHeight);
    7676
     77        dlg.setModal(true);
    7778        dlg.setVisible(true);
    7879        if (pane.getValue() instanceof Integer && (Integer)pane.getValue() == JOptionPane.OK_OPTION)
    7980            prefDlg.ok();
  • src/org/openstreetmap/josm/plugins/PluginDownloader.java

     
    1010import java.io.BufferedReader;
    1111import java.io.BufferedWriter;
    1212import java.io.File;
    13 import java.io.FileNotFoundException;
    1413import java.io.FileOutputStream;
    15 import java.io.FileWriter;
    1614import java.io.FilenameFilter;
    1715import java.io.IOException;
    1816import java.io.InputStream;
    1917import java.io.InputStreamReader;
    2018import java.io.OutputStream;
    2119import java.io.OutputStreamWriter;
    22 import java.net.MalformedURLException;
    2320import java.net.URL;
    2421import java.util.Arrays;
    25 import java.util.concurrent.Future;
    2622import java.util.Collection;
    2723import java.util.LinkedList;
    2824
    2925import javax.swing.JOptionPane;
    3026
    31 import org.openstreetmap.josm.actions.AboutAction;
    3227import org.openstreetmap.josm.Main;
     28import org.openstreetmap.josm.actions.AboutAction;
    3329import org.openstreetmap.josm.gui.ExtendedDialog;
    3430import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3531import org.xml.sax.SAXException;
     
    6561            File pluginDir = Main.pref.getPluginsDirFile();
    6662            if (!pluginDir.exists())
    6763                pluginDir.mkdirs();
     64            Main.pleaseWaitDlg.progress.setMaximum(toUpdate.size());
     65            int progressValue = 0;
    6866            for (PluginInformation d : toUpdate) {
     67                Main.pleaseWaitDlg.progress.setValue(progressValue++);
    6968                Main.pleaseWaitDlg.currentAction.setText(tr("Downloading Plugin {0}...", d.name));
    7069                File pluginFile = new File(pluginDir, d.name + ".jar.new");
    7170                if(download(d, pluginFile))
     
    144143    }
    145144
    146145    public Collection<PluginInformation> download(Collection<PluginInformation> download) {
     146        // Execute task in current thread instead of executing it in other thread and waiting for result
     147        // Waiting for result is not a good idea because the waiting thread will probably be either EDT
     148        // or worker thread. Blocking one of these threads will cause deadlock
    147149        UpdateTask t = new UpdateTask(download, false);
    148         try {
    149             Future<UpdateTask> ta = Main.worker.submit(t, t);
    150             t = ta.get();
    151             return t.failed;
    152         }
    153         catch(java.lang.InterruptedException e) {e.printStackTrace();}
    154         catch(java.util.concurrent.ExecutionException e) {e.printStackTrace();}
    155         return download;
     150        t.run();
     151        return t.failed;
    156152    }
    157153
    158154    public static boolean moveUpdatedPlugins() {
  • src/org/openstreetmap/josm/plugins/PluginSelection.java

     
    88import java.awt.Insets;
    99import java.awt.event.ActionEvent;
    1010import java.awt.event.ActionListener;
    11 
    1211import java.io.BufferedReader;
    1312import java.io.ByteArrayInputStream;
    1413import java.io.File;
    1514import java.io.FileInputStream;
    1615import java.io.InputStreamReader;
    17 import java.io.IOException;
    1816import java.util.Arrays;
    1917import java.util.Collection;
    2018import java.util.Collections;
     
    9694        drawPanel(pluginPanel);
    9795    }
    9896
    99     public Boolean finish() {
     97    public boolean finish() {
    10098        Collection<PluginInformation> toDownload = new LinkedList<PluginInformation>();
    10199        String msg = "";
    102100        for (Entry<String, Boolean> entry : pluginMap.entrySet()) {
  • src/org/openstreetmap/josm/gui/PleaseWaitRunnable.java

     
    88import java.awt.event.ActionListener;
    99import java.awt.event.WindowAdapter;
    1010import java.awt.event.WindowEvent;
     11import java.awt.event.WindowListener;
    1112import java.io.FileNotFoundException;
    1213import java.io.IOException;
    1314import java.lang.reflect.InvocationTargetException;
     
    3536
    3637    private final String title;
    3738
     39    private ActionListener cancelListener = new ActionListener(){
     40        public void actionPerformed(ActionEvent e) {
     41            if (!cancelled) {
     42                cancelled = true;
     43                cancel();
     44            }
     45        }
     46    };
     47
     48    private WindowListener windowListener = new WindowAdapter(){
     49        @Override public void windowClosing(WindowEvent e) {
     50            if (!closeDialogCalled) {
     51                if (!cancelled) {
     52                    cancelled = true;
     53                    cancel();
     54                }
     55                closeDialog();
     56            }
     57        }
     58    };
     59
    3860    /**
    3961     * Create the runnable object with a given message for the user.
    4062     */
     
    5274    public PleaseWaitRunnable(String title, boolean ignoreException) {
    5375        this.title = title;
    5476        this.ignoreException = ignoreException;
    55         Main.pleaseWaitDlg.cancel.addActionListener(new ActionListener(){
    56             public void actionPerformed(ActionEvent e) {
    57                 if (!cancelled) {
    58                     cancelled = true;
    59                     cancel();
    60                 }
    61             }
    62         });
    63         Main.pleaseWaitDlg.addWindowListener(new WindowAdapter(){
    64             @Override public void windowClosing(WindowEvent e) {
    65                 if (!closeDialogCalled) {
    66                     if (!cancelled) {
    67                         cancelled = true;
    68                         cancel();
    69                     }
    70                     closeDialog();
    71                 }
    72             }
    73         });
    7477    }
    7578
    76     public final void run() {
    77         try {
    78             try {
    79                 if (cancelled)
    80                     return; // since realRun isn't executed, do not call to finish
     79    private void prepareDialog() {
     80        // reset dialog state
     81        errorMessage = null;
     82        closeDialogCalled = false;
    8183
    82                 // reset dialog state
    83                 Main.pleaseWaitDlg.setTitle(title);
    84                 Main.pleaseWaitDlg.cancel.setEnabled(true);
    85                 Main.pleaseWaitDlg.setCustomText("");
    86                 errorMessage = null;
    87                 closeDialogCalled = false;
    88 
    89                 // show the dialog
    90                 synchronized (this) {
    91                     EventQueue.invokeLater(new Runnable() {
    92                         public void run() {
    93                             synchronized (PleaseWaitRunnable.this) {
    94                                 PleaseWaitRunnable.this.notifyAll();
    95                             }
    96                             Main.pleaseWaitDlg.setVisible(true);
    97                         }
    98                     });
    99                     try {wait();} catch (InterruptedException e) {}
    100                 }
     84        Main.pleaseWaitDlg.setTitle(title);
     85        Main.pleaseWaitDlg.cancel.setEnabled(true);
     86        Main.pleaseWaitDlg.setCustomText("");
     87        Main.pleaseWaitDlg.cancel.addActionListener(cancelListener);
     88        Main.pleaseWaitDlg.addWindowListener(windowListener);
     89        Main.pleaseWaitDlg.setVisible(true);
     90    }
    10191
     92    private void doRealRun() {
     93        try {
     94            try {
    10295                realRun();
    10396            } catch (SAXException x) {
    10497                x.printStackTrace();
     
    131124        }
    132125    }
    133126
     127    public final void run() {
     128        if (cancelled)
     129            return; // since realRun isn't executed, do not call to finish
     130
     131        if (EventQueue.isDispatchThread()) {
     132            new Thread(new Runnable() {
     133                public void run() {
     134                    doRealRun();
     135                }
     136            }).start();
     137            prepareDialog();
     138        } else {
     139            EventQueue.invokeLater(new Runnable() {
     140                public void run() {
     141                    prepareDialog();
     142                }
     143            });
     144            doRealRun();
     145        }
     146    }
     147
    134148    /**
    135149     * User pressed cancel button.
    136150     */
     
    164178                    } finally {
    165179                        Main.pleaseWaitDlg.setVisible(false);
    166180                        Main.pleaseWaitDlg.dispose();
     181                        Main.pleaseWaitDlg.removeWindowListener(windowListener);
     182                        Main.pleaseWaitDlg.cancel.removeActionListener(cancelListener);
    167183                    }
    168184                    if (errorMessage != null && !silent) {
    169185                        JOptionPane.showMessageDialog(Main.parent, errorMessage);