Skip to content

Commit 0798e1c

Browse files
matthijskooijmancmaglie
authored andcommitted
Pass around sketch File objects instead of filenames
This saves a few conversions from File object to String and is generally cleaner.
1 parent 87bdaa8 commit 0798e1c

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

app/src/processing/app/Base.java

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public Base(String[] args) throws Exception {
462462
}
463463

464464
boolean showEditor = (action == ACTION.GUI);
465-
if (handleOpen(path, nextEditorLocation(), showEditor) == null) {
465+
if (handleOpen(new File(path), nextEditorLocation(), showEditor) == null) {
466466
String mess = I18n.format(_("Failed to open sketch: \"{0}\""), path);
467467
// Open failure is fatal in upload/verify mode
468468
if (action == ACTION.VERIFY || action == ACTION.UPLOAD)
@@ -654,7 +654,7 @@ protected boolean restoreSketches() throws Exception {
654654
location = nextEditorLocation();
655655
}
656656
// If file did not exist, null will be returned for the Editor
657-
if (handleOpen(path, location, true) != null) {
657+
if (handleOpen(new File(path), location, true) != null) {
658658
opened++;
659659
}
660660
}
@@ -812,7 +812,7 @@ protected int[] nextEditorLocation() {
812812
* @param shift whether shift is pressed, which will invert prompt setting
813813
* @param noPrompt disable prompt, no matter the setting
814814
*/
815-
protected String createNewUntitled() throws IOException {
815+
protected File createNewUntitled() throws IOException {
816816
File newbieDir = null;
817817
String newbieName = null;
818818

@@ -859,7 +859,7 @@ protected String createNewUntitled() throws IOException {
859859
throw new IOException();
860860
}
861861
FileUtils.copyFile(new File(getContentFile("examples"), "01.Basics" + File.separator + "BareMinimum" + File.separator + "BareMinimum.ino"), newbieFile);
862-
return newbieFile.getAbsolutePath();
862+
return newbieFile;
863863
}
864864

865865

@@ -869,9 +869,9 @@ protected String createNewUntitled() throws IOException {
869869
*/
870870
public void handleNew() throws Exception {
871871
try {
872-
String path = createNewUntitled();
873-
if (path != null) {
874-
Editor editor = handleOpen(path);
872+
File file = createNewUntitled();
873+
if (file != null) {
874+
Editor editor = handleOpen(file);
875875
editor.untitled = true;
876876
}
877877

@@ -900,9 +900,9 @@ public void handleNewReplace() {
900900

901901
protected void handleNewReplaceImpl() {
902902
try {
903-
String path = createNewUntitled();
904-
if (path != null) {
905-
activeEditor.handleOpenInternal(path);
903+
File file = createNewUntitled();
904+
if (file != null) {
905+
activeEditor.handleOpenInternal(file);
906906
activeEditor.untitled = true;
907907
}
908908
// return true;
@@ -918,14 +918,14 @@ protected void handleNewReplaceImpl() {
918918
* Open a sketch, replacing the sketch in the current window.
919919
* @param path Location of the primary pde file for the sketch.
920920
*/
921-
public void handleOpenReplace(String path) {
921+
public void handleOpenReplace(File file) {
922922
if (!activeEditor.checkModified()) {
923923
return; // sketch was modified, and user canceled
924924
}
925925
// Close the running window, avoid window boogers with multiple sketches
926926
activeEditor.internalCloseRunner();
927927

928-
boolean loaded = activeEditor.handleOpenInternal(path);
928+
boolean loaded = activeEditor.handleOpenInternal(file);
929929
if (!loaded) {
930930
// replace the document without checking if that's ok
931931
handleNewReplaceImpl();
@@ -956,30 +956,30 @@ public void handleOpenPrompt() throws Exception {
956956
File inputFile = fd.getSelectedFile();
957957

958958
Preferences.set("last.folder", inputFile.getAbsolutePath());
959-
handleOpen(inputFile.getAbsolutePath());
959+
handleOpen(inputFile);
960960
}
961961

962962

963963
/**
964964
* Open a sketch in a new window.
965-
* @param path Path to the pde file for the sketch in question
965+
* @param file File to open
966966
* @return the Editor object, so that properties (like 'untitled')
967967
* can be set by the caller
968968
* @throws Exception
969969
*/
970-
public Editor handleOpen(String path) throws Exception {
971-
return handleOpen(path, nextEditorLocation(), true);
970+
public Editor handleOpen(File file) throws Exception {
971+
return handleOpen(file, nextEditorLocation(), true);
972972
}
973973

974974

975-
protected Editor handleOpen(String path, int[] location, boolean showEditor) throws Exception {
975+
protected Editor handleOpen(File file, int[] location, boolean showEditor) throws Exception {
976976
// System.err.println("entering handleOpen " + path);
977977

978-
File file = new File(path);
979978
if (!file.exists()) return null;
980979

981980
// System.err.println(" editors: " + editors);
982981
// Cycle through open windows to make sure that it's not already open.
982+
String path = file.getAbsolutePath();
983983
for (Editor editor : editors) {
984984
if (editor.getSketch().getMainFilePath().equals(path)) {
985985
editor.toFront();
@@ -1003,7 +1003,7 @@ protected Editor handleOpen(String path, int[] location, boolean showEditor) thr
10031003
// }
10041004

10051005
// System.err.println(" creating new editor");
1006-
Editor editor = new Editor(this, path, location);
1006+
Editor editor = new Editor(this, file, location);
10071007
// Editor editor = null;
10081008
// try {
10091009
// editor = new Editor(this, path, location);
@@ -1746,16 +1746,17 @@ private boolean addSketchesSubmenu(JMenu menu, String name, File folder,
17461746
ActionListener listener = new ActionListener() {
17471747
public void actionPerformed(ActionEvent e) {
17481748
String path = e.getActionCommand();
1749-
if (new File(path).exists()) {
1749+
File file = new File(path);
1750+
if (file.exists()) {
17501751
boolean replace = replaceExisting;
17511752
if ((e.getModifiers() & ActionEvent.SHIFT_MASK) != 0) {
17521753
replace = !replace;
17531754
}
17541755
if (replace) {
1755-
handleOpenReplace(path);
1756+
handleOpenReplace(file);
17561757
} else {
17571758
try {
1758-
handleOpen(path);
1759+
handleOpen(file);
17591760
} catch (Exception e1) {
17601761
e1.printStackTrace();
17611762
}

app/src/processing/app/Editor.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public class Editor extends JFrame implements RunnerListener {
155155
Runnable exportAppHandler;
156156

157157

158-
public Editor(Base ibase, String path, int[] location) throws Exception {
158+
public Editor(Base ibase, File file, int[] location) throws Exception {
159159
super("Arduino");
160160
this.base = ibase;
161161

@@ -310,7 +310,7 @@ public void windowDeactivated(WindowEvent e) {
310310
// System.out.println("t4");
311311

312312
// Open the document that was passed in
313-
boolean loaded = handleOpenInternal(path);
313+
boolean loaded = handleOpenInternal(file);
314314
if (!loaded) sketch = null;
315315

316316
// System.out.println("t5");
@@ -2093,10 +2093,10 @@ protected boolean checkModified() {
20932093
* Open a sketch from a particular path, but don't check to save changes.
20942094
* Used by Sketch.saveAs() to re-open a sketch after the "Save As"
20952095
*/
2096-
protected void handleOpenUnchecked(String path, int codeIndex,
2096+
protected void handleOpenUnchecked(File file, int codeIndex,
20972097
int selStart, int selStop, int scrollPos) {
20982098
internalCloseRunner();
2099-
handleOpenInternal(path);
2099+
handleOpenInternal(file);
21002100
// Replacing a document that may be untitled. If this is an actual
21012101
// untitled document, then editor.untitled will be set by Base.
21022102
untitled = false;
@@ -2111,10 +2111,9 @@ protected void handleOpenUnchecked(String path, int codeIndex,
21112111
* Second stage of open, occurs after having checked to see if the
21122112
* modifications (if any) to the previous sketch need to be saved.
21132113
*/
2114-
protected boolean handleOpenInternal(String path) {
2114+
protected boolean handleOpenInternal(File file) {
21152115
// check to make sure that this .pde file is
21162116
// in a folder of the same name
2117-
File file = new File(path);
21182117
String fileName = file.getName();
21192118
File parent = file.getParentFile();
21202119
String parentName = parent.getName();
@@ -2128,10 +2127,10 @@ protected boolean handleOpenInternal(String path) {
21282127

21292128
} else if (altPdeFile.exists()) {
21302129
// user selected a .java from the same sketch, but open the .pde instead
2131-
path = altPdeFile.getAbsolutePath();
2130+
file = altPdeFile;
21322131
} else if (altInoFile.exists()) {
2133-
path = altInoFile.getAbsolutePath();
2134-
} else if (!path.endsWith(".ino") && !path.endsWith(".pde")) {
2132+
file = altInoFile;
2133+
} else if (!fileName.endsWith(".ino") && !fileName.endsWith(".pde")) {
21352134
Base.showWarning(_("Bad file selected"),
21362135
_("Processing can only open its own sketches\n" +
21372136
"and other files ending in .ino or .pde"), null);
@@ -2180,27 +2179,26 @@ protected boolean handleOpenInternal(String path) {
21802179
}
21812180
// copy the sketch inside
21822181
File properPdeFile = new File(properFolder, file.getName());
2183-
File origPdeFile = new File(path);
21842182
try {
2185-
Base.copyFile(origPdeFile, properPdeFile);
2183+
Base.copyFile(file, properPdeFile);
21862184
} catch (IOException e) {
21872185
Base.showWarning(_("Error"), _("Could not copy to a proper location."), e);
21882186
return false;
21892187
}
21902188

21912189
// remove the original file, so user doesn't get confused
2192-
origPdeFile.delete();
2190+
file.delete();
21932191

21942192
// update with the new path
2195-
path = properPdeFile.getAbsolutePath();
2193+
file = properPdeFile;
21962194

21972195
} else if (result == JOptionPane.NO_OPTION) {
21982196
return false;
21992197
}
22002198
}
22012199

22022200
try {
2203-
sketch = new Sketch(this, path);
2201+
sketch = new Sketch(this, file);
22042202
} catch (IOException e) {
22052203
Base.showWarning(_("Error"), _("Could not create the sketch."), e);
22062204
return false;

app/src/processing/app/Sketch.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ public class Sketch {
106106
* path is location of the main .pde file, because this is also
107107
* simplest to use when opening the file from the finder/explorer.
108108
*/
109-
public Sketch(Editor editor, String path) throws IOException {
109+
public Sketch(Editor editor, File file) throws IOException {
110110
this.editor = editor;
111111

112-
primaryFile = new File(path);
112+
primaryFile = file;
113113

114114
// get the name of the sketch by chopping .pde or .java
115115
// off of the main file name
@@ -136,7 +136,7 @@ public Sketch(Editor editor, String path) throws IOException {
136136
tempBuildFolder = Base.getBuildFolder();
137137
//Base.addBuildFolderToClassPath();
138138

139-
folder = new File(new File(path).getParent());
139+
folder = new File(file.getParent());
140140
//System.out.println("sketch dir is " + folder);
141141

142142
load();
@@ -516,12 +516,11 @@ protected void nameCode(String newName) {
516516
// if successful, set base properties for the sketch
517517

518518
File newMainFile = new File(newFolder, newName + ".ino");
519-
String newMainFilePath = newMainFile.getAbsolutePath();
520519

521520
// having saved everything and renamed the folder and the main .pde,
522521
// use the editor to re-open the sketch to re-init state
523522
// (unfortunately this will kill positions for carets etc)
524-
editor.handleOpenUnchecked(newMainFilePath,
523+
editor.handleOpenUnchecked(newMainFile,
525524
currentIndex,
526525
editor.getSelectionStart(),
527526
editor.getSelectionStop(),
@@ -915,7 +914,7 @@ protected boolean saveAs() throws IOException {
915914
File newFile = new File(newFolder, newName + ".ino");
916915
code[0].saveAs(newFile);
917916

918-
editor.handleOpenUnchecked(newFile.getPath(),
917+
editor.handleOpenUnchecked(newFile,
919918
currentIndex,
920919
editor.getSelectionStart(),
921920
editor.getSelectionStop(),

app/src/processing/app/macosx/ThinkDifferent.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
import com.apple.eawt.*;
2828

29+
import java.io.File;
30+
2931

3032
/**
3133
* Deal with issues related to thinking different. This handles the basic
@@ -97,7 +99,7 @@ public void handleOpenFile(ApplicationEvent ae) {
9799
// System.out.println("got open file event " + ae.getFilename());
98100
String filename = ae.getFilename();
99101
try {
100-
base.handleOpen(filename);
102+
base.handleOpen(new File(filename));
101103
} catch (Exception e) {
102104
e.printStackTrace();
103105
}

0 commit comments

Comments
 (0)