/** Remove a piece of code from the sketch and from the disk. */ public void handleDeleteCode() throws IOException { SketchFile current = editor.getCurrentTab().getSketchFile(); editor.status.clearState(); // make sure the user didn't hide the sketch folder ensureExistence(); // if read-only, give an error if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { // if the files are read-only, need to first do a "save as". Base.showMessage( tr("Sketch is Read-Only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save the sketch in another location,\n" + "and try again.")); return; } // confirm deletion with user, yes/no Object[] options = {tr("OK"), tr("Cancel")}; String prompt = current.isPrimary() ? tr("Are you sure you want to delete this sketch?") : I18n.format(tr("Are you sure you want to delete \"{0}\"?"), current.getPrettyName()); int result = JOptionPane.showOptionDialog( editor, prompt, tr("Delete"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result == JOptionPane.YES_OPTION) { if (current.isPrimary()) { sketch.delete(); editor.base.handleClose(editor); } else { // delete the file if (!current.delete(sketch.getBuildPath().toPath())) { Base.showMessage( tr("Couldn't do it"), I18n.format(tr("Could not delete \"{0}\"."), current.getFileName())); return; } // just set current tab to the main tab editor.selectTab(0); // update the tabs editor.header.repaint(); } } }
private File saveSketchInTempFolder() throws IOException { File tempFolder = FileUtils.createTempFolder("arduino_modified_sketch_"); FileUtils.copy(sketch.getFolder(), tempFolder); for (SketchFile file : Stream.of(sketch.getFiles()).filter(SketchFile::isModified).collect(Collectors.toList())) { Files.write( Paths.get(tempFolder.getAbsolutePath(), file.getFileName()), file.getProgram().getBytes()); } return Paths.get(tempFolder.getAbsolutePath(), sketch.getPrimaryFile().getFileName()).toFile(); }