diff --git a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java index 66ad0e44b..67221f91c 100644 --- a/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java +++ b/app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java @@ -35,12 +35,12 @@ import java.awt.Dialog; import java.awt.Frame; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Optional; -import java.util.function.Predicate; import javax.swing.Box; import javax.swing.JComboBox; @@ -66,7 +66,6 @@ public class LibraryManagerUI extends InstallerJDialog typeFilter; @Override protected FilteredAbstractTableModel createContribModel() { @@ -115,63 +114,60 @@ public class LibraryManagerUI extends InstallerJDialog selected = (DropdownItem) typeChooser.getSelectedItem(); previousRowAtPoint = -1; - if (selected != null && typeFilter != selected.getFilterPredicate()) { - typeFilter = selected.getFilterPredicate(); + if (selected != null && extraFilter != selected.getFilterPredicate()) { + extraFilter = selected.getFilterPredicate(); if (contribTable.getCellEditor() != null) { contribTable.getCellEditor().stopCellEditing(); } - updateIndexFilter(filters, categoryFilter.and(typeFilter)); + updateIndexFilter(filters, categoryFilter.and(extraFilter)); } } }; - public void updateUI() { - DropdownItem previouslySelectedCategory = (DropdownItem) categoryChooser.getSelectedItem(); - DropdownItem previouslySelectedType = (DropdownItem) typeChooser.getSelectedItem(); + private Collection oldCategories = new ArrayList<>(); + private Collection oldTypes = new ArrayList<>(); - categoryChooser.removeActionListener(categoryChooserActionListener); - typeChooser.removeActionListener(typeChooserActionListener); + public void updateUI() { + // Check if categories or types have changed + Collection categories = BaseNoGui.librariesIndexer.getIndex().getCategories(); + List types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes()); + Collections.sort(types, new LibraryTypeComparator()); + + if (categories.equals(oldCategories) && types.equals(oldTypes)) { + return; + } + oldCategories = categories; + oldTypes = types; // Load categories categoryFilter = x -> true; + categoryChooser.removeActionListener(categoryChooserActionListener); categoryChooser.removeAllItems(); categoryChooser.addItem(new DropdownAllLibraries()); - Collection categories = BaseNoGui.librariesIndexer.getIndex().getCategories(); for (String category : categories) { categoryChooser.addItem(new DropdownLibraryOfCategoryItem(category)); } - categoryChooser.setEnabled(categoryChooser.getItemCount() > 1); - categoryChooser.addActionListener(categoryChooserActionListener); - if (previouslySelectedCategory != null) { - categoryChooser.setSelectedItem(previouslySelectedCategory); - } else { - categoryChooser.setSelectedIndex(0); - } + categoryChooser.setSelectedIndex(0); - typeFilter = x -> true; + // Load types + extraFilter = x -> true; + typeChooser.removeActionListener(typeChooserActionListener); typeChooser.removeAllItems(); typeChooser.addItem(new DropdownAllLibraries()); typeChooser.addItem(new DropdownUpdatableLibrariesItem()); typeChooser.addItem(new DropdownInstalledLibraryItem()); - List types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes()); - Collections.sort(types, new LibraryTypeComparator()); for (String type : types) { typeChooser.addItem(new DropdownLibraryOfTypeItem(type)); } typeChooser.setEnabled(typeChooser.getItemCount() > 1); typeChooser.addActionListener(typeChooserActionListener); - if (previouslySelectedType != null) { - typeChooser.setSelectedItem(previouslySelectedType); - } else { - typeChooser.setSelectedIndex(0); - } + typeChooser.setSelectedIndex(0); filterField.setEnabled(contribModel.getRowCount() > 0); } diff --git a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java index 2888cd688..8abff8f34 100644 --- a/app/src/cc/arduino/contributions/ui/InstallerJDialog.java +++ b/app/src/cc/arduino/contributions/ui/InstallerJDialog.java @@ -71,7 +71,6 @@ import javax.swing.text.DefaultEditorKit; import cc.arduino.contributions.ui.listeners.AbstractKeyListener; import processing.app.Base; -import processing.app.Theme; public abstract class InstallerJDialog extends JDialog { @@ -82,6 +81,7 @@ public abstract class InstallerJDialog extends JDialog { protected final FilterJTextField filterField; protected final JPanel filtersContainer; // Currently selected category and filters + protected Predicate extraFilter = x -> true; protected Predicate categoryFilter; protected String[] filters; protected final String noConnectionErrorMessage; @@ -329,7 +329,6 @@ public abstract class InstallerJDialog extends JDialog { } protected final ActionListener categoryChooserActionListener = new ActionListener() { - @Override public void actionPerformed(ActionEvent event) { DropdownItem selected = (DropdownItem) categoryChooser.getSelectedItem(); @@ -339,7 +338,7 @@ public abstract class InstallerJDialog extends JDialog { if (contribTable.getCellEditor() != null) { contribTable.getCellEditor().stopCellEditing(); } - updateIndexFilter(filters, categoryFilter); + updateIndexFilter(filters, categoryFilter.and(extraFilter)); } } };