1
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
4
SketchCode - data class for a single file inside a sketch
5
Part of the Processing project - http://processing.org
7
Copyright (c) 2004-08 Ben Fry and Casey Reas
8
Copyright (c) 2001-04 Massachusetts Institute of Technology
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software Foundation,
22
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
package processing.app;
29
import javax.swing.text.Document;
30
import javax.swing.undo.*;
34
* Represents a single tab of a sketch.
36
public class SketchCode {
37
/** Pretty name (no extension), not the full file name */
38
private String prettyName;
40
/** File object for where this code is located */
43
/** Extension for this file (no dots, and in lowercase). */
44
private String extension;
46
/** Text of the program text for this tab */
47
private String program;
49
/** Document object for this tab. Currently this is a SyntaxDocument. */
50
private Document document;
53
* Undo Manager for this tab, each tab keeps track of their own
54
* Editor.undo will be set to this object when this code is the tab
55
* that's currently the front.
57
private UndoManager undo = new UndoManager();
59
// saved positions from last time this tab was used
60
private int selectionStart;
61
private int selectionStop;
62
private int scrollPosition;
64
private boolean modified;
66
/** name of .java file after preproc */
67
// private String preprocName;
68
/** where this code starts relative to the concat'd code */
69
private int preprocOffset;
72
public SketchCode(File file, String extension) {
74
this.extension = extension;
80
} catch (IOException e) {
81
System.err.println("Error while loading code " + file.getName());
86
protected void makePrettyName() {
87
prettyName = file.getName();
88
int dot = prettyName.indexOf('.');
89
prettyName = prettyName.substring(0, dot);
93
public File getFile() {
98
protected boolean fileExists() {
103
protected boolean fileReadOnly() {
104
return !file.canWrite();
108
protected boolean deleteFile() {
109
return file.delete();
113
protected boolean renameTo(File what, String ext) {
114
boolean success = file.renameTo(what);
116
this.file = what; // necessary?
117
this.extension = ext;
124
protected void copyTo(File dest) throws IOException {
125
Base.saveFile(program, dest);
129
public String getFileName() {
130
return file.getName();
134
public String getPrettyName() {
139
public String getExtension() {
144
public boolean isExtension(String what) {
145
return extension.equals(what);
149
public String getProgram() {
154
public void setProgram(String replacement) {
155
program = replacement;
159
public int getLineCount() {
160
return Base.countLines(program);
164
public void setModified(boolean modified) {
165
this.modified = modified;
169
public boolean isModified() {
174
// public void setPreprocName(String preprocName) {
175
// this.preprocName = preprocName;
179
// public String getPreprocName() {
180
// return preprocName;
184
public void setPreprocOffset(int preprocOffset) {
185
this.preprocOffset = preprocOffset;
189
public int getPreprocOffset() {
190
return preprocOffset;
194
public void addPreprocOffset(int extra) {
195
preprocOffset += extra;
199
public Document getDocument() {
204
public void setDocument(Document d) {
209
public UndoManager getUndo() {
214
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
217
// TODO these could probably be handled better, since it's a general state
218
// issue that's read/write from only one location in Editor (on tab switch.)
221
public int getSelectionStart() {
222
return selectionStart;
226
public int getSelectionStop() {
227
return selectionStop;
231
public int getScrollPosition() {
232
return scrollPosition;
236
protected void setState(String p, int start, int stop, int pos) {
238
selectionStart = start;
239
selectionStop = stop;
240
scrollPosition = pos;
244
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
248
* Load this piece of code from a file.
250
public void load() throws IOException {
251
program = Base.loadFile(file);
253
if (program.indexOf('\uFFFD') != -1) {
254
System.err.println(file.getName() + " contains unrecognized characters.");
255
System.err.println("If this code was created with an older version of Processing,");
256
System.err.println("you may need to use Tools -> Fix Encoding & Reload to update");
257
System.err.println("the sketch to use UTF-8 encoding. If not, you may need to");
258
System.err.println("delete the bad characters to get rid of this warning.");
259
System.err.println();
267
* Save this piece of code, regardless of whether the modified
268
* flag is set or not.
270
public void save() throws IOException {
271
// TODO re-enable history
272
//history.record(s, SketchHistory.SAVE);
274
Base.saveFile(program, file);
280
* Save this file to another location, used by Sketch.saveAs()
282
public void saveAs(File newFile) throws IOException {
283
Base.saveFile(program, newFile);