From 1d6e710eacef3d3f13734f143e2262164a8abc95 Mon Sep 17 00:00:00 2001 From: Federico Fissore Date: Tue, 19 May 2015 12:57:06 +0200 Subject: [PATCH] Rewritten library menu generation; libraries are now grouped by their type. Fixes #2879 --- .../libraries/LibraryByTypeComparator.java | 24 ++++++++ .../LibraryOfSameTypeComparator.java | 15 +++++ app/src/processing/app/Base.java | 61 +++++++++++-------- 3 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 app/src/cc/arduino/contributions/libraries/LibraryByTypeComparator.java create mode 100644 app/src/cc/arduino/contributions/libraries/LibraryOfSameTypeComparator.java diff --git a/app/src/cc/arduino/contributions/libraries/LibraryByTypeComparator.java b/app/src/cc/arduino/contributions/libraries/LibraryByTypeComparator.java new file mode 100644 index 000000000..09d91fc3d --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/LibraryByTypeComparator.java @@ -0,0 +1,24 @@ +package cc.arduino.contributions.libraries; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; + +public class LibraryByTypeComparator implements Comparator { + + private final List types; + + public LibraryByTypeComparator() { + this("Arduino", "Recommended", "Contributed"); + } + + public LibraryByTypeComparator(String... types) { + this.types = Arrays.asList(types); + } + + @Override + public int compare(ContributedLibrary o1, ContributedLibrary o2) { + return types.indexOf(o1.getTypes().get(0)) - types.indexOf(o2.getTypes().get(0)); + } + +} diff --git a/app/src/cc/arduino/contributions/libraries/LibraryOfSameTypeComparator.java b/app/src/cc/arduino/contributions/libraries/LibraryOfSameTypeComparator.java new file mode 100644 index 000000000..171f07db0 --- /dev/null +++ b/app/src/cc/arduino/contributions/libraries/LibraryOfSameTypeComparator.java @@ -0,0 +1,15 @@ +package cc.arduino.contributions.libraries; + +import java.util.Comparator; + +public class LibraryOfSameTypeComparator implements Comparator { + + @Override + public int compare(ContributedLibrary o1, ContributedLibrary o2) { + if (!o1.getTypes().get(0).equals(o2.getTypes().get(0))) { + return 0; + } + return o1.getName().compareTo(o2.getName()); + } + +} diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 900ae8260..62c2db364 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -25,9 +25,7 @@ package processing.app; import cc.arduino.contributions.BuiltInCoreIsNewerCheck; import cc.arduino.contributions.DownloadableContributionVersionComparator; import cc.arduino.contributions.VersionHelper; -import cc.arduino.contributions.libraries.ContributedLibrary; -import cc.arduino.contributions.libraries.LibrariesIndexer; -import cc.arduino.contributions.libraries.LibraryInstaller; +import cc.arduino.contributions.libraries.*; import cc.arduino.contributions.libraries.ui.LibraryManagerUI; import cc.arduino.contributions.packages.ContributedPlatform; import cc.arduino.contributions.packages.ContributionInstaller; @@ -1195,6 +1193,13 @@ public class Base { return new LibraryList(libs); } + private List getSortedLibraries() { + List installedLibraries = new LinkedList(BaseNoGui.librariesIndexer.getInstalledLibraries()); + Collections.sort(installedLibraries, new LibraryByTypeComparator()); + Collections.sort(installedLibraries, new LibraryOfSameTypeComparator()); + return installedLibraries; + } + public void rebuildImportMenu(JMenu importMenu) { if (importMenu == null) return; @@ -1225,33 +1230,35 @@ public class Base { TargetPlatform targetPlatform = getTargetPlatform(); if (targetPlatform != null) { - LibraryList ideLibs = getIDELibs(); - LibraryList userLibs = getUserLibs(); - try { - // Find the current target. Get the platform, and then select the - // correct name and core path. - String platformNameLabel; - platformNameLabel = StringUtils.capitalize(targetPlatform.getContainerPackage().getId()) + "/" + StringUtils.capitalize(targetPlatform.getId()); - platformNameLabel = I18n.format(_("{0} libraries"), platformNameLabel); - JMenuItem platformItem = new JMenuItem(_(platformNameLabel)); - platformItem.setEnabled(false); - importMenu.add(platformItem); - - if (ideLibs.size() > 0) { - addLibraries(importMenu, ideLibs); - } - - if (userLibs.size() > 0) { - if (ideLibs.size() > 0) { + List libs = getSortedLibraries(); + String lastLibType = null; + for (ContributedLibrary lib : libs) { + if (lastLibType == null || !lastLibType.equals(lib.getTypes().get(0))) { + if (lastLibType != null) { importMenu.addSeparator(); } - JMenuItem contributedLibraryItem = new JMenuItem(_("Contributed libraries")); - contributedLibraryItem.setEnabled(false); - importMenu.add(contributedLibraryItem); - addLibraries(importMenu, userLibs); + lastLibType = lib.getTypes().get(0); + JMenuItem platformItem = new JMenuItem(I18n.format(_("{0} libraries"), lastLibType)); + platformItem.setEnabled(false); + importMenu.add(platformItem); } - } catch (IOException e) { - e.printStackTrace(); + + AbstractAction action = new AbstractAction(lib.getName()) { + public void actionPerformed(ActionEvent event) { + UserLibrary l = (UserLibrary) getValue("library"); + try { + activeEditor.getSketch().importLibrary(l); + } catch (IOException e) { + showWarning(_("Error"), I18n.format("Unable to list header files in {0}", l.getSrcFolder()), e); + } + } + }; + action.putValue("library", lib); + + // Add new element at the bottom + JMenuItem item = new JMenuItem(action); + item.putClientProperty("library", lib); + importMenu.add(item); } } }