|
28 | 28 | import java.util.*;
|
29 | 29 |
|
30 | 30 | import javax.swing.*;
|
| 31 | +import javax.swing.filechooser.FileNameExtensionFilter; |
31 | 32 |
|
32 | 33 | import processing.app.debug.Compiler;
|
33 | 34 | import processing.app.debug.Target;
|
| 35 | +import processing.app.helpers.FileUtils; |
| 36 | +import processing.app.helpers.filefilters.OnlyDirs; |
34 | 37 | import processing.app.tools.ZipDeflater;
|
35 | 38 | import processing.core.*;
|
36 | 39 | import static processing.app.I18n._;
|
@@ -949,9 +952,10 @@ public void rebuildImportMenu(JMenu importMenu, final Editor editor) {
|
949 | 952 | JMenuItem addLibraryMenuItem = new JMenuItem(_("Add Library..."));
|
950 | 953 | addLibraryMenuItem.addActionListener(new ActionListener() {
|
951 | 954 | public void actionPerformed(ActionEvent e) {
|
952 |
| - Base.this.handleAddZipLibrary(editor); |
| 955 | + Base.this.handleAddLibrary(editor); |
953 | 956 | Base.this.onBoardOrPortChange();
|
954 | 957 | Base.this.rebuildImportMenu(Editor.importMenu, editor);
|
| 958 | + Base.this.rebuildExamplesMenu(Editor.examplesMenu); |
955 | 959 | }
|
956 | 960 | });
|
957 | 961 | importMenu.add(addLibraryMenuItem);
|
@@ -2374,24 +2378,70 @@ static protected void listFiles(String basePath,
|
2374 | 2378 | }
|
2375 | 2379 | }
|
2376 | 2380 |
|
| 2381 | + public void handleAddLibrary(Editor editor) { |
| 2382 | + JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home")); |
| 2383 | + fileChooser.setDialogTitle(_("Select a zip file or a folder containing the library you'd like to add")); |
| 2384 | + fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); |
| 2385 | + fileChooser.setFileFilter(new FileNameExtensionFilter(_("ZIP files or folders"), "zip")); |
2377 | 2386 |
|
2378 |
| - public void handleAddZipLibrary(Editor editor) { |
2379 |
| - String prompt = _("Select a zip file containing the library you'd like to add"); |
2380 |
| - FileDialog fd = new FileDialog(editor, prompt, FileDialog.LOAD); |
2381 |
| - fd.setDirectory(System.getProperty("user.home")); |
2382 |
| - fd.setVisible(true); |
| 2387 | + Dimension preferredSize = fileChooser.getPreferredSize(); |
| 2388 | + fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200)); |
2383 | 2389 |
|
2384 |
| - String directory = fd.getDirectory(); |
2385 |
| - String filename = fd.getFile(); |
2386 |
| - if (filename == null) return; |
| 2390 | + int returnVal = fileChooser.showOpenDialog(editor); |
| 2391 | + |
| 2392 | + if (returnVal != JFileChooser.APPROVE_OPTION) { |
| 2393 | + return; |
| 2394 | + } |
| 2395 | + |
| 2396 | + File sourceFile = fileChooser.getSelectedFile(); |
| 2397 | + File tmpFolder = null; |
2387 | 2398 |
|
2388 |
| - File sourceFile = new File(directory, filename); |
2389 | 2399 | try {
|
2390 |
| - ZipDeflater zipDeflater = new ZipDeflater(sourceFile, getSketchbookLibrariesFolder()); |
2391 |
| - zipDeflater.deflate(); |
| 2400 | + // unpack ZIP |
| 2401 | + if (!sourceFile.isDirectory()) { |
| 2402 | + try { |
| 2403 | + tmpFolder = FileUtils.createTempFolder(); |
| 2404 | + ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder); |
| 2405 | + zipDeflater.deflate(); |
| 2406 | + File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs()); |
| 2407 | + if (foldersInTmpFolder.length != 1) { |
| 2408 | + throw new IOException(_("Zip doesn't contain a library")); |
| 2409 | + } |
| 2410 | + sourceFile = foldersInTmpFolder[0]; |
| 2411 | + } catch (IOException e) { |
| 2412 | + editor.statusError(e); |
| 2413 | + return; |
| 2414 | + } |
| 2415 | + } |
| 2416 | + |
| 2417 | + // is there a valid library? |
| 2418 | + File libFolder = sourceFile; |
| 2419 | + String libName = libFolder.getName(); |
| 2420 | + if (!Sketch.isSanitaryName(libName)) { |
| 2421 | + String mess = I18n.format(_("The library \"{0}\" cannot be used.\n" |
| 2422 | + + "Library names must contain only basic letters and numbers.\n" |
| 2423 | + + "(ASCII only and no spaces, and it cannot start with a number)"), |
| 2424 | + libName); |
| 2425 | + editor.statusError(mess); |
| 2426 | + return; |
| 2427 | + } |
| 2428 | + |
| 2429 | + // copy folder |
| 2430 | + File destinationFolder = new File(getSketchbookLibrariesFolder(), sourceFile.getName()); |
| 2431 | + if (!destinationFolder.mkdir()) { |
| 2432 | + editor.statusError(I18n.format(_("A library named {0} already exists"), sourceFile.getName())); |
| 2433 | + return; |
| 2434 | + } |
| 2435 | + try { |
| 2436 | + FileUtils.copy(sourceFile, destinationFolder); |
| 2437 | + } catch (IOException e) { |
| 2438 | + editor.statusError(e); |
| 2439 | + return; |
| 2440 | + } |
2392 | 2441 | editor.statusNotice(_("Library added to your libraries. Check \"Import library\" menu"));
|
2393 |
| - } catch (IOException e) { |
2394 |
| - editor.statusError(e); |
| 2442 | + } finally { |
| 2443 | + // delete zip created temp folder, if exists |
| 2444 | + FileUtils.recursiveDelete(tmpFolder); |
2395 | 2445 | }
|
2396 | 2446 | }
|
2397 | 2447 | }
|
0 commit comments