-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEdits.java
More file actions
155 lines (130 loc) · 4.58 KB
/
Edits.java
File metadata and controls
155 lines (130 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package piecetable;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Nick
Implementation of Linear Undo Model
1.)user makes editStack and PieceTable buffer has n pieces of variable size
|--|---|--|-|------|---|
2.)user wants to undo this series of editStack in a linear fashion
so for lets imagine the buffer shown in (1) are only pieces added directly
after one another.
|--|---|--|
3.) user wants to redo after a series of undo actions. Clearly we can see that we must store each undo.
|--|---|--|-|
^ ^ ^
|------|---|
*
*
*/
public class Edits implements Serializable{
private ArrayList<Edit> editStack = new ArrayList<>();
private ArrayList<Edit> redoStack = new ArrayList<>();
public Edit currentEdit;
public Edit currentRedo;
public int editIndex = -1;
public int redoIndex = -1;
public int removeIndex;
public void pushEdit(Edit newEdit){
editStack.add(newEdit);
editIndex+=1;
}
private void push(Edit edit){
editStack.add(edit);
editIndex+=1;
}
private void pop(){
editStack.remove(editIndex);
editIndex-=1;
}
private void handleRemove(Edit edit,RandomAccessFile buffer,RandomAccessFile removeStack){
byte[] removeSequence = new byte[edit.length];
try {
removeStack.seek(0);
removeStack.read(removeSequence);
buffer.seek(buffer.length());
buffer.write(removeSequence);
} catch (IOException ex) {
Logger.getLogger(Edits.class.getName()).log(Level.SEVERE, null, ex);
}
}
public PieceTable undo(PieceTable sequence, RandomAccessFile buffer,RandomAccessFile originalBuffer,RandomAccessFile removeStack){
if(editIndex>=0){
currentEdit = editStack.get(editIndex);
switch(currentEdit.editType)
{
case "addition":
sequence.remove(currentEdit.offset,currentEdit.length);
break;
case "remove":
handleRemove(currentEdit,buffer,removeStack);
sequence.add(currentEdit.length,currentEdit.offset,buffer);
break;
}
redoStack.add(currentEdit);
redoIndex+=1;
pop();
}else{
System.out.println("At initial state");
}
return sequence;
}
public PieceTable redo(PieceTable sequence, RandomAccessFile buffer, RandomAccessFile originalBuffer){
if(redoIndex>=0){
currentRedo = redoStack.get(redoIndex);
switch(currentRedo.editType){
case "addition":
if (currentRedo.in_added){
try {
byte[] bytes = new byte[(int) originalBuffer.length()];
originalBuffer.seek(0);
originalBuffer.read(bytes);
buffer.seek(buffer.length());
buffer.write(bytes);
}
catch(IOException ignored){ }
}
sequence.add(currentRedo.length,currentRedo.offset,buffer);
break;
case "remove":
sequence.remove(currentRedo.offset, currentRedo.length);
break;
}
push(currentRedo);
redoStack.remove(redoIndex);
redoIndex-=1;
}else{
System.out.println("At latest state");
}
return sequence;
}
public void printEdits(){
System.out.println("Edit Stack");
editStack.forEach((edit) -> {
System.out.println("editType: "+edit.editType+", editOffset: "+edit.offset+", editLength: "+edit.length);
});
}
public void printRedo(){
System.out.println("Redo Stack");
redoStack.forEach((edit) -> {
System.out.println("editType: "+edit.editType+", editOffset: "+edit.offset+", editLength: "+edit.length);
});
}
public void emptyRedoStack(){
redoStack=new ArrayList<>();
redoIndex = -1;
}
void emptyUndoStack() {
editStack = new ArrayList<>();
editIndex = -1;
}
void emptyEdits() {
emptyUndoStack();
emptyRedoStack();
}
}