diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 78dda9ce3..a0e8adcc5 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -116,7 +116,7 @@ public class Base { private Mode defaultMode; private Mode[] coreModes; - private List contribModes; + List contribModes; private JMenu sketchbookMenu; @@ -341,7 +341,7 @@ public class Base { contribModes = new ArrayList(); ArrayList newContribs = ModeContribution - .list(this, getSketchbookModesFolder(), false); + .list(this, getSketchbookModesFolder()); for (ModeContribution contrib : newContribs) { if (!contribModes.contains(contrib)) { if (contrib.instantiateModeClass()) { @@ -358,7 +358,7 @@ public class Base { // Delete all modes and tools that have been flagged for deletion before // they are initialized by an editor. ArrayList contribs = new ArrayList(); - contribs.addAll(ModeContribution.list(this, getSketchbookModesFolder(), false)); + contribs.addAll(ModeContribution.list(this, getSketchbookModesFolder())); contribs.addAll(ToolContribution.list(getSketchbookToolsFolder(), false)); for (InstalledContribution contrib : contribs) { if (ContributionManager.isFlaggedForDeletion(contrib)) { @@ -1610,14 +1610,6 @@ public class Base { updateManagerFrame.showFrame(activeEditor); } - /** - * Installed the libraries in the given file after a confirmation from the - * user. Returns the number of libraries installed. - */ - public boolean handleConfirmAndInstallLibrary(File libFile) { - return ContributionManager.confirmAndInstallLibrary(activeEditor, libFile, null) != null; - } - // ................................................................... diff --git a/app/src/processing/app/ContributionManager.java b/app/src/processing/app/ContributionManager.java index 5266929a8..be73bed53 100644 --- a/app/src/processing/app/ContributionManager.java +++ b/app/src/processing/app/ContributionManager.java @@ -18,18 +18,6 @@ interface ErrorWidget { public class ContributionManager { - static private final String DOUBLE_CLICK_SECONDARY = - "Click “Yes� to install this library to your sketchbook..."; - - static private final String DISCOVERY_INTERNAL_ERROR_MESSAGE = - "An internal error occured while searching for contributions in the downloaded file."; - - static private final String DISCOVERY_NONE_FOUND_ERROR_MESSAGE = - "Maybe it's just us, but it looks like there are no contributions in this file."; - - static private final String ERROR_OVERWRITING_PROPERTIES_MESSAGE = - "Error overwriting .properties file."; - static public final String DELETION_FLAG = "flagged_for_deletion"; static public final ContributionListing contribListing; @@ -130,21 +118,9 @@ public class ContributionManager { installProgressMonitor.startTask("Installing", ProgressMonitor.UNKNOWN); InstalledContribution contribution = null; - switch (ad.getType()) { - case LIBRARY: - contribution = installLibrary(editor, libDest, ad, false, statusBar); - break; - case LIBRARY_COMPILATION: - contribution = installLibraryCompilation(editor, libDest, statusBar); - break; - case TOOL: - contribution = installTool(editor, libDest, ad, statusBar); - break; - } + contribution = install(editor, libDest, ad, false, statusBar); if (contribution != null) { - // XXX contributionListing.getInformationFromAdvertised(contribution); - // get the category at least contribListing.replaceContribution(ad, contribution); refreshInstalled(editor); } @@ -156,210 +132,247 @@ public class ContributionManager { } - static public LibraryCompilation installLibraryCompilation(Editor editor, - File f, - ErrorWidget statusBar) { - File parentDir = unzipFileToTemp(f, statusBar); - - LibraryCompilation compilation = LibraryCompilation.create(parentDir); + static public void refreshInstalled(Editor editor) { + editor.getMode().rebuildImportMenu(); + editor.rebuildToolMenu(); + } - if (compilation == null) { - statusBar.setErrorMessage(DISCOVERY_NONE_FOUND_ERROR_MESSAGE); + static ArrayList discover(Type type, File tempDir) { + switch (type) { + case LIBRARY: + return Library.discover(tempDir); + case LIBRARY_COMPILATION: + // XXX Implement return null; - } - - String folderName = compilation.getName(); - - File dest = new File(editor.getBase().getSketchbookLibrariesFolder(), folderName); - - // XXX: Check for conflicts with other library names, etc. - boolean errorEncountered = false; - if (dest.exists()) { - if (!dest.delete()) { - // Problem - errorEncountered = true; - } - } - - if (!errorEncountered) { - // Install it, return it - if (parentDir.renameTo(dest)) { - return LibraryCompilation.create(dest); - } + case TOOL: + return ToolContribution.discover(tempDir); + case MODE: + return ModeContribution.discover(tempDir); } return null; } - static public Library confirmAndInstallLibrary(Editor editor, File libFile, - ErrorWidget statusBar) { - - int result = Base.showYesNoQuestion(editor, "Install", - "Install libraries from " + libFile.getName() + "?", - ContributionManager.DOUBLE_CLICK_SECONDARY); - - if (result == JOptionPane.YES_OPTION) { - return installLibrary(editor, libFile, null, true, statusBar); + static String getPropertiesFileName(Type type) { + switch (type) { + case LIBRARY: + return Library.propertiesFileName; + case LIBRARY_COMPILATION: + return LibraryCompilation.propertiesFileName; + case TOOL: + return ToolContribution.propertiesFileName; + case MODE: + return ModeContribution.propertiesFileName; } return null; } + + static InstalledContribution create(Base base, Type type, File folder) { + switch (type) { + case LIBRARY: + return new Library(folder, null); + case LIBRARY_COMPILATION: + return LibraryCompilation.create(folder); + case TOOL: + return ToolContribution.getTool(folder); + case MODE: + return ModeContribution.getContributedMode(base, folder); + } + + return null; + } + + static ArrayList getContributions(Type type, Editor editor) { + ArrayList contribs = new ArrayList(); + switch (type) { + case LIBRARY: + contribs.addAll(editor.getMode().contribLibraries); + break; + case LIBRARY_COMPILATION: + contribs.addAll(LibraryCompilation.list(editor.getMode().contribLibraries)); + break; + case TOOL: + contribs.addAll(editor.contribTools); + break; + case MODE: + contribs.addAll(editor.getBase().contribModes); + break; + } + return contribs; + } + + static void initialize(InstalledContribution contribution) throws Exception { + if (contribution instanceof ToolContribution) { + ((ToolContribution) contribution).initializeToolClass(); + } else if (contribution instanceof ModeContribution) { + ((ModeContribution) contribution).instantiateModeClass(); + } + } /** - * Creates a temporary folder and unzips a file to a subdirectory of the temp - * folder. The subdirectory is the only file of the tempo folder. - * - * e.g. if the contents of foo.zip are /hello and /world, then the resulting - * files will be - * /tmp/foo9432423uncompressed/foo/hello - * /tmp/foo9432423uncompress/foo/world - * ...and "/tmp/id9432423uncompress/foo/" will be returned. - * - * @return the folder where the zips contents have been unzipped to (the - * subdirectory of the temp folder). + * @param libFile + * a zip file containing the library to install + * @param ad + * the advertised version of this library, if it was downloaded + * through the Contribution Manager. This is used to check the type + * of library being installed, and to replace the .properties file in + * the zip + * @param confirmReplace + * true to open a dialog asking the user to confirm removing/moving + * the library when a library by the same name already exists + * @return */ - static public File unzipFileToTemp(File libFile, - ErrorWidget statusBar) { + static public InstalledContribution install(Editor editor, File libFile, + AdvertisedContribution ad, + boolean confirmReplace, + ErrorWidget statusBar) { - String fileName = ContributionManager.getFileName(libFile); - File tmpFolder = null; + File tempDir = ContributionManager.unzipFileToTemp(libFile, statusBar); - try { - tmpFolder = Base.createTempFolder(fileName, "uncompressed"); - tmpFolder = new File(tmpFolder, fileName); - tmpFolder.mkdirs(); - } catch (IOException e) { - statusBar.setErrorMessage("Could not create temp folder to uncompressed zip file."); - } + ArrayList libfolders = ContributionManager.discover(ad.getType(), tempDir); - ContributionManager.unzip(libFile, tmpFolder); - - return tmpFolder; - } - - static public File getTemporaryFile(URL url, - ErrorWidget statusBar) { - try { - File tmpFolder = Base.createTempFolder("library", "download"); - - String[] segments = url.getFile().split("/"); - File libFile = new File(tmpFolder, segments[segments.length - 1]); - libFile.setWritable(true); - - return libFile; - } catch (IOException e) { - statusBar.setErrorMessage("Could not create a temp folder for download."); - } - - return null; - } - - /** - * Returns the name of a file without its path or extension. - * - * For example, - * "/path/to/helpfullib.zip" returns "helpfullib" - * "helpfullib-0.1.1.plb" returns "helpfullib-0.1.1" - */ - static public String getFileName(File libFile) { - String path = libFile.getPath(); - int lastSeparator = path.lastIndexOf(File.separatorChar); - - String fileName; - if (lastSeparator != -1) { - fileName = path.substring(lastSeparator + 1); - } else { - fileName = path; - } - - int lastDot = fileName.lastIndexOf('.'); - if (lastDot != -1) { - return fileName.substring(0, lastDot); - } - - return fileName; - } - - static public ToolContribution installTool(Editor editor, - File zippedToolFile, - AdvertisedContribution ad, - ErrorWidget statusBar) { - - File tempDir = unzipFileToTemp(zippedToolFile, statusBar); - - ArrayList toolFolders = ToolContribution.discover(tempDir); - if (toolFolders.isEmpty()) { - // Sometimes tool authors place all their folders in the base + if (libfolders.isEmpty()) { + // Sometimes library authors place all their folders in the base // directory of a zip file instead of in single folder as the // guidelines suggest. If this is the case, we might be able to find the // library by stepping up a directory and searching for libraries again. - toolFolders = ToolContribution.discover(tempDir.getParentFile()); + libfolders = ContributionManager.discover(ad.getType(), tempDir.getParentFile()); } - if (toolFolders != null && toolFolders.size() == 1) { - File toolFolder = toolFolders.get(0); - final ToolContribution tool = ToolContribution.getTool(toolFolder); + if (libfolders != null && libfolders.size() == 1) { + File libfolder = libfolders.get(0); + File propFile = new File(libfolder, getPropertiesFileName(ad.getType())); - File propFile = new File(tool.getFolder(), "tool.properties"); - - if (ad == null || writePropertiesFile(propFile, ad)) { - return installTool(editor, tool, statusBar); + if (writePropertiesFile(propFile, ad)) { + InstalledContribution newcontrib = ContributionManager.create(editor + .getBase(), ad.getType(), libfolder); + + return ContributionManager.installContribution(editor, newcontrib, + confirmReplace, + statusBar); } else { - statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE); + statusBar.setErrorMessage("Error overwriting .properties file."); } } else { // Diagnose the problem and notify the user - if (toolFolders == null || toolFolders.isEmpty()) { - statusBar.setErrorMessage(DISCOVERY_INTERNAL_ERROR_MESSAGE); + if (libfolders == null) { + statusBar.setErrorMessage("An internal error occured while searching " + + "for contributions in the downloaded file."); + } else if (libfolders.isEmpty()) { + statusBar + .setErrorMessage("Maybe it's just us, but it looks like there " + + "are no contributions in the file for \"" + ad.getName() + + ".\""); } else { - statusBar.setErrorMessage("There were multiple tools in the file, so we're ignoring it."); + statusBar.setErrorMessage("There were multiple libraries in the file, " + + "so we're ignoring it."); } } return null; } - static public ToolContribution installTool(Editor editor, - ToolContribution newTool, - ErrorWidget statusBar) { + /** + * @param confirmReplace + * if true and the library is already installed, opens a prompt to + * ask the user if it's okay to replace the library. If false, the + * library is always replaced with the new copy. + */ + static public InstalledContribution installContribution(Editor editor, InstalledContribution newLib, + boolean confirmReplace, ErrorWidget statusBar) { - ArrayList oldTools = editor.contribTools; + ArrayList oldLibs = getContributions(newLib.getType(), editor); - String toolFolderName = newTool.getFolder().getName(); + String libFolderName = newLib.getFolder().getName(); - File toolDestination = editor.getBase().getSketchbookToolsFolder(); - File newToolDest = new File(toolDestination, toolFolderName); + File libraryDestination = editor.getBase().getSketchbookLibrariesFolder(); + File newLibDest = new File(libraryDestination, libFolderName); - for (ToolContribution oldTool : oldTools) { + for (InstalledContribution oldLib : oldLibs) { - // XXX: Handle other cases when installing tools. + // XXX: Handle other cases when installing libraries. // -What if a library by the same name is already installed? - // -What if newLibDest exists, but isn't used by an existing tools? - if (oldTool.getFolder().exists() && oldTool.getFolder().equals(newToolDest)) { + // -What if newLibDest exists, but isn't used by an existing library? + if (oldLib.getFolder().exists() && oldLib.getFolder().equals(newLibDest)) { - // XXX: We can't replace stuff, soooooo.... do something different - if (!backupContribution(editor, oldTool, false, statusBar)) { - return null; + if (ContributionManager.requiresRestart(newLib)) { + // XXX: We can't replace stuff, soooooo.... do something different + if (!backupContribution(editor, oldLib, false, statusBar)) { + return null; + } + } else { + int result = 0; + boolean doBackup = Preferences.getBoolean("contribution.backup.on_install"); + if (confirmReplace) { + if (doBackup) { + result = Base.showYesNoQuestion(editor, "Replace", + "Replace pre-existing \"" + oldLib.getName() + "\" library?", + "A pre-existing copy of the \"" + oldLib.getName() + "\" library
"+ + "has been found in your sketchbook. Clicking “Yes”
"+ + "will move the existing library to a backup folder
" + + " in libraries/old before replacing it."); + if (result != JOptionPane.YES_OPTION || !backupContribution(editor, oldLib, true, statusBar)) { + return null; + } + } else { + result = Base.showYesNoQuestion(editor, "Replace", + "Replace pre-existing \"" + oldLib.getName() + "\" library?", + "A pre-existing copy of the \"" + oldLib.getName() + "\" library
"+ + "has been found in your sketchbook. Clicking “Yes”
"+ + "will permanently delete this library and all of its contents
"+ + "before replacing it."); + if (result != JOptionPane.YES_OPTION || !oldLib.getFolder().delete()) { + return null; + } + } + } else { + if (doBackup && !backupContribution(editor, oldLib, true, statusBar) + || !doBackup && !oldLib.getFolder().delete()) { + return null; + } + } } } } - if (newToolDest.exists()) { - newToolDest.delete(); + if (newLibDest.exists()) { + Base.removeDir(newLibDest); } - // Move newTool to the sketchbook library folder - if (newTool.getFolder().renameTo(newToolDest)) { - ToolContribution movedTool = ToolContribution.getTool(newToolDest); + // Move newLib to the sketchbook library folder + if (newLib.getFolder().renameTo(newLibDest)) { + InstalledContribution contrib = ContributionManager.create(editor + .getBase(), newLib.getType(), newLibDest); try { - movedTool.initializeToolClass(); - return movedTool; + initialize(contrib); + return contrib; } catch (Exception e) { e.printStackTrace(); } + +// try { +// FileUtils.copyDirectory(newLib.folder, libFolder); +// FileUtils.deleteQuietly(newLib.folder); +// newLib.folder = libFolder; +// } catch (IOException e) { } else { - statusBar.setErrorMessage("Could not move tool \"" + newTool.getName() - + "\" to sketchbook."); + String errorMsg = null; + switch (newLib.getType()) { + case LIBRARY: + errorMsg = "Could not move library \"" + newLib.getName() + + "\" to sketchbook."; + break; + case LIBRARY_COMPILATION: + break; + case TOOL: + errorMsg = "Could not move tool \"" + newLib.getName() + + "\" to sketchbook."; + break; + case MODE: + break; + } + statusBar.setErrorMessage(errorMsg); } return null; @@ -388,141 +401,6 @@ public class ContributionManager { return false; } - - /** - * @param libFile - * a zip file containing the library to install - * @param ad - * the advertised version of this library, if it was downloaded - * through the Contribution Manager, or null. This is used to replace - * the library.properties file in the zip - * @param confirmReplace - * true to open a dialog asking the user to confirm removing/moving - * the library when a library by the same name already exists - * @return - */ - static public Library installLibrary(Editor editor, File libFile, - AdvertisedContribution ad, - boolean confirmReplace, - ErrorWidget statusBar) { - File tempDir = unzipFileToTemp(libFile, statusBar); - - try { - ArrayList libfolders = Library.discover(tempDir); - if (libfolders.isEmpty()) { - // Sometimes library authors place all their folders in the base - // directory of a zip file instead of in single folder as the - // guidelines suggest. If this is the case, we might be able to find the - // library by stepping up a directory and searching for libraries again. - libfolders = Library.discover(tempDir.getParentFile()); - } - - if (libfolders != null && libfolders.size() == 1) { - File libfolder = libfolders.get(0); - File propFile = new File(libfolder, "library.properties"); - - if (ad == null || writePropertiesFile(propFile, ad)) { - Library newlib = new Library(libfolder, null); - return installLibrary(editor, newlib, confirmReplace, statusBar); - } else { - statusBar.setErrorMessage(ERROR_OVERWRITING_PROPERTIES_MESSAGE); - } - } else { - // Diagnose the problem and notify the user - if (libfolders == null) { - statusBar.setErrorMessage(ContributionManager.DISCOVERY_INTERNAL_ERROR_MESSAGE); - } else if (libfolders.isEmpty()) { - statusBar.setErrorMessage(ContributionManager.DISCOVERY_NONE_FOUND_ERROR_MESSAGE); - } else { - statusBar.setErrorMessage("There were multiple libraries in the file, so we're ignoring it."); - } - } - } catch (IOException ioe) { - statusBar.setErrorMessage(ContributionManager.DISCOVERY_INTERNAL_ERROR_MESSAGE); - } - - return null; - } - - /** - * @param confirmReplace - * if true and the library is already installed, opens a prompt to - * ask the user if it's okay to replace the library. If false, the - * library is always replaced with the new copy. - */ - static public Library installLibrary(Editor editor, Library newLib, - boolean confirmReplace, ErrorWidget statusBar) { - - ArrayList oldLibs = editor.getMode().contribLibraries; - - String libFolderName = newLib.getFolder().getName(); - - File libraryDestination = editor.getBase().getSketchbookLibrariesFolder(); - File newLibDest = new File(libraryDestination, libFolderName); - - for (Library oldLib : oldLibs) { - - // XXX: Handle other cases when installing libraries. - // -What if a library by the same name is already installed? - // -What if newLibDest exists, but isn't used by an existing library? - if (oldLib.getFolder().exists() && oldLib.getFolder().equals(newLibDest)) { - - int result = 0; - boolean doBackup = Preferences.getBoolean("contribution.backup.on_install"); - if (confirmReplace) { - if (doBackup) { - result = Base.showYesNoQuestion(editor, "Replace", - "Replace pre-existing \"" + oldLib.getName() + "\" library?", - "A pre-existing copy of the \"" + oldLib.getName() + "\" library
"+ - "has been found in your sketchbook. Clicking “Yes”
"+ - "will move the existing library to a backup folder
" + - " in libraries/old before replacing it."); - if (result != JOptionPane.YES_OPTION || !backupContribution(editor, oldLib, true, statusBar)) { - return null; - } - } else { - result = Base.showYesNoQuestion(editor, "Replace", - "Replace pre-existing \"" + oldLib.getName() + "\" library?", - "A pre-existing copy of the \"" + oldLib.getName() + "\" library
"+ - "has been found in your sketchbook. Clicking “Yes”
"+ - "will permanently delete this library and all of its contents
"+ - "before replacing it."); - if (result != JOptionPane.YES_OPTION || !oldLib.getFolder().delete()) { - return null; - } - } - } else { - if (doBackup && !backupContribution(editor, oldLib, true, statusBar) - || !doBackup && !oldLib.getFolder().delete()) { - return null; - } - } - } - } - - if (newLibDest.exists()) { - newLibDest.delete(); - } - - // Move newLib to the sketchbook library folder - if (newLib.getFolder().renameTo(newLibDest)) { - return new Library(newLibDest, null); -// try { -// FileUtils.copyDirectory(newLib.folder, libFolder); -// FileUtils.deleteQuietly(newLib.folder); -// newLib.folder = libFolder; -// } catch (IOException e) { - } else { - statusBar.setErrorMessage("Could not move library \"" - + newLib.getName() + "\" to sketchbook."); - return null; - } - } - - static public void refreshInstalled(Editor editor) { - editor.getMode().rebuildImportMenu(); - editor.rebuildToolMenu(); - } /** * Moves the given contribution to a backup folder. @@ -578,20 +456,6 @@ public class ContributionManager { return success; } - static public File createBackupFolder(File backupFolder, - ErrorWidget logger, - String errorMessage) { - - if (!backupFolder.exists() || !backupFolder.isDirectory()) { - if (!backupFolder.mkdirs()) { - logger.setErrorMessage(errorMessage); - return null; - } - } - - return backupFolder; - } - static public File createLibraryBackupFolder(Editor editor, ErrorWidget logger) { File libraryBackupFolder = new File(editor.getBase() @@ -608,6 +472,20 @@ public class ContributionManager { "Could not create backup folder for tool."); } + static private File createBackupFolder(File backupFolder, + ErrorWidget logger, + String errorMessage) { + + if (!backupFolder.exists() || !backupFolder.isDirectory()) { + if (!backupFolder.mkdirs()) { + logger.setErrorMessage(errorMessage); + return null; + } + } + + return backupFolder; + } + /** * Returns a file in the parent folder that does not exist yet. If * parent/fileName already exists, this will look for parent/fileName(2) @@ -631,6 +509,81 @@ public class ContributionManager { return backupFolderForLib; } + static public File getTemporaryFile(URL url, + ErrorWidget statusBar) { + try { + File tmpFolder = Base.createTempFolder("library", "download"); + + String[] segments = url.getFile().split("/"); + File libFile = new File(tmpFolder, segments[segments.length - 1]); + libFile.setWritable(true); + + return libFile; + } catch (IOException e) { + statusBar.setErrorMessage("Could not create a temp folder for download."); + } + + return null; + } + + /** + * Creates a temporary folder and unzips a file to a subdirectory of the temp + * folder. The subdirectory is the only file of the tempo folder. + * + * e.g. if the contents of foo.zip are /hello and /world, then the resulting + * files will be + * /tmp/foo9432423uncompressed/foo/hello + * /tmp/foo9432423uncompress/foo/world + * ...and "/tmp/id9432423uncompress/foo/" will be returned. + * + * @return the folder where the zips contents have been unzipped to (the + * subdirectory of the temp folder). + */ + static public File unzipFileToTemp(File libFile, + ErrorWidget statusBar) { + + String fileName = ContributionManager.getFileName(libFile); + File tmpFolder = null; + + try { + tmpFolder = Base.createTempFolder(fileName, "uncompressed"); + tmpFolder = new File(tmpFolder, fileName); + tmpFolder.mkdirs(); + } catch (IOException e) { + statusBar.setErrorMessage("Could not create temp folder to uncompressed zip file."); + } + + ContributionManager.unzip(libFile, tmpFolder); + + return tmpFolder; + } + + /** + * Returns the name of a file without its path or extension. + * + * For example, + * "/path/to/helpfullib.zip" returns "helpfullib" + * "helpfullib-0.1.1.plb" returns "helpfullib-0.1.1" + */ + static public String getFileName(File libFile) { + String path = libFile.getPath(); + int lastSeparator = path.lastIndexOf(File.separatorChar); + + String fileName; + if (lastSeparator != -1) { + fileName = path.substring(lastSeparator + 1); + } else { + fileName = path; + } + + int lastDot = fileName.lastIndexOf('.'); + if (lastDot != -1) { + return fileName.substring(0, lastDot); + } + + return fileName; + } + public static void unzip(File zipFile, File dest) { try { FileInputStream fis = new FileInputStream(zipFile); diff --git a/app/src/processing/app/Library.java b/app/src/processing/app/Library.java index 7bf61614c..dec6611fb 100644 --- a/app/src/processing/app/Library.java +++ b/app/src/processing/app/Library.java @@ -34,6 +34,8 @@ public class Library extends InstalledContribution { * this might be the windows64 subfolder with the library. */ String nativeLibraryPath; + + static String propertiesFileName = "library.properties"; /** Filter to pull out just files and no directories, and to skip export.txt */ static FilenameFilter standardFilter = new FilenameFilter() { @@ -57,7 +59,7 @@ public class Library extends InstalledContribution { }; public Library(File folder, String subfolder) { - super(folder, "library.properties"); + super(folder, Library.propertiesFileName); this.group = subfolder; libraryFolder = new File(folder, "library"); @@ -369,13 +371,13 @@ public class Library extends InstalledContribution { } }; - static protected ArrayList discover(File folder) throws IOException { + static protected ArrayList discover(File folder) { ArrayList libraries = new ArrayList(); discover(folder, libraries); return libraries; } - static protected void discover(File folder, ArrayList libraries) throws IOException { + static protected void discover(File folder, ArrayList libraries) { String[] list = folder.list(junkFolderFilter); // if a bad folder or something like that, this might come back null @@ -409,13 +411,13 @@ public class Library extends InstalledContribution { } } - static protected ArrayList list(File folder) throws IOException { + static protected ArrayList list(File folder) { ArrayList libraries = new ArrayList(); list(folder, libraries); return libraries; } - static protected void list(File folder, ArrayList libraries) throws IOException { + static protected void list(File folder, ArrayList libraries) { ArrayList librariesFolders = new ArrayList(); discover(folder, librariesFolders); diff --git a/app/src/processing/app/LibraryCompilation.java b/app/src/processing/app/LibraryCompilation.java index 3af7964f2..ead494cf0 100644 --- a/app/src/processing/app/LibraryCompilation.java +++ b/app/src/processing/app/LibraryCompilation.java @@ -9,9 +9,11 @@ public class LibraryCompilation extends InstalledContribution { ArrayList libraries; + static String propertiesFileName = "compilation.properties"; + private LibraryCompilation(File folder) throws IOException { - super(folder, "compilation.properties"); + super(folder, LibraryCompilation.propertiesFileName); libraries = new ArrayList(); ArrayList librariesFolders = new ArrayList(); diff --git a/app/src/processing/app/Mode.java b/app/src/processing/app/Mode.java index 8cdb39740..0302fe9f2 100644 --- a/app/src/processing/app/Mode.java +++ b/app/src/processing/app/Mode.java @@ -117,21 +117,14 @@ public abstract class Mode { // reset the table mapping imports to libraries importToLibraryTable = new HashMap(); - try { - coreLibraries = Library.list(librariesFolder); - contribLibraries = Library.list(base.getSketchbookLibrariesFolder()); - - for (Library lib : coreLibraries) { - lib.addPackageList(importToLibraryTable); - } - for (Library lib : contribLibraries) { - lib.addPackageList(importToLibraryTable); - } - - } catch (IOException e) { - Base.showWarning("Unhappiness", - "An error occurred while loading libraries.\n" + - "Not all the books will be in place.", e); + coreLibraries = Library.list(librariesFolder); + contribLibraries = Library.list(base.getSketchbookLibrariesFolder()); + + for (Library lib : coreLibraries) { + lib.addPackageList(importToLibraryTable); + } + for (Library lib : contribLibraries) { + lib.addPackageList(importToLibraryTable); } } diff --git a/app/src/processing/app/ModeContribution.java b/app/src/processing/app/ModeContribution.java index 0b62a7e0e..579b849b8 100644 --- a/app/src/processing/app/ModeContribution.java +++ b/app/src/processing/app/ModeContribution.java @@ -43,6 +43,8 @@ public class ModeContribution extends InstalledContribution { Mode mode; + static String propertiesFileName = "mode.properties"; + static public Mode getCoreMode(Base base, String classname, File folder) { try { Class c = Class.forName(classname); @@ -64,7 +66,7 @@ public class ModeContribution extends InstalledContribution { } private ModeContribution(Base base, File folder) { - super(folder, "mode.properties"); + super(folder, ModeContribution.propertiesFileName); this.base = base; @@ -135,18 +137,26 @@ public class ModeContribution extends InstalledContribution { return loader.equals(other.loader) && className.equals(other.className); } - static protected ArrayList list(Base base, File folder, - boolean instantiateMode) { + static protected ArrayList list(Base base, File folder) { ArrayList modes = new ArrayList(); - list(base, folder, modes, instantiateMode); + ArrayList modeFolders = discover(folder); + + for (File potentialModeFolder : modeFolders) { + ModeContribution contrib = getContributedMode(base, potentialModeFolder); + if (contrib != null) { + modes.add(contrib); + } + } return modes; } - - static protected void list(Base base, File folder, - ArrayList modes, - boolean instantiateModeClass) { - if (!folder.isDirectory()) - return; + + static protected ArrayList discover(File folder) { + ArrayList modeFolders = new ArrayList(); + discover(folder, modeFolders); + return modeFolders; + } + + static protected void discover(File folder, ArrayList modeFolders) { File[] folders = folder.listFiles(new FileFilter() { public boolean accept(File potentialModeFolder) { @@ -160,14 +170,7 @@ public class ModeContribution extends InstalledContribution { } for (File potentialModeFolder : folders) { - ModeContribution contrib = getContributedMode(base, potentialModeFolder); - if (contrib != null) { - if (!instantiateModeClass - || (instantiateModeClass && contrib.instantiateModeClass())) { - modes.add(contrib); - } - - } + modeFolders.add(potentialModeFolder); } } diff --git a/app/src/processing/app/ToolContribution.java b/app/src/processing/app/ToolContribution.java index a5eecdbf8..f66310d42 100644 --- a/app/src/processing/app/ToolContribution.java +++ b/app/src/processing/app/ToolContribution.java @@ -37,6 +37,8 @@ public class ToolContribution extends InstalledContribution implements Tool { URLClassLoader loader; Tool tool; + + static String propertiesFileName = "tool.properties"; static public ToolContribution getTool(File folder) { ToolContribution tool = new ToolContribution(folder); @@ -47,7 +49,7 @@ public class ToolContribution extends InstalledContribution implements Tool { } private ToolContribution(File folder) { - super(folder, "tool.properties"); + super(folder, ToolContribution.propertiesFileName); File toolDirectory = new File(folder, "tool"); // add dir to classpath for .classes