From d49401bdbfa0bf4675e236eba2244c31c8406c09 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Thu, 28 Jul 2016 16:58:28 -0700 Subject: [PATCH 1/4] Improve Examples menu --- app/src/processing/app/Base.java | 165 ++++++++++++++++++++++++------- 1 file changed, 129 insertions(+), 36 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 6376be835..514e10701 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -80,10 +80,6 @@ import static processing.app.I18n.tr; */ public class Base { - public static final Predicate CONTRIBUTED = library -> library.getTypes() == null || library.getTypes().isEmpty() || library.getTypes().contains("Contributed"); - public static final Predicate RETIRED = library -> library.getTypes() != null && library.getTypes().contains("Retired"); - public static final Predicate COMPATIBLE = library -> library.getArchitectures() != null && (library.getArchitectures().contains("*") || library.getArchitectures().contains(BaseNoGui.getTargetPlatform().getId())); - private static final int RECENT_SKETCHES_MAX_SIZE = 10; private static boolean commandLine; @@ -1053,30 +1049,6 @@ public class Base { } } - public LibraryList getIDELibs() { - LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries()); - List libs = installedLibraries.stream() - .filter(CONTRIBUTED.negate()) - .filter(RETIRED.negate()) - .filter(COMPATIBLE) - .collect(Collectors.toList()); - return new LibraryList(libs); - } - - public LibraryList getIDERetiredLibs() { - LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries()); - List libs = installedLibraries.stream() - .filter(RETIRED) - .collect(Collectors.toList()); - return new LibraryList(libs); - } - - public LibraryList getUserLibs() { - LibraryList installedLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries()); - List libs = installedLibraries.stream().filter(CONTRIBUTED).collect(Collectors.toList()); - return new LibraryList(libs); - } - private List getSortedLibraries() { List installedLibraries = new LinkedList(BaseNoGui.librariesIndexer.getInstalledLibraries()); Collections.sort(installedLibraries, new LibraryByTypeComparator()); @@ -1159,11 +1131,92 @@ public class Base { menu.addSeparator(); } + // Libraries can come from 4 locations: collect info about all four + File ideLibraryPath = BaseNoGui.getContentFile("libraries"); + File sketchbookLibraryPath = BaseNoGui.getSketchbookLibrariesFolder(); + File platformLibraryPath = null; + File referencedPlatformLibraryPath = null; + String platformName = null; + String referencedPlatformName = null; + String myArch = null; + TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform(); + if (targetPlatform != null) { + myArch = targetPlatform.getId(); + platformName = targetPlatform.getPreferences().get("name"); + platformLibraryPath = new File(targetPlatform.getFolder(), "libraries"); + String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino"); + if (core.contains(":")) { + String refcore = core.split(":")[0]; + TargetPlatform referencedPlatform = BaseNoGui.getTargetPlatform(refcore, myArch); + if (referencedPlatform != null) { + referencedPlatformName = referencedPlatform.getPreferences().get("name"); + referencedPlatformLibraryPath = new File(referencedPlatform.getFolder(), "libraries"); + } + } + } + + // Divide the libraries into 7 lists, corresponding to the 4 locations + // with the retired IDE libs further divided into their own list, and + // any incompatible sketchbook libs further divided into their own list. + // The 7th list of "other" libraries should always be empty, but serves + // as a safety feature to prevent any library from vanishing. + LibraryList allLibraries = new LibraryList(BaseNoGui.librariesIndexer.getInstalledLibraries()); + LibraryList ideLibs = new LibraryList(); + LibraryList retiredIdeLibs = new LibraryList(); + LibraryList platformLibs = new LibraryList(); + LibraryList referencedPlatformLibs = new LibraryList(); + LibraryList sketchbookLibs = new LibraryList(); + LibraryList sketchbookIncompatibleLibs = new LibraryList(); + LibraryList otherLibs = new LibraryList(); + for (UserLibrary lib : allLibraries) { + // Get the library's location - used for sorting into categories + File libraryLocation = lib.getInstalledFolder().getParentFile(); + // Is this library compatible? + List arch = lib.getArchitectures(); + boolean compatible; + if (myArch == null || arch == null || arch.contains("*")) { + compatible = true; + } else { + compatible = arch.contains(myArch); + } + // IDE Libaries (including retired) + if (libraryLocation.equals(ideLibraryPath)) { + if (compatible) { + // only compatible IDE libs are shown + boolean retired = false; + List types = lib.getTypes(); + if (types != null) retired = types.contains("Retired"); + if (retired) { + retiredIdeLibs.add(lib); + } else { + ideLibs.add(lib); + } + } + // Platform Libraries + } else if (libraryLocation.equals(platformLibraryPath)) { + // all platform libs are assumed to be compatible + platformLibs.add(lib); + // Referenced Platform Libraries + } else if (libraryLocation.equals(referencedPlatformLibraryPath)) { + // all referenced platform libs are assumed to be compatible + referencedPlatformLibs.add(lib); + // Sketchbook Libraries (including incompatible) + } else if (libraryLocation.equals(sketchbookLibraryPath)) { + if (compatible) { + sketchbookLibs.add(lib); + } else { + sketchbookIncompatibleLibs.add(lib); + } + // Other libraries of unknown type (should never occur) + } else { + otherLibs.add(lib); + } + } + // Add examples from libraries - LibraryList ideLibs = getIDELibs(); ideLibs.sort(); if (!ideLibs.isEmpty()) { - label = new JMenuItem(tr("Examples from Libraries")); + label = new JMenuItem(tr("Examples from Built-in Libraries")); label.setEnabled(false); menu.add(label); } @@ -1171,9 +1224,8 @@ public class Base { addSketchesSubmenu(menu, lib); } - LibraryList retiredIdeLibs = getIDERetiredLibs(); - retiredIdeLibs.sort(); if (!retiredIdeLibs.isEmpty()) { + retiredIdeLibs.sort(); JMenu retired = new JMenu(tr("RETIRED")); menu.add(retired); for (UserLibrary lib : retiredIdeLibs) { @@ -1181,14 +1233,55 @@ public class Base { } } - LibraryList userLibs = getUserLibs(); - if (userLibs.size() > 0) { + if (!platformLibs.isEmpty()) { menu.addSeparator(); - userLibs.sort(); + platformLibs.sort(); + label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName)); + label.setEnabled(false); + menu.add(label); + for (UserLibrary lib : platformLibs) { + addSketchesSubmenu(menu, lib); + } + } + + if (!referencedPlatformLibs.isEmpty()) { + menu.addSeparator(); + referencedPlatformLibs.sort(); + label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName)); + label.setEnabled(false); + menu.add(label); + for (UserLibrary lib : referencedPlatformLibs) { + addSketchesSubmenu(menu, lib); + } + } + + if (!sketchbookLibs.isEmpty()) { + menu.addSeparator(); + sketchbookLibs.sort(); label = new JMenuItem(tr("Examples from Custom Libraries")); label.setEnabled(false); menu.add(label); - for (UserLibrary lib : userLibs) { + for (UserLibrary lib : sketchbookLibs) { + addSketchesSubmenu(menu, lib); + } + } + + if (!sketchbookIncompatibleLibs.isEmpty()) { + sketchbookIncompatibleLibs.sort(); + JMenu incompatible = new JMenu(tr("INCOMPATIBLE")); + menu.add(incompatible); + for (UserLibrary lib : sketchbookIncompatibleLibs) { + addSketchesSubmenu(incompatible, lib); + } + } + + if (!otherLibs.isEmpty()) { + menu.addSeparator(); + otherLibs.sort(); + label = new JMenuItem(tr("Examples from Other Libraries")); + label.setEnabled(false); + menu.add(label); + for (UserLibrary lib : otherLibs) { addSketchesSubmenu(menu, lib); } } From afc7c596fc1e507f2f7805f3e264fac342fd2ea6 Mon Sep 17 00:00:00 2001 From: PaulStoffregen Date: Tue, 2 Aug 2016 02:43:04 -0700 Subject: [PATCH 2/4] Only sort ideLibs if it's not empty --- app/src/processing/app/Base.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 514e10701..7e4d602ed 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1214,8 +1214,8 @@ public class Base { } // Add examples from libraries - ideLibs.sort(); if (!ideLibs.isEmpty()) { + ideLibs.sort(); label = new JMenuItem(tr("Examples from Built-in Libraries")); label.setEnabled(false); menu.add(label); From 821ee53d40e4fc8b02b1b2a7645f65c4402992fe Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 19 Sep 2016 16:16:41 +0200 Subject: [PATCH 3/4] Replaced some example menu labels From: `Examples from Built-in Libraries` To: `Examples for any board` From: `Examples from Arduino AVR Boards Libraries` (selected platform) To: `Examples for Arduino/Genuino Micro` (selected board) From: `Examples from Arduino AVR Boards Libraries` (referenced platform) To: `Examples for Arduino AVR Boards` (referenced platform) --- app/src/processing/app/Base.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 7e4d602ed..86eb4f265 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1137,11 +1137,13 @@ public class Base { File platformLibraryPath = null; File referencedPlatformLibraryPath = null; String platformName = null; + String boardId = null; String referencedPlatformName = null; String myArch = null; TargetPlatform targetPlatform = BaseNoGui.getTargetPlatform(); if (targetPlatform != null) { myArch = targetPlatform.getId(); + boardId = BaseNoGui.getTargetBoard().getName(); platformName = targetPlatform.getPreferences().get("name"); platformLibraryPath = new File(targetPlatform.getFolder(), "libraries"); String core = BaseNoGui.getBoardPreferences().get("build.core", "arduino"); @@ -1216,7 +1218,7 @@ public class Base { // Add examples from libraries if (!ideLibs.isEmpty()) { ideLibs.sort(); - label = new JMenuItem(tr("Examples from Built-in Libraries")); + label = new JMenuItem(tr("Examples for any board")); label.setEnabled(false); menu.add(label); } @@ -1236,7 +1238,7 @@ public class Base { if (!platformLibs.isEmpty()) { menu.addSeparator(); platformLibs.sort(); - label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), platformName)); + label = new JMenuItem(I18n.format(tr("Examples for {0}"), boardId)); label.setEnabled(false); menu.add(label); for (UserLibrary lib : platformLibs) { @@ -1247,7 +1249,7 @@ public class Base { if (!referencedPlatformLibs.isEmpty()) { menu.addSeparator(); referencedPlatformLibs.sort(); - label = new JMenuItem(I18n.format(tr("Examples from {0} Libraries"), referencedPlatformName)); + label = new JMenuItem(I18n.format(tr("Examples for {0}"), referencedPlatformName)); label.setEnabled(false); menu.add(label); for (UserLibrary lib : referencedPlatformLibs) { From 096e545257797ef6348605429a8895efa606c8f0 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 19 Sep 2016 16:30:18 +0200 Subject: [PATCH 4/4] Examples of arch-independent libs from Arduino goes into `Examples for any board` The other cases remain unchanged --- app/src/processing/app/Base.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 86eb4f265..bef549f44 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1205,7 +1205,13 @@ public class Base { // Sketchbook Libraries (including incompatible) } else if (libraryLocation.equals(sketchbookLibraryPath)) { if (compatible) { - sketchbookLibs.add(lib); + // libraries promoted from sketchbook (behave as builtin) + if (lib.getTypes() != null && lib.getTypes().contains("Arduino") + && lib.getArchitectures().contains("*")) { + ideLibs.add(lib); + } else { + sketchbookLibs.add(lib); + } } else { sketchbookIncompatibleLibs.add(lib); }