Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Pyramid-Bloc/PyramidAddChildCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ PyramidAddChildCommand >> commandInverse [

{ #category : #'as yet unclassified' }
PyramidAddChildCommand >> setValueFor: aBlElement with: aChildToAdd [

aBlElement addChild: aChildToAdd
| index |
index := aChildToAdd userData at: #removedAtIndex ifAbsent: [ 0 ].
(index = 0 or: [ index > aBlElement children size ])
ifTrue: [ aBlElement addChild: aChildToAdd ]
ifFalse: [ aBlElement addChild: aChildToAdd at: index ]
]
4 changes: 2 additions & 2 deletions src/Pyramid-Bloc/PyramidAddChildrenCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ PyramidAddChildrenCommand >> commandInverse [

{ #category : #'as yet unclassified' }
PyramidAddChildrenCommand >> setValueFor: aBlElement with: aChildrenToAdd [

aBlElement addChildren: aChildrenToAdd
aChildrenToAdd do: [ :child |
aBlElement addChild: child ]
]
29 changes: 17 additions & 12 deletions src/Pyramid-Bloc/PyramidRemoveSelectedElementsCommand.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ PyramidRemoveSelectedElementsCommand >> getValueFor: anElementOfSelection [

{ #category : #'as yet unclassified' }
PyramidRemoveSelectedElementsCommand >> mementoFor: anElement withArguments: anArgument [

(anArgument includes: anElement) ifTrue: [
^ PyramidCommandMemento new
command: PyramidRemoveFromCollectionCommand new commandInverse;
target: anArgument;
arguments: anElement;
yourself ].
^ PyramidCommandMemento new
command: PyramidRemoveChildCommand new commandInverse;
target: anElement parent;
arguments: anElement;
yourself
| index |
"Save index before removing"
index := anElement parent
ifNotNil: [ :p | p children indexOf: anElement ]
ifNil: [ 0 ].
anElement userData at: #removedAtIndex put: index.
(anArgument includes: anElement) ifTrue: [
^ PyramidCommandMemento new
command: PyramidRemoveFromCollectionCommand new commandInverse;
target: anArgument;
arguments: anElement;
yourself ].
^ PyramidCommandMemento new
command: PyramidRemoveChildCommand new commandInverse;
target: anElement parent;
arguments: anElement;
yourself
]

{ #category : #accessing }
Expand Down
87 changes: 87 additions & 0 deletions src/Pyramid-Tests/PyramidUndoRemoveChildCommandTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Class {
#name : #PyramidUndoRemoveChildCommandTest,
#superclass : #TestCase,
#category : #'Pyramid-Tests-cases-plugin-bloc'
}

{ #category : #tests }
PyramidUndoRemoveChildCommandTest >> testUndoRemoveAllChildrenRestoresIndex [

| history commandExecutor parent child1 child2 child3 child4|
parent := BlElement new.
child1 := BlElement new.
child2 := BlElement new.
child3 := BlElement new.
child4 := BlElement new.
parent addChildren: {child1. child2 .child3. child4}.

history := PyramidHistory new.
commandExecutor := PyramidHistoryCommandExecutor new
history: history;
wrappee: PyramidMainCommandExecutor new;
yourself.
commandExecutor
use: PyramidRemoveSelectedElementsCommand new
on: {
child4.
child2.
child1 }
with: { parent }.

self deny: (parent children includes: child2).
self deny: (parent children includes: child1).
self deny: (parent children includes: child4).

"Undo"
history undo.

self assert: (parent children includes: child1).
self assert: (parent children includes: child2).
self assert: (parent children indexOf: child1) equals: 1.
self assert: (parent children indexOf: child2) equals: 2.
self assert: (parent children indexOf: child4) equals: 4
]

{ #category : #tests }
PyramidUndoRemoveChildCommandTest >> testUndoRemoveChildRestoresIndex [

| history commandExecutor parent child1 child2 child3 |

parent := BlElement new.
child1 := BlElement new.
child2 := BlElement new.
child3 := BlElement new.
parent addChild: child1.
parent addChild: child2.
parent addChild: child3.

history := PyramidHistory new.
commandExecutor := PyramidHistoryCommandExecutor new
history: history;
wrappee: PyramidMainCommandExecutor new;
yourself.

commandExecutor
use: PyramidRemoveSelectedElementsCommand new
on: { child2 }
with: { parent }.

self deny: (parent children includes: child2).

"Undo"
history undo.

self assert: (parent children includes: child2).
self assert: (parent children indexOf: child2) equals: 2.

"Redo"
history redo.

self deny: (parent children includes: child2).

"Undo"
history undo.

self assert: (parent children includes: child2).
self assert: (parent children indexOf: child2) equals: 2
]
7 changes: 6 additions & 1 deletion src/Pyramid/PyramidCompositeMemento.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ PyramidCompositeMemento >> mementos: anObject [
{ #category : #'window management' }
PyramidCompositeMemento >> restore [

self mementos do: [ :each | each restore ]
(self mementos allSatisfy: [ :m | m arguments isKindOf: BlElement ])
ifFalse: [ self mementos do: [ :each | each restore ]. ^ self ].
(self mementos asSortedCollection: [ :a :b |
(a arguments userData at: #removedAtIndex ifAbsent: [ 0 ])
< (b arguments userData at: #removedAtIndex ifAbsent: [ 0 ]) ])
do: [ :each | each restore ]
]
Loading