Skip to content

Commit 7235f9d

Browse files
Improve Examples menu
1 parent 3db7ec2 commit 7235f9d

File tree

1 file changed

+129
-36
lines changed

1 file changed

+129
-36
lines changed

app/src/processing/app/Base.java

Lines changed: 129 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@
8080
*/
8181
public class Base {
8282

83-
public static final Predicate<UserLibrary> CONTRIBUTED = library -> library.getTypes() == null || library.getTypes().isEmpty() || library.getTypes().contains("Contributed");
84-
public static final Predicate<UserLibrary> RETIRED = library -> library.getTypes() != null && library.getTypes().contains("Retired");
85-
public static final Predicate<UserLibrary> COMPATIBLE = library -> library.getArchitectures() != null && (library.getArchitectures().contains("*") || library.getArchitectures().contains(BaseNoGui.getTargetPlatform().getId()));
86-
8783
private static final int RECENT_SKETCHES_MAX_SIZE = 10;
8884

8985
private static boolean commandLine;
@@ -1100,30 +1096,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
11001096
}
11011097
}
11021098

1103-
public LibraryList getIDELibs() {
1104-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1105-
List<UserLibrary> libs = installedLibraries.stream()
1106-
.filter(CONTRIBUTED.negate())
1107-
.filter(RETIRED.negate())
1108-
.filter(COMPATIBLE)
1109-
.collect(Collectors.toList());
1110-
return new LibraryList(libs);
1111-
}
1112-
1113-
public LibraryList getIDERetiredLibs() {
1114-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1115-
List<UserLibrary> libs = installedLibraries.stream()
1116-
.filter(RETIRED)
1117-
.collect(Collectors.toList());
1118-
return new LibraryList(libs);
1119-
}
1120-
1121-
public LibraryList getUserLibs() {
1122-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1123-
List<UserLibrary> libs = installedLibraries.stream().filter(CONTRIBUTED).collect(Collectors.toList());
1124-
return new LibraryList(libs);
1125-
}
1126-
11271099
private List<ContributedLibrary> getSortedLibraries() {
11281100
List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
11291101
Collections.sort(installedLibraries, new LibraryByTypeComparator());
@@ -1206,36 +1178,157 @@ public void rebuildExamplesMenu(JMenu menu) {
12061178
menu.addSeparator();
12071179
}
12081180

1181+
// Libraries can come from 4 locations: collect info about all four
1182+
File ideLibraryPath = BaseNoGui.getContentFile("libraries");
1183+
File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder();
1184+
File platformLibraryPath = null;
1185+
File referencedPlatformLibraryPath = null;
1186+
String platformName = null;
1187+
String referencedPlatformName = null;
1188+
String myArch = null;
1189+
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
1190+
if (targetPlatform != null) {
1191+
myArch = targetPlatform.getId();
1192+
platformName = targetPlatform.getPreferences().get("name");
1193+
platformLibraryPath = new File(targetPlatform.getFolder(), "libraries");
1194+
String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino");
1195+
if (core.contains(":")) {
1196+
String refcore = core.split(":")[0];
1197+
TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch);
1198+
if (referencedPlatform != null) {
1199+
referencedPlatformName = referencedPlatform.getPreferences().get("name");
1200+
referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries");
1201+
}
1202+
}
1203+
}
1204+
1205+
// Divide the libraries into 7 lists, corresponding to the 4 locations
1206+
// with the retired IDE libs further divided into their own list, and
1207+
// any incompatible sketchbook libs further divided into their own list.
1208+
// The 7th list of "other" libraries should always be empty, but serves
1209+
// as a safety feature to prevent any library from vanishing.
1210+
LibraryList allLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1211+
LibraryList ideLibs = new LibraryList();
1212+
LibraryList retiredIdeLibs = new LibraryList();
1213+
LibraryList platformLibs = new LibraryList();
1214+
LibraryList referencedPlatformLibs = new LibraryList();
1215+
LibraryList sketchbookLibs = new LibraryList();
1216+
LibraryList sketchbookIncompatibleLibs = new LibraryList();
1217+
LibraryList otherLibs = new LibraryList();
1218+
for (UserLibrary lib : allLibraries) {
1219+
// Get the library's location - used for sorting into categories
1220+
File libraryLocation = lib.getInstalledFolder().getParentFile();
1221+
// Is this library compatible?
1222+
List<String> arch = lib.getArchitectures();
1223+
boolean compatible;
1224+
if (myArch == null || arch == null || arch.contains("*")) {
1225+
compatible = true;
1226+
} else {
1227+
compatible = arch.contains(myArch);
1228+
}
1229+
// IDE Libaries (including retired)
1230+
if (libraryLocation.equals(ideLibraryPath)) {
1231+
if (compatible) {
1232+
// only compatible IDE libs are shown
1233+
boolean retired = false;
1234+
List<String> types = lib.getTypes();
1235+
if (types != null) retired = types.contains("Retired");
1236+
if (retired) {
1237+
retiredIdeLibs.add(lib);
1238+
} else {
1239+
ideLibs.add(lib);
1240+
}
1241+
}
1242+
// Platform Libraries
1243+
} else if (libraryLocation.equals(platformLibraryPath)) {
1244+
// all platform libs are assumed to be compatible
1245+
platformLibs.add(lib);
1246+
// Referenced Platform Libraries
1247+
} else if (libraryLocation.equals(referencedPlatformLibraryPath)) {
1248+
// all referenced platform libs are assumed to be compatible
1249+
referencedPlatformLibs.add(lib);
1250+
// Sketchbook Libraries (including incompatible)
1251+
} else if (libraryLocation.equals(sketchbookLibraryPath)) {
1252+
if (compatible) {
1253+
sketchbookLibs.add(lib);
1254+
} else {
1255+
sketchbookIncompatibleLibs.add(lib);
1256+
}
1257+
// Other libraries of unknown type (should never occur)
1258+
} else {
1259+
otherLibs.add(lib);
1260+
}
1261+
}
1262+
12091263
// Add examples from libraries
1210-
LibraryList ideLibs = getIDELibs();
12111264
ideLibs.sort();
12121265
if (!ideLibs.isEmpty()) {
1213-
label = new JMenuItem(tr("Examples from Libraries"));
1266+
label = new JMenuItem(tr("Examples from Built-in Libraries"));
12141267
label.setEnabled(false);
12151268
menu.add(label);
12161269
}
12171270
for (UserLibrary lib : ideLibs) {
12181271
addSketchesSubmenu(menu, lib);
12191272
}
12201273

1221-
LibraryList retiredIdeLibs = getIDERetiredLibs();
1222-
retiredIdeLibs.sort();
12231274
if (!retiredIdeLibs.isEmpty()) {
1275+
retiredIdeLibs.sort();
12241276
JMenu retired = new JMenu(tr("RETIRED"));
12251277
menu.add(retired);
12261278
for (UserLibrary lib : retiredIdeLibs) {
12271279
addSketchesSubmenu(retired, lib);
12281280
}
12291281
}
12301282

1231-
LibraryList userLibs = getUserLibs();
1232-
if (userLibs.size() > 0) {
1283+
if (!platformLibs.isEmpty()) {
12331284
menu.addSeparator();
1234-
userLibs.sort();
1285+
platformLibs.sort();
1286+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName));
1287+
label.setEnabled(false);
1288+
menu.add(label);
1289+
for (UserLibrary lib : platformLibs) {
1290+
addSketchesSubmenu(menu, lib);
1291+
}
1292+
}
1293+
1294+
if (!referencedPlatformLibs.isEmpty()) {
1295+
menu.addSeparator();
1296+
referencedPlatformLibs.sort();
1297+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName));
1298+
label.setEnabled(false);
1299+
menu.add(label);
1300+
for (UserLibrary lib : referencedPlatformLibs) {
1301+
addSketchesSubmenu(menu, lib);
1302+
}
1303+
}
1304+
1305+
if (!sketchbookLibs.isEmpty()) {
1306+
menu.addSeparator();
1307+
sketchbookLibs.sort();
12351308
label = new JMenuItem(tr("Examples from Custom Libraries"));
12361309
label.setEnabled(false);
12371310
menu.add(label);
1238-
for (UserLibrary lib : userLibs) {
1311+
for (UserLibrary lib : sketchbookLibs) {
1312+
addSketchesSubmenu(menu, lib);
1313+
}
1314+
}
1315+
1316+
if (!sketchbookIncompatibleLibs.isEmpty()) {
1317+
sketchbookIncompatibleLibs.sort();
1318+
JMenu incompatible = new JMenu(tr("INCOMPATIBLE"));
1319+
menu.add(incompatible);
1320+
for (UserLibrary lib : sketchbookIncompatibleLibs) {
1321+
addSketchesSubmenu(incompatible, lib);
1322+
}
1323+
}
1324+
1325+
if (!otherLibs.isEmpty()) {
1326+
menu.addSeparator();
1327+
otherLibs.sort();
1328+
label = new JMenuItem(tr("Examples from Other Libraries"));
1329+
label.setEnabled(false);
1330+
menu.add(label);
1331+
for (UserLibrary lib : otherLibs) {
12391332
addSketchesSubmenu(menu, lib);
12401333
}
12411334
}

0 commit comments

Comments
 (0)