47
|
1 |
Object subclass:#UndoHistory
|
|
2 |
instanceVariableNames:'history transaction enabled modifiedAction'
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
privateIn:UIObjectView
|
|
6 |
!
|
|
7 |
|
50
|
8 |
!UIObjectView::UndoHistory class methodsFor:'instance creation'!
|
44
|
9 |
|
50
|
10 |
new
|
|
11 |
^ self basicNew initialize
|
44
|
12 |
|
|
13 |
|
|
14 |
! !
|
|
15 |
|
47
|
16 |
!UIObjectView::UndoHistory class methodsFor:'constants'!
|
|
17 |
|
|
18 |
maxHistorySize
|
|
19 |
"returns maximum size of history before removing oldest
|
|
20 |
record
|
|
21 |
"
|
|
22 |
^ 50
|
|
23 |
|
|
24 |
|
|
25 |
! !
|
|
26 |
|
|
27 |
!UIObjectView::UndoHistory methodsFor:'accessing'!
|
|
28 |
|
|
29 |
modifiedAction:aBlockWithOneArg
|
|
30 |
"the block is evaluated whenever the history changed; the argument to the
|
|
31 |
block is the newest transaction identifier retrived from 'openTransaction'
|
|
32 |
"
|
|
33 |
modifiedAction := aBlockWithOneArg
|
|
34 |
|
|
35 |
|
|
36 |
! !
|
|
37 |
|
|
38 |
!UIObjectView::UndoHistory methodsFor:'initialization'!
|
|
39 |
|
|
40 |
initialize
|
|
41 |
super initialize.
|
|
42 |
self reinitialize.
|
|
43 |
|
|
44 |
|
|
45 |
!
|
|
46 |
|
|
47 |
reinitialize
|
|
48 |
"reinitialize all attributes
|
|
49 |
"
|
|
50 |
history := OrderedCollection new.
|
|
51 |
transaction := nil.
|
|
52 |
enabled := true.
|
|
53 |
|
|
54 |
|
|
55 |
! !
|
|
56 |
|
|
57 |
!UIObjectView::UndoHistory methodsFor:'notifications'!
|
|
58 |
|
|
59 |
modified
|
|
60 |
"raise notification; history changed
|
|
61 |
"
|
|
62 |
modifiedAction notNil ifTrue:[
|
|
63 |
|what|
|
|
64 |
|
|
65 |
history isEmpty ifTrue:[what := nil]
|
|
66 |
ifFalse:[what := history last first].
|
|
67 |
|
|
68 |
modifiedAction value:what
|
|
69 |
]
|
|
70 |
|
|
71 |
|
|
72 |
! !
|
|
73 |
|
|
74 |
!UIObjectView::UndoHistory methodsFor:'testing'!
|
|
75 |
|
|
76 |
isEmpty
|
|
77 |
"returns true if undo history is empty
|
|
78 |
"
|
|
79 |
^ history isEmpty
|
|
80 |
|
|
81 |
|
|
82 |
!
|
|
83 |
|
|
84 |
isTransactionOpen
|
|
85 |
^ (enabled and:[transaction notNil])
|
|
86 |
!
|
|
87 |
|
|
88 |
notEmpty
|
|
89 |
"returns true if undo history is not empty
|
|
90 |
"
|
|
91 |
^ history notEmpty
|
|
92 |
|
|
93 |
|
|
94 |
! !
|
|
95 |
|
|
96 |
!UIObjectView::UndoHistory methodsFor:'transaction'!
|
|
97 |
|
|
98 |
addUndoBlock:anUndoBlock
|
|
99 |
"undo block to restore changes; add block to current transaction
|
|
100 |
"
|
|
101 |
enabled ifTrue:[
|
|
102 |
transaction isNil ifTrue:[
|
|
103 |
"no existing transaction
|
|
104 |
"
|
|
105 |
self halt
|
|
106 |
] ifFalse:[
|
|
107 |
(transaction at:2) add:anUndoBlock
|
|
108 |
]
|
|
109 |
]
|
|
110 |
|
|
111 |
|
|
112 |
!
|
|
113 |
|
|
114 |
closeTransaction
|
|
115 |
"close current transaction
|
|
116 |
"
|
|
117 |
self isTransactionOpen ifTrue:[
|
|
118 |
transaction last isEmpty ifTrue:[
|
|
119 |
"empty undo transaction
|
|
120 |
"
|
|
121 |
transaction := nil
|
|
122 |
] ifFalse:[
|
|
123 |
history addLast:transaction.
|
|
124 |
transaction := nil.
|
|
125 |
|
|
126 |
history size > (self class maxHistorySize) ifTrue:[
|
|
127 |
history removeFirst
|
|
128 |
].
|
|
129 |
self modified
|
|
130 |
]
|
|
131 |
]
|
|
132 |
|
|
133 |
|
|
134 |
!
|
|
135 |
|
|
136 |
disabledTransitionDo:aBlock
|
|
137 |
"disable transitions during evaluating the block
|
|
138 |
"
|
|
139 |
|oldState|
|
|
140 |
|
|
141 |
oldState := enabled.
|
|
142 |
enabled := false.
|
|
143 |
aBlock value.
|
|
144 |
enabled := oldState.
|
|
145 |
!
|
|
146 |
|
|
147 |
openTransaction:what
|
|
148 |
"open a new transaction
|
|
149 |
"
|
|
150 |
enabled ifTrue:[
|
|
151 |
transaction notNil ifTrue:[
|
|
152 |
"transaction within transaction
|
|
153 |
"
|
|
154 |
self halt.
|
|
155 |
] ifFalse:[
|
|
156 |
transaction := Array with:what with:OrderedCollection new
|
|
157 |
]
|
|
158 |
]
|
|
159 |
|
|
160 |
!
|
|
161 |
|
|
162 |
transactionNamed:what do:aBlock
|
|
163 |
"open a transaction; perform the block; at least close the transaction
|
|
164 |
"
|
|
165 |
self isTransactionOpen ifFalse:[
|
|
166 |
self openTransaction:what.
|
|
167 |
aBlock value.
|
|
168 |
self closeTransaction
|
|
169 |
] ifTrue:[
|
|
170 |
aBlock value
|
|
171 |
]
|
|
172 |
! !
|
|
173 |
|
|
174 |
!UIObjectView::UndoHistory methodsFor:'undo'!
|
|
175 |
|
|
176 |
undoLast
|
|
177 |
"undo last transactions; an open transaction will be closed;
|
|
178 |
transactions during undo are disabled
|
|
179 |
"
|
|
180 |
self undoLast:1
|
|
181 |
|
|
182 |
|
|
183 |
!
|
|
184 |
|
|
185 |
undoLast:nTransactions
|
|
186 |
"undo last n transactions; an open transaction will be closed;
|
|
187 |
transactions during undo are disabled
|
|
188 |
"
|
|
189 |
|actions n|
|
|
190 |
|
|
191 |
transaction := nil.
|
|
192 |
n := nTransactions min:(history size).
|
|
193 |
|
|
194 |
n ~~ 0 ifTrue:[
|
|
195 |
enabled := false.
|
|
196 |
|
|
197 |
n timesRepeat:[
|
|
198 |
actions := (history removeLast) last.
|
|
199 |
|
|
200 |
actions reverseDo:[:aUndoBlock|
|
|
201 |
aUndoBlock value
|
|
202 |
]
|
|
203 |
].
|
|
204 |
enabled := true.
|
|
205 |
self modified.
|
|
206 |
]
|
|
207 |
|
|
208 |
|
|
209 |
! !
|
|
210 |
|