-
-
Notifications
You must be signed in to change notification settings - Fork 7k
/
Copy pathSketchCode.java
243 lines (181 loc) · 5.42 KB
/
SketchCode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
SketchCode - data class for a single file inside a sketch
Part of the Processing project - http://processing.org
Copyright (c) 2004-08 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app;
import processing.app.helpers.FileUtils;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static processing.app.I18n.tr;
/**
* Represents a single tab of a sketch.
*/
public class SketchCode {
/**
* File object for where this code is located
*/
private File file;
/**
* Text of the program text for this tab
*/
private String program;
private boolean modified;
private Object metadata;
public SketchCode(File file) {
init(file, null);
}
public SketchCode(File file, Object metadata) {
init(file, metadata);
}
private void init(File file, Object metadata) {
this.file = file;
this.metadata = metadata;
try {
load();
} catch (IOException e) {
System.err.println(
I18n.format(tr("Error while loading code {0}"), file.getName()));
}
}
public File getFile() {
return file;
}
protected boolean fileExists() {
return file.exists();
}
protected boolean fileReadOnly() {
return !file.canWrite();
}
protected boolean deleteFile(Path tempBuildFolder) throws IOException {
if (!file.delete()) {
return false;
}
List<Path> tempBuildFolders = Stream.of(tempBuildFolder, tempBuildFolder.resolve("sketch"))
.filter(path -> Files.exists(path)).collect(Collectors.toList());
for (Path folder : tempBuildFolders) {
if (!deleteCompiledFilesFrom(folder)) {
return false;
}
}
return true;
}
private boolean deleteCompiledFilesFrom(Path tempBuildFolder) throws IOException {
List<Path> compiledFiles = Files.list(tempBuildFolder)
.filter(pathname -> pathname.getFileName().toString().startsWith(getFileName()))
.collect(Collectors.toList());
for (Path compiledFile : compiledFiles) {
try {
Files.delete(compiledFile);
} catch (IOException e) {
return false;
}
}
return true;
}
protected boolean renameTo(File what) {
boolean success = file.renameTo(what);
if (success) {
file = what;
}
return success;
}
public String getFileName() {
return file.getName();
}
public String getPrettyName() {
String prettyName = getFileName();
int dot = prettyName.lastIndexOf('.');
return prettyName.substring(0, dot);
}
public String getFileNameWithExtensionIfNotIno() {
if (getFileName().endsWith(".ino")) {
return getPrettyName();
}
return getFileName();
}
public boolean isExtension(String... extensions) {
return isExtension(Arrays.asList(extensions));
}
public boolean isExtension(List<String> extensions) {
return FileUtils.hasExtension(file, extensions);
}
public String getProgram() {
return program;
}
public void setProgram(String replacement) {
program = replacement;
}
public int getLineCount() {
return BaseNoGui.countLines(program);
}
public void setModified(boolean modified) {
this.modified = modified;
}
public boolean isModified() {
return modified;
}
/**
* Load this piece of code from a file.
*/
private void load() throws IOException {
program = BaseNoGui.loadFile(file);
if (program == null) {
throw new IOException();
}
if (program.indexOf('\uFFFD') != -1) {
System.err.println(
I18n.format(
tr("\"{0}\" contains unrecognized characters. " +
"If this code was created with an older version of Arduino, " +
"you may need to use Tools -> Fix Encoding & Reload to update " +
"the sketch to use UTF-8 encoding. If not, you may need to " +
"delete the bad characters to get rid of this warning."),
file.getName()
)
);
System.err.println();
}
setModified(false);
}
/**
* Save this piece of code, regardless of whether the modified
* flag is set or not.
*/
public void save() throws IOException {
// TODO re-enable history
//history.record(s, SketchHistory.SAVE);
BaseNoGui.saveFile(program, file);
setModified(false);
}
/**
* Save this file to another location, used by Sketch.saveAs()
*/
public void saveAs(File newFile) throws IOException {
BaseNoGui.saveFile(program, newFile);
}
public Object getMetadata() {
return metadata;
}
public void setMetadata(Object metadata) {
this.metadata = metadata;
}
}