mirror of
https://github.com/processing/processing4.git
synced 2026-06-16 04:26:26 +02:00
Progress bar to indicate progress of downloading the library list
This commit is contained in:
@@ -86,6 +86,8 @@ public class FileDownloader implements Runnable {
|
||||
if (post != null) {
|
||||
post.run();
|
||||
}
|
||||
|
||||
progressMonitor.finished();
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
|
||||
@@ -59,10 +59,11 @@ public class LibraryListPanel extends JPanel implements Scrollable {
|
||||
|
||||
HashMap<LibraryInfo, LibraryPanel> 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<LibraryInfo, LibraryPanel>();
|
||||
|
||||
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<String> filters) {
|
||||
|
||||
List<LibraryInfo> 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<LibraryInfo> 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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<String> categories = new ArrayList<String>(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<String> categories;
|
||||
if (libraryListing != null) {
|
||||
categoryChooser.removeAllItems();
|
||||
categories = new ArrayList<String>(libraryListing.getCategories());
|
||||
Collections.sort(categories);
|
||||
categories.add(0, ANY_CATEGORY);
|
||||
} else {
|
||||
categories = new ArrayList<String>();
|
||||
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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user