1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-02-07 01:54:26 +01:00

LibraryManager: better type filtering

This commit is contained in:
Federico Fissore 2015-03-26 17:11:04 +01:00
parent 74a8ccdeb4
commit 6e498ee5b9
14 changed files with 181 additions and 76 deletions

View File

@ -0,0 +1,39 @@
package cc.arduino.contributions.libraries.filters;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.LibrariesIndex;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import java.util.Collection;
public class InstalledLibraryPredicate implements Predicate<ContributedLibrary> {
private final LibrariesIndex index;
public InstalledLibraryPredicate(LibrariesIndex index) {
this.index = index;
}
@Override
public boolean apply(ContributedLibrary input) {
if (input.isInstalled()) {
return true;
}
Collection<ContributedLibrary> installed = Collections2.filter(index.find(input.getName()), new Predicate<ContributedLibrary>() {
@Override
public boolean apply(ContributedLibrary input) {
return input.isInstalled();
}
});
return !installed.isEmpty();
}
@Override
public boolean equals(Object obj) {
return obj instanceof InstalledLibraryPredicate;
}
}

View File

@ -33,6 +33,7 @@ import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.libraries.ContributedLibrary; import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryComparator; import cc.arduino.contributions.libraries.ContributedLibraryComparator;
import cc.arduino.contributions.libraries.filters.BuiltInPredicate; import cc.arduino.contributions.libraries.filters.BuiltInPredicate;
import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate;
import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate; import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate;
import cc.arduino.contributions.ui.InstallerTableCell; import cc.arduino.contributions.ui.InstallerTableCell;
import cc.arduino.contributions.ui.listeners.DelegatingKeyListener; import cc.arduino.contributions.ui.listeners.DelegatingKeyListener;
@ -65,6 +66,7 @@ import static processing.app.I18n.format;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ContributedLibraryTableCell extends InstallerTableCell { public class ContributedLibraryTableCell extends InstallerTableCell {
private final LibraryManagerUI indexer;
private JPanel panel; private JPanel panel;
private JButton installButton; private JButton installButton;
private Component installButtonPlaceholder; private Component installButtonPlaceholder;
@ -75,7 +77,9 @@ public class ContributedLibraryTableCell extends InstallerTableCell {
private JPanel inactiveButtonsPanel; private JPanel inactiveButtonsPanel;
private JLabel statusLabel; private JLabel statusLabel;
public ContributedLibraryTableCell() { public ContributedLibraryTableCell(LibraryManagerUI indexer) {
this.indexer = indexer;
{ {
installButton = new JButton(_("Install")); installButton = new JButton(_("Install"));
installButton.addActionListener(new ActionListener() { installButton.addActionListener(new ActionListener() {

View File

@ -0,0 +1,27 @@
package cc.arduino.contributions.libraries.ui;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.LibrariesIndex;
import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate;
import cc.arduino.contributions.ui.DropdownItem;
import com.google.common.base.Predicate;
import static processing.app.I18n._;
public class DropdownInstalledLibraryItem implements DropdownItem<ContributedLibrary> {
private final LibrariesIndex index;
public DropdownInstalledLibraryItem(LibrariesIndex index) {
this.index = index;
}
public String toString() {
return _("Installed");
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new InstalledLibraryPredicate(index);
}
}

View File

@ -39,7 +39,6 @@ import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import static processing.app.I18n._; import static processing.app.I18n._;
@ -62,12 +61,12 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
@Override @Override
protected InstallerTableCell createCellRenderer() { protected InstallerTableCell createCellRenderer() {
return new ContributedLibraryTableCell(); return new ContributedLibraryTableCell(this);
} }
@Override @Override
protected InstallerTableCell createCellEditor() { protected InstallerTableCell createCellEditor() {
return new ContributedLibraryTableCell() { return new ContributedLibraryTableCell(this) {
@Override @Override
protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) { protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) {
if (selectedLibrary.isReadOnly()) { if (selectedLibrary.isReadOnly()) {
@ -150,7 +149,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
typeFilter = null; typeFilter = null;
typeChooser.removeAllItems(); typeChooser.removeAllItems();
typeChooser.addItem(new DropdownAllItem()); typeChooser.addItem(new DropdownAllItem());
typeChooser.addItem(new DropdownInstalledContributionItem()); typeChooser.addItem(new DropdownInstalledLibraryItem(indexer.getIndex()));
Collection<String> types = indexer.getIndex().getTypes(); Collection<String> types = indexer.getIndex().getTypes();
for (String type : types) { for (String type : types) {
typeChooser.addItem(new DropdownLibraryOfTypeItem(type)); typeChooser.addItem(new DropdownLibraryOfTypeItem(type));
@ -169,6 +168,10 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
}; };
} }
public LibrariesIndexer getIndexer() {
return indexer;
}
public void setProgress(Progress progress) { public void setProgress(Progress progress) {
progressBar.setValue(progress); progressBar.setValue(progress);
} }

View File

@ -1,20 +0,0 @@
package cc.arduino.contributions.ui;
import cc.arduino.contributions.filters.InstalledPredicate;
import cc.arduino.contributions.packages.DownloadableContribution;
import cc.arduino.contributions.ui.DropdownItem;
import com.google.common.base.Predicate;
import static processing.app.I18n._;
public class DropdownInstalledContributionItem implements DropdownItem<DownloadableContribution> {
public String toString() {
return _("Installed");
}
@Override
public Predicate<DownloadableContribution> getFilterPredicate() {
return new InstalledPredicate();
}
}

View File

@ -26,19 +26,33 @@
* invalidate any other reasons why the executable file might be covered by * invalidate any other reasons why the executable file might be covered by
* the GNU General Public License. * the GNU General Public License.
*/ */
package cc.arduino.contributions.libraries; package cc.arduino.contributions.libraries;
import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import java.util.*; import java.util.*;
public abstract class LibrariesIndex { public abstract class LibrariesIndex {
public abstract List<ContributedLibrary> getLibraries(); public abstract List<ContributedLibrary> getLibraries();
public List<ContributedLibrary> find(final String name) {
return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new Predicate<ContributedLibrary>() {
@Override
public boolean apply(ContributedLibrary contributedLibrary) {
return name.equals(contributedLibrary.getName());
}
}));
}
public ContributedLibrary find(String name, String version) { public ContributedLibrary find(String name, String version) {
for (ContributedLibrary lib : getLibraries()) { for (ContributedLibrary lib : find(name)) {
if (lib.getName().equals(name) && lib.getVersion().equals(version)) if (lib.getVersion().equals(version)) {
return lib; return lib;
} }
}
return null; return null;
} }

View File

@ -28,14 +28,9 @@
*/ */
package cc.arduino.contributions.libraries; package cc.arduino.contributions.libraries;
import static processing.app.I18n._; import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File; import com.fasterxml.jackson.module.mrbean.MrBeanModule;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import processing.app.BaseNoGui; import processing.app.BaseNoGui;
import processing.app.I18n; import processing.app.I18n;
import processing.app.helpers.FileUtils; import processing.app.helpers.FileUtils;
@ -44,9 +39,14 @@ import processing.app.packages.LegacyUserLibrary;
import processing.app.packages.LibraryList; import processing.app.packages.LibraryList;
import processing.app.packages.UserLibrary; import processing.app.packages.UserLibrary;
import com.fasterxml.jackson.databind.DeserializationFeature; import java.io.File;
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.FileInputStream;
import com.fasterxml.jackson.module.mrbean.MrBeanModule; import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import static processing.app.I18n._;
public class LibrariesIndexer { public class LibrariesIndexer {
@ -119,21 +119,18 @@ public class LibrariesIndexer {
try { try {
scanLibrary(subfolder); scanLibrary(subfolder);
} catch (IOException e) { } catch (IOException e) {
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"), System.out.println(I18n.format(_("Invalid library found in {0}: {1}"), subfolder, e.getMessage()));
subfolder, e.getMessage()));
} }
} }
} }
private void scanLibrary(File folder) throws IOException { private void scanLibrary(File folder) throws IOException {
boolean readOnly = !FileUtils boolean readOnly = !FileUtils.isSubDirectory(sketchbookLibrariesFolder, folder);
.isSubDirectory(sketchbookLibrariesFolder, folder);
// A library is considered "legacy" if it doesn't contains // A library is considered "legacy" if it doesn't contains
// a file called "library.properties" // a file called "library.properties"
File check = new File(folder, "library.properties"); File check = new File(folder, "library.properties");
if (!check.exists() || !check.isFile()) { if (!check.exists() || !check.isFile()) {
// Create a legacy library and exit // Create a legacy library and exit
LegacyUserLibrary lib = LegacyUserLibrary.create(folder); LegacyUserLibrary lib = LegacyUserLibrary.create(folder);
lib.setReadOnly(readOnly); lib.setReadOnly(readOnly);
@ -148,12 +145,20 @@ public class LibrariesIndexer {
// Check if we can find the same library in the index // Check if we can find the same library in the index
// and mark it as installed // and mark it as installed
String libName = folder.getName(); // XXX: lib.getName()? ContributedLibrary foundLib = index.find(lib.getName(), lib.getVersion());
ContributedLibrary foundLib = index.find(libName, lib.getVersion());
if (foundLib != null) { if (foundLib != null) {
foundLib.setInstalled(true); foundLib.setInstalled(true);
foundLib.setInstalledFolder(folder); foundLib.setInstalledFolder(folder);
foundLib.setReadOnly(readOnly); foundLib.setReadOnly(readOnly);
lib.setTypes(foundLib.getTypes());
}
if (lib.isReadOnly() && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
lib.setTypes(lib.getDeclaredTypes());
}
if (lib.getTypes() == null) {
lib.setTypes(Arrays.asList("Contributed"));
} }
} }

View File

@ -28,17 +28,18 @@
*/ */
package processing.app.packages; package processing.app.packages;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryReference;
import processing.app.helpers.FileUtils;
import processing.app.helpers.PreferencesMap;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.LinkedList;
import java.util.List; import java.util.List;
import processing.app.helpers.FileUtils;
import processing.app.helpers.PreferencesMap;
import cc.arduino.contributions.libraries.ContributedLibrary;
import cc.arduino.contributions.libraries.ContributedLibraryReference;
public class UserLibrary extends ContributedLibrary { public class UserLibrary extends ContributedLibrary {
private String name; private String name;
@ -51,6 +52,8 @@ public class UserLibrary extends ContributedLibrary {
private String category; private String category;
private String license; private String license;
private List<String> architectures; private List<String> architectures;
private List<String> types;
private List<String> declaredTypes;
private static final List<String> MANDATORY_PROPERTIES = Arrays private static final List<String> MANDATORY_PROPERTIES = Arrays
.asList(new String[]{"name", "version", "author", "maintainer", .asList(new String[]{"name", "version", "author", "maintainer",
@ -72,9 +75,9 @@ public class UserLibrary extends ContributedLibrary {
// Compatibility with 1.5 rev.1 libraries: // Compatibility with 1.5 rev.1 libraries:
// "email" field changed to "maintainer" // "email" field changed to "maintainer"
if (!properties.containsKey("maintainer") && if (!properties.containsKey("maintainer") && properties.containsKey("email")) {
properties.containsKey("email"))
properties.put("maintainer", properties.get("email")); properties.put("maintainer", properties.get("email"));
}
// Compatibility with 1.5 rev.1 libraries: // Compatibility with 1.5 rev.1 libraries:
// "arch" folder no longer supported // "arch" folder no longer supported
@ -110,8 +113,7 @@ public class UserLibrary extends ContributedLibrary {
for (File file : libFolder.listFiles()) { for (File file : libFolder.listFiles()) {
if (file.isDirectory()) { if (file.isDirectory()) {
if (FileUtils.isSCCSOrHiddenFile(file)) { if (FileUtils.isSCCSOrHiddenFile(file)) {
System.out.println("WARNING: Spurious " + file.getName() + System.out.println("WARNING: Spurious " + file.getName() + " folder in '" + properties.get("name") + "' library");
" folder in '" + properties.get("name") + "' library");
continue; continue;
} }
} }
@ -135,8 +137,18 @@ public class UserLibrary extends ContributedLibrary {
} }
String license = properties.get("license"); String license = properties.get("license");
if (license == null) if (license == null) {
license = "Unspecified"; license = "Unspecified";
}
String types = properties.get("types");
if (types == null) {
types = "Contributed";
}
List<String> typesList = new LinkedList<String>();
for (String type : types.split(",")) {
typesList.add(type.trim());
}
UserLibrary res = new UserLibrary(); UserLibrary res = new UserLibrary();
res.setInstalledFolder(libFolder); res.setInstalledFolder(libFolder);
@ -152,6 +164,7 @@ public class UserLibrary extends ContributedLibrary {
res.license = license.trim(); res.license = license.trim();
res.architectures = archs; res.architectures = archs;
res.layout = layout; res.layout = layout;
res.declaredTypes = typesList;
return res; return res;
} }
@ -192,7 +205,11 @@ public class UserLibrary extends ContributedLibrary {
@Override @Override
public List<String> getTypes() { public List<String> getTypes() {
return Arrays.asList("Contributed"); return types;
}
public void setTypes(List<String> types) {
this.types = types;
} }
@Override @Override
@ -244,9 +261,13 @@ public class UserLibrary extends ContributedLibrary {
return null; return null;
} }
public List<String> getDeclaredTypes() {
return declaredTypes;
}
protected enum LibraryLayout { protected enum LibraryLayout {
FLAT, RECURSIVE FLAT, RECURSIVE
}; }
protected LibraryLayout layout; protected LibraryLayout layout;

View File

@ -6,3 +6,5 @@ sentence=Enables reading and writing to the permanent board storage. For all Ard
paragraph= paragraph=
url=http://arduino.cc/en/Reference/EEPROM url=http://arduino.cc/en/Reference/EEPROM
architectures=avr architectures=avr
types=Arduino

View File

@ -6,3 +6,5 @@ sentence=Enables the communication with devices that use the Serial Peripheral I
paragraph= paragraph=
url=http://arduino.cc/en/Reference/SPI url=http://arduino.cc/en/Reference/SPI
architectures=avr architectures=avr
types=Arduino

View File

@ -6,3 +6,5 @@ sentence=Enables serial communication on digital pins. For all Arduino boards, B
paragraph= paragraph=
url=http://arduino.cc/en/Reference/SoftwareSerial url=http://arduino.cc/en/Reference/SoftwareSerial
architectures=avr architectures=avr
types=Arduino

View File

@ -6,3 +6,5 @@ sentence=Allows the communication between devices or sensors connected via Two W
paragraph= paragraph=
url=http://arduino.cc/en/Reference/Wire url=http://arduino.cc/en/Reference/Wire
architectures=avr architectures=avr
types=Arduino

View File

@ -6,3 +6,5 @@ sentence=Enables the communication with devices that use the Serial Peripheral I
paragraph= paragraph=
url=http://arduino.cc/en/Reference/SPI url=http://arduino.cc/en/Reference/SPI
architectures=sam architectures=sam
types=Arduino

View File

@ -6,3 +6,5 @@ sentence=Allows the communication between devices or sensors connected via Two W
paragraph= paragraph=
url=http://arduino.cc/en/Reference/Wire url=http://arduino.cc/en/Reference/Wire
architectures=sam architectures=sam
types=Arduino