From b83b8e94d3de3b054ce9de49f9b3be02fec6de55 Mon Sep 17 00:00:00 2001 From: pesckal Date: Tue, 14 Jun 2011 02:22:13 +0000 Subject: [PATCH] Drop and drop to install libraries (.plb files) --- app/src/processing/app/Base.java | 9 ++++ app/src/processing/app/Editor.java | 56 ++++++++++++++++++---- app/src/processing/app/LibraryManager.java | 26 ++++++++-- 3 files changed, 78 insertions(+), 13 deletions(-) diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 5e9d64662..b203e464e 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -1399,6 +1399,15 @@ public class Base { if (libraryManagerFrame == null) libraryManagerFrame = new LibraryManager(); libraryManagerFrame.showFrame(activeEditor); } + + /** + * Installed the libraries in the given file after a confirmation from the + * user. Returns the number of libraries installed. + */ + public int handleConfirmAndInstallLibrary(File libFile) { + if (libraryManagerFrame == null) libraryManagerFrame = new LibraryManager(); + return libraryManagerFrame.confirmAndInstallLibrary(activeEditor, libFile); + } // ................................................................... diff --git a/app/src/processing/app/Editor.java b/app/src/processing/app/Editor.java index ad156137f..2b5679d00 100644 --- a/app/src/processing/app/Editor.java +++ b/app/src/processing/app/Editor.java @@ -253,6 +253,9 @@ public abstract class Editor extends JFrame implements RunnerListener { public boolean importData(JComponent src, Transferable transferable) { int successful = 0; + ArrayList libraryFiles = new ArrayList(); + ArrayList sketchFiles = new ArrayList(); + try { DataFlavor uriListFlavor = new DataFlavor("text/uri-list;class=java.lang.String"); @@ -262,8 +265,10 @@ public abstract class Editor extends JFrame implements RunnerListener { transferable.getTransferData(DataFlavor.javaFileListFlavor); for (int i = 0; i < list.size(); i++) { File file = (File) list.get(i); - if (sketch.addFile(file)) { - successful++; + if (file.getName().endsWith(".plb")) { + libraryFiles.add(file); + } else { + sketchFiles.add(file); } } } else if (transferable.isDataFlavorSupported(uriListFlavor)) { @@ -280,8 +285,12 @@ public abstract class Editor extends JFrame implements RunnerListener { } else if (pieces[i].startsWith("file:/")) { path = pieces[i].substring(5); } - if (sketch.addFile(new File(path))) { - successful++; + + File file = new File(path); + if (path.endsWith(".plb")) { + libraryFiles.add(file); + } else { + sketchFiles.add(file); } } } @@ -290,16 +299,43 @@ public abstract class Editor extends JFrame implements RunnerListener { "An error occurred while trying to add files to the sketch.", e); return false; } + + if (libraryFiles.isEmpty()) { + for (File file : sketchFiles) { + if (sketch.addFile(file)) { + successful++; + } + } + + if (successful == 0) { + statusError("No files were added to the sketch."); - if (successful == 0) { - statusError("No files were added to the sketch."); - - } else if (successful == 1) { - statusNotice("One file added to the sketch."); + } else if (successful == 1) { + statusNotice("One file added to the sketch."); + } else { + statusNotice(successful + " files added to the sketch."); + } } else { - statusNotice(successful + " files added to the sketch."); + if (sketchFiles.isEmpty()) { + for (File file : libraryFiles) { + successful += getBase().handleConfirmAndInstallLibrary(file); + } + + if (successful == 0) { + statusError("No libraries were added to the sketchbook."); + + } else if (successful == 1) { + statusNotice("One library added to the sketchbook."); + + } else { + statusNotice(successful + " libraries added to the sketchbook."); + } + } else { + statusError("Can't install libraries and add files to the sketch in a single operation."); + } } + return true; } } diff --git a/app/src/processing/app/LibraryManager.java b/app/src/processing/app/LibraryManager.java index 98ad1d928..36c0d2980 100644 --- a/app/src/processing/app/LibraryManager.java +++ b/app/src/processing/app/LibraryManager.java @@ -147,22 +147,24 @@ public class LibraryManager { * Installs the given library file to the active sketchbook. The contents of * the library are extracted to a temporary folder before being moved. */ - protected void installLibrary(File libFile) { + protected int installLibrary(File libFile) { try { String libName = guessLibraryName(libFile.getPath()); File tmpFolder = Base.createTempFolder(libName, "uncompressed"); unzip(libFile, tmpFolder); - installLibraries(Library.list(tmpFolder)); + return installLibraries(Library.list(tmpFolder)); } catch (IOException e) { Base.showError("Trouble creating temporary folder", "Could not create a place to store libary's uncompressed contents.\n" + "That's gonna prevent us from continuing.", e); } + + return 0; } - protected void installLibraries(ArrayList newLibs) { + protected int installLibraries(ArrayList newLibs) { ArrayList oldLibs = editor.getMode().contribLibraries; // Remove any libraries that are already installed. @@ -187,6 +189,8 @@ public class LibraryManager { newLib.folder.renameTo(new File(editor.getBase().getSketchbookLibrariesFolder(), libFolderName)); } + + return newLibs.size(); } /** @@ -265,4 +269,20 @@ public class LibraryManager { dialog.setVisible(true); } + public int confirmAndInstallLibrary(Editor editor, File libFile) { + this.editor = editor; + + String prompt = "Install libraries from " + libFile.getName() + "? "; + + int result = JOptionPane.showConfirmDialog(this.editor, prompt, "Close", + JOptionPane.YES_NO_OPTION, + JOptionPane.QUESTION_MESSAGE); + + if (result == 0) { + return installLibrary(libFile); + } + + return 0; + } + }