-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathSketchCode.java
More file actions
369 lines (260 loc) · 8.38 KB
/
SketchCode.java
File metadata and controls
369 lines (260 loc) · 8.38 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
SketchCode - data class for a single file inside a sketch
Part of the Processing project - http://processing.org
Copyright (c) 2004-11 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 java.io.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.undo.*;
/**
* Represents a single tab of a sketch.
*/
public class SketchCode {
/** Pretty name (no extension), not the full file name */
private String prettyName;
/** File object for where this code is located */
private File file;
/** Extension for this file (no dots, and in lowercase). */
private String extension;
/** Text of the program text for this tab */
private String program;
/** Last version of the program on disk. */
private String savedProgram;
/** Document object for this tab. Currently this is a SyntaxDocument. */
private Document document;
/** Last time this tab was visited */
long visited;
/** The last time this tab was saved to disk */
private long lastModified;
/**
* Undo Manager for this tab, each tab keeps track of their own
* Editor.undo will be set to this object when this code is the tab
* that's currently the front.
*/
private UndoManager undo = new UndoManager();
/** What was on top of the undo stack when last saved. */
// private UndoableEdit lastEdit;
// saved positions from last time this tab was used
private int selectionStart;
private int selectionStop;
private int scrollPosition;
private boolean modified;
/** name of .java file after preproc */
// private String preprocName;
/** where this code starts relative to the concat'd code */
private int preprocOffset;
public SketchCode(File file, String extension) {
this.file = file;
this.extension = extension;
makePrettyName();
try {
load();
} catch (IOException e) {
System.err.println("Error while loading code " + file.getName());
}
}
protected void makePrettyName() {
prettyName = file.getName();
int dot = prettyName.lastIndexOf('.');
prettyName = prettyName.substring(0, dot);
}
public File getFile() {
return file;
}
protected boolean fileExists() {
return file.exists();
}
protected boolean fileReadOnly() {
return !file.canWrite();
}
protected boolean deleteFile() {
return file.delete();
}
protected boolean renameTo(File what, String ext) {
// System.out.println("renaming " + file);
// System.out.println(" to " + what);
boolean success = file.renameTo(what);
if (success) {
this.file = what; // necessary?
this.extension = ext;
makePrettyName();
}
return success;
}
public void copyTo(File dest) throws IOException {
Util.saveFile(program, dest);
}
public String getFileName() {
return file.getName();
}
public String getPrettyName() {
return prettyName;
}
public String getExtension() {
return extension;
}
public boolean isExtension(String what) {
return extension.equals(what);
}
/** get the current text for this tab */
public String getProgram() {
return program;
}
/** set the current text for this tab */
public void setProgram(String replacement) {
program = replacement;
}
/** get the last version saved of this tab */
public String getSavedProgram() {
return savedProgram;
}
public int getLineCount() {
return Util.countLines(program);
}
public void setModified(boolean modified) {
this.modified = modified;
}
public boolean isModified() {
return modified;
}
// public void setPreprocName(String preprocName) {
// this.preprocName = preprocName;
// }
//
//
// public String getPreprocName() {
// return preprocName;
// }
public void setPreprocOffset(int preprocOffset) {
this.preprocOffset = preprocOffset;
}
public int getPreprocOffset() {
return preprocOffset;
}
public void addPreprocOffset(int extra) {
preprocOffset += extra;
}
public Document getDocument() {
return document;
}
public String getDocumentText() throws BadLocationException {
return document.getText(0, document.getLength());
}
public void setDocument(Document d) {
document = d;
}
public UndoManager getUndo() {
return undo;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
// TODO these could probably be handled better, since it's a general state
// issue that's read/write from only one location in Editor (on tab switch.)
public int getSelectionStart() {
return selectionStart;
}
public int getSelectionStop() {
return selectionStop;
}
public int getScrollPosition() {
return scrollPosition;
}
protected void setState(String p, int start, int stop, int pos) {
program = p;
selectionStart = start;
selectionStop = stop;
scrollPosition = pos;
}
public long lastVisited() {
return visited;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Load this piece of code from a file.
*/
public void load() throws IOException {
program = Util.loadFile(file);
if (program == null) {
System.err.println("There was a problem loading " + file);
System.err.println("This may happen because you don't have permissions to read the file, or the file has gone missing.");
throw new IOException("Cannot read or access " + file);
}
// Remove NUL characters because they'll cause problems,
// and their presence is very difficult to debug.
// https://github.com/processing/processing/issues/1973
if (program.indexOf('\0') != -1) {
program = program.replaceAll("\0", "");
}
savedProgram = program;
// This used to be the "Fix Encoding and Reload" warning, but since that
// tool has been removed, let's ramble about text editors and encodings.
if (program.indexOf('\uFFFD') != -1) {
System.err.println(file.getName() + " contains unrecognized characters.");
System.err.println("You should re-open " + file.getName() +
" with a text editor,");
System.err.println("and re-save it in UTF-8 format. Otherwise, you can");
System.err.println("delete the bad characters to get rid of this warning.");
System.err.println();
}
setLastModified();
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);
Util.saveFile(program, file);
savedProgram = program;
lastModified = file.lastModified();
setModified(false);
}
/**
* Save this file to another location, used by Sketch.saveAs()
*/
public void saveAs(File newFile) throws IOException {
Util.saveFile(program, newFile);
savedProgram = program;
file = newFile;
makePrettyName();
setLastModified();
setModified(false);
}
/**
* Called when the sketch folder name/location has changed. Called when
* renaming tab 0, the main code.
*/
public void setFolder(File sketchFolder) {
file = new File(sketchFolder, file.getName());
}
/**
* Set the last known modification time, so that we're not re-firing
* "hey, this is modified!" events incessantly.
*/
public void setLastModified() {
lastModified = file.lastModified();
}
/**
* Used to determine whether this file was modified externally
* @return The time the file was last modified
*/
public long getLastModified() {
return lastModified;
}
}