1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Removed GUI dependencies from SketchCode class.

Moved GUI fields into a SketchCodeDocument container class.
This commit is contained in:
Claudio Indellicati 2014-01-30 13:48:07 +01:00 committed by Cristian Maglie
parent af19257fbd
commit e6563cfebf
5 changed files with 155 additions and 138 deletions

View File

@ -1637,19 +1637,19 @@ public class Editor extends JFrame implements RunnerListener {
* Switch between tabs, this swaps out the Document object * Switch between tabs, this swaps out the Document object
* that's currently being manipulated. * that's currently being manipulated.
*/ */
protected void setCode(SketchCode code) { protected void setCode(SketchCodeDocument codeDoc) {
SyntaxDocument document = (SyntaxDocument) code.getDocument(); SyntaxDocument document = (SyntaxDocument) codeDoc.getDocument();
if (document == null) { // this document not yet inited if (document == null) { // this document not yet inited
document = new SyntaxDocument(); document = new SyntaxDocument();
code.setDocument(document); codeDoc.setDocument(document);
// turn on syntax highlighting // turn on syntax highlighting
document.setTokenMarker(new PdeKeywords()); document.setTokenMarker(new PdeKeywords());
// insert the program text into the document object // insert the program text into the document object
try { try {
document.insertString(0, code.getProgram(), null); document.insertString(0, codeDoc.getCode().getProgram(), null);
} catch (BadLocationException bl) { } catch (BadLocationException bl) {
bl.printStackTrace(); bl.printStackTrace();
} }
@ -1674,12 +1674,12 @@ public class Editor extends JFrame implements RunnerListener {
// update the document object that's in use // update the document object that's in use
textarea.setDocument(document, textarea.setDocument(document,
code.getSelectionStart(), code.getSelectionStop(), codeDoc.getSelectionStart(), codeDoc.getSelectionStop(),
code.getScrollPosition()); codeDoc.getScrollPosition());
textarea.requestFocus(); // get the caret blinking textarea.requestFocus(); // get the caret blinking
this.undo = code.getUndo(); this.undo = codeDoc.getUndo();
undoAction.updateUndoState(); undoAction.updateUndoState();
redoAction.updateRedoState(); redoAction.updateRedoState();
} }

View File

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

View File

@ -40,7 +40,6 @@ import static processing.app.I18n._;
import java.io.*; import java.io.*;
import java.util.*; import java.util.*;
import java.util.List;
import javax.swing.*; import javax.swing.*;
@ -75,7 +74,9 @@ public class Sketch {
private File codeFolder; private File codeFolder;
private SketchCode current; private SketchCode current;
private SketchCodeDocument currentCodeDoc;
private int currentIndex; private int currentIndex;
/** /**
* Number of sketchCode objects (tabs) in the current sketch. Note that this * Number of sketchCode objects (tabs) in the current sketch. Note that this
* will be the same as code.length, because the getCode() method returns * will be the same as code.length, because the getCode() method returns
@ -84,7 +85,7 @@ public class Sketch {
* http://dev.processing.org/bugs/show_bug.cgi?id=940 * http://dev.processing.org/bugs/show_bug.cgi?id=940
*/ */
private int codeCount; private int codeCount;
private SketchCode[] code; private SketchCodeDocument[] codeDocs;
/** Class name for the PApplet, as determined by the preprocessor. */ /** Class name for the PApplet, as determined by the preprocessor. */
private String appletClassName; private String appletClassName;
@ -168,7 +169,7 @@ public class Sketch {
// external editor event. (fix for 0099) // external editor event. (fix for 0099)
codeCount = 0; codeCount = 0;
code = new SketchCode[list.length]; codeDocs = new SketchCodeDocument[list.length];
List<String> extensions = getExtensions(); List<String> extensions = getExtensions();
@ -191,8 +192,8 @@ public class Sketch {
// Don't allow people to use files with invalid names, since on load, // Don't allow people to use files with invalid names, since on load,
// it would be otherwise possible to sneak in nasty filenames. [0116] // it would be otherwise possible to sneak in nasty filenames. [0116]
if (Sketch.isSanitaryName(base)) { if (Sketch.isSanitaryName(base)) {
code[codeCount++] = codeDocs[codeCount++] =
new SketchCode(new File(folder, filename)); new SketchCodeDocument(new File(folder, filename));
} else { } else {
System.err.println(I18n.format("File name {0} is invalid: ignored", filename)); System.err.println(I18n.format("File name {0} is invalid: ignored", filename));
} }
@ -204,16 +205,16 @@ public class Sketch {
throw new IOException(_("No valid code files found")); throw new IOException(_("No valid code files found"));
// Remove any code that wasn't proper // Remove any code that wasn't proper
code = (SketchCode[]) PApplet.subset(code, 0, codeCount); codeDocs = (SketchCodeDocument[]) PApplet.subset(codeDocs, 0, codeCount);
// move the main class to the first tab // move the main class to the first tab
// start at 1, if it's at zero, don't bother // start at 1, if it's at zero, don't bother
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
//if (code[i].file.getName().equals(mainFilename)) { //if (code[i].file.getName().equals(mainFilename)) {
if (code[i].getFile().equals(primaryFile)) { if (codeDocs[i].getCode().getFile().equals(primaryFile)) {
SketchCode temp = code[0]; SketchCodeDocument temp = codeDocs[0];
code[0] = code[i]; codeDocs[0] = codeDocs[i];
code[i] = temp; codeDocs[i] = temp;
break; break;
} }
} }
@ -230,8 +231,8 @@ public class Sketch {
protected void replaceCode(SketchCode newCode) { protected void replaceCode(SketchCode newCode) {
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].getFileName().equals(newCode.getFileName())) { if (codeDocs[i].getCode().getFileName().equals(newCode.getFileName())) {
code[i] = newCode; codeDocs[i].setCode(newCode);
break; break;
} }
} }
@ -244,7 +245,7 @@ public class Sketch {
// add file to the code/codeCount list, resort the list // add file to the code/codeCount list, resort the list
//if (codeCount == code.length) { //if (codeCount == code.length) {
code = (SketchCode[]) PApplet.append(code, newCode); codeDocs = (SketchCodeDocument[]) PApplet.append(codeDocs, newCode);
codeCount++; codeCount++;
//} //}
//code[codeCount++] = newCode; //code[codeCount++] = newCode;
@ -257,14 +258,14 @@ public class Sketch {
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
int who = i; int who = i;
for (int j = i + 1; j < codeCount; j++) { for (int j = i + 1; j < codeCount; j++) {
if (code[j].getFileName().compareTo(code[who].getFileName()) < 0) { if (codeDocs[j].getCode().getFileName().compareTo(codeDocs[who].getCode().getFileName()) < 0) {
who = j; // this guy is earlier in the alphabet who = j; // this guy is earlier in the alphabet
} }
} }
if (who != i) { // swap with someone if changes made if (who != i) { // swap with someone if changes made
SketchCode temp = code[who]; SketchCodeDocument temp = codeDocs[who];
code[who] = code[i]; codeDocs[who] = codeDocs[i];
code[i] = temp; codeDocs[i] = temp;
} }
} }
} }
@ -379,7 +380,7 @@ public class Sketch {
// Don't let the user create the main tab as a .java file instead of .pde // Don't let the user create the main tab as a .java file instead of .pde
if (!isDefaultExtension(newExtension)) { if (!isDefaultExtension(newExtension)) {
if (renamingCode) { // If creating a new tab, don't show this error if (renamingCode) { // If creating a new tab, don't show this error
if (current == code[0]) { // If this is the main tab, disallow if (current == codeDocs[0].getCode()) { // If this is the main tab, disallow
Base.showWarning(_("Problem with rename"), Base.showWarning(_("Problem with rename"),
_("The main file can't use an extension.\n" + _("The main file can't use an extension.\n" +
"(It may be time for your to graduate to a\n" + "(It may be time for your to graduate to a\n" +
@ -401,12 +402,12 @@ public class Sketch {
// In Arduino, we want to allow files with the same name but different // In Arduino, we want to allow files with the same name but different
// extensions, so compare the full names (including extensions). This // extensions, so compare the full names (including extensions). This
// might cause problems: http://dev.processing.org/bugs/show_bug.cgi?id=543 // might cause problems: http://dev.processing.org/bugs/show_bug.cgi?id=543
for (SketchCode c : code) { for (SketchCodeDocument c : codeDocs) {
if (newName.equalsIgnoreCase(c.getFileName())) { if (newName.equalsIgnoreCase(c.getCode().getFileName())) {
Base.showMessage(_("Nope"), Base.showMessage(_("Nope"),
I18n.format( I18n.format(
_("A file named \"{0}\" already exists in \"{1}\""), _("A file named \"{0}\" already exists in \"{1}\""),
c.getFileName(), c.getCode().getFileName(),
folder.getAbsolutePath() folder.getAbsolutePath()
)); ));
return; return;
@ -424,8 +425,8 @@ public class Sketch {
if (renamingCode && currentIndex == 0) { if (renamingCode && currentIndex == 0) {
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
if (sanitaryName.equalsIgnoreCase(code[i].getPrettyName()) && if (sanitaryName.equalsIgnoreCase(codeDocs[i].getCode().getPrettyName()) &&
code[i].isExtension("cpp")) { codeDocs[i].getCode().isExtension("cpp")) {
Base.showMessage(_("Nope"), Base.showMessage(_("Nope"),
I18n.format( I18n.format(
_("You can't rename the sketch to \"{0}\"\n" + _("You can't rename the sketch to \"{0}\"\n" +
@ -500,7 +501,7 @@ public class Sketch {
// save each of the other tabs because this is gonna be re-opened // save each of the other tabs because this is gonna be re-opened
try { try {
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
code[i].save(); codeDocs[i].getCode().save();
} }
} catch (Exception e) { } catch (Exception e) {
Base.showWarning(_("Error"), _("Could not rename the sketch. (1)"), e); Base.showWarning(_("Error"), _("Could not rename the sketch. (1)"), e);
@ -644,12 +645,12 @@ public class Sketch {
// remove it from the internal list of files // remove it from the internal list of files
// resort internal list of files // resort internal list of files
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i] == which) { if (codeDocs[i].getCode() == which) {
for (int j = i; j < codeCount-1; j++) { for (int j = i; j < codeCount-1; j++) {
code[j] = code[j+1]; codeDocs[j] = codeDocs[j+1];
} }
codeCount--; codeCount--;
code = (SketchCode[]) PApplet.shorten(code); codeDocs = (SketchCodeDocument[]) PApplet.shorten(codeDocs);
return; return;
} }
} }
@ -689,7 +690,7 @@ public class Sketch {
protected void calcModified() { protected void calcModified() {
modified = false; modified = false;
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].isModified()) { if (codeDocs[i].getCode().isModified()) {
modified = true; modified = true;
break; break;
} }
@ -773,8 +774,8 @@ public class Sketch {
} }
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].isModified()) if (codeDocs[i].getCode().isModified())
code[i].save(); codeDocs[i].getCode().save();
} }
calcModified(); calcModified();
return true; return true;
@ -782,13 +783,13 @@ public class Sketch {
protected boolean renameCodeToInoExtension(File pdeFile) { protected boolean renameCodeToInoExtension(File pdeFile) {
for (SketchCode c : code) { for (SketchCodeDocument c : codeDocs) {
if (!c.getFile().equals(pdeFile)) if (!c.getCode().getFile().equals(pdeFile))
continue; continue;
String pdeName = pdeFile.getPath(); String pdeName = pdeFile.getPath();
pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino"; pdeName = pdeName.substring(0, pdeName.length() - 4) + ".ino";
return c.renameTo(new File(pdeName)); return c.getCode().renameTo(new File(pdeName));
} }
return false; return false;
} }
@ -834,8 +835,8 @@ public class Sketch {
// but ignore this situation for the first tab, since it's probably being // but ignore this situation for the first tab, since it's probably being
// resaved (with the same name) to another location/folder. // resaved (with the same name) to another location/folder.
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
if (newName.equalsIgnoreCase(code[i].getPrettyName()) && if (newName.equalsIgnoreCase(codeDocs[i].getCode().getPrettyName()) &&
code[i].isExtension("cpp")) { codeDocs[i].getCode().isExtension("cpp")) {
Base.showMessage(_("Nope"), Base.showMessage(_("Nope"),
I18n.format( I18n.format(
_("You can't save the sketch as \"{0}\"\n" + _("You can't save the sketch as \"{0}\"\n" +
@ -886,8 +887,8 @@ public class Sketch {
// save the other tabs to their new location // save the other tabs to their new location
for (int i = 1; i < codeCount; i++) { for (int i = 1; i < codeCount; i++) {
File newFile = new File(newFolder, code[i].getFileName()); File newFile = new File(newFolder, codeDocs[i].getCode().getFileName());
code[i].saveAs(newFile); codeDocs[i].getCode().saveAs(newFile);
} }
// re-copy the data folder (this may take a while.. add progress bar?) // re-copy the data folder (this may take a while.. add progress bar?)
@ -912,7 +913,7 @@ public class Sketch {
// save the main tab with its new name // save the main tab with its new name
File newFile = new File(newFolder, newName + ".ino"); File newFile = new File(newFolder, newName + ".ino");
code[0].saveAs(newFile); codeDocs[0].getCode().saveAs(newFile);
editor.handleOpenUnchecked(newFile, editor.handleOpenUnchecked(newFile,
currentIndex, currentIndex,
@ -1095,7 +1096,7 @@ public class Sketch {
if (editor.untitled) { // TODO probably not necessary? problematic? if (editor.untitled) { // TODO probably not necessary? problematic?
// If a file has been added, mark the main code as modified so // If a file has been added, mark the main code as modified so
// that the sketch is properly saved. // that the sketch is properly saved.
code[0].setModified(true); codeDocs[0].getCode().setModified(true);
} }
} }
return true; return true;
@ -1155,16 +1156,18 @@ public class Sketch {
// get the text currently being edited // get the text currently being edited
if (current != null) { if (current != null) {
current.setState(editor.getText(), current.setProgram(editor.getText());
editor.getSelectionStart(), currentCodeDoc.setSelectionStart(editor.getSelectionStart());
editor.getSelectionStop(), currentCodeDoc.setSelectionStop(editor.getSelectionStop());
editor.getScrollPosition()); currentCodeDoc.setScrollPosition(editor.getScrollPosition());
} }
current = code[which]; currentCodeDoc = codeDocs[which];
current = currentCodeDoc.getCode();
currentIndex = which; currentIndex = which;
editor.setCode(current); editor.setCode(currentCodeDoc);
editor.header.rebuild(); editor.header.rebuild();
} }
@ -1175,8 +1178,8 @@ public class Sketch {
*/ */
protected void setCurrentCode(String findName) { protected void setCurrentCode(String findName) {
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (findName.equals(code[i].getFileName()) || if (findName.equals(codeDocs[i].getCode().getFileName()) ||
findName.equals(code[i].getPrettyName())) { findName.equals(codeDocs[i].getCode().getPrettyName())) {
setCurrentCode(i); setCurrentCode(i);
return; return;
} }
@ -1336,7 +1339,8 @@ public class Sketch {
StringBuffer bigCode = new StringBuffer(); StringBuffer bigCode = new StringBuffer();
int bigCount = 0; int bigCount = 0;
for (SketchCode sc : code) { for (SketchCodeDocument scd : codeDocs) {
SketchCode sc = scd.getCode();
if (sc.isExtension("ino") || sc.isExtension("pde")) { if (sc.isExtension("ino") || sc.isExtension("pde")) {
sc.setPreprocOffset(bigCount); sc.setPreprocOffset(bigCount);
// These #line directives help the compiler report errors with // These #line directives help the compiler report errors with
@ -1396,7 +1400,8 @@ public class Sketch {
// 3. then loop over the code[] and save each .java file // 3. then loop over the code[] and save each .java file
for (SketchCode sc : code) { for (SketchCodeDocument scd : codeDocs) {
SketchCode sc = scd.getCode();
if (sc.isExtension("c") || sc.isExtension("cpp") || sc.isExtension("h")) { if (sc.isExtension("c") || sc.isExtension("cpp") || sc.isExtension("h")) {
// no pre-processing services necessary for java files // no pre-processing services necessary for java files
// just write the the contents of 'program' to a .java file // just write the the contents of 'program' to a .java file
@ -1769,7 +1774,7 @@ public class Sketch {
modified = true; modified = true;
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
code[i].save(); // this will force a save codeDocs[i].getCode().save(); // this will force a save
} }
calcModified(); calcModified();
@ -1804,8 +1809,8 @@ public class Sketch {
// check to see if each modified code file can be written to // check to see if each modified code file can be written to
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (code[i].isModified() && code[i].fileReadOnly() && if (codeDocs[i].getCode().isModified() && codeDocs[i].getCode().fileReadOnly() &&
code[i].fileExists()) { codeDocs[i].getCode().fileExists()) {
// System.err.println("found a read-only file " + code[i].file); // System.err.println("found a read-only file " + code[i].file);
return true; return true;
} }
@ -1948,8 +1953,8 @@ public class Sketch {
} }
public SketchCode[] getCode() { public SketchCodeDocument[] getCodeDocs() {
return code; return codeDocs;
} }
@ -1959,13 +1964,13 @@ public class Sketch {
public SketchCode getCode(int index) { public SketchCode getCode(int index) {
return code[index]; return codeDocs[index].getCode();
} }
public int getCodeIndex(SketchCode who) { public int getCodeIndex(SketchCode who) {
for (int i = 0; i < codeCount; i++) { for (int i = 0; i < codeCount; i++) {
if (who == code[i]) { if (who == codeDocs[i].getCode()) {
return i; return i;
} }
} }

View File

@ -28,8 +28,6 @@ import java.io.*;
import java.util.List; import java.util.List;
import java.util.Arrays; import java.util.Arrays;
import javax.swing.text.Document;
import static processing.app.I18n._; import static processing.app.I18n._;
import processing.app.helpers.FileUtils; import processing.app.helpers.FileUtils;
@ -47,21 +45,6 @@ public class SketchCode {
/** Text of the program text for this tab */ /** Text of the program text for this tab */
private String program; private String program;
/** Document object for this tab. Currently this is a SyntaxDocument. */
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.
*/
private LastUndoableEditAwareUndoManager undo = new LastUndoableEditAwareUndoManager();
// saved positions from last time this tab was used
private int selectionStart;
private int selectionStop;
private int scrollPosition;
private boolean modified; private boolean modified;
/** name of .java file after preproc */ /** name of .java file after preproc */
@ -183,16 +166,6 @@ public class SketchCode {
} }
// public void setPreprocName(String preprocName) {
// this.preprocName = preprocName;
// }
//
//
// public String getPreprocName() {
// return preprocName;
// }
public void setPreprocOffset(int preprocOffset) { public void setPreprocOffset(int preprocOffset) {
this.preprocOffset = preprocOffset; this.preprocOffset = preprocOffset;
} }
@ -208,51 +181,6 @@ public class SketchCode {
} }
public Document getDocument() {
return document;
}
public void setDocument(Document d) {
document = d;
}
public LastUndoableEditAwareUndoManager 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;
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

View File

@ -0,0 +1,83 @@
package processing.app;
import java.io.File;
import javax.swing.text.Document;
public class SketchCodeDocument {
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.
*/
private LastUndoableEditAwareUndoManager undo = new LastUndoableEditAwareUndoManager();
public LastUndoableEditAwareUndoManager getUndo() {
return undo;
}
public void setUndo(LastUndoableEditAwareUndoManager undo) {
this.undo = undo;
}
public int getSelectionStart() {
return selectionStart;
}
public void setSelectionStart(int selectionStart) {
this.selectionStart = selectionStart;
}
public int getSelectionStop() {
return selectionStop;
}
public void setSelectionStop(int selectionStop) {
this.selectionStop = selectionStop;
}
public int getScrollPosition() {
return scrollPosition;
}
public void setScrollPosition(int scrollPosition) {
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;
}
public void setDocument(Document document) {
this.document = document;
}
}