Skip to content

Commit 9d65e01

Browse files
committed
rewrite undo manager handling of modifications
1 parent 65a4411 commit 9d65e01

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

app/src/processing/app/Editor.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public abstract class Editor extends JFrame implements RunnerListener {
8989
private JMenuItem undoItem, redoItem;
9090
protected UndoAction undoAction;
9191
protected RedoAction redoAction;
92+
/** the currently selected tab's undo manager */
9293
private UndoManager undo;
9394
// used internally, and only briefly
9495
private CompoundEdit compoundEdit;
@@ -1068,6 +1069,9 @@ public void actionPerformed(ActionEvent e) {
10681069
}
10691070
updateUndoState();
10701071
redoAction.updateRedoState();
1072+
if (sketch != null) {
1073+
sketch.setModified(!getText().equals(sketch.getCurrentCode().getSavedProgram()));
1074+
}
10711075
}
10721076

10731077
protected void updateUndoState() {
@@ -1076,17 +1080,17 @@ protected void updateUndoState() {
10761080
undoItem.setEnabled(true);
10771081
undoItem.setText(undo.getUndoPresentationName());
10781082
putValue(Action.NAME, undo.getUndoPresentationName());
1079-
if (sketch != null) {
1080-
sketch.setModified(true); // 0107
1081-
}
1083+
// if (sketch != null) {
1084+
// sketch.setModified(true); // 0107, removed for 0196
1085+
// }
10821086
} else {
10831087
this.setEnabled(false);
10841088
undoItem.setEnabled(false);
10851089
undoItem.setText("Undo");
10861090
putValue(Action.NAME, "Undo");
1087-
if (sketch != null) {
1088-
sketch.setModified(false); // 0107
1089-
}
1091+
// if (sketch != null) {
1092+
// sketch.setModified(false); // 0107
1093+
// }
10901094
}
10911095
}
10921096
}
@@ -1113,6 +1117,9 @@ public void actionPerformed(ActionEvent e) {
11131117
}
11141118
updateRedoState();
11151119
undoAction.updateUndoState();
1120+
if (sketch != null) {
1121+
sketch.setModified(!getText().equals(sketch.getCurrentCode().getSavedProgram()));
1122+
}
11161123
}
11171124

11181125
protected void updateRedoState() {

app/src/processing/app/Sketch.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,11 @@ public void handleNextCode() {
626626
*/
627627
public void setModified(boolean state) {
628628
//System.out.println("setting modified to " + state);
629-
//new Exception().printStackTrace();
630-
current.setModified(state);
631-
calcModified();
629+
//new Exception().printStackTrace(System.out);
630+
if (current.isModified() != state) {
631+
current.setModified(state);
632+
calcModified();
633+
}
632634
}
633635

634636

app/src/processing/app/SketchCode.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ public class SketchCode {
4545

4646
/** Text of the program text for this tab */
4747
private String program;
48+
49+
/** Last version of the program on disk. */
50+
private String savedProgram;
4851

4952
/** Document object for this tab. Currently this is a SyntaxDocument. */
5053
private Document document;
@@ -58,6 +61,9 @@ public class SketchCode {
5861
* that's currently the front.
5962
*/
6063
private UndoManager undo = new UndoManager();
64+
65+
/** What was on top of the undo stack when last saved. */
66+
// private UndoableEdit lastEdit;
6167

6268
// saved positions from last time this tab was used
6369
private int selectionStart;
@@ -150,16 +156,24 @@ public boolean isExtension(String what) {
150156
return extension.equals(what);
151157
}
152158

153-
159+
160+
/** get the current text for this tab */
154161
public String getProgram() {
155162
return program;
156163
}
157-
158-
164+
165+
166+
/** set the current text for this tab */
159167
public void setProgram(String replacement) {
160168
program = replacement;
161169
}
162-
170+
171+
172+
/** get the last version saved of this tab */
173+
public String getSavedProgram() {
174+
return savedProgram;
175+
}
176+
163177

164178
public int getLineCount() {
165179
return Base.countLines(program);
@@ -259,6 +273,7 @@ public long lastVisited() {
259273
*/
260274
public void load() throws IOException {
261275
program = Base.loadFile(file);
276+
savedProgram = program;
262277

263278
if (program.indexOf('\uFFFD') != -1) {
264279
System.err.println(file.getName() + " contains unrecognized characters.");
@@ -282,6 +297,7 @@ public void save() throws IOException {
282297
//history.record(s, SketchHistory.SAVE);
283298

284299
Base.saveFile(program, file);
300+
savedProgram = program;
285301
setModified(false);
286302
}
287303

@@ -291,12 +307,13 @@ public void save() throws IOException {
291307
*/
292308
public void saveAs(File newFile) throws IOException {
293309
Base.saveFile(program, newFile);
310+
savedProgram = program;
294311
file = newFile;
295312
makePrettyName();
296313
setModified(false);
297314
}
298315

299-
316+
300317
/**
301318
* Called when the sketch folder name/location has changed. Called when
302319
* renaming tab 0, the main code.

app/src/processing/mode/java/PdeKeyListener.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ public boolean keyPressed(KeyEvent event) {
128128
}
129129

130130
// TODO i don't like these accessors. clean em up later.
131-
if (!editor.getSketch().isModified()) {
132-
if ((code == KeyEvent.VK_BACK_SPACE) || (code == KeyEvent.VK_TAB) ||
133-
(code == KeyEvent.VK_ENTER) || ((c >= 32) && (c < 128))) {
134-
sketch.setModified(true);
135-
}
131+
// if (!editor.getSketch().isModified()) {
132+
if ((code == KeyEvent.VK_BACK_SPACE) || (code == KeyEvent.VK_TAB) ||
133+
(code == KeyEvent.VK_ENTER) || ((c >= 32) && (c < 128))) {
134+
sketch.setModified(true);
136135
}
136+
// }
137137

138138
if ((code == KeyEvent.VK_UP) &&
139139
((event.getModifiers() & KeyEvent.CTRL_MASK) != 0)) {

0 commit comments

Comments
 (0)