author | Claus Gittinger <cg@exept.de> |
Sat, 08 Mar 2008 11:53:23 +0100 | |
changeset 1932 | 7bafb4c076d3 |
parent 1588 | 7a69b6a027b0 |
child 2136 | 2accac6ffb0f |
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 |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
16 |
infoOfCurrentTransaction' |
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 |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
82 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
83 |
|
1454 | 84 |
!UndoSupport methodsFor:'initialization'! |
85 |
||
86 |
actionPerformer:something |
|
87 |
actionPerformer := something. |
|
88 |
! |
|
89 |
||
90 |
initialize |
|
91 |
self resetHistories. |
|
92 |
! |
|
93 |
||
94 |
resetHistories |
|
95 |
transaction := nil. |
|
96 |
undoList := OrderedCollection new. |
|
97 |
redoList := OrderedCollection new. |
|
98 |
! ! |
|
99 |
||
100 |
!UndoSupport methodsFor:'undo & again'! |
|
101 |
||
102 |
addUndo:action |
|
103 |
transaction notNil ifTrue:[ |
|
104 |
transaction add:action |
|
105 |
]. |
|
106 |
! |
|
107 |
||
108 |
closeTransactionAndAddTo:aList |
|
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
109 |
|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
|
110 |
|
1454 | 111 |
transaction notEmptyOrNil ifTrue:[ |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
112 |
canCombine := false. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
113 |
(transaction size == 1 and:[infoOfCurrentTransaction isNil]) ifTrue:[ |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
114 |
actionToAdd := transaction first. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
115 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
116 |
aList notEmpty ifTrue:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
117 |
lastAction := aList last. |
1550
7710a4b44cc9
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1549
diff
changeset
|
118 |
canCombine := lastAction perform:#canCombineWithNext: with:actionToAdd ifNotUnderstood:false. |
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
119 |
]. |
1454 | 120 |
] ifFalse:[ |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
121 |
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
|
122 |
actionToAdd info:infoOfCurrentTransaction. |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
123 |
]. |
1549
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 |
canCombine ifTrue:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
126 |
lastAction combineWithNext:actionToAdd. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
127 |
] ifFalse:[ |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
128 |
aList add:actionToAdd. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
129 |
]. |
1454 | 130 |
]. |
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
131 |
infoOfCurrentTransaction := nil. |
1454 | 132 |
transaction := nil |
133 |
! |
|
134 |
||
135 |
executeActionFrom:doList addUndoTo:unDoList |
|
1588 | 136 |
|action previousTransaction| |
1454 | 137 |
|
138 |
doList notEmptyOrNil ifTrue:[ |
|
139 |
action := doList removeLast. |
|
140 |
||
1588 | 141 |
previousTransaction := transaction. |
142 |
[ |
|
143 |
transaction := OrderedCollection new. |
|
1454 | 144 |
|
1588 | 145 |
action executeIn:actionPerformer. |
1454 | 146 |
|
1588 | 147 |
self closeTransactionAndAddTo:unDoList. |
148 |
] ensure:[ |
|
149 |
transaction := previousTransaction. |
|
150 |
] |
|
1454 | 151 |
] |
152 |
! |
|
153 |
||
154 |
hasRedoAction |
|
155 |
^ redoList size > 0 |
|
156 |
! |
|
157 |
||
158 |
hasUndoAction |
|
159 |
^ undoList size > 0 |
|
160 |
! |
|
161 |
||
162 |
lastRedoAction |
|
163 |
^ redoList removeLast |
|
164 |
! |
|
165 |
||
166 |
lastUndoAction |
|
167 |
^ undoList removeLast |
|
168 |
! |
|
169 |
||
170 |
nonUndoableDo:aBlock |
|
171 |
|prev| |
|
172 |
||
173 |
prev := transaction. |
|
174 |
transaction := nil. |
|
175 |
aBlock |
|
176 |
ensure:[ |
|
177 |
transaction := prev. |
|
178 |
]. |
|
179 |
! |
|
180 |
||
181 |
redo |
|
182 |
self executeActionFrom:redoList addUndoTo:undoList |
|
183 |
! |
|
184 |
||
1932
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
185 |
redoActionInfo |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
186 |
^ redoList last info |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
187 |
! |
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
188 |
|
1454 | 189 |
undo |
190 |
self executeActionFrom:undoList addUndoTo:redoList |
|
191 |
! |
|
192 |
||
1500 | 193 |
undoActionInfo |
194 |
^ undoList last info |
|
195 |
! |
|
196 |
||
1454 | 197 |
undoableDo:aBlock |
1526 | 198 |
self undoableDo:aBlock info:nil. |
199 |
! |
|
200 |
||
201 |
undoableDo:aBlock info:aString |
|
1454 | 202 |
transaction notNil ifTrue:[ |
1526 | 203 |
infoOfCurrentTransaction := infoOfCurrentTransaction ? aString. |
1454 | 204 |
aBlock value. |
205 |
] ifFalse:[ |
|
206 |
transaction := OrderedCollection new. |
|
1526 | 207 |
infoOfCurrentTransaction := aString. |
1454 | 208 |
aBlock |
209 |
ensure:[ |
|
210 |
self closeTransactionAndAddTo:undoList. |
|
211 |
]. |
|
212 |
]. |
|
213 |
! ! |
|
214 |
||
1525
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
215 |
!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
|
216 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
217 |
info |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
218 |
^ userFriendlyInfo |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
219 |
! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
220 |
|
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
221 |
info:aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
222 |
userFriendlyInfo := aString |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
223 |
! ! |
6384f91af636
better info of replace action (compound-undo-action has info, too)
Claus Gittinger <cg@exept.de>
parents:
1522
diff
changeset
|
224 |
|
1454 | 225 |
!UndoSupport::CompoundAction methodsFor:'adding'! |
226 |
||
227 |
actions:aCollection |
|
228 |
actions := aCollection |
|
229 |
! |
|
230 |
||
231 |
add:action |
|
232 |
actions isNil ifTrue:[ |
|
233 |
actions := OrderedCollection new. |
|
234 |
]. |
|
235 |
actions add:action |
|
236 |
! ! |
|
237 |
||
238 |
!UndoSupport::CompoundAction methodsFor:'execution'! |
|
239 |
||
240 |
executeIn:editor |
|
241 |
actions reverseDo:[:each | each executeIn:editor ] |
|
242 |
! ! |
|
243 |
||
1549
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
244 |
!UndoSupport::CompoundAction methodsFor:'queries'! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
245 |
|
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
246 |
canCombineWithNext:nextAction |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
247 |
^ false. |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
248 |
! ! |
b35755379765
undo - combine multiple-single keystrokes
Claus Gittinger <cg@exept.de>
parents:
1526
diff
changeset
|
249 |
|
1454 | 250 |
!UndoSupport class methodsFor:'documentation'! |
251 |
||
252 |
version |
|
1932
7bafb4c076d3
also show \"what-would-be-done\" in redo item
Claus Gittinger <cg@exept.de>
parents:
1588
diff
changeset
|
253 |
^ '$Header: /cvs/stx/stx/libbasic2/UndoSupport.st,v 1.9 2008-03-08 10:53:23 cg Exp $' |
1454 | 254 |
! ! |