Applied (a sort of) decorator pattern to SketchCodeDoc.

SketchCodeDoc renamed to SketchCodeDocument.
Compiler is now independent from SketchCodeDocument.
This commit is contained in:
Cristian Maglie 2014-02-01 20:47:00 +01:00
parent bbd3782a9c
commit 54f3f538f2
6 changed files with 109 additions and 156 deletions

View File

@ -1649,7 +1649,7 @@ public class Editor extends JFrame implements RunnerListener {
// insert the program text into the document object
try {
document.insertString(0, codeDoc.getCode().getProgram(), null);
document.insertString(0, codeDoc.getProgram(), null);
} catch (BadLocationException bl) {
bl.printStackTrace();
}
@ -1659,17 +1659,17 @@ public class Editor extends JFrame implements RunnerListener {
// connect the undo listener to the editor
document.addUndoableEditListener(new UndoableEditListener() {
public void undoableEditHappened(UndoableEditEvent e) {
if (compoundEdit != null) {
compoundEdit.addEdit(e.getEdit());
public void undoableEditHappened(UndoableEditEvent e) {
if (compoundEdit != null) {
compoundEdit.addEdit(e.getEdit());
} else if (undo != null) {
undo.addEdit(new CaretAwareUndoableEdit(e.getEdit(), textarea));
undoAction.updateUndoState();
redoAction.updateRedoState();
}
} else if (undo != null) {
undo.addEdit(new CaretAwareUndoableEdit(e.getEdit(), textarea));
undoAction.updateUndoState();
redoAction.updateRedoState();
}
});
}
});
}
// update the document object that's in use

View File

@ -359,8 +359,7 @@ public class EditorHeader extends JComponent {
editor.getSketch().setCurrentCode(e.getActionCommand());
}
};
for (SketchCodeDocument codeDoc : sketch.getCodeDocs()) {
SketchCode code = codeDoc.getCode();
for (SketchCode code : sketch.getCodes()) {
item = new JMenuItem(code.isExtension(sketch.getDefaultExtension()) ?
code.getPrettyName() : code.getFileName());
item.setActionCommand(code.getFileName());

View File

@ -65,8 +65,7 @@ public class Sketch {
/** code folder location for this sketch (may not exist yet) */
private File codeFolder;
private SketchCode current;
private SketchCodeDocument currentCodeDoc;
private SketchCodeDocument current;
private int currentIndex;
private SketchData data;
@ -167,7 +166,7 @@ public class Sketch {
// Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116]
if (Sketch.isSanitaryName(base)) {
data.addCodeDoc(new SketchCodeDocument(new File(folder, filename)));
data.addCode(new SketchCodeDocument(new File(folder, filename)));
} else {
System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
}
@ -180,10 +179,10 @@ public class Sketch {
// move the main class to the first tab
// start at 1, if it's at zero, don't bother
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
for (SketchCode code : data.getCodes()) {
//if (code[i].file.getName().equals(mainFilename)) {
if (codeDoc.getCode().getFile().equals(primaryFile)) {
data.moveCodeDocToFront(codeDoc);
if (code.getFile().equals(primaryFile)) {
data.moveCodeToFront(code);
break;
}
}
@ -308,7 +307,7 @@ public class Sketch {
// Don't let the user create the main tab as a .java file instead of .pde
if (!isDefaultExtension(newExtension)) {
if (renamingCode) { // If creating a new tab, don't show this error
if (current == data.getCodeDoc(0).getCode()) { // If this is the main tab, disallow
if (current == data.getCode(0)) { // If this is the main tab, disallow
Base.showWarning(_("Problem with rename"),
_("The main file can't use an extension.\n" +
"(It may be time for your to graduate to a\n" +
@ -330,12 +329,12 @@ public class Sketch {
// In Arduino, we want to allow files with the same name but different
// extensions, so compare the full names (including extensions). This
// might cause problems: http://dev.processing.org/bugs/show_bug.cgi?id=543
for (SketchCodeDocument c : data.getCodeDocs()) {
if (newName.equalsIgnoreCase(c.getCode().getFileName())) {
for (SketchCode c : data.getCodes()) {
if (newName.equalsIgnoreCase(c.getFileName())) {
Base.showMessage(_("Nope"),
I18n.format(
_("A file named \"{0}\" already exists in \"{1}\""),
c.getCode().getFileName(),
c.getFileName(),
folder.getAbsolutePath()
));
return;
@ -352,15 +351,13 @@ public class Sketch {
}
if (renamingCode && currentIndex == 0) {
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (sanitaryName.equalsIgnoreCase(codeDoc.getCode().getPrettyName()) &&
codeDoc.getCode().isExtension("cpp")) {
for (SketchCode code : data.getCodes()) {
if (sanitaryName.equalsIgnoreCase(code.getPrettyName()) &&
code.isExtension("cpp")) {
Base.showMessage(_("Nope"),
I18n.format(
_("You can't rename the sketch to \"{0}\"\n" +
"because the sketch already has a .cpp file with that name."),
sanitaryName
));
I18n.format(_("You can't rename the sketch to \"{0}\"\n"
+ "because the sketch already has a .cpp file with that name."),
sanitaryName));
return;
}
}
@ -428,8 +425,8 @@ public class Sketch {
// save each of the other tabs because this is gonna be re-opened
try {
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
codeDoc.getCode().save();
for (SketchCode code : data.getCodes()) {
code.save();
}
} catch (Exception e) {
Base.showWarning(_("Error"), _("Could not rename the sketch. (1)"), e);
@ -486,10 +483,8 @@ public class Sketch {
), e);
return;
}
SketchCode newCode = new SketchCode(newFile);
//System.out.println("new code is named " + newCode.getPrettyName() + " " + newCode.getFile());
ensureExistence();
data.insertCode(newCode);
data.addCode(new SketchCodeDocument(newFile));
}
// sort the entries
@ -601,8 +596,8 @@ public class Sketch {
protected void calcModified() {
modified = false;
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (codeDoc.getCode().isModified()) {
for (SketchCode code : data.getCodes()) {
if (code.isModified()) {
modified = true;
break;
}
@ -685,9 +680,9 @@ public class Sketch {
}
}
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (codeDoc.getCode().isModified())
codeDoc.getCode().save();
for (SketchCode code : data.getCodes()) {
if (code.isModified())
code.save();
}
calcModified();
return true;
@ -695,13 +690,13 @@ public class Sketch {
protected boolean renameCodeToInoExtension(File pdeFile) {
for (SketchCodeDocument c : data.getCodeDocs()) {
if (!c.getCode().getFile().equals(pdeFile))
for (SketchCode c : data.getCodes()) {
if (!c.getFile().equals(pdeFile))
continue;
String pdeName = pdeFile.getPath();
pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino";
return c.getCode().renameTo(new File(pdeName));
return c.renameTo(new File(pdeName));
}
return false;
}
@ -746,9 +741,9 @@ public class Sketch {
// make sure there doesn't exist a .cpp file with that name already
// but ignore this situation for the first tab, since it's probably being
// resaved (with the same name) to another location/folder.
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (newName.equalsIgnoreCase(codeDoc.getCode().getPrettyName()) &&
codeDoc.getCode().isExtension("cpp")) {
for (SketchCode code : data.getCodes()) {
if (newName.equalsIgnoreCase(code.getPrettyName()) &&
code.isExtension("cpp")) {
Base.showMessage(_("Nope"),
I18n.format(
_("You can't save the sketch as \"{0}\"\n" +
@ -798,10 +793,10 @@ public class Sketch {
}
// save the other tabs to their new location
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (data.indexOfCodeDoc(codeDoc) == 0) continue;
File newFile = new File(newFolder, codeDoc.getCode().getFileName());
codeDoc.getCode().saveAs(newFile);
for (SketchCode code : data.getCodes()) {
if (data.indexOfCode(code) == 0) continue;
File newFile = new File(newFolder, code.getFileName());
code.saveAs(newFile);
}
// re-copy the data folder (this may take a while.. add progress bar?)
@ -826,7 +821,7 @@ public class Sketch {
// save the main tab with its new name
File newFile = new File(newFolder, newName + ".ino");
data.getCodeDoc(0).getCode().saveAs(newFile);
data.getCode(0).saveAs(newFile);
editor.handleOpenUnchecked(newFile,
currentIndex,
@ -989,14 +984,14 @@ public class Sketch {
}
if (codeExtension != null) {
SketchCode newCode = new SketchCode(destFile);
SketchCode newCode = new SketchCodeDocument(destFile);
if (replacement) {
data.replaceCode(newCode);
} else {
ensureExistence();
data.insertCode(newCode);
data.addCode(newCode);
data.sortCode();
}
setCurrentCode(filename);
@ -1010,7 +1005,7 @@ public class Sketch {
if (editor.untitled) { // TODO probably not necessary? problematic?
// If a file has been added, mark the main code as modified so
// that the sketch is properly saved.
data.getCodeDoc(0).getCode().setModified(true);
data.getCode(0).setModified(true);
}
}
return true;
@ -1071,16 +1066,15 @@ public class Sketch {
// get the text currently being edited
if (current != null) {
current.setProgram(editor.getText());
currentCodeDoc.setSelectionStart(editor.getSelectionStart());
currentCodeDoc.setSelectionStop(editor.getSelectionStop());
currentCodeDoc.setScrollPosition(editor.getScrollPosition());
current.setSelectionStart(editor.getSelectionStart());
current.setSelectionStop(editor.getSelectionStop());
current.setScrollPosition(editor.getScrollPosition());
}
currentCodeDoc = data.getCodeDoc(which);
current = currentCodeDoc.getCode();
current = (SketchCodeDocument) data.getCode(which);
currentIndex = which;
editor.setCode(currentCodeDoc);
editor.setCode(current);
editor.header.rebuild();
}
@ -1091,10 +1085,10 @@ public class Sketch {
* @param findName the file name (not pretty name) to be shown
*/
protected void setCurrentCode(String findName) {
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (findName.equals(codeDoc.getCode().getFileName()) ||
findName.equals(codeDoc.getCode().getPrettyName())) {
setCurrentCode(data.indexOfCodeDoc(codeDoc));
for (SketchCode code : data.getCodes()) {
if (findName.equals(code.getFileName()) ||
findName.equals(code.getPrettyName())) {
setCurrentCode(data.indexOfCode(code));
return;
}
}
@ -1527,8 +1521,8 @@ public class Sketch {
folder.mkdirs();
modified = true;
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
codeDoc.getCode().save(); // this will force a save
for (SketchCode code : data.getCodes()) {
code.save(); // this will force a save
}
calcModified();
@ -1562,9 +1556,8 @@ public class Sketch {
// } else if (!folder.canWrite()) {
// check to see if each modified code file can be written to
for (SketchCodeDocument codeDoc : data.getCodeDocs()) {
if (codeDoc.getCode().isModified() && codeDoc.getCode().fileReadOnly() &&
codeDoc.getCode().fileExists()) {
for (SketchCode code : data.getCodes()) {
if (code.isModified() && code.fileReadOnly() && code.fileExists()) {
// System.err.println("found a read-only file " + code[i].file);
return true;
}
@ -1702,8 +1695,8 @@ public class Sketch {
}
public SketchCodeDocument[] getCodeDocs() {
return data.getCodeDocs();
public SketchCode[] getCodes() {
return data.getCodes();
}
@ -1713,7 +1706,7 @@ public class Sketch {
public SketchCode getCode(int index) {
return data.getCodeDoc(index).getCode();
return data.getCode(index);
}

View File

@ -4,19 +4,24 @@ import java.io.File;
import javax.swing.text.Document;
public class SketchCodeDocument {
public class SketchCodeDocument extends SketchCode {
SketchCode code;
private Document document;
/**
* 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.
*/
// 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 LastUndoableEditAwareUndoManager undo = new LastUndoableEditAwareUndoManager();
// saved positions from last time this tab was used
private int selectionStart;
private int selectionStop;
private int scrollPosition;
public SketchCodeDocument(File file) {
super(file);
}
public LastUndoableEditAwareUndoManager getUndo() {
return undo;
}
@ -49,29 +54,6 @@ public class SketchCodeDocument {
this.scrollPosition = scrollPosition;
}
// saved positions from last time this tab was used
private int selectionStart;
private int selectionStop;
private int scrollPosition;
public SketchCodeDocument(SketchCode sketchCode, Document doc) {
code = sketchCode;
document = doc;
}
public SketchCodeDocument(File file) {
code = new SketchCode(file);
}
public SketchCode getCode() {
return code;
}
public void setCode(SketchCode code) {
this.code = code;
}
public Document getDocument() {
return document;
}

View File

@ -1,7 +1,5 @@
package processing.app;
import static processing.app.I18n._;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
@ -14,82 +12,68 @@ public class SketchData {
* extension)
*/
private String name;
private List<SketchCodeDocument> codeDocs = new ArrayList<SketchCodeDocument>();
private static final Comparator<SketchCodeDocument> CODE_DOCS_COMPARATOR = new Comparator<SketchCodeDocument>() {
private List<SketchCode> codes = new ArrayList<SketchCode>();
private static final Comparator<SketchCode> CODE_DOCS_COMPARATOR = new Comparator<SketchCode>() {
@Override
public int compare(SketchCodeDocument cd1, SketchCodeDocument cd2) {
return cd1.getCode().getFileName().compareTo(cd2.getCode().getFileName());
public int compare(SketchCode x, SketchCode y) {
return x.getFileName().compareTo(y.getFileName());
}
};
public int getCodeCount() {
return codeDocs.size();
return codes.size();
}
public SketchCodeDocument[] getCodeDocs() {
return codeDocs.toArray(new SketchCodeDocument[0]);
public SketchCode[] getCodes() {
return codes.toArray(new SketchCode[0]);
}
public void addCodeDoc(SketchCodeDocument sketchCodeDoc) {
codeDocs.add(sketchCodeDoc);
public void addCode(SketchCode sketchCode) {
codes.add(sketchCode);
}
public void moveCodeDocToFront(SketchCodeDocument codeDoc) {
codeDocs.remove(codeDoc);
codeDocs.add(0, codeDoc);
public void moveCodeToFront(SketchCode codeDoc) {
codes.remove(codeDoc);
codes.add(0, codeDoc);
}
protected void replaceCode(SketchCode newCode) {
for (SketchCodeDocument codeDoc : codeDocs) {
if (codeDoc.getCode().getFileName().equals(newCode.getFileName())) {
codeDoc.setCode(newCode);
for (SketchCode code : codes) {
if (code.getFileName().equals(newCode.getFileName())) {
codes.set(codes.indexOf(code), newCode);
return;
}
}
}
protected void insertCode(SketchCode sketchCode) {
addCodeDoc(new SketchCodeDocument(sketchCode, null));
}
protected void sortCode() {
if (codeDocs.size() < 2)
if (codes.size() < 2)
return;
SketchCodeDocument first = codeDocs.remove(0);
Collections.sort(codeDocs, CODE_DOCS_COMPARATOR);
codeDocs.add(0, first);
}
public SketchCodeDocument getCodeDoc(int i) {
return codeDocs.get(i);
SketchCode first = codes.remove(0);
Collections.sort(codes, CODE_DOCS_COMPARATOR);
codes.add(0, first);
}
public SketchCode getCode(int i) {
return codeDocs.get(i).getCode();
return codes.get(i);
}
protected void removeCode(SketchCode which) {
for (SketchCodeDocument codeDoc : codeDocs) {
if (codeDoc.getCode() == which) {
codeDocs.remove(codeDoc);
for (SketchCode code : codes) {
if (code == which) {
codes.remove(code);
return;
}
}
System.err.println(_("removeCode: internal error.. could not find code"));
}
public int indexOfCodeDoc(SketchCodeDocument codeDoc) {
return codeDocs.indexOf(codeDoc);
System.err.println("removeCode: internal error.. could not find code");
}
public int indexOfCode(SketchCode who) {
for (SketchCodeDocument codeDoc : codeDocs) {
if (codeDoc.getCode() == who) {
return codeDocs.indexOf(codeDoc);
}
for (SketchCode code : codes) {
if (code == who)
return codes.indexOf(code);
}
return -1;
}
@ -103,6 +87,6 @@ public class SketchData {
}
public void clearCodeDocs() {
codeDocs.clear();
codes.clear();
}
}

View File

@ -40,7 +40,6 @@ import processing.app.Base;
import processing.app.I18n;
import processing.app.Preferences;
import processing.app.SketchCode;
import processing.app.SketchCodeDocument;
import processing.app.SketchData;
import processing.app.helpers.FileUtils;
import processing.app.helpers.PreferencesMap;
@ -50,7 +49,6 @@ import processing.app.helpers.filefilters.OnlyDirs;
import processing.app.packages.Library;
import processing.app.packages.LibraryList;
import processing.app.preproc.PdePreprocessor;
import processing.core.PApplet;
public class Compiler implements MessageConsumer {
@ -901,8 +899,7 @@ public class Compiler implements MessageConsumer {
StringBuffer bigCode = new StringBuffer();
int bigCount = 0;
for (SketchCodeDocument scd : sketch.getCodeDocs()) {
SketchCode sc = scd.getCode();
for (SketchCode sc : sketch.getCodes()) {
if (sc.isExtension("ino") || sc.isExtension("pde")) {
sc.setPreprocOffset(bigCount);
// These #line directives help the compiler report errors with
@ -962,8 +959,7 @@ public class Compiler implements MessageConsumer {
// 3. then loop over the code[] and save each .java file
for (SketchCodeDocument scd : sketch.getCodeDocs()) {
SketchCode sc = scd.getCode();
for (SketchCode sc : sketch.getCodes()) {
if (sc.isExtension("c") || sc.isExtension("cpp") || sc.isExtension("h")) {
// no pre-processing services necessary for java files
// just write the the contents of 'program' to a .java file
@ -1005,8 +1001,7 @@ public class Compiler implements MessageConsumer {
// Placing errors is simple, because we inserted #line directives
// into the preprocessed source. The compiler gives us correct
// the file name and line number. :-)
for (SketchCodeDocument codeDoc : sketch.getCodeDocs()) {
SketchCode code = codeDoc.getCode();
for (SketchCode code : sketch.getCodes()) {
if (dotJavaFilename.equals(code.getFileName())) {
return new RunnerException(message, sketch.indexOfCode(code), dotJavaLine);
}