|
79 | 79 | */
|
80 | 80 | public class Base {
|
81 | 81 |
|
| 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 | + |
82 | 86 | private static final int RECENT_SKETCHES_MAX_SIZE = 10;
|
83 | 87 |
|
84 | 88 | private static boolean commandLine;
|
@@ -1097,6 +1101,30 @@ protected void rebuildSketchbookMenu(JMenu menu) {
|
1097 | 1101 | }
|
1098 | 1102 | }
|
1099 | 1103 |
|
| 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 | + |
1100 | 1128 | private List<ContributedLibrary> getSortedLibraries() {
|
1101 | 1129 | List<ContributedLibrary> installedLibraries = new LinkedList<ContributedLibrary>(BaseNoGui.librariesIndexer.getInstalledLibraries());
|
1102 | 1130 | Collections.sort(installedLibraries, new LibraryByTypeComparator());
|
@@ -1179,157 +1207,36 @@ public void rebuildExamplesMenu(JMenu menu) {
|
1179 | 1207 | menu.addSeparator();
|
1180 | 1208 | }
|
1181 | 1209 |
|
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 |
| - |
1264 | 1210 | // Add examples from libraries
|
| 1211 | + LibraryList ideLibs = getIDELibs(); |
1265 | 1212 | ideLibs.sort();
|
1266 | 1213 | if (!ideLibs.isEmpty()) {
|
1267 |
| - label = new JMenuItem(tr("Examples from Built-in Libraries")); |
| 1214 | + label = new JMenuItem(tr("Examples from Libraries")); |
1268 | 1215 | label.setEnabled(false);
|
1269 | 1216 | menu.add(label);
|
1270 | 1217 | }
|
1271 | 1218 | for (UserLibrary lib : ideLibs) {
|
1272 | 1219 | addSketchesSubmenu(menu, lib);
|
1273 | 1220 | }
|
1274 | 1221 |
|
| 1222 | + LibraryList retiredIdeLibs = getIDERetiredLibs(); |
| 1223 | + retiredIdeLibs.sort(); |
1275 | 1224 | if (!retiredIdeLibs.isEmpty()) {
|
1276 |
| - retiredIdeLibs.sort(); |
1277 | 1225 | JMenu retired = new JMenu(tr("RETIRED"));
|
1278 | 1226 | menu.add(retired);
|
1279 | 1227 | for (UserLibrary lib : retiredIdeLibs) {
|
1280 | 1228 | addSketchesSubmenu(retired, lib);
|
1281 | 1229 | }
|
1282 | 1230 | }
|
1283 | 1231 |
|
1284 |
| - if (!platformLibs.isEmpty()) { |
| 1232 | + LibraryList userLibs = getUserLibs(); |
| 1233 | + if (userLibs.size() > 0) { |
1285 | 1234 | 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(); |
1309 | 1236 | label = new JMenuItem(tr("Examples from Custom Libraries"));
|
1310 | 1237 | label.setEnabled(false);
|
1311 | 1238 | 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) { |
1333 | 1240 | addSketchesSubmenu(menu, lib);
|
1334 | 1241 | }
|
1335 | 1242 | }
|
|
0 commit comments