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

Category filters for libraries and cores

This commit is contained in:
Federico Fissore 2015-03-13 15:59:32 +01:00
parent 0110fd8ff1
commit d0817f7142
19 changed files with 274 additions and 68 deletions

View File

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

View File

@ -0,0 +1,20 @@
package cc.arduino.libraries.contributions.ui;
import cc.arduino.libraries.contributions.ContributedLibrary;
import cc.arduino.libraries.contributions.ui.filters.BuiltInPredicate;
import cc.arduino.ui.DropdownItem;
import com.google.common.base.Predicate;
import static processing.app.I18n._;
public class DropdownBuiltInLibrariesItem implements DropdownItem<ContributedLibrary> {
public String toString() {
return _("Built-in");
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new BuiltInPredicate();
}
}

View File

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

View File

@ -0,0 +1,24 @@
package cc.arduino.libraries.contributions.ui;
import cc.arduino.libraries.contributions.ContributedLibrary;
import cc.arduino.libraries.contributions.ui.filters.CategoryPredicate;
import cc.arduino.ui.DropdownItem;
import com.google.common.base.Predicate;
public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLibrary> {
private final String category;
public DropdownLibraryOfCategoryItem(String category) {
this.category = category;
}
public String toString() {
return category;
}
@Override
public Predicate<ContributedLibrary> getFilterPredicate() {
return new CategoryPredicate(category);
}
}

View File

@ -30,7 +30,6 @@ package cc.arduino.libraries.contributions.ui;
import cc.arduino.libraries.contributions.ContributedLibrary; import cc.arduino.libraries.contributions.ContributedLibrary;
import cc.arduino.libraries.contributions.LibrariesIndexer; import cc.arduino.libraries.contributions.LibrariesIndexer;
import cc.arduino.packages.contributions.ContributedPackage;
import cc.arduino.packages.contributions.ContributedPlatform; import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.VersionComparator; import cc.arduino.packages.contributions.VersionComparator;
import cc.arduino.ui.FilteredAbstractTableModel; import cc.arduino.ui.FilteredAbstractTableModel;
@ -39,14 +38,12 @@ import com.google.common.collect.Collections2;
import java.util.*; import java.util.*;
import static cc.arduino.packages.contributions.VersionComparator.VERSION_COMPARATOR;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class LibrariesIndexTableModel extends FilteredAbstractTableModel { public class LibrariesIndexTableModel extends FilteredAbstractTableModel<ContributedLibrary> {
public final static int DESCRIPTION_COL = 0; public final static int DESCRIPTION_COL = 0;
public static class ContributedLibraryReleases implements Comparable<ContributedLibraryReleases> { public static class ContributedLibraryReleases implements Comparable<ContributedLibraryReleases> {
public String name; public String name;
public List<ContributedLibrary> releases = new ArrayList<ContributedLibrary>(); public List<ContributedLibrary> releases = new ArrayList<ContributedLibrary>();
public List<String> versions = new ArrayList<String>(); public List<String> versions = new ArrayList<String>();
@ -139,11 +136,11 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel {
indexer = _index; indexer = _index;
} }
String selectedCategory = null; Predicate<ContributedLibrary> selectedCategoryFilter = null;
String selectedFilters[] = null; String selectedFilters[] = null;
public void updateIndexFilter(String category, String filters[]) { public void updateIndexFilter(Predicate<ContributedLibrary> categoryFilter, String filters[]) {
selectedCategory = category; selectedCategoryFilter = categoryFilter;
selectedFilters = filters; selectedFilters = filters;
update(); update();
} }
@ -245,10 +242,8 @@ public class LibrariesIndexTableModel extends FilteredAbstractTableModel {
} }
private void applyFilterToLibrary(ContributedLibrary lib) { private void applyFilterToLibrary(ContributedLibrary lib) {
if (selectedCategory != null && !selectedCategory.isEmpty()) { if (selectedCategoryFilter != null && !selectedCategoryFilter.apply(lib)) {
if (lib.getCategory() == null || !lib.getCategory().equals(selectedCategory)) { return;
return;
}
} }
if (!stringContainsAll(lib.getName(), selectedFilters) && !stringContainsAll(lib.getParagraph(), selectedFilters) && !stringContainsAll(lib.getSentence(), selectedFilters)) { if (!stringContainsAll(lib.getName(), selectedFilters) && !stringContainsAll(lib.getParagraph(), selectedFilters) && !stringContainsAll(lib.getSentence(), selectedFilters)) {
return; return;

View File

@ -31,13 +31,9 @@ package cc.arduino.libraries.contributions.ui;
import cc.arduino.libraries.contributions.ContributedLibrary; import cc.arduino.libraries.contributions.ContributedLibrary;
import cc.arduino.libraries.contributions.LibrariesIndexer; import cc.arduino.libraries.contributions.LibrariesIndexer;
import cc.arduino.packages.contributions.ui.InstallerJDialogUncaughtExceptionHandler; import cc.arduino.packages.contributions.ui.InstallerJDialogUncaughtExceptionHandler;
import cc.arduino.ui.DropdownALLItem; import cc.arduino.ui.*;
import cc.arduino.ui.FilteredAbstractTableModel;
import cc.arduino.ui.InstallerJDialog;
import cc.arduino.ui.InstallerTableCell;
import cc.arduino.utils.Progress; import cc.arduino.utils.Progress;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Collection; import java.util.Collection;
@ -87,17 +83,20 @@ public class LibraryManagerUI extends InstallerJDialog {
// return a FilteredAbstractTableModel // return a FilteredAbstractTableModel
getContribModel().setIndexer(indexer); getContribModel().setIndexer(indexer);
category = null; categoryFilter = null;
categoryChooser.removeAllItems(); categoryChooser.removeAllItems();
getContribModel().addTableModelListener(tableModelListener); getContribModel().addTableModelListener(tableModelListener);
categoryChooser.addActionListener(categoryChooserActionListener); categoryChooser.addActionListener(categoryChooserActionListener);
// Load categories // Load categories
categoryChooser.addItem(new DropdownAllLibrariesItem());
categoryChooser.addItem(new DropdownInstalledLibrariesItem());
categoryChooser.addItem(new DropdownBuiltInLibrariesItem());
Collection<String> categories = indexer.getIndex().getCategories(); Collection<String> categories = indexer.getIndex().getCategories();
categoryChooser.addItem(new DropdownALLItem()); for (String category : categories) {
for (String s : categories) categoryChooser.addItem(new DropdownLibraryOfCategoryItem(category));
categoryChooser.addItem(s); }
// Enable categories combo only if there are two or more choices // Enable categories combo only if there are two or more choices
int count = categoryChooser.getItemCount(); int count = categoryChooser.getItemCount();

View File

@ -0,0 +1,17 @@
package cc.arduino.libraries.contributions.ui.filters;
import cc.arduino.libraries.contributions.ContributedLibrary;
import com.google.common.base.Predicate;
public class BuiltInPredicate implements Predicate<ContributedLibrary> {
@Override
public boolean apply(ContributedLibrary input) {
return input.isReadOnly();
}
@Override
public boolean equals(Object obj) {
return obj instanceof BuiltInPredicate;
}
}

View File

@ -0,0 +1,24 @@
package cc.arduino.libraries.contributions.ui.filters;
import cc.arduino.libraries.contributions.ContributedLibrary;
import com.google.common.base.Predicate;
public class CategoryPredicate implements Predicate<ContributedLibrary> {
private final String category;
public CategoryPredicate(String category) {
this.category = category;
}
@Override
public boolean apply(ContributedLibrary input) {
return input.getCategory() != null && category.equals(input.getCategory());
}
@Override
public boolean equals(Object obj) {
return obj instanceof CategoryPredicate && ((CategoryPredicate) obj).category.equals(category);
}
}

View File

@ -0,0 +1,17 @@
package cc.arduino.libraries.contributions.ui.filters;
import cc.arduino.libraries.contributions.ContributedLibrary;
import com.google.common.base.Predicate;
public class InstalledPredicate implements Predicate<ContributedLibrary> {
@Override
public boolean apply(ContributedLibrary input) {
return input.isInstalled();
}
@Override
public boolean equals(Object obj) {
return obj instanceof InstalledPredicate;
}
}

View File

@ -32,6 +32,7 @@ import cc.arduino.packages.contributions.ContributedPackage;
import cc.arduino.packages.contributions.ContributedPlatform; import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.ContributionsIndex; import cc.arduino.packages.contributions.ContributionsIndex;
import cc.arduino.ui.FilteredAbstractTableModel; import cc.arduino.ui.FilteredAbstractTableModel;
import com.google.common.base.Predicate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
@ -40,7 +41,7 @@ import java.util.List;
import static processing.app.I18n._; import static processing.app.I18n._;
@SuppressWarnings("serial") @SuppressWarnings("serial")
public class ContributionIndexTableModel extends FilteredAbstractTableModel { public class ContributionIndexTableModel extends FilteredAbstractTableModel<ContributedPlatform> {
public final static int DESCRIPTION_COL = 0; public final static int DESCRIPTION_COL = 0;
@ -137,13 +138,12 @@ public class ContributionIndexTableModel extends FilteredAbstractTableModel {
index = _index; index = _index;
} }
public void updateIndexFilter(String category, String filters[]) { public void updateIndexFilter(Predicate<ContributedPlatform> categoryFilter, String filters[]) {
contributions.clear(); contributions.clear();
for (ContributedPackage pack : index.getPackages()) { for (ContributedPackage pack : index.getPackages()) {
for (ContributedPlatform platform : pack.getPlatforms()) { for (ContributedPlatform platform : pack.getPlatforms()) {
if (category != null) { if (!categoryFilter.apply(platform)) {
if (!category.equals(platform.getCategory())) continue;
continue;
} }
if (!stringContainsAll(platform.getName(), filters)) if (!stringContainsAll(platform.getName(), filters))
continue; continue;

View File

@ -28,15 +28,16 @@
*/ */
package cc.arduino.packages.contributions.ui; package cc.arduino.packages.contributions.ui;
import cc.arduino.libraries.contributions.ui.DropdownLibraryOfCategoryItem;
import cc.arduino.packages.contributions.ContributedPlatform; import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.ContributionInstaller; import cc.arduino.packages.contributions.ContributionInstaller;
import cc.arduino.packages.contributions.ContributionsIndexer; import cc.arduino.packages.contributions.ContributionsIndexer;
import cc.arduino.libraries.contributions.ui.DropdownAllLibrariesItem;
import cc.arduino.ui.FilteredAbstractTableModel; import cc.arduino.ui.FilteredAbstractTableModel;
import cc.arduino.ui.InstallerJDialog; import cc.arduino.ui.InstallerJDialog;
import cc.arduino.ui.InstallerTableCell; import cc.arduino.ui.InstallerTableCell;
import cc.arduino.utils.Progress; import cc.arduino.utils.Progress;
import javax.swing.*;
import java.awt.*; import java.awt.*;
import java.util.Collection; import java.util.Collection;
@ -86,7 +87,7 @@ public class ContributionManagerUI extends InstallerJDialog {
getContribModel().setIndex(indexer.getIndex()); getContribModel().setIndex(indexer.getIndex());
category = null; categoryFilter = null;
categoryChooser.removeAllItems(); categoryChooser.removeAllItems();
filterField.setEnabled(getContribModel().getRowCount() > 0); filterField.setEnabled(getContribModel().getRowCount() > 0);
@ -95,12 +96,11 @@ public class ContributionManagerUI extends InstallerJDialog {
categoryChooser.addActionListener(categoryChooserActionListener); categoryChooser.addActionListener(categoryChooserActionListener);
// Enable categories combo only if there are two or more choices // Enable categories combo only if there are two or more choices
categoryChooser.addItem(new DropdownAllCoresItem());
Collection<String> categories = indexer.getIndex().getCategories(); Collection<String> categories = indexer.getIndex().getCategories();
int count = categories.size(); for (String s : categories) {
categoryChooser.setEnabled(count > 1); categoryChooser.addItem(new DropdownCoreOfCategoryItem(s));
}
for (String s : categories)
categoryChooser.addItem(s);
// Create ConstributionInstaller tied with the provided index // Create ConstributionInstaller tied with the provided index
installer = new ContributionInstaller(indexer) { installer = new ContributionInstaller(indexer) {

View File

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

View File

@ -0,0 +1,24 @@
package cc.arduino.packages.contributions.ui;
import cc.arduino.packages.contributions.ContributedPlatform;
import cc.arduino.packages.contributions.ui.filters.CategoryPredicate;
import cc.arduino.ui.DropdownItem;
import com.google.common.base.Predicate;
public class DropdownCoreOfCategoryItem implements DropdownItem<ContributedPlatform> {
private final String category;
public DropdownCoreOfCategoryItem(String category) {
this.category = category;
}
public String toString() {
return category;
}
@Override
public Predicate<ContributedPlatform> getFilterPredicate() {
return new CategoryPredicate(category);
}
}

View File

@ -0,0 +1,24 @@
package cc.arduino.packages.contributions.ui.filters;
import cc.arduino.packages.contributions.ContributedPlatform;
import com.google.common.base.Predicate;
public class CategoryPredicate implements Predicate<ContributedPlatform> {
private final String category;
public CategoryPredicate(String category) {
this.category = category;
}
@Override
public boolean apply(ContributedPlatform input) {
return input.getCategory() != null && category.equals(input.getCategory());
}
@Override
public boolean equals(Object obj) {
return obj instanceof CategoryPredicate && ((CategoryPredicate) obj).category.equals(category);
}
}

View File

@ -1,15 +0,0 @@
package cc.arduino.ui;
import static processing.app.I18n._;
public class DropdownALLItem implements DropdownItem {
@Override
public String toString() {
return _("All");
}
public String getItemValue() {
return "";
}
}

View File

@ -1,6 +1,9 @@
package cc.arduino.ui; package cc.arduino.ui;
public interface DropdownItem { import com.google.common.base.Predicate;
public interface DropdownItem<T> {
Predicate<T> getFilterPredicate();
String getItemValue();
} }

View File

@ -28,10 +28,13 @@
*/ */
package cc.arduino.ui; package cc.arduino.ui;
import cc.arduino.libraries.contributions.ContributedLibrary;
import com.google.common.base.Predicate;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
public abstract class FilteredAbstractTableModel extends AbstractTableModel { public abstract class FilteredAbstractTableModel<T> extends AbstractTableModel {
abstract public void updateIndexFilter(String category, String[] filters); abstract public void updateIndexFilter(Predicate<T> categoryFilter, String[] filters);
} }

View File

@ -28,6 +28,8 @@
*/ */
package cc.arduino.ui; package cc.arduino.ui;
import cc.arduino.libraries.contributions.ContributedLibrary;
import com.google.common.base.Predicate;
import processing.app.Base; import processing.app.Base;
import processing.app.Theme; import processing.app.Theme;
@ -46,7 +48,7 @@ import java.awt.event.WindowEvent;
import static cc.arduino.packages.contributions.ui.ContributionIndexTableModel.DESCRIPTION_COL; import static cc.arduino.packages.contributions.ui.ContributionIndexTableModel.DESCRIPTION_COL;
import static processing.app.I18n._; import static processing.app.I18n._;
public abstract class InstallerJDialog extends JDialog { public abstract class InstallerJDialog<T> extends JDialog {
// Toolbar on top of the window: // Toolbar on top of the window:
// - Categories drop-down menu // - Categories drop-down menu
@ -58,7 +60,7 @@ public abstract class InstallerJDialog extends JDialog {
// - Search text-field // - Search text-field
protected FilterJTextField filterField; protected FilterJTextField filterField;
// Currently selected category and filters // Currently selected category and filters
protected String category; protected Predicate<T> categoryFilter;
protected String[] filters; protected String[] filters;
// Real contribution table // Real contribution table
@ -118,7 +120,7 @@ public abstract class InstallerJDialog extends JDialog {
protected void onFilter(String[] _filters) { protected void onFilter(String[] _filters) {
filters = _filters; filters = _filters;
cellEditor.stopCellEditing(); cellEditor.stopCellEditing();
contribModel.updateIndexFilter(category, filters); contribModel.updateIndexFilter(categoryFilter, filters);
} }
}; };
@ -271,22 +273,13 @@ public abstract class InstallerJDialog extends JDialog {
protected ActionListener categoryChooserActionListener = new ActionListener() { protected ActionListener categoryChooserActionListener = new ActionListener() {
public String getSelectedItem() {
Object obj = categoryChooser.getSelectedItem();
if (obj instanceof String) {
return (String) obj;
}
DropdownItem item = (DropdownItem) obj;
return item.getItemValue();
}
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
String selected = getSelectedItem(); DropdownItem<T> selected = (DropdownItem<T>) categoryChooser.getSelectedItem();
if (category == null || !category.equals(selected)) { if (categoryFilter == null || !categoryFilter.equals(selected)) {
category = selected; categoryFilter = selected.getFilterPredicate();
cellEditor.stopCellEditing(); cellEditor.stopCellEditing();
contribModel.updateIndexFilter(category, filters); contribModel.updateIndexFilter(categoryFilter, filters);
} }
} }
}; };

View File

@ -0,0 +1,18 @@
package cc.arduino.ui.filters;
import com.google.common.base.Predicate;
public class NoopPredicate<T> implements Predicate<T> {
@Override
public boolean apply(T input) {
return true;
}
@Override
public boolean equals(Object obj) {
return obj instanceof NoopPredicate;
}
}