Skip to content

Commit d49401b

Browse files
PaulStoffregencmaglie
authored andcommitted
Improve Examples menu
1 parent c1291ee commit d49401b

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;
@@ -1053,30 +1049,6 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10531049
}
10541050
}
10551051

1056-
public LibraryList getIDELibs() {
1057-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1058-
List<UserLibrary> libs = installedLibraries.stream()
1059-
.filter(CONTRIBUTED.negate())
1060-
.filter(RETIRED.negate())
1061-
.filter(COMPATIBLE)
1062-
.collect(Collectors.toList());
1063-
return new LibraryList(libs);
1064-
}
1065-
1066-
public LibraryList getIDERetiredLibs() {
1067-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1068-
List<UserLibrary> libs = installedLibraries.stream()
1069-
.filter(RETIRED)
1070-
.collect(Collectors.toList());
1071-
return new LibraryList(libs);
1072-
}
1073-
1074-
public LibraryList getUserLibs() {
1075-
LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1076-
List<UserLibrary> libs = installedLibraries.stream().filter(CONTRIBUTED).collect(Collectors.toList());
1077-
return new LibraryList(libs);
1078-
}
1079-
10801052
private List<ContributedLibrary> getSortedLibraries() {
10811053
List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
10821054
Collections.sort(installedLibraries, new LibraryByTypeComparator());
@@ -1159,36 +1131,157 @@ public void rebuildExamplesMenu(JMenu menu) {
11591131
menu.addSeparator();
11601132
}
11611133

1134+
// Libraries can come from 4 locations: collect info about all four
1135+
File ideLibraryPath = BaseNoGui.getContentFile("libraries");
1136+
File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder();
1137+
File platformLibraryPath = null;
1138+
File referencedPlatformLibraryPath = null;
1139+
String platformName = null;
1140+
String referencedPlatformName = null;
1141+
String myArch = null;
1142+
TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform();
1143+
if (targetPlatform != null) {
1144+
myArch = targetPlatform.getId();
1145+
platformName = targetPlatform.getPreferences().get("name");
1146+
platformLibraryPath = new File(targetPlatform.getFolder(), "libraries");
1147+
String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino");
1148+
if (core.contains(":")) {
1149+
String refcore = core.split(":")[0];
1150+
TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch);
1151+
if (referencedPlatform != null) {
1152+
referencedPlatformName = referencedPlatform.getPreferences().get("name");
1153+
referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries");
1154+
}
1155+
}
1156+
}
1157+
1158+
// Divide the libraries into 7 lists, corresponding to the 4 locations
1159+
// with the retired IDE libs further divided into their own list, and
1160+
// any incompatible sketchbook libs further divided into their own list.
1161+
// The 7th list of "other" libraries should always be empty, but serves
1162+
// as a safety feature to prevent any library from vanishing.
1163+
LibraryList allLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries());
1164+
LibraryList ideLibs = new LibraryList();
1165+
LibraryList retiredIdeLibs = new LibraryList();
1166+
LibraryList platformLibs = new LibraryList();
1167+
LibraryList referencedPlatformLibs = new LibraryList();
1168+
LibraryList sketchbookLibs = new LibraryList();
1169+
LibraryList sketchbookIncompatibleLibs = new LibraryList();
1170+
LibraryList otherLibs = new LibraryList();
1171+
for (UserLibrary lib : allLibraries) {
1172+
// Get the library's location - used for sorting into categories
1173+
File libraryLocation = lib.getInstalledFolder().getParentFile();
1174+
// Is this library compatible?
1175+
List<String> arch = lib.getArchitectures();
1176+
boolean compatible;
1177+
if (myArch == null || arch == null || arch.contains("*")) {
1178+
compatible = true;
1179+
} else {
1180+
compatible = arch.contains(myArch);
1181+
}
1182+
// IDE Libaries (including retired)
1183+
if (libraryLocation.equals(ideLibraryPath)) {
1184+
if (compatible) {
1185+
// only compatible IDE libs are shown
1186+
boolean retired = false;
1187+
List<String> types = lib.getTypes();
1188+
if (types != null) retired = types.contains("Retired");
1189+
if (retired) {
1190+
retiredIdeLibs.add(lib);
1191+
} else {
1192+
ideLibs.add(lib);
1193+
}
1194+
}
1195+
// Platform Libraries
1196+
} else if (libraryLocation.equals(platformLibraryPath)) {
1197+
// all platform libs are assumed to be compatible
1198+
platformLibs.add(lib);
1199+
// Referenced Platform Libraries
1200+
} else if (libraryLocation.equals(referencedPlatformLibraryPath)) {
1201+
// all referenced platform libs are assumed to be compatible
1202+
referencedPlatformLibs.add(lib);
1203+
// Sketchbook Libraries (including incompatible)
1204+
} else if (libraryLocation.equals(sketchbookLibraryPath)) {
1205+
if (compatible) {
1206+
sketchbookLibs.add(lib);
1207+
} else {
1208+
sketchbookIncompatibleLibs.add(lib);
1209+
}
1210+
// Other libraries of unknown type (should never occur)
1211+
} else {
1212+
otherLibs.add(lib);
1213+
}
1214+
}
1215+
11621216
// Add examples from libraries
1163-
LibraryList ideLibs = getIDELibs();
11641217
ideLibs.sort();
11651218
if (!ideLibs.isEmpty()) {
1166-
label = new JMenuItem(tr("Examples from Libraries"));
1219+
label = new JMenuItem(tr("Examples from Built-in Libraries"));
11671220
label.setEnabled(false);
11681221
menu.add(label);
11691222
}
11701223
for (UserLibrary lib : ideLibs) {
11711224
addSketchesSubmenu(menu, lib);
11721225
}
11731226

1174-
LibraryList retiredIdeLibs = getIDERetiredLibs();
1175-
retiredIdeLibs.sort();
11761227
if (!retiredIdeLibs.isEmpty()) {
1228+
retiredIdeLibs.sort();
11771229
JMenu retired = new JMenu(tr("RETIRED"));
11781230
menu.add(retired);
11791231
for (UserLibrary lib : retiredIdeLibs) {
11801232
addSketchesSubmenu(retired, lib);
11811233
}
11821234
}
11831235

1184-
LibraryList userLibs = getUserLibs();
1185-
if (userLibs.size() > 0) {
1236+
if (!platformLibs.isEmpty()) {
11861237
menu.addSeparator();
1187-
userLibs.sort();
1238+
platformLibs.sort();
1239+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName));
1240+
label.setEnabled(false);
1241+
menu.add(label);
1242+
for (UserLibrary lib : platformLibs) {
1243+
addSketchesSubmenu(menu, lib);
1244+
}
1245+
}
1246+
1247+
if (!referencedPlatformLibs.isEmpty()) {
1248+
menu.addSeparator();
1249+
referencedPlatformLibs.sort();
1250+
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName));
1251+
label.setEnabled(false);
1252+
menu.add(label);
1253+
for (UserLibrary lib : referencedPlatformLibs) {
1254+
addSketchesSubmenu(menu, lib);
1255+
}
1256+
}
1257+
1258+
if (!sketchbookLibs.isEmpty()) {
1259+
menu.addSeparator();
1260+
sketchbookLibs.sort();
11881261
label = new JMenuItem(tr("Examples from Custom Libraries"));
11891262
label.setEnabled(false);
11901263
menu.add(label);
1191-
for (UserLibrary lib : userLibs) {
1264+
for (UserLibrary lib : sketchbookLibs) {
1265+
addSketchesSubmenu(menu, lib);
1266+
}
1267+
}
1268+
1269+
if (!sketchbookIncompatibleLibs.isEmpty()) {
1270+
sketchbookIncompatibleLibs.sort();
1271+
JMenu incompatible = new JMenu(tr("INCOMPATIBLE"));
1272+
menu.add(incompatible);
1273+
for (UserLibrary lib : sketchbookIncompatibleLibs) {
1274+
addSketchesSubmenu(incompatible, lib);
1275+
}
1276+
}
1277+
1278+
if (!otherLibs.isEmpty()) {
1279+
menu.addSeparator();
1280+
otherLibs.sort();
1281+
label = new JMenuItem(tr("Examples from Other Libraries"));
1282+
label.setEnabled(false);
1283+
menu.add(label);
1284+
for (UserLibrary lib : otherLibs) {
11921285
addSketchesSubmenu(menu, lib);
11931286
}
11941287
}

0 commit comments

Comments
 (0)