Removed inheritance relationship between SketchCodeDocument and SketchCode.

This commit is contained in:
Claudio Indellicati 2014-09-18 15:01:38 +02:00 committed by Cristian Maglie
parent c2223107b1
commit b2a88ecdd2
4 changed files with 70 additions and 31 deletions

View File

@ -1652,7 +1652,7 @@ public class Editor extends JFrame implements RunnerListener {
// insert the program text into the document object // insert the program text into the document object
try { try {
document.insertString(0, codeDoc.getProgram(), null); document.insertString(0, codeDoc.getCode().getProgram(), null);
} catch (BadLocationException bl) { } catch (BadLocationException bl) {
bl.printStackTrace(); bl.printStackTrace();
} }

View File

@ -105,6 +105,11 @@ public class Sketch {
protected void load() throws IOException { protected void load() throws IOException {
data.load(); data.load();
for (SketchCode code : data.getCodes()) {
if (code.getMetadata() == null)
code.setMetadata(new SketchCodeDocument(code));
}
// set the main file to be the current tab // set the main file to be the current tab
if (editor != null) { if (editor != null) {
setCurrentCode(0); setCurrentCode(0);
@ -165,8 +170,8 @@ public class Sketch {
renamingCode = true; renamingCode = true;
String prompt = (currentIndex == 0) ? String prompt = (currentIndex == 0) ?
"New name for sketch:" : "New name for file:"; "New name for sketch:" : "New name for file:";
String oldName = (current.isExtension("ino")) ? String oldName = (current.getCode().isExtension("ino")) ?
current.getPrettyName() : current.getFileName(); current.getCode().getPrettyName() : current.getCode().getFileName();
editor.status.edit(prompt, oldName); editor.status.edit(prompt, oldName);
} }
@ -193,7 +198,7 @@ public class Sketch {
// (osx is case insensitive but preserving, windows insensitive, // (osx is case insensitive but preserving, windows insensitive,
// *nix is sensitive and preserving.. argh) // *nix is sensitive and preserving.. argh)
if (renamingCode) { if (renamingCode) {
if (newName.equalsIgnoreCase(current.getFileName())) { if (newName.equalsIgnoreCase(current.getCode().getFileName())) {
// exit quietly for the 'rename' case. // exit quietly for the 'rename' case.
// if it's a 'new' then an error will occur down below // if it's a 'new' then an error will occur down below
return; return;
@ -222,7 +227,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 == data.getCode(0)) { // If this is the main tab, disallow if (current.getCode() == data.getCode(0)) { // 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" +
@ -317,22 +322,22 @@ public class Sketch {
// however this *will* first save the sketch, then rename // however this *will* first save the sketch, then rename
// first get the contents of the editor text area // first get the contents of the editor text area
if (current.isModified()) { if (current.getCode().isModified()) {
current.setProgram(editor.getText()); current.getCode().setProgram(editor.getText());
try { try {
// save this new SketchCode // save this new SketchCode
current.save(); current.getCode().save();
} catch (Exception e) { } catch (Exception e) {
Base.showWarning(_("Error"), _("Could not rename the sketch. (0)"), e); Base.showWarning(_("Error"), _("Could not rename the sketch. (0)"), e);
return; return;
} }
} }
if (!current.renameTo(newFile)) { if (!current.getCode().renameTo(newFile)) {
Base.showWarning(_("Error"), Base.showWarning(_("Error"),
I18n.format( I18n.format(
_("Could not rename \"{0}\" to \"{1}\""), _("Could not rename \"{0}\" to \"{1}\""),
current.getFileName(), current.getCode().getFileName(),
newFile.getName() newFile.getName()
), null); ), null);
return; return;
@ -372,11 +377,11 @@ public class Sketch {
editor.base.rebuildSketchbookMenus(); editor.base.rebuildSketchbookMenus();
} else { // else if something besides code[0] } else { // else if something besides code[0]
if (!current.renameTo(newFile)) { if (!current.getCode().renameTo(newFile)) {
Base.showWarning(_("Error"), Base.showWarning(_("Error"),
I18n.format( I18n.format(
_("Could not rename \"{0}\" to \"{1}\""), _("Could not rename \"{0}\" to \"{1}\""),
current.getFileName(), current.getCode().getFileName(),
newFile.getName() newFile.getName()
), null); ), null);
return; return;
@ -399,7 +404,7 @@ public class Sketch {
return; return;
} }
ensureExistence(); ensureExistence();
data.addCode(new SketchCodeDocument(newFile)); data.addCode((new SketchCodeDocument(newFile)).getCode());
} }
// sort the entries // sort the entries
@ -434,7 +439,7 @@ public class Sketch {
Object[] options = { _("OK"), _("Cancel") }; Object[] options = { _("OK"), _("Cancel") };
String prompt = (currentIndex == 0) ? String prompt = (currentIndex == 0) ?
_("Are you sure you want to delete this sketch?") : _("Are you sure you want to delete this sketch?") :
I18n.format(_("Are you sure you want to delete \"{0}\"?"), current.getPrettyName()); I18n.format(_("Are you sure you want to delete \"{0}\"?"), current.getCode().getPrettyName());
int result = JOptionPane.showOptionDialog(editor, int result = JOptionPane.showOptionDialog(editor,
prompt, prompt,
_("Delete"), _("Delete"),
@ -461,14 +466,14 @@ public class Sketch {
} else { } else {
// delete the file // delete the file
if (!current.deleteFile(tempBuildFolder)) { if (!current.getCode().deleteFile(tempBuildFolder)) {
Base.showMessage(_("Couldn't do it"), Base.showMessage(_("Couldn't do it"),
I18n.format(_("Could not delete \"{0}\"."), current.getFileName())); I18n.format(_("Could not delete \"{0}\"."), current.getCode().getFileName()));
return; return;
} }
// remove code from the list // remove code from the list
data.removeCode(current); data.removeCode(current.getCode());
// just set current tab to the main tab // just set current tab to the main tab
setCurrentCode(0); setCurrentCode(0);
@ -504,7 +509,7 @@ public class Sketch {
public void setModified(boolean state) { public void setModified(boolean state) {
//System.out.println("setting modified to " + state); //System.out.println("setting modified to " + state);
//new Exception().printStackTrace(); //new Exception().printStackTrace();
current.setModified(state); current.getCode().setModified(state);
calcModified(); calcModified();
} }
@ -540,8 +545,8 @@ public class Sketch {
ensureExistence(); ensureExistence();
// first get the contents of the editor text area // first get the contents of the editor text area
if (current.isModified()) { if (current.getCode().isModified()) {
current.setProgram(editor.getText()); current.getCode().setProgram(editor.getText());
} }
// don't do anything if not actually modified // don't do anything if not actually modified
@ -700,8 +705,8 @@ public class Sketch {
// grab the contents of the current tab before saving // grab the contents of the current tab before saving
// first get the contents of the editor text area // first get the contents of the editor text area
if (current.isModified()) { if (current.getCode().isModified()) {
current.setProgram(editor.getText()); current.getCode().setProgram(editor.getText());
} }
// save the other tabs to their new location // save the other tabs to their new location
@ -896,7 +901,7 @@ public class Sketch {
} }
if (codeExtension != null) { if (codeExtension != null) {
SketchCode newCode = new SketchCodeDocument(destFile); SketchCode newCode = (new SketchCodeDocument(destFile)).getCode();
if (replacement) { if (replacement) {
data.replaceCode(newCode); data.replaceCode(newCode);
@ -910,7 +915,7 @@ public class Sketch {
editor.header.repaint(); editor.header.repaint();
if (editor.untitled) { // TODO probably not necessary? problematic? if (editor.untitled) { // TODO probably not necessary? problematic?
// Mark the new code as modified so that the sketch is saved // Mark the new code as modified so that the sketch is saved
current.setModified(true); current.getCode().setModified(true);
} }
} else { } else {
@ -941,7 +946,7 @@ public class Sketch {
// import statements into the main sketch file (code[0]) // import statements into the main sketch file (code[0])
// if the current code is a .java file, insert into current // if the current code is a .java file, insert into current
//if (current.flavor == PDE) { //if (current.flavor == PDE) {
if (hasDefaultExtension(current)) { if (hasDefaultExtension(current.getCode())) {
setCurrentCode(0); setCurrentCode(0);
} }
// could also scan the text in the file to see if each import // could also scan the text in the file to see if each import
@ -977,13 +982,13 @@ public class Sketch {
// get the text currently being edited // get the text currently being edited
if (current != null) { if (current != null) {
current.setProgram(editor.getText()); current.getCode().setProgram(editor.getText());
current.setSelectionStart(editor.getSelectionStart()); current.setSelectionStart(editor.getSelectionStart());
current.setSelectionStop(editor.getSelectionStop()); current.setSelectionStop(editor.getSelectionStop());
current.setScrollPosition(editor.getScrollPosition()); current.setScrollPosition(editor.getScrollPosition());
} }
current = (SketchCodeDocument) data.getCode(which); current = (SketchCodeDocument) data.getCode(which).getMetadata();
currentIndex = which; currentIndex = which;
editor.setCode(current); editor.setCode(current);
@ -1043,7 +1048,7 @@ public class Sketch {
// make sure the user didn't hide the sketch folder // make sure the user didn't hide the sketch folder
ensureExistence(); ensureExistence();
current.setProgram(editor.getText()); current.getCode().setProgram(editor.getText());
// TODO record history here // TODO record history here
//current.history.record(program, SketchHistory.RUN); //current.history.record(program, SketchHistory.RUN);
@ -1430,7 +1435,7 @@ public class Sketch {
public SketchCode getCurrentCode() { public SketchCode getCurrentCode() {
return current; return current.getCode();
} }

View File

@ -48,9 +48,19 @@ public class SketchCode {
/** where this code starts relative to the concat'd code */ /** where this code starts relative to the concat'd code */
private int preprocOffset; private int preprocOffset;
private Object metadata;
public SketchCode(File file) { public SketchCode(File file) {
init(file, null);
}
public SketchCode(File file, Object metadata) {
init(file, metadata);
}
private void init(File file, Object metadata) {
this.file = file; this.file = file;
this.metadata = metadata;
makePrettyName(); makePrettyName();
@ -223,4 +233,14 @@ public class SketchCode {
public void saveAs(File newFile) throws IOException { public void saveAs(File newFile) throws IOException {
BaseNoGui.saveFile(program, newFile); BaseNoGui.saveFile(program, newFile);
} }
public Object getMetadata() {
return metadata;
}
public void setMetadata(Object metadata) {
this.metadata = metadata;
}
} }

View File

@ -4,8 +4,9 @@ import java.io.File;
import javax.swing.text.Document; import javax.swing.text.Document;
public class SketchCodeDocument extends SketchCode { public class SketchCodeDocument{
private SketchCode code;
private Document document; private Document document;
// Undo Manager for this tab, each tab keeps track of their own Editor.undo // Undo Manager for this tab, each tab keeps track of their own Editor.undo
@ -18,8 +19,13 @@ public class SketchCodeDocument extends SketchCode {
private int selectionStop; private int selectionStop;
private int scrollPosition; private int scrollPosition;
public SketchCodeDocument(SketchCode code) {
this.code = code;
this.code.setMetadata(this);
}
public SketchCodeDocument(File file) { public SketchCodeDocument(File file) {
super(file); this.code = new SketchCode(file, this);
} }
public LastUndoableEditAwareUndoManager getUndo() { public LastUndoableEditAwareUndoManager getUndo() {
@ -54,6 +60,14 @@ public class SketchCodeDocument extends SketchCode {
this.scrollPosition = scrollPosition; this.scrollPosition = scrollPosition;
} }
public SketchCode getCode() {
return code;
}
public void setCode(SketchCode code) {
this.code = code;
}
public Document getDocument() { public Document getDocument() {
return document; return document;
} }