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