diff --git a/app/src/processing/app/LibraryListPanel.java b/app/src/processing/app/LibraryListPanel.java index 7aa805188..57e202646 100644 --- a/app/src/processing/app/LibraryListPanel.java +++ b/app/src/processing/app/LibraryListPanel.java @@ -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(""); header.append(""); if (libInfo.url == null) { - header.append(libInfo.name); + header.append(libInfo.displayName); } else { - header.append("" + libInfo.name + ""); + header.append("" + libInfo.displayName + ""); } header.append(""); 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(); diff --git a/app/src/processing/app/LibraryListing.java b/app/src/processing/app/LibraryListing.java index 99372b731..160597adf 100644 --- a/app/src/processing/app/LibraryListing.java +++ b/app/src/processing/app/LibraryListing.java @@ -54,6 +54,22 @@ public class LibraryListing { Collections.sort(allLibraries); } + + public void updateInstalled(List installed) { + HashSet installedLibraries = new HashSet(); + 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 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 { 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 names; boolean isInstalled = false; public String brief; - + public LibraryInfo() { authors = new ArrayList(); + names = new ArrayList(); } 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"); diff --git a/app/src/processing/app/LibraryManager.java b/app/src/processing/app/LibraryManager.java index 2755fbcbd..db095d227 100644 --- a/app/src/processing/app/LibraryManager.java +++ b/app/src/processing/app/LibraryManager.java @@ -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 oldLibs = editor.getMode().contribLibraries; ArrayList libsToBeBackuped = new ArrayList(); - // Remove any libraries that are already installed. Iterator it = newLibs.iterator(); while (it.hasNext()) { Library lib = it.next();