Bug 480169 - Add proper support for platform libraries.
authorDoug Schaefer <dschaefer@qnx.com>
Wed, 21 Oct 2015 02:53:17 +0000 (22:53 -0400)
committerDoug Schaefer <dschaefer@qnx.com>
Wed, 21 Oct 2015 23:25:42 +0000 (19:25 -0400)
Change-Id: I3016ee6db3cd892f59b44d74d4181c30ca1b5b10

toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoLibrary.java
toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoManager.java
toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/ArduinoPlatform.java
toolchains/arduino/org.eclipse.cdt.arduino.core/src/org/eclipse/cdt/arduino/core/internal/board/LibraryIndex.java
toolchains/arduino/org.eclipse.cdt.arduino.ui/src/org/eclipse/cdt/arduino/ui/internal/project/LibrariesPropertyPage.java

index 04ea83d..6c90c00 100644 (file)
@@ -228,4 +228,18 @@ public class ArduinoLibrary {
                return sources;
        }
 
+       @Override
+       public boolean equals(Object obj) {
+               if (obj instanceof ArduinoLibrary) {
+                       return getName().equals(((ArduinoLibrary) obj).getName());
+               } else {
+                       return false;
+               }
+       }
+
+       @Override
+       public int hashCode() {
+               return getName().hashCode();
+       }
+
 }
index edf0f70..bba428f 100644 (file)
@@ -228,9 +228,18 @@ public class ArduinoManager {
                }.getType();
                Set<String> libraryNames = new Gson().fromJson(librarySetting, stringSet);
                LibraryIndex index = ArduinoManager.instance.getLibraryIndex();
+
+               ArduinoPlatform platform = project.getActiveBuildConfig().getAdapter(ArduinoBuildConfiguration.class).getBoard()
+                               .getPlatform();
                List<ArduinoLibrary> libraries = new ArrayList<>(libraryNames.size());
                for (String name : libraryNames) {
-                       libraries.add(index.getLibrary(name));
+                       ArduinoLibrary lib = index.getLibrary(name);
+                       if (lib == null) {
+                               lib = platform.getLibrary(name);
+                       }
+                       if (lib != null) {
+                               libraries.add(lib);
+                       }
                }
                return libraries;
        }
index b3fac36..f3e38ca 100644 (file)
@@ -51,6 +51,7 @@ public class ArduinoPlatform {
        private HierarchicalProperties boardsProperties;
        private Properties platformProperties;
        private Map<String, String> menus = new HashMap<>();
+       private Map<String, ArduinoLibrary> libraries;
 
        void setOwner(ArduinoPackage pkg) {
                this.pkg = pkg;
@@ -218,6 +219,39 @@ public class ArduinoPlatform {
                return sources;
        }
 
+       private void initLibraries() throws CoreException {
+               libraries = new HashMap<>();
+               File[] libraryDirs = getInstallPath().resolve("libraries").toFile().listFiles(); //$NON-NLS-1$
+               if (libraryDirs != null) {
+                       for (File libraryDir : libraryDirs) {
+                               Path propsPath = libraryDir.toPath().resolve("library.properties"); //$NON-NLS-1$
+                               if (propsPath.toFile().exists()) {
+                                       try {
+                                               ArduinoLibrary lib = new ArduinoLibrary(propsPath);
+                                               libraries.put(lib.getName(), lib);
+                                       } catch (IOException e) {
+                                               throw new CoreException(
+                                                               new Status(IStatus.ERROR, Activator.getId(), "Loading " + propsPath, e)); //$NON-NLS-1$
+                                       }
+                               }
+                       }
+               }
+       }
+
+       public synchronized Collection<ArduinoLibrary> getLibraries() throws CoreException {
+               if (libraries == null && isInstalled()) {
+                       initLibraries();
+               }
+               return libraries.values();
+       }
+
+       public synchronized ArduinoLibrary getLibrary(String name) throws CoreException {
+               if (libraries == null && isInstalled()) {
+                       initLibraries();
+               }
+               return libraries != null ? libraries.get(name) : null;
+       }
+
        public IStatus install(IProgressMonitor monitor) {
                // Check if we're installed already
                if (isInstalled()) {
@@ -253,9 +287,6 @@ public class ArduinoPlatform {
                        return status;
                }
 
-               // Reload the library index to pick up platform libraries
-               ArduinoManager.instance.loadLibraryIndex(false);
-
                return Status.OK_STATUS;
        }
 
index 4df9626..10ef898 100644 (file)
@@ -1,8 +1,6 @@
 package org.eclipse.cdt.arduino.core.internal.board;
 
-import java.io.File;
 import java.io.IOException;
-import java.nio.file.Path;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -22,25 +20,6 @@ public class LibraryIndex {
        private Map<String, ArduinoLibrary> latestLibs = new HashMap<>();
 
        public void resolve() throws IOException {
-               // Add in platform libraries
-               for (PackageIndex index : ArduinoManager.instance.getPackageIndices()) {
-                       for (ArduinoPackage pkg : index.getPackages()) {
-                               for (ArduinoPlatform platform : pkg.getPlatforms()) {
-                                       if (platform.isInstalled()) {
-                                               File[] libraryDirs = platform.getInstallPath().resolve("libraries").toFile().listFiles(); //$NON-NLS-1$
-                                               if (libraryDirs != null) {
-                                                       for (File libraryDir : libraryDirs) {
-                                                               Path propsPath = libraryDir.toPath().resolve("library.properties"); //$NON-NLS-1$
-                                                               if (propsPath.toFile().exists()) {
-                                                                       libraries.add(new ArduinoLibrary(propsPath));
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               }
-                       }
-               }
-
                for (ArduinoLibrary library : libraries) {
                        String name = library.getName();
 
index e173fa1..879db2f 100644 (file)
@@ -6,7 +6,9 @@ import java.util.List;
 
 import org.eclipse.cdt.arduino.core.internal.board.ArduinoLibrary;
 import org.eclipse.cdt.arduino.core.internal.board.ArduinoManager;
+import org.eclipse.cdt.arduino.core.internal.board.ArduinoPlatform;
 import org.eclipse.cdt.arduino.core.internal.board.LibraryIndex;
+import org.eclipse.cdt.arduino.core.internal.build.ArduinoBuildConfiguration;
 import org.eclipse.cdt.arduino.ui.internal.Activator;
 import org.eclipse.cdt.arduino.ui.internal.Messages;
 import org.eclipse.core.resources.IProject;
@@ -33,7 +35,7 @@ import org.eclipse.ui.dialogs.PropertyPage;
 
 public class LibrariesPropertyPage extends PropertyPage {
 
-       private static class ContentProvider implements ITreeContentProvider {
+       private class ContentProvider implements ITreeContentProvider {
                private LibraryIndex index;
 
                @Override
@@ -51,6 +53,13 @@ public class LibrariesPropertyPage extends PropertyPage {
                                return !index.getCategories().isEmpty();
                        } else if (element instanceof String) { // category
                                return !index.getLibraries((String) element).isEmpty();
+                       } else if (element instanceof ArduinoPlatform) {
+                               try {
+                                       return !((ArduinoPlatform) element).getLibraries().isEmpty();
+                               } catch (CoreException e) {
+                                       Activator.log(e);
+                                       return false;
+                               }
                        } else if (element instanceof ArduinoLibrary) {
                                return false;
                        } else {
@@ -61,8 +70,22 @@ public class LibrariesPropertyPage extends PropertyPage {
                @Override
                public Object getParent(Object element) {
                        if (element instanceof ArduinoLibrary) {
-                               return ((ArduinoLibrary) element).getCategory();
-                       } else if (element instanceof String) {
+                               ArduinoLibrary lib = (ArduinoLibrary) element;
+                               String category = lib.getCategory();
+                               if (category != null) {
+                                       return category;
+                               }
+
+                               try {
+                                       ArduinoPlatform platform = getPlatform();
+                                       if (platform.getLibrary(lib.getName()) != null) {
+                                               return platform;
+                                       }
+                               } catch (CoreException e) {
+                                       Activator.log(e);
+                               }
+                               return null;
+                       } else if (element instanceof String || element instanceof ArduinoPlatform) {
                                return index;
                        } else {
                                return null;
@@ -71,15 +94,32 @@ public class LibrariesPropertyPage extends PropertyPage {
 
                @Override
                public Object[] getElements(Object inputElement) {
-                       return ((LibraryIndex) inputElement).getCategories().toArray(new String[0]);
+                       List<Object> categories = new ArrayList<>();
+
+                       try {
+                               ArduinoPlatform platform = getPlatform();
+                               categories.add(platform);
+                       } catch (CoreException e) {
+                               Activator.log(e);
+                       }
+
+                       categories.addAll(((LibraryIndex) inputElement).getCategories());
+                       return categories.toArray();
                }
 
                @Override
                public Object[] getChildren(Object parentElement) {
                        if (parentElement instanceof String) {
                                return index.getLibraries((String) parentElement).toArray(new ArduinoLibrary[0]);
+                       } else if (parentElement instanceof ArduinoPlatform) {
+                               try {
+                                       return ((ArduinoPlatform) parentElement).getLibraries().toArray();
+                               } catch (CoreException e) {
+                                       Activator.log(e);
+                                       return new Object[0];
+                               }
                        } else {
-                               return null;
+                               return new Object[0];
                        }
                }
        }
@@ -94,6 +134,8 @@ public class LibrariesPropertyPage extends PropertyPage {
                public String getColumnText(Object element, int columnIndex) {
                        if (element instanceof String) {
                                return columnIndex == 0 ? (String) element : null;
+                       } else if (element instanceof ArduinoPlatform) {
+                               return columnIndex == 0 ? ((ArduinoPlatform) element).getName() : null;
                        } else if (element instanceof ArduinoLibrary) {
                                switch (columnIndex) {
                                case 0:
@@ -139,6 +181,7 @@ public class LibrariesPropertyPage extends PropertyPage {
                                                }
                                        }
                                }, true) {
+
                        @Override
                        protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
                                return new ContainerCheckedTreeViewer(parent, style);
@@ -171,8 +214,16 @@ public class LibrariesPropertyPage extends PropertyPage {
                } catch (CoreException e) {
                        Activator.log(e);
                }
-
                return comp;
+
+       }
+
+       private IProject getProject() {
+               return getElement().getAdapter(IProject.class);
+       }
+
+       private ArduinoPlatform getPlatform() throws CoreException {
+               return getProject().getActiveBuildConfig().getAdapter(ArduinoBuildConfiguration.class).getBoard().getPlatform();
        }
 
        @Override
@@ -187,7 +238,7 @@ public class LibrariesPropertyPage extends PropertyPage {
                        }
                }
                try {
-                       ArduinoManager.instance.setLibraries(getElement().getAdapter(IProject.class), libs);
+                       ArduinoManager.instance.setLibraries(getProject(), libs);
                } catch (CoreException e) {
                        Activator.log(e);
                }