Skip to content

Commit 59e7c9e

Browse files
committed
Revert "Improve Examples menu"
This reverts commit 7235f9d.
1 parent b583697 commit 59e7c9e

File tree

1 file changed

+36
-129
lines changed

1 file changed

+36
-129
lines changed

app/src/processing/app/Base.java

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

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

8488
private static boolean commandLine;
@@ -1097,6 +1101,30 @@ protected void rebuildSketchbookMenu(JMenu menu) {
10971101
}
10981102
}
10991103

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

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

1222+
LibraryList retiredIdeLibs = getIDERetiredLibs();
1223+
retiredIdeLibs.sort();
12751224
if (!retiredIdeLibs.isEmpty()) {
1276-
retiredIdeLibs.sort();
12771225
JMenu retired = new JMenu(tr("RETIRED"));
12781226
menu.add(retired);
12791227
for (UserLibrary lib : retiredIdeLibs) {
12801228
addSketchesSubmenu(retired, lib);
12811229
}
12821230
}
12831231

1284-
if (!platformLibs.isEmpty()) {
1232+
LibraryList userLibs = getUserLibs();
1233+
if (userLibs.size() > 0) {
12851234
menu.addSeparator();
1286-
platformLibs.sort();
1287-
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName));
1288-
label.setEnabled(false);
1289-
menu.add(label);
1290-
for (UserLibrary lib : platformLibs) {
1291-
addSketchesSubmenu(menu, lib);
1292-
}
1293-
}
1294-
1295-
if (!referencedPlatformLibs.isEmpty()) {
1296-
menu.addSeparator();
1297-
referencedPlatformLibs.sort();
1298-
label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName));
1299-
label.setEnabled(false);
1300-
menu.add(label);
1301-
for (UserLibrary lib : referencedPlatformLibs) {
1302-
addSketchesSubmenu(menu, lib);
1303-
}
1304-
}
1305-
1306-
if (!sketchbookLibs.isEmpty()) {
1307-
menu.addSeparator();
1308-
sketchbookLibs.sort();
1235+
userLibs.sort();
13091236
label = new JMenuItem(tr("Examples from Custom Libraries"));
13101237
label.setEnabled(false);
13111238
menu.add(label);
1312-
for (UserLibrary lib : sketchbookLibs) {
1313-
addSketchesSubmenu(menu, lib);
1314-
}
1315-
}
1316-
1317-
if (!sketchbookIncompatibleLibs.isEmpty()) {
1318-
sketchbookIncompatibleLibs.sort();
1319-
JMenu incompatible = new JMenu(tr("INCOMPATIBLE"));
1320-
menu.add(incompatible);
1321-
for (UserLibrary lib : sketchbookIncompatibleLibs) {
1322-
addSketchesSubmenu(incompatible, lib);
1323-
}
1324-
}
1325-
1326-
if (!otherLibs.isEmpty()) {
1327-
menu.addSeparator();
1328-
otherLibs.sort();
1329-
label = new JMenuItem(tr("Examples from Other Libraries"));
1330-
label.setEnabled(false);
1331-
menu.add(label);
1332-
for (UserLibrary lib : otherLibs) {
1239+
for (UserLibrary lib : userLibs) {
13331240
addSketchesSubmenu(menu, lib);
13341241
}
13351242
}

0 commit comments

Comments
 (0)