From c28226249874396c8c001feb15ee8114b59bec3d Mon Sep 17 00:00:00 2001 From: pesckal Date: Thu, 30 Jun 2011 03:56:02 +0000 Subject: [PATCH] Progress bar to indicate progress of downloading the library list --- app/src/processing/app/FileDownloader.java | 2 + app/src/processing/app/LibraryListPanel.java | 74 +++++++++++++----- app/src/processing/app/LibraryListing.java | 22 +++--- app/src/processing/app/LibraryManager.java | 80 ++++++++++++++++---- app/src/processing/app/ProgressMonitor.java | 20 ++++- 5 files changed, 150 insertions(+), 48 deletions(-) diff --git a/app/src/processing/app/FileDownloader.java b/app/src/processing/app/FileDownloader.java index 3076cbd83..860566cb3 100644 --- a/app/src/processing/app/FileDownloader.java +++ b/app/src/processing/app/FileDownloader.java @@ -86,6 +86,8 @@ public class FileDownloader implements Runnable { if (post != null) { post.run(); } + + progressMonitor.finished(); } public File getFile() { diff --git a/app/src/processing/app/LibraryListPanel.java b/app/src/processing/app/LibraryListPanel.java index 4053f446b..0d187d2fc 100644 --- a/app/src/processing/app/LibraryListPanel.java +++ b/app/src/processing/app/LibraryListPanel.java @@ -59,10 +59,11 @@ public class LibraryListPanel extends JPanel implements Scrollable { HashMap libPanelsByInfo; private PreferredViewPositionListener preferredViewPositionListener; - private LibraryManager libraryManager; LibraryListing libraries; - - public LibraryListPanel(LibraryManager libraryManager) { + LibraryManager libraryManager; + JProgressBar setupProgressBar; + + public LibraryListPanel(LibraryManager libraryManager, LibraryListing libraryListing) { super(); this.libraryManager = libraryManager; @@ -74,8 +75,6 @@ public class LibraryListPanel extends JPanel implements Scrollable { }; - libraries = libraryManager.getLibraryListing(null); - setLayout(new GridBagLayout()); setFocusable(true); setOpaque(true); @@ -92,6 +91,40 @@ public class LibraryListPanel extends JPanel implements Scrollable { libPanelsByInfo = new HashMap(); + addMouseListener(new MouseAdapter() { + + public void mousePressed(MouseEvent mouseEvent) { + requestFocusInWindow(); + } + }); + + if (libraryListing == null) { + GridBagConstraints c = new GridBagConstraints(); + c.fill = GridBagConstraints.HORIZONTAL; + c.weightx = 1; + c.weighty = 1; + c.anchor = GridBagConstraints.CENTER; + + setupProgressBar = new JProgressBar(); + setupProgressBar.setString(""); + setupProgressBar.setStringPainted(true); + add(setupProgressBar, c); + } else { + setLibraryList(libraryListing); + } + + } + + public void setLibraryList(LibraryListing libraryListing) { + libraries = libraryListing; + if (setupProgressBar != null) { + remove(setupProgressBar); + setupProgressBar = null; + } + populateLibraryPanels(); + } + + private void populateLibraryPanels() { int row = 0; for (LibraryInfo libInfo : libraries.getAllLibararies()) { GridBagConstraints c = new GridBagConstraints(); @@ -106,26 +139,21 @@ public class LibraryListPanel extends JPanel implements Scrollable { add(libPanel, c); } - addMouseListener(new MouseAdapter() { - - public void mousePressed(MouseEvent mouseEvent) { - requestFocusInWindow(); - } - }); - updateColors(); } - + public void filterLibraries(String category, List filters) { - List hiddenLibraries = libraries.getAllLibararies(); - for (LibraryInfo lib : libraries.getFilteredLibraryList(category, filters)) { - libPanelsByInfo.get(lib).setVisible(true); - hiddenLibraries.remove(lib); - } - - for (LibraryInfo lib : hiddenLibraries) { - libPanelsByInfo.get(lib).setVisible(false); + if (libraries != null) { + List hiddenLibraries = libraries.getAllLibararies(); + for (LibraryInfo lib : libraries.getFilteredLibraryList(category, filters)) { + libPanelsByInfo.get(lib).setVisible(true); + hiddenLibraries.remove(lib); + } + + for (LibraryInfo lib : hiddenLibraries) { + libPanelsByInfo.get(lib).setVisible(false); + } } } @@ -555,4 +583,8 @@ public class LibraryListPanel extends JPanel implements Scrollable { } + public JProgressBar getSetupProgressBar() { + return setupProgressBar; + } + } diff --git a/app/src/processing/app/LibraryListing.java b/app/src/processing/app/LibraryListing.java index e3ea5e0e4..4212a8793 100644 --- a/app/src/processing/app/LibraryListing.java +++ b/app/src/processing/app/LibraryListing.java @@ -111,7 +111,7 @@ public class LibraryListing { } - public static class LibraryListFetcher { + public static class LibraryListFetcher implements Runnable { LibraryListing libListing; @@ -123,8 +123,12 @@ public class LibraryListing { Thread downloaderThread; + ProgressMonitor progressMonitor; public LibraryListFetcher() { + + progressMonitor = new NullProgressMonitor(); + libListing = null; try { File tmpFolder = Base.createTempFolder("libarylist", "download"); @@ -135,13 +139,16 @@ public class LibraryListing { url = new URL("http://dl.dropbox.com/u/700641/libraries.xml"); } catch (IOException e) { - // TODO Auto-generated catch block e.printStackTrace(); } } + + public void setProgressMonitor(ProgressMonitor pm) { + progressMonitor = pm; + } - public void fetchLibraryList(ProgressMonitor pm) { - downloader = new FileDownloader(url, dest, pm); + public void run() { + downloader = new FileDownloader(url, dest, progressMonitor); downloader.setPostOperation(new Runnable() { public void run() { @@ -152,12 +159,7 @@ public class LibraryListing { } }); - downloaderThread = new Thread(downloader); - downloaderThread.start(); - } - - public boolean isDone() { - return !downloaderThread.isAlive(); + downloader.run(); } public LibraryListing getLibraryListing() { diff --git a/app/src/processing/app/LibraryManager.java b/app/src/processing/app/LibraryManager.java index f72d89e5c..1c10ca7e1 100644 --- a/app/src/processing/app/LibraryManager.java +++ b/app/src/processing/app/LibraryManager.java @@ -38,7 +38,7 @@ import javax.swing.event.*; import processing.app.LibraryListPanel.PreferredViewPositionListener; import processing.app.LibraryListing.LibraryListFetcher; -class JProgressMonitor extends AbstractProgressMonitor { +abstract class JProgressMonitor extends AbstractProgressMonitor { JProgressBar progressBar; public JProgressMonitor(JProgressBar progressBar) { @@ -46,6 +46,7 @@ class JProgressMonitor extends AbstractProgressMonitor { } public void startTask(String name, int maxValue) { + isFinished = false; progressBar.setString(name); progressBar.setIndeterminate(maxValue == UNKNOWN); progressBar.setMaximum(maxValue); @@ -56,6 +57,14 @@ class JProgressMonitor extends AbstractProgressMonitor { progressBar.setValue(value); } + @Override + public void finished() { + super.finished(); + finishedAction(); + } + + public abstract void finishedAction(); + } /** @@ -180,7 +189,12 @@ public class LibraryManager { URL url = new URL(libraryUrl.getText()); - final JProgressMonitor pm = new JProgressMonitor(installProgressBar); + final JProgressMonitor pm = new JProgressMonitor(installProgressBar) { + + @Override + public void finishedAction() { + } + }; dialog.addWindowListener(new WindowAdapter() { @@ -252,7 +266,11 @@ public class LibraryManager { pane.add(filterField, c); - libraryListPane = new LibraryListPanel(this); + libraryListPane = new LibraryListPanel(this, libraryListing); + if (libraryListing == null) { + JProgressBar progressBar = libraryListPane.getSetupProgressBar(); + getLibraryListing(progressBar); + } c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; @@ -292,11 +310,8 @@ public class LibraryManager { c.gridx = 1; c.gridy = 2; - ArrayList categories = new ArrayList(getLibraryListing(null).getCategories()); - Collections.sort(categories); - categories.add(0, ANY_CATEGORY); - - categoryChooser = new JComboBox(categories.toArray()); + categoryChooser = new JComboBox(); + updateCategoryChooser(); pane.add(categoryChooser, c); categoryChooser.addItemListener(new ItemListener() { @@ -313,6 +328,22 @@ public class LibraryManager { dialog.setMinimumSize(new Dimension(650, 400)); } + private void updateCategoryChooser() { + ArrayList categories; + if (libraryListing != null) { + categoryChooser.removeAllItems(); + categories = new ArrayList(libraryListing.getCategories()); + Collections.sort(categories); + categories.add(0, ANY_CATEGORY); + } else { + categories = new ArrayList(); + categories.add(0, ANY_CATEGORY); + } + for (String s : categories) { + categoryChooser.addItem(s); + } + } + private void registerDisposeListeners() { dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { @@ -352,16 +383,33 @@ public class LibraryManager { dialog.dispose(); } - public LibraryListing getLibraryListing(ProgressMonitor pm) { + + /** + * @return true if the library listing has already been downloaded + */ + public boolean hasLibraryListing() { + return libraryListing != null; + } + + private LibraryListing getLibraryListing(JProgressBar progressBar) { if (libraryListing == null) { - LibraryListFetcher llf = new LibraryListFetcher(); - llf.fetchLibraryList(pm); + final LibraryListFetcher llf = new LibraryListFetcher(); + llf.setProgressMonitor(new JProgressMonitor(progressBar) { + + @Override + public void finishedAction() { + libraryListing = llf.getLibraryListing(); + synchronized (libraryListing) { + libraryListing = llf.getLibraryListing(); + if (libraryListPane != null) { + libraryListPane.setLibraryList(libraryListing); + } + updateCategoryChooser(); + } + } + }); + new Thread(llf).start(); - // This is dumb. Lets make it better. - while (!llf.isDone()) { - Thread.yield(); - } - libraryListing = llf.getLibraryListing(); } return libraryListing; diff --git a/app/src/processing/app/ProgressMonitor.java b/app/src/processing/app/ProgressMonitor.java index c2358d97e..d8ee2f57a 100644 --- a/app/src/processing/app/ProgressMonitor.java +++ b/app/src/processing/app/ProgressMonitor.java @@ -60,7 +60,17 @@ public interface ProgressMonitor { * Requests for the task to be cancelled by setting isCanceled() to true. */ public void cancel(); - + + /** + * Returns true if this task is complete + */ + public boolean isFinished(); + + /** + * This is called when the current task is finished + */ + public void finished(); + } abstract class AbstractProgressMonitor implements ProgressMonitor { @@ -85,6 +95,14 @@ abstract class AbstractProgressMonitor implements ProgressMonitor { public void cancel() { isCanceled = true; } + + public boolean isFinished() { + return isFinished; + } + + public void finished() { + isFinished = true; + } }