1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-11-29 10:24:12 +01:00

Pass around sketch File objects instead of filenames

This saves a few conversions from File object to String and is generally
cleaner.
This commit is contained in:
Matthijs Kooijman 2014-04-08 12:35:22 +02:00 committed by Cristian Maglie
parent 87bdaa88cd
commit 0798e1cf6f
4 changed files with 43 additions and 43 deletions

View File

@ -462,7 +462,7 @@ public class Base {
}
boolean showEditor = (action == ACTION.GUI);
if (handleOpen(path, nextEditorLocation(), showEditor) == null) {
if (handleOpen(new File(path), nextEditorLocation(), showEditor) == null) {
String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path);
// Open failure is fatal in upload/verify mode
if (action == ACTION.VERIFY || action == ACTION.UPLOAD)
@ -654,7 +654,7 @@ public class Base {
location = nextEditorLocation();
}
// If file did not exist, null will be returned for the Editor
if (handleOpen(path, location, true) != null) {
if (handleOpen(new File(path), location, true) != null) {
opened++;
}
}
@ -812,7 +812,7 @@ public class Base {
* @param shift whether shift is pressed, which will invert prompt setting
* @param noPrompt disable prompt, no matter the setting
*/
protected String createNewUntitled() throws IOException {
protected File createNewUntitled() throws IOException {
File newbieDir = null;
String newbieName = null;
@ -859,7 +859,7 @@ public class Base {
throw new IOException();
}
FileUtils.copyFile(new File(getContentFile("examples"), "01.Basics" + File.separator + "BareMinimum" + File.separator + "BareMinimum.ino"), newbieFile);
return newbieFile.getAbsolutePath();
return newbieFile;
}
@ -869,9 +869,9 @@ public class Base {
*/
public void handleNew() throws Exception {
try {
String path = createNewUntitled();
if (path != null) {
Editor editor = handleOpen(path);
File file = createNewUntitled();
if (file != null) {
Editor editor = handleOpen(file);
editor.untitled = true;
}
@ -900,9 +900,9 @@ public class Base {
protected void handleNewReplaceImpl() {
try {
String path = createNewUntitled();
if (path != null) {
activeEditor.handleOpenInternal(path);
File file = createNewUntitled();
if (file != null) {
activeEditor.handleOpenInternal(file);
activeEditor.untitled = true;
}
// return true;
@ -918,14 +918,14 @@ public class Base {
* Open a sketch, replacing the sketch in the current window.
* @param path Location of the primary pde file for the sketch.
*/
public void handleOpenReplace(String path) {
public void handleOpenReplace(File file) {
if (!activeEditor.checkModified()) {
return; // sketch was modified, and user canceled
}
// Close the running window, avoid window boogers with multiple sketches
activeEditor.internalCloseRunner();
boolean loaded = activeEditor.handleOpenInternal(path);
boolean loaded = activeEditor.handleOpenInternal(file);
if (!loaded) {
// replace the document without checking if that's ok
handleNewReplaceImpl();
@ -956,30 +956,30 @@ public class Base {
File inputFile = fd.getSelectedFile();
Preferences.set("last.folder", inputFile.getAbsolutePath());
handleOpen(inputFile.getAbsolutePath());
handleOpen(inputFile);
}
/**
* Open a sketch in a new window.
* @param path Path to the pde file for the sketch in question
* @param file File to open
* @return the Editor object, so that properties (like 'untitled')
* can be set by the caller
* @throws Exception
*/
public Editor handleOpen(String path) throws Exception {
return handleOpen(path, nextEditorLocation(), true);
public Editor handleOpen(File file) throws Exception {
return handleOpen(file, nextEditorLocation(), true);
}
protected Editor handleOpen(String path, int[] location, boolean showEditor) throws Exception {
protected Editor handleOpen(File file, int[] location, boolean showEditor) throws Exception {
// System.err.println("entering handleOpen " + path);
File file = new File(path);
if (!file.exists()) return null;
// System.err.println(" editors: " + editors);
// Cycle through open windows to make sure that it's not already open.
String path = file.getAbsolutePath();
for (Editor editor : editors) {
if (editor.getSketch().getMainFilePath().equals(path)) {
editor.toFront();
@ -1003,7 +1003,7 @@ public class Base {
// }
// System.err.println(" creating new editor");
Editor editor = new Editor(this, path, location);
Editor editor = new Editor(this, file, location);
// Editor editor = null;
// try {
// editor = new Editor(this, path, location);
@ -1746,16 +1746,17 @@ public class Base {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
String path = e.getActionCommand();
if (new File(path).exists()) {
File file = new File(path);
if (file.exists()) {
boolean replace = replaceExisting;
if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0) {
replace = !replace;
}
if (replace) {
handleOpenReplace(path);
handleOpenReplace(file);
} else {
try {
handleOpen(path);
handleOpen(file);
} catch (Exception e1) {
e1.printStackTrace();
}

View File

@ -155,7 +155,7 @@ public class Editor extends JFrame implements RunnerListener {
Runnable exportAppHandler;
public Editor(Base ibase, String path, int[] location) throws Exception {
public Editor(Base ibase, File file, int[] location) throws Exception {
super("Arduino");
this.base = ibase;
@ -310,7 +310,7 @@ public class Editor extends JFrame implements RunnerListener {
// System.out.println("t4");
// Open the document that was passed in
boolean loaded = handleOpenInternal(path);
boolean loaded = handleOpenInternal(file);
if (!loaded) sketch = null;
// System.out.println("t5");
@ -2093,10 +2093,10 @@ public class Editor extends JFrame implements RunnerListener {
* Open a sketch from a particular path, but don't check to save changes.
* Used by Sketch.saveAs() to re-open a sketch after the "Save As"
*/
protected void handleOpenUnchecked(String path, int codeIndex,
protected void handleOpenUnchecked(File file, int codeIndex,
int selStart, int selStop, int scrollPos) {
internalCloseRunner();
handleOpenInternal(path);
handleOpenInternal(file);
// Replacing a document that may be untitled. If this is an actual
// untitled document, then editor.untitled will be set by Base.
untitled = false;
@ -2111,10 +2111,9 @@ public class Editor extends JFrame implements RunnerListener {
* Second stage of open, occurs after having checked to see if the
* modifications (if any) to the previous sketch need to be saved.
*/
protected boolean handleOpenInternal(String path) {
protected boolean handleOpenInternal(File file) {
// check to make sure that this .pde file is
// in a folder of the same name
File file = new File(path);
String fileName = file.getName();
File parent = file.getParentFile();
String parentName = parent.getName();
@ -2128,10 +2127,10 @@ public class Editor extends JFrame implements RunnerListener {
} else if (altPdeFile.exists()) {
// user selected a .java from the same sketch, but open the .pde instead
path = altPdeFile.getAbsolutePath();
file = altPdeFile;
} else if (altInoFile.exists()) {
path = altInoFile.getAbsolutePath();
} else if (!path.endsWith(".ino") && !path.endsWith(".pde")) {
file = altInoFile;
} else if (!fileName.endsWith(".ino") && !fileName.endsWith(".pde")) {
Base.showWarning(_("Bad file selected"),
_("Processing can only open its own sketches\n" +
"and other files ending in .ino or .pde"), null);
@ -2180,19 +2179,18 @@ public class Editor extends JFrame implements RunnerListener {
}
// copy the sketch inside
File properPdeFile = new File(properFolder, file.getName());
File origPdeFile = new File(path);
try {
Base.copyFile(origPdeFile, properPdeFile);
Base.copyFile(file, properPdeFile);
} catch (IOException e) {
Base.showWarning(_("Error"), _("Could not copy to a proper location."), e);
return false;
}
// remove the original file, so user doesn't get confused
origPdeFile.delete();
file.delete();
// update with the new path
path = properPdeFile.getAbsolutePath();
file = properPdeFile;
} else if (result == JOptionPane.NO_OPTION) {
return false;
@ -2200,7 +2198,7 @@ public class Editor extends JFrame implements RunnerListener {
}
try {
sketch = new Sketch(this, path);
sketch = new Sketch(this, file);
} catch (IOException e) {
Base.showWarning(_("Error"), _("Could not create the sketch."), e);
return false;

View File

@ -106,10 +106,10 @@ public class Sketch {
* path is location of the main .pde file, because this is also
* simplest to use when opening the file from the finder/explorer.
*/
public Sketch(Editor editor, String path) throws IOException {
public Sketch(Editor editor, File file) throws IOException {
this.editor = editor;
primaryFile = new File(path);
primaryFile = file;
// get the name of the sketch by chopping .pde or .java
// off of the main file name
@ -136,7 +136,7 @@ public class Sketch {
tempBuildFolder = Base.getBuildFolder();
//Base.addBuildFolderToClassPath();
folder = new File(new File(path).getParent());
folder = new File(file.getParent());
//System.out.println("sketch dir is " + folder);
load();
@ -516,12 +516,11 @@ public class Sketch {
// if successful, set base properties for the sketch
File newMainFile = new File(newFolder, newName + ".ino");
String newMainFilePath = newMainFile.getAbsolutePath();
// having saved everything and renamed the folder and the main .pde,
// use the editor to re-open the sketch to re-init state
// (unfortunately this will kill positions for carets etc)
editor.handleOpenUnchecked(newMainFilePath,
editor.handleOpenUnchecked(newMainFile,
currentIndex,
editor.getSelectionStart(),
editor.getSelectionStop(),
@ -915,7 +914,7 @@ public class Sketch {
File newFile = new File(newFolder, newName + ".ino");
code[0].saveAs(newFile);
editor.handleOpenUnchecked(newFile.getPath(),
editor.handleOpenUnchecked(newFile,
currentIndex,
editor.getSelectionStart(),
editor.getSelectionStop(),

View File

@ -26,6 +26,8 @@ import processing.app.Base;
import com.apple.eawt.*;
import java.io.File;
/**
* Deal with issues related to thinking different. This handles the basic
@ -97,7 +99,7 @@ public class ThinkDifferent implements ApplicationListener {
// System.out.println("got open file event " + ae.getFilename());
String filename = ae.getFilename();
try {
base.handleOpen(filename);
base.handleOpen(new File(filename));
} catch (Exception e) {
e.printStackTrace();
}