From 3ec6748dc4e736ef0c0c02537a804c6f2868e23b Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Sun, 17 Dec 2017 21:47:10 +0100 Subject: [PATCH] Lib manager Predicates must act on ContributedLibraryReleases Previously the filtering was made on ContributedLibrary, but the objects that is actually displayed is a ContributeLibraryReleases, so it comens natural to filter on this class of objects. Apparently the functionality is not affected by this commit, but there may be some side-effect that may probably fix some visualization bug. --- .../ui/DropdownAllLibraries.java} | 8 +-- .../ui/DropdownInstalledLibraryItem.java | 17 +++--- .../ui/DropdownLibraryOfCategoryItem.java | 13 +++-- .../ui/DropdownLibraryOfTypeItem.java | 15 ++++-- .../ui/DropdownUpdatableLibrariesItem.java | 17 ++++-- .../ui/LibrariesIndexTableModel.java | 53 ++++++++++--------- .../libraries/ui/LibraryManagerUI.java | 16 +++--- 7 files changed, 82 insertions(+), 57 deletions(-) rename app/src/cc/arduino/contributions/{ui/DropdownAllItem.java => libraries/ui/DropdownAllLibraries.java} (85%) diff --git a/app/src/cc/arduino/contributions/ui/DropdownAllItem.java b/app/src/cc/arduino/contributions/libraries/ui/DropdownAllLibraries.java similarity index 85% rename from app/src/cc/arduino/contributions/ui/DropdownAllItem.java rename to app/src/cc/arduino/contributions/libraries/ui/DropdownAllLibraries.java index 793313921..bc64a4414 100644 --- a/app/src/cc/arduino/contributions/ui/DropdownAllItem.java +++ b/app/src/cc/arduino/contributions/libraries/ui/DropdownAllLibraries.java @@ -27,22 +27,22 @@ * the GNU General Public License. */ -package cc.arduino.contributions.ui; +package cc.arduino.contributions.libraries.ui; -import cc.arduino.contributions.DownloadableContribution; +import cc.arduino.contributions.ui.DropdownItem; import java.util.function.Predicate; import static processing.app.I18n.tr; -public class DropdownAllItem implements DropdownItem { +public class DropdownAllLibraries implements DropdownItem { public String toString() { return tr("All"); } @Override - public Predicate getFilterPredicate() { + public Predicate getFilterPredicate() { return x -> true; } diff --git a/app/src/cc/arduino/contributions/libraries/ui/DropdownInstalledLibraryItem.java b/app/src/cc/arduino/contributions/libraries/ui/DropdownInstalledLibraryItem.java index 8a0e2b4ba..b708bc2f2 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/DropdownInstalledLibraryItem.java +++ b/app/src/cc/arduino/contributions/libraries/ui/DropdownInstalledLibraryItem.java @@ -29,23 +29,26 @@ package cc.arduino.contributions.libraries.ui; -import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate; -import cc.arduino.contributions.ui.DropdownItem; +import static processing.app.I18n.tr; import java.util.function.Predicate; -import static processing.app.I18n.tr; +import cc.arduino.contributions.ui.DropdownItem; -public class DropdownInstalledLibraryItem implements DropdownItem { +public class DropdownInstalledLibraryItem implements DropdownItem { public String toString() { return tr("Installed"); } @Override - public Predicate getFilterPredicate() { - return new InstalledLibraryPredicate(); + public Predicate getFilterPredicate() { + return new Predicate() { + @Override + public boolean test(ContributedLibraryReleases t) { + return t.getInstalled().isPresent(); + } + }; } } diff --git a/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java b/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java index cb3a36c21..8191e35d0 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java +++ b/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java @@ -30,14 +30,13 @@ package cc.arduino.contributions.libraries.ui; import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.filters.CategoryPredicate; import cc.arduino.contributions.ui.DropdownItem; import java.util.function.Predicate; import static processing.app.I18n.tr; -public class DropdownLibraryOfCategoryItem implements DropdownItem { +public class DropdownLibraryOfCategoryItem implements DropdownItem { private final String category; @@ -50,8 +49,14 @@ public class DropdownLibraryOfCategoryItem implements DropdownItem getFilterPredicate() { - return new CategoryPredicate(category); + public Predicate getFilterPredicate() { + return new Predicate() { + @Override + public boolean test(ContributedLibraryReleases rel) { + ContributedLibrary lib = rel.getLatest(); + return category.equals(lib.getCategory()); + } + }; } } diff --git a/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java b/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java index 404fe0586..a5a692604 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java +++ b/app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java @@ -29,15 +29,14 @@ package cc.arduino.contributions.libraries.ui; -import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.filters.TypePredicate; import cc.arduino.contributions.ui.DropdownItem; +import java.util.List; import java.util.function.Predicate; import static processing.app.I18n.tr; -public class DropdownLibraryOfTypeItem implements DropdownItem { +public class DropdownLibraryOfTypeItem implements DropdownItem { private final String type; @@ -50,8 +49,14 @@ public class DropdownLibraryOfTypeItem implements DropdownItem getFilterPredicate() { - return new TypePredicate(type); + public Predicate getFilterPredicate() { + return new Predicate() { + @Override + public boolean test(ContributedLibraryReleases lib) { + List types = lib.getLatest().getTypes(); + return types != null && types.contains(type); + } + }; } } diff --git a/app/src/cc/arduino/contributions/libraries/ui/DropdownUpdatableLibrariesItem.java b/app/src/cc/arduino/contributions/libraries/ui/DropdownUpdatableLibrariesItem.java index b160df21e..7095d2361 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/DropdownUpdatableLibrariesItem.java +++ b/app/src/cc/arduino/contributions/libraries/ui/DropdownUpdatableLibrariesItem.java @@ -30,18 +30,27 @@ package cc.arduino.contributions.libraries.ui; import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.filters.UpdatableLibraryPredicate; import cc.arduino.contributions.ui.DropdownItem; +import java.util.Optional; import java.util.function.Predicate; import static processing.app.I18n.tr; -public class DropdownUpdatableLibrariesItem implements DropdownItem { +public class DropdownUpdatableLibrariesItem implements DropdownItem { @Override - public Predicate getFilterPredicate() { - return new UpdatableLibraryPredicate(); + public Predicate getFilterPredicate() { + return new Predicate() { + @Override + public boolean test(ContributedLibraryReleases lib) { + Optional mayInstalled = lib.getInstalled(); + if (!mayInstalled.isPresent()) { + return false; + } + return !lib.getLatest().equals(mayInstalled.get()); + } + }; } @Override diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java index 3b9fde47e..e81bb2754 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java @@ -41,7 +41,7 @@ import java.util.function.Predicate; @SuppressWarnings("serial") public class LibrariesIndexTableModel - extends FilteredAbstractTableModel { + extends FilteredAbstractTableModel { private final List contributions = new ArrayList<>(); @@ -49,11 +49,11 @@ public class LibrariesIndexTableModel private final Class[] columnTypes = { ContributedPlatform.class }; - Predicate selectedCategoryFilter = null; + Predicate selectedCategoryFilter = null; String selectedFilters[] = null; public void updateIndexFilter(String filters[], - Predicate additionalFilter) { + Predicate additionalFilter) { selectedCategoryFilter = additionalFilter; selectedFilters = filters; update(); @@ -86,17 +86,6 @@ public class LibrariesIndexTableModel return true; } - private void addContribution(ContributedLibrary lib) { - for (ContributedLibraryReleases contribution : contributions) { - if (!contribution.shouldContain(lib)) - continue; - contribution.add(lib); - return; - } - - contributions.add(new ContributedLibraryReleases(lib)); - } - @Override public int getColumnCount() { return columnNames.length; @@ -149,17 +138,19 @@ public class LibrariesIndexTableModel fireTableDataChanged(); } - private void applyFilterToLibrary(ContributedLibrary lib) { + private boolean filterCondition(ContributedLibraryReleases lib) { if (selectedCategoryFilter != null && !selectedCategoryFilter.test(lib)) { - return; + return false; } - String compoundTargetSearchText = lib.getName() + "\n" + lib.getParagraph() - + "\n" + lib.getSentence(); + ContributedLibrary latest = lib.getLatest(); + String compoundTargetSearchText = latest.getName() + "\n" + latest.getParagraph() + + "\n" + latest.getSentence(); if (!stringContainsAll(compoundTargetSearchText, selectedFilters)) { - return; + return false; } - addContribution(lib); + + return true; } public void updateLibrary(ContributedLibrary lib) { @@ -189,12 +180,26 @@ public class LibrariesIndexTableModel fireTableRowsDeleted(row, row); } + private List rebuildContributionsFromIndex() { + List res = new ArrayList<>(); + BaseNoGui.librariesIndexer.getIndex().getLibraries(). // + forEach(lib -> { + for (ContributedLibraryReleases contribution : res) { + if (!contribution.shouldContain(lib)) + continue; + contribution.add(lib); + return; + } + + res.add(new ContributedLibraryReleases(lib)); + }); + return res; + } + private void updateContributions() { + List all = rebuildContributionsFromIndex(); contributions.clear(); - BaseNoGui.librariesIndexer.getIndex().getLibraries() - .forEach(this::applyFilterToLibrary); - BaseNoGui.librariesIndexer.getInstalledLibraries() - .forEach(this::applyFilterToLibrary); + all.stream().filter(this::filterCondition).forEach(contributions::add); Collections.sort(contributions, new ContributedLibraryReleasesComparator("Arduino")); } diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 0ec5f5365..7ee097e35 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -47,11 +47,9 @@ import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.table.TableCellRenderer; -import cc.arduino.contributions.DownloadableContribution; import cc.arduino.contributions.libraries.ContributedLibrary; import cc.arduino.contributions.libraries.LibraryInstaller; import cc.arduino.contributions.libraries.LibraryTypeComparator; -import cc.arduino.contributions.ui.DropdownAllItem; import cc.arduino.contributions.ui.DropdownItem; import cc.arduino.contributions.ui.FilteredAbstractTableModel; import cc.arduino.contributions.ui.InstallerJDialog; @@ -61,11 +59,11 @@ import cc.arduino.utils.Progress; import processing.app.BaseNoGui; @SuppressWarnings("serial") -public class LibraryManagerUI extends InstallerJDialog { +public class LibraryManagerUI extends InstallerJDialog { private final JComboBox typeChooser; private final LibraryInstaller installer; - private Predicate typeFilter; + private Predicate typeFilter; @Override protected FilteredAbstractTableModel createContribModel() { @@ -117,7 +115,7 @@ public class LibraryManagerUI extends InstallerJDialog { @Override public void actionPerformed(ActionEvent event) { - DropdownItem selected = (DropdownItem) typeChooser.getSelectedItem(); + DropdownItem selected = (DropdownItem) typeChooser.getSelectedItem(); previousRowAtPoint = -1; if (selected != null && typeFilter != selected.getFilterPredicate()) { typeFilter = selected.getFilterPredicate(); @@ -130,8 +128,8 @@ public class LibraryManagerUI extends InstallerJDialog { }; public void updateUI() { - DropdownItem previouslySelectedCategory = (DropdownItem) categoryChooser.getSelectedItem(); - DropdownItem previouslySelectedType = (DropdownItem) typeChooser.getSelectedItem(); + DropdownItem previouslySelectedCategory = (DropdownItem) categoryChooser.getSelectedItem(); + DropdownItem previouslySelectedType = (DropdownItem) typeChooser.getSelectedItem(); categoryChooser.removeActionListener(categoryChooserActionListener); typeChooser.removeActionListener(typeChooserActionListener); @@ -140,7 +138,7 @@ public class LibraryManagerUI extends InstallerJDialog { // Load categories categoryFilter = x -> true; categoryChooser.removeAllItems(); - categoryChooser.addItem(new DropdownAllItem()); + categoryChooser.addItem(new DropdownAllLibraries()); Collection categories = BaseNoGui.librariesIndexer.getIndex().getCategories(); for (String category : categories) { categoryChooser.addItem(new DropdownLibraryOfCategoryItem(category)); @@ -157,7 +155,7 @@ public class LibraryManagerUI extends InstallerJDialog { typeFilter = x -> true; typeChooser.removeAllItems(); - typeChooser.addItem(new DropdownAllItem()); + typeChooser.addItem(new DropdownAllLibraries()); typeChooser.addItem(new DropdownUpdatableLibrariesItem()); typeChooser.addItem(new DropdownInstalledLibraryItem()); java.util.List types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());