forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxUndoableEdit.java
More file actions
184 lines (155 loc) · 2.84 KB
/
mxUndoableEdit.java
File metadata and controls
184 lines (155 loc) · 2.84 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright (c) 2007-2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.util;
import java.util.ArrayList;
import java.util.List;
/**
* Implements a 2-dimensional rectangle with double precision coordinates.
*/
public class mxUndoableEdit
{
/**
* Defines the requirements for an undoable change.
*/
public interface mxUndoableChange
{
/**
* Undoes or redoes the change depending on its undo state.
*/
void execute();
}
/**
* Holds the source of the undoable edit.
*/
protected Object source;
/**
* Holds the list of changes that make up this undoable edit.
*/
protected List<mxUndoableChange> changes = new ArrayList<mxUndoableChange>();
/**
* Specifies this undoable edit is significant. Default is true.
*/
protected boolean significant = true;
/**
* Specifies the state of the undoable edit.
*/
protected boolean undone, redone;
/**
* Constructs a new undoable edit for the given source.
*/
public mxUndoableEdit(Object source)
{
this(source, true);
}
/**
* Constructs a new undoable edit for the given source.
*/
public mxUndoableEdit(Object source, boolean significant)
{
this.source = source;
this.significant = significant;
}
/**
* Hook to notify any listeners of the changes after an undo or redo
* has been carried out. This implementation is empty.
*/
public void dispatch()
{
// empty
}
/**
* Hook to free resources after the edit has been removed from the command
* history. This implementation is empty.
*/
public void die()
{
// empty
}
/**
* @return the source
*/
public Object getSource()
{
return source;
}
/**
* @return the changes
*/
public List<mxUndoableChange> getChanges()
{
return changes;
}
/**
* @return the significant
*/
public boolean isSignificant()
{
return significant;
}
/**
* @return the undone
*/
public boolean isUndone()
{
return undone;
}
/**
* @return the redone
*/
public boolean isRedone()
{
return redone;
}
/**
* Returns true if the this edit contains no changes.
*/
public boolean isEmpty()
{
return changes.isEmpty();
}
/**
* Adds the specified change to this edit. The change is an object that is
* expected to either have an undo and redo, or an execute function.
*/
public void add(mxUndoableChange change)
{
changes.add(change);
}
/**
*
*/
public void undo()
{
if (!undone)
{
int count = changes.size();
for (int i = count - 1; i >= 0; i--)
{
mxUndoableChange change = changes.get(i);
change.execute();
}
undone = true;
redone = false;
}
dispatch();
}
/**
*
*/
public void redo()
{
if (!redone)
{
int count = changes.size();
for (int i = 0; i < count; i++)
{
mxUndoableChange change = changes.get(i);
change.execute();
}
undone = false;
redone = true;
}
dispatch();
}
}