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

View File

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

View File

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

View File

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