Drop and drop to install libraries (.plb files)

This commit is contained in:
pesckal
2011-06-14 02:22:13 +00:00
parent 42248aa321
commit b83b8e94d3
3 changed files with 78 additions and 13 deletions
+9
View File
@@ -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);
}
// ...................................................................
+46 -10
View File
@@ -253,6 +253,9 @@ public abstract class Editor extends JFrame implements RunnerListener {
public boolean importData(JComponent src, Transferable transferable) {
int successful = 0;
ArrayList<File> libraryFiles = new ArrayList<File>();
ArrayList<File> sketchFiles = new ArrayList<File>();
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;
}
}
+23 -3
View File
@@ -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<Library> newLibs) {
protected int installLibraries(ArrayList<Library> newLibs) {
ArrayList<Library> 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;
}
}