/** Save all code in the current sketch. */ public boolean save() throws IOException { // make sure the user didn't hide the sketch folder ensureExistence(); if (isReadOnly( BaseNoGui.librariesIndexer.getInstalledLibraries(), BaseNoGui.getExamplesPath())) { Base.showMessage( tr("Sketch is read-only"), tr( "Some files are marked \"read-only\", so you'll\n" + "need to re-save this sketch to another location.")); return saveAs(); } // rename .pde files to .ino List<SketchFile> oldFiles = new ArrayList<>(); for (SketchFile file : sketch.getFiles()) { if (file.isExtension(Sketch.OLD_SKETCH_EXTENSIONS)) oldFiles.add(file); } if (oldFiles.size() > 0) { if (PreferencesData.get("editor.update_extension") == null) { Object[] options = {tr("OK"), tr("Cancel")}; int result = JOptionPane.showOptionDialog( editor, tr( "In Arduino 1.0, the default file extension has changed\n" + "from .pde to .ino. New sketches (including those created\n" + "by \"Save-As\") will use the new extension. The extension\n" + "of existing sketches will be updated on save, but you can\n" + "disable this in the Preferences dialog.\n" + "\n" + "Save sketch and update its extension?"), tr(".pde -> .ino"), JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (result != JOptionPane.OK_OPTION) return false; // save cancelled PreferencesData.setBoolean("editor.update_extension", true); } if (PreferencesData.getBoolean("editor.update_extension")) { // Do rename of all .pde files to new .ino extension for (SketchFile file : oldFiles) { File newName = FileUtils.replaceExtension(file.getFile(), Sketch.DEFAULT_SKETCH_EXTENSION); file.renameTo(newName.getName()); } } } sketch.save(); return true; }
private boolean upload(String suggestedClassName, boolean usingProgrammer) throws Exception { UploaderUtils uploaderInstance = new UploaderUtils(); Uploader uploader = uploaderInstance.getUploaderByPreferences(false); boolean success = false; do { if (uploader.requiresAuthorization() && !PreferencesData.has(uploader.getAuthorizationKey())) { PasswordAuthorizationDialog dialog = new PasswordAuthorizationDialog( editor, tr("Type board password to upload a new sketch")); dialog.setLocationRelativeTo(editor); dialog.setVisible(true); if (dialog.isCancelled()) { editor.statusNotice(tr("Upload cancelled")); return false; } PreferencesData.set(uploader.getAuthorizationKey(), dialog.getPassword()); } List<String> warningsAccumulator = new LinkedList<>(); try { success = uploaderInstance.upload( sketch, uploader, suggestedClassName, usingProgrammer, false, warningsAccumulator); } finally { if (uploader.requiresAuthorization() && !success) { PreferencesData.remove(uploader.getAuthorizationKey()); } } for (String warning : warningsAccumulator) { System.out.print(tr("Warning")); System.out.print(": "); System.out.println(warning); } } while (uploader.requiresAuthorization() && !success); if (!success) { String errorMessage = uploader.getFailureMessage(); if (errorMessage.equals("")) { errorMessage = tr("An error occurred while uploading the sketch"); } editor.statusError(errorMessage); } return success; }
/** * Prompt the user for a new file to the sketch, then call the other addFile() function to * actually add it. */ public void handleAddFile() { // 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; } // get a dialog, select a file to add to the sketch FileDialog fd = new FileDialog( editor, tr("Select an image or other data file to copy to your sketch"), FileDialog.LOAD); fd.setVisible(true); String directory = fd.getDirectory(); String filename = fd.getFile(); if (filename == null) return; // copy the file into the folder. if people would rather // it move instead of copy, they can do it by hand File sourceFile = new File(directory, filename); // now do the work of adding the file boolean result = addFile(sourceFile); if (result) { editor.statusNotice(tr("One file added to the sketch.")); PreferencesData.set("last.folder", sourceFile.getAbsolutePath()); } }