2222
2323package processing .app ;
2424
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 ;
2730import java .util .Arrays ;
31+ import java .util .List ;
2832
2933import static processing .app .I18n ._ ;
30- import processing .app .helpers .FileUtils ;
3134
3235/**
33- * Represents a single tab of a sketch.
36+ * Represents a single tab of a sketch.
3437 */
3538public class SketchCode {
36-
37- /** Pretty name (no extension), not the full file name */
38- private String prettyName ;
3939
40- /** File object for where this code is located */
40+ /**
41+ * File object for where this code is located
42+ */
4143 private File file ;
4244
43- /** Text of the program text for this tab */
45+ /**
46+ * Text of the program text for this tab
47+ */
4448 private String program ;
4549
4650 private boolean modified ;
4751
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 ;
5056
5157 private Object metadata ;
5258
@@ -62,8 +68,6 @@ private void init(File file, Object metadata) {
6268 this .file = file ;
6369 this .metadata = metadata ;
6470
65- makePrettyName ();
66-
6771 try {
6872 load ();
6973 } catch (IOException e ) {
@@ -73,28 +77,21 @@ private void init(File file, Object metadata) {
7377 }
7478
7579
76- protected void makePrettyName () {
77- prettyName = file .getName ();
78- int dot = prettyName .lastIndexOf ('.' );
79- prettyName = prettyName .substring (0 , dot );
80- }
81-
82-
8380 public File getFile () {
8481 return file ;
8582 }
86-
87-
83+
84+
8885 protected boolean fileExists () {
8986 return file .exists ();
9087 }
91-
92-
88+
89+
9390 protected boolean fileReadOnly () {
9491 return !file .canWrite ();
9592 }
96-
97-
93+
94+
9895 protected boolean deleteFile (File tempBuildFolder ) {
9996 if (!file .delete ()) {
10097 return false ;
@@ -106,62 +103,66 @@ public boolean accept(File pathname) {
106103 }
107104 });
108105 for (File compiledFile : compiledFiles ) {
109- compiledFile .delete ();
106+ if (!compiledFile .delete ()) {
107+ return false ;
108+ }
110109 }
111110
112111 return true ;
113112 }
114-
115-
113+
114+
116115 protected boolean renameTo (File what ) {
117116 boolean success = file .renameTo (what );
118117 if (success ) {
119118 file = what ;
120- makePrettyName ();
121119 }
122120 return success ;
123121 }
124-
125-
126- protected void copyTo (File dest ) throws IOException {
127- BaseNoGui .saveFile (program , dest );
128- }
129-
122+
130123
131124 public String getFileName () {
132125 return file .getName ();
133126 }
134-
135-
127+
128+
136129 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 ();
138140 }
139-
140-
141+
141142 public boolean isExtension (String ... extensions ) {
142143 return isExtension (Arrays .asList (extensions ));
143144 }
144145
145146 public boolean isExtension (List <String > extensions ) {
146147 return FileUtils .hasExtension (file , extensions );
147148 }
148-
149-
149+
150+
150151 public String getProgram () {
151152 return program ;
152153 }
153-
154-
154+
155+
155156 public void setProgram (String replacement ) {
156157 program = replacement ;
157158 }
158-
159-
159+
160+
160161 public int getLineCount () {
161162 return BaseNoGui .countLines (program );
162163 }
163-
164-
164+
165+
165166 public void setModified (boolean modified ) {
166167 this .modified = modified ;
167168 }
@@ -177,25 +178,21 @@ public void setPreprocOffset(int preprocOffset) {
177178 }
178179
179180
180- public int getPreprocOffset () {
181- return preprocOffset ;
182- }
183-
184-
185181 public void addPreprocOffset (int extra ) {
186182 preprocOffset += extra ;
187183 }
188184
189185
190- // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
191-
192-
193186 /**
194187 * Load this piece of code from a file.
195188 */
196- public void load () throws IOException {
189+ private void load () throws IOException {
197190 program = BaseNoGui .loadFile (file );
198191
192+ if (program == null ) {
193+ throw new IOException ();
194+ }
195+
199196 if (program .indexOf ('\uFFFD' ) != -1 ) {
200197 System .err .println (
201198 I18n .format (
@@ -209,7 +206,7 @@ public void load() throws IOException {
209206 );
210207 System .err .println ();
211208 }
212-
209+
213210 setModified (false );
214211 }
215212
0 commit comments