forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxUndoManager.java
More file actions
163 lines (142 loc) · 3.07 KB
/
mxUndoManager.java
File metadata and controls
163 lines (142 loc) · 3.07 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
/**
* Copyright (c) 2007-2010, Gaudenz Alder, David Benson
*/
package com.mxgraph.util;
import java.util.ArrayList;
import java.util.List;
/**
* Implements an undo history.
*
* This class fires the following events:
*
* mxEvent.CLEAR fires after clear was executed. The event has no properties.
*
* mxEvent.UNDO fires afer a significant edit was undone in undo. The
* <code>edit</code> property contains the mxUndoableEdit that was undone.
*
* mxEvent.REDO fires afer a significant edit was redone in redo. The
* <code>edit</code> property contains the mxUndoableEdit that was redone.
*
* mxEvent.ADD fires after an undoable edit was added to the history. The
* <code>edit</code> property contains the mxUndoableEdit that was added.
*/
public class mxUndoManager extends mxEventSource
{
/**
* Maximum command history size. 0 means unlimited history. Default is 100.
*/
protected int size;
/**
* List that contains the steps of the command history.
*/
protected List<mxUndoableEdit> history;
/**
* Index of the element to be added next.
*/
protected int indexOfNextAdd;
/**
* Constructs a new undo manager with a default history size.
*/
public mxUndoManager()
{
this(100);
}
/**
* Constructs a new undo manager for the specified size.
*/
public mxUndoManager(int size)
{
this.size = size;
clear();
}
/**
*
*/
public boolean isEmpty()
{
return history.isEmpty();
}
/**
* Clears the command history.
*/
public void clear()
{
history = new ArrayList<mxUndoableEdit>(size);
indexOfNextAdd = 0;
fireEvent(new mxEventObject(mxEvent.CLEAR));
}
/**
* Returns true if an undo is possible.
*/
public boolean canUndo()
{
return indexOfNextAdd > 0;
}
/**
* Undoes the last change.
*/
public void undo()
{
while (indexOfNextAdd > 0)
{
mxUndoableEdit edit = history.get(--indexOfNextAdd);
edit.undo();
if (edit.isSignificant())
{
fireEvent(new mxEventObject(mxEvent.UNDO, "edit", edit));
break;
}
}
}
/**
* Returns true if a redo is possible.
*/
public boolean canRedo()
{
return indexOfNextAdd < history.size();
}
/**
* Redoes the last change.
*/
public void redo()
{
int n = history.size();
while (indexOfNextAdd < n)
{
mxUndoableEdit edit = history.get(indexOfNextAdd++);
edit.redo();
if (edit.isSignificant())
{
fireEvent(new mxEventObject(mxEvent.REDO, "edit", edit));
break;
}
}
}
/**
* Method to be called to add new undoable edits to the history.
*/
public void undoableEditHappened(mxUndoableEdit undoableEdit)
{
trim();
if (size > 0 && size == history.size())
{
history.remove(0);
}
history.add(undoableEdit);
indexOfNextAdd = history.size();
fireEvent(new mxEventObject(mxEvent.ADD, "edit", undoableEdit));
}
/**
* Removes all pending steps after indexOfNextAdd from the history,
* invoking die on each edit. This is called from undoableEditHappened.
*/
protected void trim()
{
while (history.size() > indexOfNextAdd)
{
mxUndoableEdit edit = history
.remove(indexOfNextAdd);
edit.die();
}
}
}