Skip to content

Commit 3ec6748

Browse files
committed
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.
1 parent 482b905 commit 3ec6748

7 files changed

+82
-57
lines changed

app/src/cc/arduino/contributions/ui/DropdownAllItem.java renamed to app/src/cc/arduino/contributions/libraries/ui/DropdownAllLibraries.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,22 @@
2727
* the GNU General Public License.
2828
*/
2929

30-
package cc.arduino.contributions.ui;
30+
package cc.arduino.contributions.libraries.ui;
3131

32-
import cc.arduino.contributions.DownloadableContribution;
32+
import cc.arduino.contributions.ui.DropdownItem;
3333

3434
import java.util.function.Predicate;
3535

3636
import static processing.app.I18n.tr;
3737

38-
public class DropdownAllItem implements DropdownItem<DownloadableContribution> {
38+
public class DropdownAllLibraries implements DropdownItem<ContributedLibraryReleases> {
3939

4040
public String toString() {
4141
return tr("All");
4242
}
4343

4444
@Override
45-
public Predicate<DownloadableContribution> getFilterPredicate() {
45+
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
4646
return x -> true;
4747
}
4848

app/src/cc/arduino/contributions/libraries/ui/DropdownInstalledLibraryItem.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,26 @@
2929

3030
package cc.arduino.contributions.libraries.ui;
3131

32-
import cc.arduino.contributions.libraries.ContributedLibrary;
33-
import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate;
34-
import cc.arduino.contributions.ui.DropdownItem;
32+
import static processing.app.I18n.tr;
3533

3634
import java.util.function.Predicate;
3735

38-
import static processing.app.I18n.tr;
36+
import cc.arduino.contributions.ui.DropdownItem;
3937

40-
public class DropdownInstalledLibraryItem implements DropdownItem<ContributedLibrary> {
38+
public class DropdownInstalledLibraryItem implements DropdownItem<ContributedLibraryReleases> {
4139

4240
public String toString() {
4341
return tr("Installed");
4442
}
4543

4644
@Override
47-
public Predicate<ContributedLibrary> getFilterPredicate() {
48-
return new InstalledLibraryPredicate();
45+
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
46+
return new Predicate<ContributedLibraryReleases>() {
47+
@Override
48+
public boolean test(ContributedLibraryReleases t) {
49+
return t.getInstalled().isPresent();
50+
}
51+
};
4952
}
5053

5154
}

app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfCategoryItem.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
package cc.arduino.contributions.libraries.ui;
3131

3232
import cc.arduino.contributions.libraries.ContributedLibrary;
33-
import cc.arduino.contributions.libraries.filters.CategoryPredicate;
3433
import cc.arduino.contributions.ui.DropdownItem;
3534

3635
import java.util.function.Predicate;
3736

3837
import static processing.app.I18n.tr;
3938

40-
public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLibrary> {
39+
public class DropdownLibraryOfCategoryItem implements DropdownItem<ContributedLibraryReleases> {
4140

4241
private final String category;
4342

@@ -50,8 +49,14 @@ public String toString() {
5049
}
5150

5251
@Override
53-
public Predicate<ContributedLibrary> getFilterPredicate() {
54-
return new CategoryPredicate(category);
52+
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
53+
return new Predicate<ContributedLibraryReleases>() {
54+
@Override
55+
public boolean test(ContributedLibraryReleases rel) {
56+
ContributedLibrary lib = rel.getLatest();
57+
return category.equals(lib.getCategory());
58+
}
59+
};
5560
}
5661

5762
}

app/src/cc/arduino/contributions/libraries/ui/DropdownLibraryOfTypeItem.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@
2929

3030
package cc.arduino.contributions.libraries.ui;
3131

32-
import cc.arduino.contributions.libraries.ContributedLibrary;
33-
import cc.arduino.contributions.libraries.filters.TypePredicate;
3432
import cc.arduino.contributions.ui.DropdownItem;
3533

34+
import java.util.List;
3635
import java.util.function.Predicate;
3736

3837
import static processing.app.I18n.tr;
3938

40-
public class DropdownLibraryOfTypeItem implements DropdownItem<ContributedLibrary> {
39+
public class DropdownLibraryOfTypeItem implements DropdownItem<ContributedLibraryReleases> {
4140

4241
private final String type;
4342

@@ -50,8 +49,14 @@ public String toString() {
5049
}
5150

5251
@Override
53-
public Predicate<ContributedLibrary> getFilterPredicate() {
54-
return new TypePredicate(type);
52+
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
53+
return new Predicate<ContributedLibraryReleases>() {
54+
@Override
55+
public boolean test(ContributedLibraryReleases lib) {
56+
List<String> types = lib.getLatest().getTypes();
57+
return types != null && types.contains(type);
58+
}
59+
};
5560
}
5661

5762
}

app/src/cc/arduino/contributions/libraries/ui/DropdownUpdatableLibrariesItem.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,27 @@
3030
package cc.arduino.contributions.libraries.ui;
3131

3232
import cc.arduino.contributions.libraries.ContributedLibrary;
33-
import cc.arduino.contributions.libraries.filters.UpdatableLibraryPredicate;
3433
import cc.arduino.contributions.ui.DropdownItem;
3534

35+
import java.util.Optional;
3636
import java.util.function.Predicate;
3737

3838
import static processing.app.I18n.tr;
3939

40-
public class DropdownUpdatableLibrariesItem implements DropdownItem<ContributedLibrary> {
40+
public class DropdownUpdatableLibrariesItem implements DropdownItem<ContributedLibraryReleases> {
4141

4242
@Override
43-
public Predicate<ContributedLibrary> getFilterPredicate() {
44-
return new UpdatableLibraryPredicate();
43+
public Predicate<ContributedLibraryReleases> getFilterPredicate() {
44+
return new Predicate<ContributedLibraryReleases>() {
45+
@Override
46+
public boolean test(ContributedLibraryReleases lib) {
47+
Optional<ContributedLibrary> mayInstalled = lib.getInstalled();
48+
if (!mayInstalled.isPresent()) {
49+
return false;
50+
}
51+
return !lib.getLatest().equals(mayInstalled.get());
52+
}
53+
};
4554
}
4655

4756
@Override

app/src/cc/arduino/contributions/libraries/ui/LibrariesIndexTableModel.java

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@
4141

4242
@SuppressWarnings("serial")
4343
public class LibrariesIndexTableModel
44-
extends FilteredAbstractTableModel<ContributedLibrary> {
44+
extends FilteredAbstractTableModel<ContributedLibraryReleases> {
4545

4646
private final List<ContributedLibraryReleases> contributions = new ArrayList<>();
4747

4848
private final String[] columnNames = { "Description" };
4949

5050
private final Class<?>[] columnTypes = { ContributedPlatform.class };
5151

52-
Predicate<ContributedLibrary> selectedCategoryFilter = null;
52+
Predicate<ContributedLibraryReleases> selectedCategoryFilter = null;
5353
String selectedFilters[] = null;
5454

5555
public void updateIndexFilter(String filters[],
56-
Predicate<ContributedLibrary> additionalFilter) {
56+
Predicate<ContributedLibraryReleases> additionalFilter) {
5757
selectedCategoryFilter = additionalFilter;
5858
selectedFilters = filters;
5959
update();
@@ -86,17 +86,6 @@ private boolean stringContainsAll(String string, String filters[]) {
8686
return true;
8787
}
8888

89-
private void addContribution(ContributedLibrary lib) {
90-
for (ContributedLibraryReleases contribution : contributions) {
91-
if (!contribution.shouldContain(lib))
92-
continue;
93-
contribution.add(lib);
94-
return;
95-
}
96-
97-
contributions.add(new ContributedLibraryReleases(lib));
98-
}
99-
10089
@Override
10190
public int getColumnCount() {
10291
return columnNames.length;
@@ -149,17 +138,19 @@ public void update() {
149138
fireTableDataChanged();
150139
}
151140

152-
private void applyFilterToLibrary(ContributedLibrary lib) {
141+
private boolean filterCondition(ContributedLibraryReleases lib) {
153142
if (selectedCategoryFilter != null && !selectedCategoryFilter.test(lib)) {
154-
return;
143+
return false;
155144
}
156145

157-
String compoundTargetSearchText = lib.getName() + "\n" + lib.getParagraph()
158-
+ "\n" + lib.getSentence();
146+
ContributedLibrary latest = lib.getLatest();
147+
String compoundTargetSearchText = latest.getName() + "\n" + latest.getParagraph()
148+
+ "\n" + latest.getSentence();
159149
if (!stringContainsAll(compoundTargetSearchText, selectedFilters)) {
160-
return;
150+
return false;
161151
}
162-
addContribution(lib);
152+
153+
return true;
163154
}
164155

165156
public void updateLibrary(ContributedLibrary lib) {
@@ -189,12 +180,26 @@ public void updateLibrary(ContributedLibrary lib) {
189180
fireTableRowsDeleted(row, row);
190181
}
191182

183+
private List<ContributedLibraryReleases> rebuildContributionsFromIndex() {
184+
List<ContributedLibraryReleases> res = new ArrayList<>();
185+
BaseNoGui.librariesIndexer.getIndex().getLibraries(). //
186+
forEach(lib -> {
187+
for (ContributedLibraryReleases contribution : res) {
188+
if (!contribution.shouldContain(lib))
189+
continue;
190+
contribution.add(lib);
191+
return;
192+
}
193+
194+
res.add(new ContributedLibraryReleases(lib));
195+
});
196+
return res;
197+
}
198+
192199
private void updateContributions() {
200+
List<ContributedLibraryReleases> all = rebuildContributionsFromIndex();
193201
contributions.clear();
194-
BaseNoGui.librariesIndexer.getIndex().getLibraries()
195-
.forEach(this::applyFilterToLibrary);
196-
BaseNoGui.librariesIndexer.getInstalledLibraries()
197-
.forEach(this::applyFilterToLibrary);
202+
all.stream().filter(this::filterCondition).forEach(contributions::add);
198203
Collections.sort(contributions,
199204
new ContributedLibraryReleasesComparator("Arduino"));
200205
}

app/src/cc/arduino/contributions/libraries/ui/LibraryManagerUI.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@
4747
import javax.swing.JOptionPane;
4848
import javax.swing.table.TableCellRenderer;
4949

50-
import cc.arduino.contributions.DownloadableContribution;
5150
import cc.arduino.contributions.libraries.ContributedLibrary;
5251
import cc.arduino.contributions.libraries.LibraryInstaller;
5352
import cc.arduino.contributions.libraries.LibraryTypeComparator;
54-
import cc.arduino.contributions.ui.DropdownAllItem;
5553
import cc.arduino.contributions.ui.DropdownItem;
5654
import cc.arduino.contributions.ui.FilteredAbstractTableModel;
5755
import cc.arduino.contributions.ui.InstallerJDialog;
@@ -61,11 +59,11 @@
6159
import processing.app.BaseNoGui;
6260

6361
@SuppressWarnings("serial")
64-
public class LibraryManagerUI extends InstallerJDialog<ContributedLibrary> {
62+
public class LibraryManagerUI extends InstallerJDialog<ContributedLibraryReleases> {
6563

6664
private final JComboBox typeChooser;
6765
private final LibraryInstaller installer;
68-
private Predicate<ContributedLibrary> typeFilter;
66+
private Predicate<ContributedLibraryReleases> typeFilter;
6967

7068
@Override
7169
protected FilteredAbstractTableModel createContribModel() {
@@ -117,7 +115,7 @@ public LibraryManagerUI(Frame parent, LibraryInstaller installer) {
117115

118116
@Override
119117
public void actionPerformed(ActionEvent event) {
120-
DropdownItem<ContributedLibrary> selected = (DropdownItem<ContributedLibrary>) typeChooser.getSelectedItem();
118+
DropdownItem<ContributedLibraryReleases> selected = (DropdownItem<ContributedLibraryReleases>) typeChooser.getSelectedItem();
121119
previousRowAtPoint = -1;
122120
if (selected != null && typeFilter != selected.getFilterPredicate()) {
123121
typeFilter = selected.getFilterPredicate();
@@ -130,8 +128,8 @@ public void actionPerformed(ActionEvent event) {
130128
};
131129

132130
public void updateUI() {
133-
DropdownItem<DownloadableContribution> previouslySelectedCategory = (DropdownItem<DownloadableContribution>) categoryChooser.getSelectedItem();
134-
DropdownItem<DownloadableContribution> previouslySelectedType = (DropdownItem<DownloadableContribution>) typeChooser.getSelectedItem();
131+
DropdownItem<ContributedLibraryReleases> previouslySelectedCategory = (DropdownItem<ContributedLibraryReleases>) categoryChooser.getSelectedItem();
132+
DropdownItem<ContributedLibraryReleases> previouslySelectedType = (DropdownItem<ContributedLibraryReleases>) typeChooser.getSelectedItem();
135133

136134
categoryChooser.removeActionListener(categoryChooserActionListener);
137135
typeChooser.removeActionListener(typeChooserActionListener);
@@ -140,7 +138,7 @@ public void updateUI() {
140138
// Load categories
141139
categoryFilter = x -> true;
142140
categoryChooser.removeAllItems();
143-
categoryChooser.addItem(new DropdownAllItem());
141+
categoryChooser.addItem(new DropdownAllLibraries());
144142
Collection<String> categories = BaseNoGui.librariesIndexer.getIndex().getCategories();
145143
for (String category : categories) {
146144
categoryChooser.addItem(new DropdownLibraryOfCategoryItem(category));
@@ -157,7 +155,7 @@ public void updateUI() {
157155

158156
typeFilter = x -> true;
159157
typeChooser.removeAllItems();
160-
typeChooser.addItem(new DropdownAllItem());
158+
typeChooser.addItem(new DropdownAllLibraries());
161159
typeChooser.addItem(new DropdownUpdatableLibrariesItem());
162160
typeChooser.addItem(new DropdownInstalledLibraryItem());
163161
java.util.List<String> types = new LinkedList<>(BaseNoGui.librariesIndexer.getIndex().getTypes());

0 commit comments

Comments
 (0)