1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

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.
This commit is contained in:
Cristian Maglie 2017-12-17 21:47:10 +01:00
parent 482b905a62
commit 3ec6748dc4
7 changed files with 82 additions and 57 deletions

View File

@ -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<DownloadableContribution> {
public class DropdownAllLibraries implements DropdownItem<ContributedLibraryReleases> {
public String toString() {
return tr("All");
}
@Override
public Predicate<DownloadableContribution> getFilterPredicate() {
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
return x -> true;
}

View File

@ -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<ContributedLibrary> {
public class DropdownInstalledLibraryItem implements DropdownItem<ContributedLibraryReleases> {
public String toString() {
return tr("Installed");
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new InstalledLibraryPredicate();
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
return new Predicate<ContributedLibraryReleases>() {
@Override
public boolean test(ContributedLibraryReleases t) {
return t.getInstalled().isPresent();
}
};
}
}

View File

@ -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<ContributedLibrary> {
public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLibraryReleases> {
private final String category;
@ -50,8 +49,14 @@ public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLi
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new CategoryPredicate(category);
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
return new Predicate<ContributedLibraryReleases>() {
@Override
public boolean test(ContributedLibraryReleases rel) {
ContributedLibrary lib = rel.getLatest();
return category.equals(lib.getCategory());
}
};
}
}

View File

@ -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<ContributedLibrary> {
public class DropdownLibraryOfTypeItem implements DropdownItem<ContributedLibraryReleases> {
private final String type;
@ -50,8 +49,14 @@ public class DropdownLibraryOfTypeItem implements DropdownItem<ContributedLibrar
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new TypePredicate(type);
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
return new Predicate<ContributedLibraryReleases>() {
@Override
public boolean test(ContributedLibraryReleases lib) {
List<String> types = lib.getLatest().getTypes();
return types != null && types.contains(type);
}
};
}
}

View File

@ -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<ContributedLibrary> {
public class DropdownUpdatableLibrariesItem implements DropdownItem<ContributedLibraryReleases> {
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new UpdatableLibraryPredicate();
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
return new Predicate<ContributedLibraryReleases>() {
@Override
public boolean test(ContributedLibraryReleases lib) {
Optional<ContributedLibrary> mayInstalled = lib.getInstalled();
if (!mayInstalled.isPresent()) {
return false;
}
return !lib.getLatest().equals(mayInstalled.get());
}
};
}
@Override

View File

@ -41,7 +41,7 @@ import java.util.function.Predicate;
@SuppressWarnings("serial")
public class LibrariesIndexTableModel
extends FilteredAbstractTableModel<ContributedLibrary> {
extends FilteredAbstractTableModel<ContributedLibraryReleases> {
private final List<ContributedLibraryReleases> contributions = new ArrayList<>();
@ -49,11 +49,11 @@ public class LibrariesIndexTableModel
private final Class<?>[] columnTypes = { ContributedPlatform.class };
Predicate<ContributedLibrary> selectedCategoryFilter = null;
Predicate<ContributedLibraryReleases> selectedCategoryFilter = null;
String selectedFilters[] = null;
public void updateIndexFilter(String filters[],
Predicate<ContributedLibrary> additionalFilter) {
Predicate<ContributedLibraryReleases> 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<ContributedLibraryReleases> rebuildContributionsFromIndex() {
List<ContributedLibraryReleases> 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<ContributedLibraryReleases> 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"));
}

View File

@ -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<ContributedLibrary> {
public class LibraryManagerUI extends InstallerJDialog<ContributedLibraryReleases> {
private final JComboBox typeChooser;
private final LibraryInstaller installer;
private Predicate<ContributedLibrary> typeFilter;
private Predicate<ContributedLibraryReleases> typeFilter;
@Override
protected FilteredAbstractTableModel createContribModel() {
@ -117,7 +115,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
@Override
public void actionPerformed(ActionEvent event) {
DropdownItem<ContributedLibrary> selected = (DropdownItem<ContributedLibrary>) typeChooser.getSelectedItem();
DropdownItem<ContributedLibraryReleases> selected = (DropdownItem<ContributedLibraryReleases>) typeChooser.getSelectedItem();
previousRowAtPoint = -1;
if (selected != null && typeFilter != selected.getFilterPredicate()) {
typeFilter = selected.getFilterPredicate();
@ -130,8 +128,8 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
};
public void updateUI() {
DropdownItem<DownloadableContribution> previouslySelectedCategory = (DropdownItem<DownloadableContribution>) categoryChooser.getSelectedItem();
DropdownItem<DownloadableContribution> previouslySelectedType = (DropdownItem<DownloadableContribution>) typeChooser.getSelectedItem();
DropdownItem<ContributedLibraryReleases> previouslySelectedCategory = (DropdownItem<ContributedLibraryReleases>) categoryChooser.getSelectedItem();
DropdownItem<ContributedLibraryReleases> previouslySelectedType = (DropdownItem<ContributedLibraryReleases>) typeChooser.getSelectedItem();
categoryChooser.removeActionListener(categoryChooserActionListener);
typeChooser.removeActionListener(typeChooserActionListener);
@ -140,7 +138,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
// Load categories
categoryFilter = x -> true;
categoryChooser.removeAllItems();
categoryChooser.addItem(new DropdownAllItem());
categoryChooser.addItem(new DropdownAllLibraries());
Collection<String> categories = BaseNoGui.librariesIndexer.getIndex().getCategories();
for (String category : categories) {
categoryChooser.addItem(new DropdownLibraryOfCategoryItem(category));
@ -157,7 +155,7 @@ public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
typeFilter = x -> true;
typeChooser.removeAllItems();
typeChooser.addItem(new DropdownAllItem());
typeChooser.addItem(new DropdownAllLibraries());
typeChooser.addItem(new DropdownUpdatableLibrariesItem());
typeChooser.addItem(new DropdownInstalledLibraryItem());
java.util.List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());