~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to src/processing/app/SketchCode.java

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
 
2
 
 
3
/*
 
4
  SketchCode - data class for a single file inside a sketch
 
5
  Part of the Processing project - http://processing.org
 
6
 
 
7
  Copyright (c) 2004-08 Ben Fry and Casey Reas
 
8
  Copyright (c) 2001-04 Massachusetts Institute of Technology
 
9
 
 
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.
 
14
 
 
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.
 
19
 
 
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
 
23
*/
 
24
 
 
25
package processing.app;
 
26
 
 
27
import java.io.*;
 
28
 
 
29
import javax.swing.text.Document;
 
30
import javax.swing.undo.*;
 
31
 
 
32
 
 
33
/**
 
34
 * Represents a single tab of a sketch. 
 
35
 */
 
36
public class SketchCode {
 
37
  /** Pretty name (no extension), not the full file name */
 
38
  private String prettyName;
 
39
 
 
40
  /** File object for where this code is located */
 
41
  private File file;
 
42
 
 
43
  /** Extension for this file (no dots, and in lowercase). */ 
 
44
  private String extension;
 
45
 
 
46
  /** Text of the program text for this tab */
 
47
  private String program;
 
48
 
 
49
  /** Document object for this tab. Currently this is a SyntaxDocument. */
 
50
  private Document document;
 
51
 
 
52
  /**
 
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.
 
56
   */
 
57
  private UndoManager undo = new UndoManager();
 
58
 
 
59
  // saved positions from last time this tab was used
 
60
  private int selectionStart;
 
61
  private int selectionStop;
 
62
  private int scrollPosition;
 
63
 
 
64
  private boolean modified;
 
65
 
 
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;  
 
70
 
 
71
 
 
72
  public SketchCode(File file, String extension) {
 
73
    this.file = file;
 
74
    this.extension = extension;
 
75
 
 
76
    makePrettyName();
 
77
 
 
78
    try {
 
79
      load();
 
80
    } catch (IOException e) {
 
81
      System.err.println("Error while loading code " + file.getName());
 
82
    }
 
83
  }
 
84
 
 
85
 
 
86
  protected void makePrettyName() {
 
87
    prettyName = file.getName();
 
88
    int dot = prettyName.indexOf('.');
 
89
    prettyName = prettyName.substring(0, dot);
 
90
  }
 
91
 
 
92
 
 
93
  public File getFile() {
 
94
    return file;
 
95
  }
 
96
  
 
97
  
 
98
  protected boolean fileExists() {
 
99
    return file.exists();
 
100
  }
 
101
  
 
102
  
 
103
  protected boolean fileReadOnly() {
 
104
    return !file.canWrite();
 
105
  }
 
106
  
 
107
  
 
108
  protected boolean deleteFile() {
 
109
    return file.delete();
 
110
  }
 
111
  
 
112
  
 
113
  protected boolean renameTo(File what, String ext) {
 
114
    boolean success = file.renameTo(what);
 
115
    if (success) {
 
116
      this.file = what;  // necessary?
 
117
      this.extension = ext;
 
118
      makePrettyName();
 
119
    }
 
120
    return success;
 
121
  }
 
122
  
 
123
  
 
124
  protected void copyTo(File dest) throws IOException {
 
125
    Base.saveFile(program, dest);
 
126
  }
 
127
  
 
128
 
 
129
  public String getFileName() {
 
130
    return file.getName();
 
131
  }
 
132
  
 
133
  
 
134
  public String getPrettyName() {
 
135
    return prettyName;
 
136
  }
 
137
  
 
138
  
 
139
  public String getExtension() {
 
140
    return extension;
 
141
  }
 
142
  
 
143
  
 
144
  public boolean isExtension(String what) {
 
145
    return extension.equals(what);
 
146
  }
 
147
  
 
148
  
 
149
  public String getProgram() {
 
150
    return program;
 
151
  }
 
152
  
 
153
  
 
154
  public void setProgram(String replacement) {
 
155
    program = replacement;
 
156
  }
 
157
  
 
158
  
 
159
  public int getLineCount() {
 
160
    return Base.countLines(program);
 
161
  }
 
162
  
 
163
  
 
164
  public void setModified(boolean modified) {
 
165
    this.modified = modified;
 
166
  }
 
167
 
 
168
 
 
169
  public boolean isModified() {
 
170
    return modified;
 
171
  }
 
172
 
 
173
 
 
174
//  public void setPreprocName(String preprocName) {
 
175
//    this.preprocName = preprocName;
 
176
//  }
 
177
//
 
178
//
 
179
//  public String getPreprocName() {
 
180
//    return preprocName;
 
181
//  }
 
182
 
 
183
 
 
184
  public void setPreprocOffset(int preprocOffset) {
 
185
    this.preprocOffset = preprocOffset;
 
186
  }
 
187
 
 
188
 
 
189
  public int getPreprocOffset() {
 
190
    return preprocOffset;
 
191
  }
 
192
  
 
193
  
 
194
  public void addPreprocOffset(int extra) {
 
195
    preprocOffset += extra;
 
196
  }
 
197
 
 
198
 
 
199
  public Document getDocument() {
 
200
    return document;
 
201
  }
 
202
  
 
203
  
 
204
  public void setDocument(Document d) {
 
205
    document = d;
 
206
  }
 
207
  
 
208
  
 
209
  public UndoManager getUndo() {
 
210
    return undo;
 
211
  }
 
212
  
 
213
  
 
214
  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
 
215
 
 
216
  
 
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.)
 
219
  
 
220
  
 
221
  public int getSelectionStart() {
 
222
    return selectionStart;
 
223
  }
 
224
  
 
225
  
 
226
  public int getSelectionStop() {
 
227
    return selectionStop;
 
228
  }
 
229
  
 
230
  
 
231
  public int getScrollPosition() {
 
232
    return scrollPosition;
 
233
  }
 
234
  
 
235
  
 
236
  protected void setState(String p, int start, int stop, int pos) {
 
237
    program = p;
 
238
    selectionStart = start;
 
239
    selectionStop = stop;
 
240
    scrollPosition = pos;
 
241
  }
 
242
  
 
243
  
 
244
  // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
 
245
  
 
246
  
 
247
  /**
 
248
   * Load this piece of code from a file.
 
249
   */
 
250
  public void load() throws IOException {
 
251
    program = Base.loadFile(file);
 
252
 
 
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();
 
260
    }
 
261
    
 
262
    setModified(false);
 
263
  }
 
264
 
 
265
 
 
266
  /**
 
267
   * Save this piece of code, regardless of whether the modified
 
268
   * flag is set or not.
 
269
   */
 
270
  public void save() throws IOException {
 
271
    // TODO re-enable history
 
272
    //history.record(s, SketchHistory.SAVE);
 
273
 
 
274
    Base.saveFile(program, file);
 
275
    setModified(false);
 
276
  }
 
277
 
 
278
 
 
279
  /**
 
280
   * Save this file to another location, used by Sketch.saveAs()
 
281
   */
 
282
  public void saveAs(File newFile) throws IOException {
 
283
    Base.saveFile(program, newFile);
 
284
  }
 
285
}