2009-05-31 15:53:33 +00:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
|
2015-06-08 15:32:15 +02:00
|
|
|
import processing.app.helpers.FileUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileFilter;
|
|
|
|
import java.io.IOException;
|
2014-07-08 15:35:38 +02:00
|
|
|
import java.util.Arrays;
|
2015-06-08 15:32:15 +02:00
|
|
|
import java.util.List;
|
2009-05-31 15:53:33 +00:00
|
|
|
|
2015-08-05 09:04:53 +02:00
|
|
|
import static processing.app.I18n.tr;
|
2009-05-31 15:53:33 +00:00
|
|
|
|
|
|
|
/**
|
2015-06-08 15:32:15 +02:00
|
|
|
* Represents a single tab of a sketch.
|
2009-05-31 15:53:33 +00:00
|
|
|
*/
|
|
|
|
public class SketchCode {
|
|
|
|
|
2015-06-08 15:32:15 +02:00
|
|
|
/**
|
|
|
|
* File object for where this code is located
|
|
|
|
*/
|
2009-05-31 15:53:33 +00:00
|
|
|
private File file;
|
|
|
|
|
2015-06-08 15:32:15 +02:00
|
|
|
/**
|
|
|
|
* Text of the program text for this tab
|
|
|
|
*/
|
2009-05-31 15:53:33 +00:00
|
|
|
private String program;
|
|
|
|
|
|
|
|
private boolean modified;
|
|
|
|
|
2015-06-08 15:32:15 +02:00
|
|
|
/**
|
|
|
|
* where this code starts relative to the concat'd code
|
|
|
|
*/
|
|
|
|
private int preprocOffset;
|
2009-05-31 15:53:33 +00:00
|
|
|
|
2014-09-18 15:01:38 +02:00
|
|
|
private Object metadata;
|
2009-05-31 15:53:33 +00:00
|
|
|
|
2014-07-08 15:35:38 +02:00
|
|
|
public SketchCode(File file) {
|
2014-09-18 15:01:38 +02:00
|
|
|
init(file, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public SketchCode(File file, Object metadata) {
|
|
|
|
init(file, metadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void init(File file, Object metadata) {
|
2009-05-31 15:53:33 +00:00
|
|
|
this.file = file;
|
2014-09-18 15:01:38 +02:00
|
|
|
this.metadata = metadata;
|
2009-05-31 15:53:33 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
load();
|
|
|
|
} catch (IOException e) {
|
2011-10-05 03:03:19 +09:00
|
|
|
System.err.println(
|
2015-08-05 09:04:53 +02:00
|
|
|
I18n.format(tr("Error while loading code {0}"), file.getName()));
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public File getFile() {
|
|
|
|
return file;
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
protected boolean fileExists() {
|
|
|
|
return file.exists();
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
protected boolean fileReadOnly() {
|
|
|
|
return !file.canWrite();
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2012-12-12 16:25:52 +01:00
|
|
|
protected boolean deleteFile(File tempBuildFolder) {
|
|
|
|
if (!file.delete()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
File[] compiledFiles = tempBuildFolder.listFiles(new FileFilter() {
|
|
|
|
public boolean accept(File pathname) {
|
|
|
|
return pathname.getName().startsWith(getFileName());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
for (File compiledFile : compiledFiles) {
|
2015-06-08 15:32:15 +02:00
|
|
|
if (!compiledFile.delete()) {
|
|
|
|
return false;
|
|
|
|
}
|
2012-12-12 16:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2014-07-08 15:35:38 +02:00
|
|
|
protected boolean renameTo(File what) {
|
2009-05-31 15:53:33 +00:00
|
|
|
boolean success = file.renameTo(what);
|
|
|
|
if (success) {
|
2011-09-10 01:16:24 +02:00
|
|
|
file = what;
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
|
|
|
|
public String getFileName() {
|
|
|
|
return file.getName();
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
public String getPrettyName() {
|
2015-06-08 15:32:15 +02:00
|
|
|
String prettyName = getFileName();
|
|
|
|
int dot = prettyName.lastIndexOf('.');
|
|
|
|
return prettyName.substring(0, dot);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFileNameWithExtensionIfNotIno() {
|
|
|
|
if (getFileName().endsWith(".ino")) {
|
|
|
|
return getPrettyName();
|
|
|
|
}
|
|
|
|
return getFileName();
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
2014-07-08 15:35:38 +02:00
|
|
|
public boolean isExtension(String... extensions) {
|
|
|
|
return isExtension(Arrays.asList(extensions));
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2014-07-08 15:35:38 +02:00
|
|
|
|
|
|
|
public boolean isExtension(List<String> extensions) {
|
|
|
|
return FileUtils.hasExtension(file, extensions);
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
public String getProgram() {
|
|
|
|
return program;
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
public void setProgram(String replacement) {
|
|
|
|
program = replacement;
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
public int getLineCount() {
|
2014-08-22 18:35:15 +02:00
|
|
|
return BaseNoGui.countLines(program);
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
public void setModified(boolean modified) {
|
|
|
|
this.modified = modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isModified() {
|
|
|
|
return modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setPreprocOffset(int preprocOffset) {
|
|
|
|
this.preprocOffset = preprocOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void addPreprocOffset(int extra) {
|
|
|
|
preprocOffset += extra;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load this piece of code from a file.
|
|
|
|
*/
|
2015-06-08 15:32:15 +02:00
|
|
|
private void load() throws IOException {
|
2014-08-22 18:35:15 +02:00
|
|
|
program = BaseNoGui.loadFile(file);
|
2009-05-31 15:53:33 +00:00
|
|
|
|
2015-06-08 15:32:15 +02:00
|
|
|
if (program == null) {
|
|
|
|
throw new IOException();
|
|
|
|
}
|
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
if (program.indexOf('\uFFFD') != -1) {
|
2011-10-05 03:03:19 +09:00
|
|
|
System.err.println(
|
|
|
|
I18n.format(
|
2015-08-05 09:04:53 +02:00
|
|
|
tr("\"{0}\" contains unrecognized characters." +
|
2014-10-08 18:36:25 +02:00
|
|
|
"If this code was created with an older version of Arduino," +
|
2011-10-05 03:03:19 +09:00
|
|
|
"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()
|
|
|
|
)
|
|
|
|
);
|
2009-05-31 15:53:33 +00:00
|
|
|
System.err.println();
|
|
|
|
}
|
2015-06-08 15:32:15 +02:00
|
|
|
|
2009-05-31 15:53:33 +00:00
|
|
|
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);
|
|
|
|
|
2014-08-22 18:35:15 +02:00
|
|
|
BaseNoGui.saveFile(program, file);
|
2009-05-31 15:53:33 +00:00
|
|
|
setModified(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save this file to another location, used by Sketch.saveAs()
|
|
|
|
*/
|
|
|
|
public void saveAs(File newFile) throws IOException {
|
2014-08-22 18:35:15 +02:00
|
|
|
BaseNoGui.saveFile(program, newFile);
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|
2014-09-18 15:01:38 +02:00
|
|
|
|
|
|
|
|
|
|
|
public Object getMetadata() {
|
|
|
|
return metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setMetadata(Object metadata) {
|
|
|
|
this.metadata = metadata;
|
|
|
|
}
|
2009-05-31 15:53:33 +00:00
|
|
|
}
|