[BAEL-3090] Added example
This commit is contained in:
parent
0eb5179a6d
commit
dd7b922f2b
@ -0,0 +1,12 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class Coordinates {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Coordinates(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextEditor {
|
||||
|
||||
private TextWindow textWindow;
|
||||
private TextWindowState savedTextWindow;
|
||||
|
||||
public TextEditor(TextWindow textWindow) {
|
||||
this.textWindow = textWindow;
|
||||
}
|
||||
|
||||
public void hitSave() {
|
||||
savedTextWindow = textWindow.save();
|
||||
}
|
||||
|
||||
public void hitUndo() {
|
||||
textWindow.restore(savedTextWindow);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextWindow {
|
||||
|
||||
private StringBuilder currentText;
|
||||
private Coordinates cursorPosition;
|
||||
|
||||
public TextWindow() {
|
||||
this.currentText = new StringBuilder();
|
||||
this.cursorPosition = new Coordinates(0, 0);
|
||||
}
|
||||
|
||||
public void addText(String text) {
|
||||
currentText.append(text);
|
||||
}
|
||||
|
||||
public TextWindowState save() {
|
||||
return new TextWindowState(currentText.toString());
|
||||
}
|
||||
|
||||
public void restore(TextWindowState save) {
|
||||
currentText = new StringBuilder(save.getText());
|
||||
cursorPosition = atTextEnd();
|
||||
}
|
||||
|
||||
private Coordinates atTextEnd() {
|
||||
String[] lines = currentText.toString().split("\n");
|
||||
return new Coordinates(lines[lines.length - 1].length(), lines.length);
|
||||
}
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.memento;
|
||||
|
||||
public class TextWindowState {
|
||||
|
||||
private String text;
|
||||
|
||||
public TextWindowState(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user