MCPcopy
hub / github.com/arduino/Arduino / handleAddLibrary

Method handleAddLibrary

app/src/processing/app/Base.java:2332–2422  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2330 }
2331
2332 public void handleAddLibrary() {
2333 JFileChooser fileChooser = new JFileChooser(System.getProperty("user.home"));
2334 fileChooser.setDialogTitle(tr("Select a zip file or a folder containing the library you'd like to add"));
2335 fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
2336 fileChooser.setFileFilter(new FileNameExtensionFilter(tr("ZIP files or folders"), "zip"));
2337
2338 Dimension preferredSize = fileChooser.getPreferredSize();
2339 fileChooser.setPreferredSize(new Dimension(preferredSize.width + 200, preferredSize.height + 200));
2340
2341 int returnVal = fileChooser.showOpenDialog(activeEditor);
2342
2343 if (returnVal != JFileChooser.APPROVE_OPTION) {
2344 return;
2345 }
2346
2347 File sourceFile = fileChooser.getSelectedFile();
2348 File tmpFolder = null;
2349
2350 try {
2351 // unpack ZIP
2352 if (!sourceFile.isDirectory()) {
2353 try {
2354 tmpFolder = FileUtils.createTempFolder();
2355 ZipDeflater zipDeflater = new ZipDeflater(sourceFile, tmpFolder);
2356 zipDeflater.deflate();
2357 File[] foldersInTmpFolder = tmpFolder.listFiles(new OnlyDirs());
2358 if (foldersInTmpFolder.length != 1) {
2359 throw new IOException(tr("Zip doesn't contain a library"));
2360 }
2361 sourceFile = foldersInTmpFolder[0];
2362 } catch (IOException e) {
2363 activeEditor.statusError(e);
2364 return;
2365 }
2366 }
2367
2368 File libFolder = sourceFile;
2369 if (FileUtils.isSubDirectory(new File(PreferencesData.get("sketchbook.path")), libFolder)) {
2370 activeEditor.statusError(tr("A subfolder of your sketchbook is not a valid library"));
2371 return;
2372 }
2373
2374 if (FileUtils.isSubDirectory(libFolder, new File(PreferencesData.get("sketchbook.path")))) {
2375 activeEditor.statusError(tr("You can't import a folder that contains your sketchbook"));
2376 return;
2377 }
2378
2379 String libName = libFolder.getName();
2380 if (!BaseNoGui.isSanitaryName(libName)) {
2381 String mess = format(tr("The library \"{0}\" cannot be used.\n"
2382 + "Library names must contain only basic letters and numbers.\n"
2383 + "(ASCII only and no spaces, and it cannot start with a number)"),
2384 libName);
2385 activeEditor.statusError(mess);
2386 return;
2387 }
2388
2389 String[] headers;

Callers 1

actionPerformedMethod · 0.80

Calls 15

createTempFolderMethod · 0.95
deflateMethod · 0.95
isSubDirectoryMethod · 0.95
getMethod · 0.95
isSanitaryNameMethod · 0.95
copyMethod · 0.95
recursiveDeleteMethod · 0.95
trMethod · 0.80
listFilesMethod · 0.80
statusErrorMethod · 0.65

Tested by

no test coverage detected