Changeset 6257 in josm for trunk/src/org/openstreetmap/josm


Ignore:
Timestamp:
2013-09-25T01:19:21+02:00 (11 years ago)
Author:
Don-vip
Message:

fix #8862, see #9101 - UI message when plugin list/icons download fail

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/plugins/ReadRemotePluginInformationTask.java

    r6248 r6257  
    44import static org.openstreetmap.josm.tools.I18n.tr;
    55
     6import java.awt.Dimension;
     7import java.awt.GridBagLayout;
    68import java.io.BufferedReader;
    79import java.io.ByteArrayInputStream;
     
    2729import java.util.List;
    2830
     31import javax.swing.JLabel;
     32import javax.swing.JOptionPane;
     33import javax.swing.JPanel;
     34import javax.swing.JScrollPane;
     35
    2936import org.openstreetmap.josm.Main;
    3037import org.openstreetmap.josm.gui.PleaseWaitRunnable;
    3138import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
    3239import org.openstreetmap.josm.gui.progress.ProgressMonitor;
     40import org.openstreetmap.josm.gui.util.GuiHelper;
     41import org.openstreetmap.josm.gui.widgets.JosmTextArea;
    3342import org.openstreetmap.josm.io.OsmTransferException;
     43import org.openstreetmap.josm.tools.GBC;
    3444import org.openstreetmap.josm.tools.ImageProvider;
    3545import org.openstreetmap.josm.tools.Utils;
     
    3747
    3848/**
    39  * An asynchronous task for downloading plugin lists from the configured plugin download
    40  * sites.
    41  *
     49 * An asynchronous task for downloading plugin lists from the configured plugin download sites.
     50 * @since 2817
    4251 */
    43 public class ReadRemotePluginInformationTask extends PleaseWaitRunnable{
     52public class ReadRemotePluginInformationTask extends PleaseWaitRunnable {
    4453
    4554    private Collection<String> sites;
     
    143152     * @return the downloaded list
    144153     */
    145     protected String downloadPluginList(String site, ProgressMonitor monitor) {
     154    protected String downloadPluginList(String site, final ProgressMonitor monitor) {
    146155        BufferedReader in = null;
    147         StringBuilder sb = new StringBuilder();
     156        String line;
    148157        try {
    149158            /* replace %<x> with empty string or x=plugins (separated with comma) */
     
    166175            }
    167176            in = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
    168             String line;
    169             while((line = in.readLine()) != null) {
     177            StringBuilder sb = new StringBuilder();
     178            while ((line = in.readLine()) != null) {
    170179                sb.append(line).append("\n");
    171180            }
    172181            return sb.toString();
    173         } catch(MalformedURLException e) {
     182        } catch (MalformedURLException e) {
    174183            if (canceled) return null;
    175184            e.printStackTrace();
    176185            return null;
    177         } catch(IOException e) {
     186        } catch (IOException e) {
    178187            if (canceled) return null;
    179             e.printStackTrace();
     188            handleIOException(monitor, e, tr("Plugin list download error"), tr("JOSM failed to download plugin list:"));
    180189            return null;
    181190        } finally {
     
    190199        }
    191200    }
     201   
     202    private void handleIOException(final ProgressMonitor monitor, IOException e, final String title, String firstMessage) {
     203        InputStream errStream = connection.getErrorStream();
     204        StringBuilder sb = new StringBuilder();
     205        if (errStream != null) {
     206            BufferedReader err = null;
     207            try {
     208                String line;
     209                err = new BufferedReader(new InputStreamReader(errStream, "UTF-8"));
     210                while ((line = err.readLine()) != null) {
     211                    sb.append(line).append("\n");
     212                }
     213            } catch (Exception ex) {
     214                Main.error(e);
     215                Main.error(ex);
     216            } finally {
     217                Utils.close(err);
     218            }
     219        }
     220        final String msg = e.getMessage();
     221        final String details = sb.toString();
     222        Main.error(details.isEmpty() ? msg : msg + " - Details:\n" + details);
     223       
     224        GuiHelper.runInEDTAndWait(new Runnable() {
     225            @Override public void run() {
     226                JPanel panel = new JPanel(new GridBagLayout());
     227                panel.add(new JLabel(tr("JOSM failed to download plugin list:")), GBC.eol().insets(0, 0, 0, 10));
     228                StringBuilder b = new StringBuilder();
     229                for (String part : msg.split("(?<=\\G.{200})")) {
     230                    b.append(part).append("\n");
     231                }
     232                panel.add(new JLabel("<html><body width=\"500\"><b>"+b.toString().trim()+"</b></body></html>"), GBC.eol().insets(0, 0, 0, 10));
     233                if (!details.isEmpty()) {
     234                    panel.add(new JLabel(tr("Details:")), GBC.eol().insets(0, 0, 0, 10));
     235                    JosmTextArea area = new JosmTextArea(details);
     236                    area.setEditable(false);
     237                    area.setLineWrap(true); 
     238                    area.setWrapStyleWord(true);
     239                    JScrollPane scrollPane = new JScrollPane(area);
     240                    scrollPane.setPreferredSize(new Dimension(500, 300));
     241                    panel.add(scrollPane, GBC.eol().fill());
     242                }
     243                JOptionPane.showMessageDialog(monitor.getWindowParent(), panel, title, JOptionPane.ERROR_MESSAGE);
     244            }
     245        });
     246    }
    192247
    193248    /**
     
    217272                out.write(buffer, 0, read);
    218273            }
    219         } catch(MalformedURLException e) {
     274        } catch (MalformedURLException e) {
    220275            if (canceled) return;
    221276            e.printStackTrace();
    222277            return;
    223         } catch(IOException e) {
     278        } catch (IOException e) {
    224279            if (canceled) return;
    225             e.printStackTrace();
     280            handleIOException(monitor, e, tr("Plugin icons download error"), tr("JOSM failed to download plugin icons:"));
    226281            return;
    227282        } finally {
Note: See TracChangeset for help on using the changeset viewer.