Skip to content

Commit 6e498ee

Browse files
author
Federico Fissore
committed
LibraryManager: better type filtering
1 parent 74a8ccd commit 6e498ee

File tree

14 files changed

+181
-76
lines changed

14 files changed

+181
-76
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cc.arduino.contributions.libraries.filters;
2+
3+
import cc.arduino.contributions.libraries.ContributedLibrary;
4+
import cc.arduino.contributions.libraries.LibrariesIndex;
5+
import com.google.common.base.Predicate;
6+
import com.google.common.collect.Collections2;
7+
8+
import java.util.Collection;
9+
10+
public class InstalledLibraryPredicate implements Predicate<ContributedLibrary> {
11+
12+
private final LibrariesIndex index;
13+
14+
public InstalledLibraryPredicate(LibrariesIndex index) {
15+
this.index = index;
16+
}
17+
18+
@Override
19+
public boolean apply(ContributedLibrary input) {
20+
if (input.isInstalled()) {
21+
return true;
22+
}
23+
24+
Collection<ContributedLibrary> installed = Collections2.filter(index.find(input.getName()), new Predicate<ContributedLibrary>() {
25+
@Override
26+
public boolean apply(ContributedLibrary input) {
27+
return input.isInstalled();
28+
}
29+
});
30+
31+
return !installed.isEmpty();
32+
}
33+
34+
@Override
35+
public boolean equals(Object obj) {
36+
return obj instanceof InstalledLibraryPredicate;
37+
}
38+
39+
}

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import cc.arduino.contributions.libraries.ContributedLibrary;
3434
import cc.arduino.contributions.libraries.ContributedLibraryComparator;
3535
import cc.arduino.contributions.libraries.filters.BuiltInPredicate;
36+
import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate;
3637
import cc.arduino.contributions.libraries.filters.OnlyUpstreamReleasePredicate;
3738
import cc.arduino.contributions.ui.InstallerTableCell;
3839
import cc.arduino.contributions.ui.listeners.DelegatingKeyListener;
@@ -65,6 +66,7 @@
6566
@SuppressWarnings("serial")
6667
public class ContributedLibraryTableCell extends InstallerTableCell {
6768

69+
private final LibraryManagerUI indexer;
6870
private JPanel panel;
6971
private JButton installButton;
7072
private Component installButtonPlaceholder;
@@ -75,7 +77,9 @@ public class ContributedLibraryTableCell extends InstallerTableCell {
7577
private JPanel inactiveButtonsPanel;
7678
private JLabel statusLabel;
7779

78-
public ContributedLibraryTableCell() {
80+
public ContributedLibraryTableCell(LibraryManagerUI indexer) {
81+
this.indexer = indexer;
82+
7983
{
8084
installButton = new JButton(_("Install"));
8185
installButton.addActionListener(new ActionListener() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cc.arduino.contributions.libraries.ui;
2+
3+
import cc.arduino.contributions.libraries.ContributedLibrary;
4+
import cc.arduino.contributions.libraries.LibrariesIndex;
5+
import cc.arduino.contributions.libraries.filters.InstalledLibraryPredicate;
6+
import cc.arduino.contributions.ui.DropdownItem;
7+
import com.google.common.base.Predicate;
8+
9+
import static processing.app.I18n._;
10+
11+
public class DropdownInstalledLibraryItem implements DropdownItem<ContributedLibrary> {
12+
13+
private final LibrariesIndex index;
14+
15+
public DropdownInstalledLibraryItem(LibrariesIndex index) {
16+
this.index = index;
17+
}
18+
19+
public String toString() {
20+
return _("Installed");
21+
}
22+
23+
@Override
24+
public Predicate<ContributedLibrary> getFilterPredicate() {
25+
return new InstalledLibraryPredicate(index);
26+
}
27+
}

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
import java.awt.*;
4040
import java.awt.event.ActionEvent;
4141
import java.awt.event.ActionListener;
42-
import java.util.Arrays;
4342
import java.util.Collection;
4443

4544
import static processing.app.I18n._;
@@ -62,12 +61,12 @@ private LibrariesIndexTableModel getContribModel() {
6261

6362
@Override
6463
protected InstallerTableCell createCellRenderer() {
65-
return new ContributedLibraryTableCell();
64+
return new ContributedLibraryTableCell(this);
6665
}
6766

6867
@Override
6968
protected InstallerTableCell createCellEditor() {
70-
return new ContributedLibraryTableCell() {
69+
return new ContributedLibraryTableCell(this) {
7170
@Override
7271
protected void onInstall(ContributedLibrary selectedLibrary, ContributedLibrary installedLibrary) {
7372
if (selectedLibrary.isReadOnly()) {
@@ -150,7 +149,7 @@ public void setIndexer(LibrariesIndexer indexer) {
150149
typeFilter = null;
151150
typeChooser.removeAllItems();
152151
typeChooser.addItem(new DropdownAllItem());
153-
typeChooser.addItem(new DropdownInstalledContributionItem());
152+
typeChooser.addItem(new DropdownInstalledLibraryItem(indexer.getIndex()));
154153
Collection<String> types = indexer.getIndex().getTypes();
155154
for (String type : types) {
156155
typeChooser.addItem(new DropdownLibraryOfTypeItem(type));
@@ -169,6 +168,10 @@ public void onProgress(Progress progress) {
169168
};
170169
}
171170

171+
public LibrariesIndexer getIndexer() {
172+
return indexer;
173+
}
174+
172175
public void setProgress(Progress progress) {
173176
progressBar.setValue(progress);
174177
}

app/src/cc/arduino/contributions/ui/DropdownInstalledContributionItem.java

-20
This file was deleted.

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndex.java

+16-2
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,32 @@
2626
* invalidate any other reasons why the executable file might be covered by
2727
* the GNU General Public License.
2828
*/
29+
2930
package cc.arduino.contributions.libraries;
3031

32+
import com.google.common.base.Predicate;
33+
import com.google.common.collect.Collections2;
34+
3135
import java.util.*;
3236

3337
public abstract class LibrariesIndex {
3438

3539
public abstract List<ContributedLibrary> getLibraries();
3640

41+
public List<ContributedLibrary> find(final String name) {
42+
return new LinkedList<ContributedLibrary>(Collections2.filter(getLibraries(), new Predicate<ContributedLibrary>() {
43+
@Override
44+
public boolean apply(ContributedLibrary contributedLibrary) {
45+
return name.equals(contributedLibrary.getName());
46+
}
47+
}));
48+
}
49+
3750
public ContributedLibrary find(String name, String version) {
38-
for (ContributedLibrary lib : getLibraries()) {
39-
if (lib.getName().equals(name) && lib.getVersion().equals(version))
51+
for (ContributedLibrary lib : find(name)) {
52+
if (lib.getVersion().equals(version)) {
4053
return lib;
54+
}
4155
}
4256
return null;
4357
}

arduino-core/src/cc/arduino/contributions/libraries/LibrariesIndexer.java

+27-22
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,9 @@
2828
*/
2929
package cc.arduino.contributions.libraries;
3030

31-
import static processing.app.I18n._;
32-
33-
import java.io.File;
34-
import java.io.FileInputStream;
35-
import java.io.IOException;
36-
import java.io.InputStream;
37-
import java.util.List;
38-
31+
import com.fasterxml.jackson.databind.DeserializationFeature;
32+
import com.fasterxml.jackson.databind.ObjectMapper;
33+
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
3934
import processing.app.BaseNoGui;
4035
import processing.app.I18n;
4136
import processing.app.helpers.FileUtils;
@@ -44,9 +39,14 @@
4439
import processing.app.packages.LibraryList;
4540
import processing.app.packages.UserLibrary;
4641

47-
import com.fasterxml.jackson.databind.DeserializationFeature;
48-
import com.fasterxml.jackson.databind.ObjectMapper;
49-
import com.fasterxml.jackson.module.mrbean.MrBeanModule;
42+
import java.io.File;
43+
import java.io.FileInputStream;
44+
import java.io.IOException;
45+
import java.io.InputStream;
46+
import java.util.Arrays;
47+
import java.util.List;
48+
49+
import static processing.app.I18n._;
5050

5151
public class LibrariesIndexer {
5252

@@ -60,7 +60,7 @@ public class LibrariesIndexer {
6060
public LibrariesIndexer(File preferencesFolder) {
6161
indexFile = new File(preferencesFolder, "library_index.json");
6262
stagingFolder = new File(preferencesFolder, "staging" + File.separator +
63-
"libraries");
63+
"libraries");
6464
}
6565

6666
public void parseIndex() throws IOException {
@@ -109,31 +109,28 @@ private void scanInstalledLibraries(File folder) {
109109
for (File subfolder : list) {
110110
if (!BaseNoGui.isSanitaryName(subfolder.getName())) {
111111
String mess = I18n.format(_("The library \"{0}\" cannot be used.\n"
112-
+ "Library names must contain only basic letters and numbers.\n"
113-
+ "(ASCII only and no spaces, and it cannot start with a number)"),
114-
subfolder.getName());
112+
+ "Library names must contain only basic letters and numbers.\n"
113+
+ "(ASCII only and no spaces, and it cannot start with a number)"),
114+
subfolder.getName());
115115
BaseNoGui.showMessage(_("Ignoring bad library name"), mess);
116116
continue;
117117
}
118118

119119
try {
120120
scanLibrary(subfolder);
121121
} catch (IOException e) {
122-
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"),
123-
subfolder, e.getMessage()));
122+
System.out.println(I18n.format(_("Invalid library found in {0}: {1}"), subfolder, e.getMessage()));
124123
}
125124
}
126125
}
127126

128127
private void scanLibrary(File folder) throws IOException {
129-
boolean readOnly = !FileUtils
130-
.isSubDirectory(sketchbookLibrariesFolder, folder);
128+
boolean readOnly = !FileUtils.isSubDirectory(sketchbookLibrariesFolder, folder);
131129

132130
// A library is considered "legacy" if it doesn't contains
133131
// a file called "library.properties"
134132
File check = new File(folder, "library.properties");
135133
if (!check.exists() || !check.isFile()) {
136-
137134
// Create a legacy library and exit
138135
LegacyUserLibrary lib = LegacyUserLibrary.create(folder);
139136
lib.setReadOnly(readOnly);
@@ -148,12 +145,20 @@ private void scanLibrary(File folder) throws IOException {
148145

149146
// Check if we can find the same library in the index
150147
// and mark it as installed
151-
String libName = folder.getName(); // XXX: lib.getName()?
152-
ContributedLibrary foundLib = index.find(libName, lib.getVersion());
148+
ContributedLibrary foundLib = index.find(lib.getName(), lib.getVersion());
153149
if (foundLib != null) {
154150
foundLib.setInstalled(true);
155151
foundLib.setInstalledFolder(folder);
156152
foundLib.setReadOnly(readOnly);
153+
lib.setTypes(foundLib.getTypes());
154+
}
155+
156+
if (lib.isReadOnly() && lib.getTypes() == null && !lib.getDeclaredTypes().isEmpty()) {
157+
lib.setTypes(lib.getDeclaredTypes());
158+
}
159+
160+
if (lib.getTypes() == null) {
161+
lib.setTypes(Arrays.asList("Contributed"));
157162
}
158163
}
159164

0 commit comments

Comments
 (0)