Remove button for installed libraries

This commit is contained in:
pesckal
2011-07-02 07:00:31 +00:00
parent 3140e00666
commit f0e7165c73
3 changed files with 87 additions and 39 deletions
+47 -31
View File
@@ -400,6 +400,9 @@ public class LibraryListPanel extends JPanel implements Scrollable {
}
HyperlinkListener hyperlinkOpener;
ActionListener removeAction;
ActionListener installLibAction;
LibraryInfo libInfo;
JTextPane headerLabel;
@@ -424,6 +427,7 @@ public class LibraryListPanel extends JPanel implements Scrollable {
createAuthorString();
createInstallRemoveActions();
addPaneComponents();
addProgressBarAndButton();
@@ -469,6 +473,45 @@ public class LibraryListPanel extends JPanel implements Scrollable {
descriptionText.addMouseListener(expandPanelMouseListener);
}
private void createInstallRemoveActions() {
removeAction = new ActionListener() {
public void actionPerformed(ActionEvent arg) {
System.out.println("Removing library");
}
};
installLibAction = new ActionListener() {
public void actionPerformed(ActionEvent arg) {
try {
URL url = new URL(libInfo.link);
installProgressBar.setVisible(true);
libraryManager.installLibraryFromUrl(url, new JProgressMonitor(installProgressBar) {
public void finishedAction() {
// Finished downloading library
}
}, new JProgressMonitor(installProgressBar) {
public void finishedAction() {
// Finished installing library
resetInstallProgressBarState();
}
});
} catch (MalformedURLException e) {
Base.showWarning("Install Failed",
"The link fetched from Processing.org is invalid.\n" +
"You can still intall this library manually by visiting\n" +
"the library's website.", e);
}
}
};
}
void stripListeners(JEditorPane editorPane) {
for (MouseListener l : editorPane.getMouseListeners()) {
if (!l.getClass().getName().endsWith("LinkController")) {
@@ -587,9 +630,9 @@ public class LibraryListPanel extends JPanel implements Scrollable {
header.append("<html><body>");
header.append("<b>");
if (libInfo.url == null) {
header.append(libInfo.name);
header.append(libInfo.displayName);
} else {
header.append("<a href=\"" + libInfo.url + "\">" + libInfo.name + "</a>");
header.append("<a href=\"" + libInfo.url + "\">" + libInfo.displayName + "</a>");
}
header.append("</b>");
header.append(createAuthorString());
@@ -663,37 +706,10 @@ public class LibraryListPanel extends JPanel implements Scrollable {
installOrRemove = new JButton();
if (libInfo.isInstalled) {
installOrRemove.setText("Remove");
installOrRemove.addActionListener(removeAction);
} else {
installOrRemove.setText("Install");
ActionListener installLibAction = new ActionListener() {
public void actionPerformed(ActionEvent arg) {
try {
URL url = new URL(libInfo.link);
installProgressBar.setVisible(true);
libraryManager.installLibraryFromUrl(url, new JProgressMonitor(installProgressBar) {
public void finishedAction() {
// Finished downloading library
}
}, new JProgressMonitor(installProgressBar) {
public void finishedAction() {
// Finished installing library
resetInstallProgressBarState();
}
});
} catch (MalformedURLException e) {
Base.showWarning("Install Failed",
"The link fetched from Processing.org is invalid.\n" +
"You can still intall this library manually by visiting\n" +
"the library's website.", e);
}
}
};
installOrRemove.addActionListener(installLibAction);
}
Dimension installButtonDimensions = installOrRemove.getPreferredSize();
+39 -6
View File
@@ -54,6 +54,22 @@ public class LibraryListing {
Collections.sort(allLibraries);
}
public void updateInstalled(List<Library> installed) {
HashSet<String> installedLibraries = new HashSet<String>();
for (Library library : installed) {
installedLibraries.add(library.name.toLowerCase());
}
for (LibraryInfo libInfo : allLibraries) {
for (String name : libInfo.names) {
if (installedLibraries.contains(name.toLowerCase())) {
libInfo.isInstalled = true;
break;
}
}
}
}
public Set<String> getCategories() {
return librariesByCategory.keySet();
@@ -107,7 +123,7 @@ public class LibraryListing {
return libInfo.description.toLowerCase().matches(filter)
|| libInfo.categoryName.toLowerCase().matches(filter)
|| libInfo.name.toLowerCase().matches(filter);
|| libInfo.displayName.toLowerCase().matches(filter);
}
@@ -170,7 +186,7 @@ public class LibraryListing {
public static class LibraryInfo implements Comparable<LibraryInfo> {
public String categoryName;
public String name;
public String displayName;
public String url;
public String description;
@@ -178,13 +194,15 @@ public class LibraryListing {
public String versionId;
public String link;
public ArrayList<String> names;
boolean isInstalled = false;
public String brief;
public LibraryInfo() {
authors = new ArrayList<Author>();
names = new ArrayList<String>();
}
public static class Author {
@@ -195,7 +213,7 @@ public class LibraryListing {
}
public int compareTo(LibraryInfo o) {
return name.toLowerCase().compareTo(o.name.toLowerCase());
return displayName.toLowerCase().compareTo(o.displayName.toLowerCase());
}
}
@@ -252,9 +270,24 @@ public class LibraryListing {
} else if ("library".equals(qName)) {
currentLibInfo = new LibraryInfo();
currentLibInfo.categoryName = currentCategoryName;
currentLibInfo.name = attributes.getValue("name");
currentLibInfo.displayName = attributes.getValue("name");
currentLibInfo.url = attributes.getValue("url");
currentLibInfo.names.add(currentLibInfo.displayName);
} else if ("installed".equals(qName)) {
// The installed tag contains a comma seperated list of the libraries
// that will be installed. This can contain 1 or more names.
// If omitted, the name of the library once installed is assumed to be
// the same as its display name.
String nameList = attributes.getValue("name");
if (nameList != null) {
currentLibInfo.names.clear();
for (String name : nameList.split(",")) {
currentLibInfo.names.add(name.trim());
}
}
} else if ("author".equals(qName)) {
currentAuthor = new Author();
currentAuthor.url = attributes.getValue("url");
+1 -2
View File
@@ -270,7 +270,7 @@ public class LibraryManager {
public void finishedAction() {
libraryListing = llf.getLibraryListing();
synchronized (libraryListing) {
libraryListing = llf.getLibraryListing();
libraryListing.updateInstalled(editor.getMode().contribLibraries);
if (libraryListPane != null) {
libraryListPane.setLibraryList(libraryListing);
}
@@ -381,7 +381,6 @@ public class LibraryManager {
ArrayList<Library> oldLibs = editor.getMode().contribLibraries;
ArrayList<Library> libsToBeBackuped = new ArrayList<Library>();
// Remove any libraries that are already installed.
Iterator<Library> it = newLibs.iterator();
while (it.hasNext()) {
Library lib = it.next();