From 8f2b07bfa7e8f1335562e3d0926e11d5e4795640 Mon Sep 17 00:00:00 2001 From: pesckal Date: Thu, 11 Aug 2011 16:30:42 +0000 Subject: [PATCH] Better handling of contribution manager when disconnected from network. Removed pop-ups, changed when file is downloaded, added boolean to indicate when the advertised list is actually downloaded. --- .../processing/app/ContributionListing.java | 24 ++++-- .../processing/app/ContributionManager.java | 30 +++---- app/src/processing/app/FileDownloader.java | 81 +++++++++---------- 3 files changed, 71 insertions(+), 64 deletions(-) diff --git a/app/src/processing/app/ContributionListing.java b/app/src/processing/app/ContributionListing.java index a80aa2fd8..8c57656b1 100644 --- a/app/src/processing/app/ContributionListing.java +++ b/app/src/processing/app/ContributionListing.java @@ -45,25 +45,28 @@ public class ContributionListing { ArrayList allContributions; - private Comparator contribComparator; + boolean hasDownloadedLatestList; + + static Comparator contribComparator = new Comparator() { + public int compare(Contribution o1, Contribution o2) { + return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase()); + } + }; public ContributionListing() { listeners = new ArrayList(); + advertisedContributions = new ArrayList(); librariesByCategory = new HashMap>(); allContributions = new ArrayList(); - - contribComparator = new Comparator() { - public int compare(Contribution o1, Contribution o2) { - return o1.getName().toLowerCase().compareTo(o2.getName().toLowerCase()); - } - }; + hasDownloadedLatestList = false; } public void setAdvertisedList(File xmlFile) { ContributionXmlParser xmlParser = new ContributionXmlParser(xmlFile); - advertisedContributions = xmlParser.getLibraries(); + advertisedContributions.clear(); + advertisedContributions.addAll(xmlParser.getLibraries()); for (Contribution contribution : advertisedContributions) { addContribution(contribution); } @@ -325,6 +328,10 @@ public class ContributionListing { return false; } + + public boolean hasDownloadedLatestList() { + return hasDownloadedLatestList; + } public static interface ContributionChangeListener { @@ -377,6 +384,7 @@ public class ContributionListing { File xmlFile = downloader.getFile(); if (xmlFile != null) { + hasDownloadedLatestList = true; setAdvertisedList(xmlFile); } } diff --git a/app/src/processing/app/ContributionManager.java b/app/src/processing/app/ContributionManager.java index 9ce2c8c42..ff47e70c8 100644 --- a/app/src/processing/app/ContributionManager.java +++ b/app/src/processing/app/ContributionManager.java @@ -94,25 +94,27 @@ public class ContributionManager { contributionListPanel = new ContributionListPanel(this); contribListing.addContributionListener(contributionListPanel); - - JProgressBar progressBar = contributionListPanel.getSetupProgressBar(); - contribListing.getAdvertisedContributions(new JProgressMonitor(progressBar) { - - @Override - public void finishedAction() { - synchronized (contribListing) { - updateContributionListing(); - updateCategoryChooser(); - - progressBar.setVisible(false); - } - } - }); } protected void showFrame(Editor editor) { this.editor = editor; + if (!contribListing.hasDownloadedLatestList()) { + JProgressBar progressBar = contributionListPanel.getSetupProgressBar(); + contribListing.getAdvertisedContributions(new JProgressMonitor(progressBar) { + + @Override + public void finishedAction() { + synchronized (contribListing) { + updateContributionListing(); + updateCategoryChooser(); + + progressBar.setVisible(false); + } + } + }); + } + updateContributionListing(); if (dialog == null) { diff --git a/app/src/processing/app/FileDownloader.java b/app/src/processing/app/FileDownloader.java index 6813c1cf0..acb0b6e3c 100644 --- a/app/src/processing/app/FileDownloader.java +++ b/app/src/processing/app/FileDownloader.java @@ -73,17 +73,8 @@ public class FileDownloader implements Runnable { } public void run() { - try { - if (downloadFile(url, dest, progressMonitor)) { - libFile = dest; - } - } catch (FileNotFoundException e) { - Base.showWarning("Trouble downloading file", - "The file was not found on the server.\n", null); - } catch (IOException e) { - Base.showWarning("Trouble downloading file", - "An error occured while downloading the file:\n" - + e.getMessage(), e); + if (downloadFile(url, dest, progressMonitor)) { + libFile = dest; } if (post != null) { @@ -104,37 +95,43 @@ public class FileDownloader implements Runnable { * @throws FileNotFoundException */ protected boolean downloadFile(URL source, File dest, - ProgressMonitor progressMonitor) - throws IOException, FileNotFoundException { - - URLConnection urlConn = source.openConnection(); - urlConn.setConnectTimeout(1000); - urlConn.setReadTimeout(5000); - - // String expectedType1 = "application/x-zip-compressed"; - // String expectedType2 = "application/zip"; - // String type = urlConn.getContentType(); - // if (expectedType1.equals(type) || expectedType2.equals(type)) { - // } - - int fileSize = urlConn.getContentLength(); - progressMonitor.startTask("Downloading", fileSize); - - InputStream in = urlConn.getInputStream(); - FileOutputStream out = new FileOutputStream(dest); - - byte[] b = new byte[256]; - int bytesDownloaded = 0, len; - while (!progressMonitor.isCanceled() && (len = in.read(b)) != -1) { - out.write(b, 0, len); - bytesDownloaded += len; - - progressMonitor.setProgress(bytesDownloaded); - } - out.close(); - - if (!progressMonitor.isCanceled()) { - return true; + ProgressMonitor progressMonitor) { + try { + URLConnection urlConn = source.openConnection(); + urlConn.setConnectTimeout(1000); + urlConn.setReadTimeout(5000); + + // String expectedType1 = "application/x-zip-compressed"; + // String expectedType2 = "application/zip"; + // String type = urlConn.getContentType(); + // if (expectedType1.equals(type) || expectedType2.equals(type)) { + // } + + int fileSize = urlConn.getContentLength(); + progressMonitor.startTask("Downloading", fileSize); + + InputStream in = urlConn.getInputStream(); + FileOutputStream out = new FileOutputStream(dest); + + byte[] b = new byte[256]; + int bytesDownloaded = 0, len; + while (!progressMonitor.isCanceled() && (len = in.read(b)) != -1) { + out.write(b, 0, len); + bytesDownloaded += len; + + progressMonitor.setProgress(bytesDownloaded); + } + out.close(); + + if (!progressMonitor.isCanceled()) { + return true; + } + } catch (IOException e) { + System.err.println("An error occured while downloading the file " + + source.toExternalForm()); +// Base.showWarning("Trouble downloading file", +// "An error occured while downloading the file:\n" +// + e.getMessage(), e); } return false;