author | Claus Gittinger <cg@exept.de> |
Mon, 04 May 2009 16:42:09 +0200 | |
changeset 2136 | 2accac6ffb0f |
parent 1932 | 7bafb4c076d3 |
child 3820 | d4100babd7c8 |
permissions | -rw-r--r-- |
1522 | 1 |
" |
2 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
1454 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
14 |
Object subclass:#UndoSupport |
|
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
15 |
instanceVariableNames:'actionPerformer transaction undoList redoList |
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
16 |
infoOfCurrentTransaction undoLimit' |
1454 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Views-Text' |
|
20 |
! |
|
21 |
||
22 |
Object subclass:#CompoundAction |
|
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
23 |
instanceVariableNames:'actions userFriendlyInfo' |
1454 | 24 |
classVariableNames:'' |
25 |
poolDictionaries:'' |
|
26 |
privateIn:UndoSupport |
|
27 |
! |
|
28 |
||
29 |
!UndoSupport class methodsFor:'documentation'! |
|
30 |
||
1522 | 31 |
copyright |
32 |
" |
|
33 |
COPYRIGHT (c) 2004 by eXept Software AG |
|
34 |
All Rights Reserved |
|
35 |
||
36 |
This software is furnished under a license and may be used |
|
37 |
only in accordance with the terms of that license and with the |
|
38 |
inclusion of the above copyright notice. This software may not |
|
39 |
be provided or otherwise made available to, or used by, any |
|
40 |
other person. No title to or ownership of the software is |
|
41 |
hereby transferred. |
|
42 |
" |
|
43 |
! |
|
44 |
||
1454 | 45 |
documentation |
46 |
" |
|
47 |
Framework for undo & redo. |
|
48 |
See concrete usage in EditTextView. |
|
49 |
||
50 |
[author:] |
|
51 |
(cg@betti) |
|
52 |
||
53 |
[instance variables:] |
|
54 |
||
55 |
[class variables:] |
|
56 |
||
57 |
[see also:] |
|
58 |
||
59 |
" |
|
60 |
! |
|
61 |
||
62 |
examples |
|
63 |
" |
|
64 |
See usage in EditTextView |
|
65 |
" |
|
66 |
! ! |
|
67 |
||
68 |
!UndoSupport class methodsFor:'instance creation'! |
|
69 |
||
70 |
for:anActionPerformer |
|
71 |
^ self new actionPerformer:anActionPerformer. |
|
72 |
! |
|
73 |
||
74 |
new |
|
75 |
^ self basicNew initialize. |
|
76 |
! ! |
|
77 |
||
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
78 |
!UndoSupport methodsFor:'accessing'! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
79 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
80 |
actionInfo:aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
81 |
infoOfCurrentTransaction := aString |
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
82 |
! |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
83 |
|
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
84 |
undoLimit |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
85 |
^ undoLimit |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
86 |
! |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
87 |
|
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
88 |
undoLimit:something |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
89 |
undoLimit := something. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
90 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
91 |
|
1454 | 92 |
!UndoSupport methodsFor:'initialization'! |
93 |
||
94 |
actionPerformer:something |
|
95 |
actionPerformer := something. |
|
96 |
! |
|
97 |
||
98 |
initialize |
|
99 |
self resetHistories. |
|
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
100 |
"/ undoLimit := 1000. |
1454 | 101 |
! |
102 |
||
103 |
resetHistories |
|
104 |
transaction := nil. |
|
105 |
undoList := OrderedCollection new. |
|
106 |
redoList := OrderedCollection new. |
|
107 |
! ! |
|
108 |
||
109 |
!UndoSupport methodsFor:'undo & again'! |
|
110 |
||
111 |
addUndo:action |
|
112 |
transaction notNil ifTrue:[ |
|
113 |
transaction add:action |
|
114 |
]. |
|
115 |
! |
|
116 |
||
117 |
closeTransactionAndAddTo:aList |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
118 |
|actionToAdd lastAction canCombine| |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
119 |
|
1454 | 120 |
transaction notEmptyOrNil ifTrue:[ |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
121 |
canCombine := false. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
122 |
(transaction size == 1 and:[infoOfCurrentTransaction isNil]) ifTrue:[ |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
123 |
actionToAdd := transaction first. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
124 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
125 |
aList notEmpty ifTrue:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
126 |
lastAction := aList last. |
1550
7710a4b44cc9
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1549
diff
changeset
|
127 |
canCombine := lastAction perform:#canCombineWithNext: with:actionToAdd ifNotUnderstood:false. |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
128 |
]. |
1454 | 129 |
] ifFalse:[ |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
130 |
actionToAdd := (CompoundAction new actions:transaction). |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
131 |
actionToAdd info:infoOfCurrentTransaction. |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
132 |
]. |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
133 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
134 |
canCombine ifTrue:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
135 |
lastAction combineWithNext:actionToAdd. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
136 |
] ifFalse:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
137 |
aList add:actionToAdd. |
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
138 |
(undoLimit notNil and:[aList size > undoLimit]) ifTrue:[ |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
139 |
Transcript showCR:'forget old undo-action'. |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
140 |
aList removeFirst. |
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
141 |
]. |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
142 |
]. |
1454 | 143 |
]. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
144 |
infoOfCurrentTransaction := nil. |
1454 | 145 |
transaction := nil |
146 |
! |
|
147 |
||
148 |
executeActionFrom:doList addUndoTo:unDoList |
|
1588 | 149 |
|action previousTransaction| |
1454 | 150 |
|
151 |
doList notEmptyOrNil ifTrue:[ |
|
152 |
action := doList removeLast. |
|
153 |
||
1588 | 154 |
previousTransaction := transaction. |
155 |
[ |
|
156 |
transaction := OrderedCollection new. |
|
1454 | 157 |
|
1588 | 158 |
action executeIn:actionPerformer. |
1454 | 159 |
|
1588 | 160 |
self closeTransactionAndAddTo:unDoList. |
161 |
] ensure:[ |
|
162 |
transaction := previousTransaction. |
|
163 |
] |
|
1454 | 164 |
] |
165 |
! |
|
166 |
||
167 |
hasRedoAction |
|
168 |
^ redoList size > 0 |
|
169 |
! |
|
170 |
||
171 |
hasUndoAction |
|
172 |
^ undoList size > 0 |
|
173 |
! |
|
174 |
||
175 |
lastRedoAction |
|
176 |
^ redoList removeLast |
|
177 |
! |
|
178 |
||
179 |
lastUndoAction |
|
180 |
^ undoList removeLast |
|
181 |
! |
|
182 |
||
183 |
nonUndoableDo:aBlock |
|
184 |
|prev| |
|
185 |
||
186 |
prev := transaction. |
|
187 |
transaction := nil. |
|
188 |
aBlock |
|
189 |
ensure:[ |
|
190 |
transaction := prev. |
|
191 |
]. |
|
192 |
! |
|
193 |
||
194 |
redo |
|
195 |
self executeActionFrom:redoList addUndoTo:undoList |
|
196 |
! |
|
197 |
||
1932
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
198 |
redoActionInfo |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
199 |
^ redoList last info |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
200 |
! |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
201 |
|
1454 | 202 |
undo |
203 |
self executeActionFrom:undoList addUndoTo:redoList |
|
204 |
! |
|
205 |
||
1500 | 206 |
undoActionInfo |
207 |
^ undoList last info |
|
208 |
! |
|
209 |
||
1454 | 210 |
undoableDo:aBlock |
1526 | 211 |
self undoableDo:aBlock info:nil. |
212 |
! |
|
213 |
||
214 |
undoableDo:aBlock info:aString |
|
1454 | 215 |
transaction notNil ifTrue:[ |
1526 | 216 |
infoOfCurrentTransaction := infoOfCurrentTransaction ? aString. |
1454 | 217 |
aBlock value. |
218 |
] ifFalse:[ |
|
219 |
transaction := OrderedCollection new. |
|
1526 | 220 |
infoOfCurrentTransaction := aString. |
1454 | 221 |
aBlock |
222 |
ensure:[ |
|
223 |
self closeTransactionAndAddTo:undoList. |
|
224 |
]. |
|
225 |
]. |
|
226 |
! ! |
|
227 |
||
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
228 |
!UndoSupport::CompoundAction methodsFor:'accessing'! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
229 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
230 |
info |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
231 |
^ userFriendlyInfo |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
232 |
! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
233 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
234 |
info:aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
235 |
userFriendlyInfo := aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
236 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
237 |
|
1454 | 238 |
!UndoSupport::CompoundAction methodsFor:'adding'! |
239 |
||
240 |
actions:aCollection |
|
241 |
actions := aCollection |
|
242 |
! |
|
243 |
||
244 |
add:action |
|
245 |
actions isNil ifTrue:[ |
|
246 |
actions := OrderedCollection new. |
|
247 |
]. |
|
248 |
actions add:action |
|
249 |
! ! |
|
250 |
||
251 |
!UndoSupport::CompoundAction methodsFor:'execution'! |
|
252 |
||
253 |
executeIn:editor |
|
254 |
actions reverseDo:[:each | each executeIn:editor ] |
|
255 |
! ! |
|
256 |
||
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
257 |
!UndoSupport::CompoundAction methodsFor:'queries'! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
258 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
259 |
canCombineWithNext:nextAction |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
260 |
^ false. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
261 |
! ! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
262 |
|
1454 | 263 |
!UndoSupport class methodsFor:'documentation'! |
264 |
||
265 |
version |
|
2136
2accac6ffb0f
allow for the undoList size to be limited
Claus Gittinger <cg@exept.de>
parents:
1932
diff
changeset
|
266 |
^ '$Header: /cvs/stx/stx/libbasic2/UndoSupport.st,v 1.10 2009-05-04 14:42:09 cg Exp $' |
1454 | 267 |
! ! |