22
22
23
23
package processing .app ;
24
24
25
- import java .io .*;
26
- import java .util .List ;
25
+ import processing .app .helpers .FileUtils ;
26
+
27
+ import java .io .File ;
28
+ import java .io .FileFilter ;
29
+ import java .io .IOException ;
27
30
import java .util .Arrays ;
31
+ import java .util .List ;
28
32
29
33
import static processing .app .I18n ._ ;
30
- import processing .app .helpers .FileUtils ;
31
34
32
35
/**
33
- * Represents a single tab of a sketch.
36
+ * Represents a single tab of a sketch.
34
37
*/
35
38
public class SketchCode {
36
-
37
- /** Pretty name (no extension), not the full file name */
38
- private String prettyName ;
39
39
40
- /** File object for where this code is located */
40
+ /**
41
+ * File object for where this code is located
42
+ */
41
43
private File file ;
42
44
43
- /** Text of the program text for this tab */
45
+ /**
46
+ * Text of the program text for this tab
47
+ */
44
48
private String program ;
45
49
46
50
private boolean modified ;
47
51
48
- /** where this code starts relative to the concat'd code */
49
- private int preprocOffset ;
52
+ /**
53
+ * where this code starts relative to the concat'd code
54
+ */
55
+ private int preprocOffset ;
50
56
51
57
private Object metadata ;
52
58
@@ -62,8 +68,6 @@ private void init(File file, Object metadata) {
62
68
this .file = file ;
63
69
this .metadata = metadata ;
64
70
65
- makePrettyName ();
66
-
67
71
try {
68
72
load ();
69
73
} catch (IOException e ) {
@@ -73,28 +77,21 @@ private void init(File file, Object metadata) {
73
77
}
74
78
75
79
76
- protected void makePrettyName () {
77
- prettyName = file .getName ();
78
- int dot = prettyName .lastIndexOf ('.' );
79
- prettyName = prettyName .substring (0 , dot );
80
- }
81
-
82
-
83
80
public File getFile () {
84
81
return file ;
85
82
}
86
-
87
-
83
+
84
+
88
85
protected boolean fileExists () {
89
86
return file .exists ();
90
87
}
91
-
92
-
88
+
89
+
93
90
protected boolean fileReadOnly () {
94
91
return !file .canWrite ();
95
92
}
96
-
97
-
93
+
94
+
98
95
protected boolean deleteFile (File tempBuildFolder ) {
99
96
if (!file .delete ()) {
100
97
return false ;
@@ -106,62 +103,66 @@ public boolean accept(File pathname) {
106
103
}
107
104
});
108
105
for (File compiledFile : compiledFiles ) {
109
- compiledFile .delete ();
106
+ if (!compiledFile .delete ()) {
107
+ return false ;
108
+ }
110
109
}
111
110
112
111
return true ;
113
112
}
114
-
115
-
113
+
114
+
116
115
protected boolean renameTo (File what ) {
117
116
boolean success = file .renameTo (what );
118
117
if (success ) {
119
118
file = what ;
120
- makePrettyName ();
121
119
}
122
120
return success ;
123
121
}
124
-
125
-
126
- protected void copyTo (File dest ) throws IOException {
127
- BaseNoGui .saveFile (program , dest );
128
- }
129
-
122
+
130
123
131
124
public String getFileName () {
132
125
return file .getName ();
133
126
}
134
-
135
-
127
+
128
+
136
129
public String getPrettyName () {
137
- return prettyName ;
130
+ String prettyName = getFileName ();
131
+ int dot = prettyName .lastIndexOf ('.' );
132
+ return prettyName .substring (0 , dot );
133
+ }
134
+
135
+ public String getFileNameWithExtensionIfNotIno () {
136
+ if (getFileName ().endsWith (".ino" )) {
137
+ return getPrettyName ();
138
+ }
139
+ return getFileName ();
138
140
}
139
-
140
-
141
+
141
142
public boolean isExtension (String ... extensions ) {
142
143
return isExtension (Arrays .asList (extensions ));
143
144
}
144
145
145
146
public boolean isExtension (List <String > extensions ) {
146
147
return FileUtils .hasExtension (file , extensions );
147
148
}
148
-
149
-
149
+
150
+
150
151
public String getProgram () {
151
152
return program ;
152
153
}
153
-
154
-
154
+
155
+
155
156
public void setProgram (String replacement ) {
156
157
program = replacement ;
157
158
}
158
-
159
-
159
+
160
+
160
161
public int getLineCount () {
161
162
return BaseNoGui .countLines (program );
162
163
}
163
-
164
-
164
+
165
+
165
166
public void setModified (boolean modified ) {
166
167
this .modified = modified ;
167
168
}
@@ -177,25 +178,21 @@ public void setPreprocOffset(int preprocOffset) {
177
178
}
178
179
179
180
180
- public int getPreprocOffset () {
181
- return preprocOffset ;
182
- }
183
-
184
-
185
181
public void addPreprocOffset (int extra ) {
186
182
preprocOffset += extra ;
187
183
}
188
184
189
185
190
- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
191
-
192
-
193
186
/**
194
187
* Load this piece of code from a file.
195
188
*/
196
- public void load () throws IOException {
189
+ private void load () throws IOException {
197
190
program = BaseNoGui .loadFile (file );
198
191
192
+ if (program == null ) {
193
+ throw new IOException ();
194
+ }
195
+
199
196
if (program .indexOf ('\uFFFD' ) != -1 ) {
200
197
System .err .println (
201
198
I18n .format (
@@ -209,7 +206,7 @@ public void load() throws IOException {
209
206
);
210
207
System .err .println ();
211
208
}
212
-
209
+
213
210
setModified (false );
214
211
}
215
212
0 commit comments