Improved version for copying and pasting subtrees between nodes. This inhibit that nodes

can appear as children of the same parent several times (clones in Leo vocabulary). Still we need to explore if cloning is useful in other contexts.
This commit is contained in:
Offray Vladimir Luna Cárdenas 2017-11-30 13:09:12 +00:00
parent 0ed3ffe53f
commit 82ba093f3f

View File

@ -78,7 +78,7 @@ GrafoscopioNode >> addLink: anUrl [
GrafoscopioNode >> addNode: aNode [ GrafoscopioNode >> addNode: aNode [
"Adds the given node to the receivers collection of children, and sets this object as the parent "Adds the given node to the receivers collection of children, and sets this object as the parent
of the node" of the node"
aNode parent = self ifTrue: [ ^ self ]. "aNode parent = self ifTrue: [ ^ self ]."
self children add: aNode. self children add: aNode.
aNode level: (self level) + 1. aNode level: (self level) + 1.
aNode parent: self. aNode parent: self.
@ -302,8 +302,7 @@ GrafoscopioNode >> content: anObject [
{ #category : #'add/remove nodes' } { #category : #'add/remove nodes' }
GrafoscopioNode >> copyToClipboard [ GrafoscopioNode >> copyToClipboard [
self class clipboard: self copy. self class clipboard: self subtreeCopy.
self class cleanTreeRootReferences
] ]
@ -845,7 +844,14 @@ GrafoscopioNode >> parent [
{ #category : #accessing } { #category : #accessing }
GrafoscopioNode >> parent: aNode [ GrafoscopioNode >> parent: aNode [
"A parent is a node that has the current node in its children" "A parent is a node that has the current node in its children"
aNode ifNil: [
parent := aNode.
^self ].
aNode parent = self ifTrue: [ ^ self ].
parent := aNode. parent := aNode.
(aNode children includes: self)
ifFalse: [ aNode addNode: self ]
] ]
{ #category : #'add/remove nodes' } { #category : #'add/remove nodes' }
@ -947,7 +953,21 @@ GrafoscopioNode >> specModelClass [
^ GrafoscopioTextModel ^ GrafoscopioTextModel
] ]
{ #category : #'as yet unclassified' } { #category : #operation }
GrafoscopioNode >> subtreeCopy [
"I return the same node if its subtree only contains the receiver, or a copy of the receivers
subtree, in other cases."
| linearSubtree linearSubtreeCopy |
linearSubtree := self preorderTraversal.
linearSubtreeCopy := OrderedCollection new.
linearSubtree do: [ :cn | linearSubtreeCopy add: cn surfaceCopy ].
linearSubtreeCopy allButFirst doWithIndex: [ :n :i | | parentPos |
parentPos := linearSubtree indexOf: (linearSubtree at: i+1) parent.
n parent: (linearSubtreeCopy at: parentPos) ].
^ linearSubtreeCopy at: 1.
]
{ #category : #operation }
GrafoscopioNode >> surfaceCopy [ GrafoscopioNode >> surfaceCopy [
"I copy the most relevant values of the receiver. I'm useful to avoid copying references "I copy the most relevant values of the receiver. I'm useful to avoid copying references
to the rest of the container tree, which could end in copying the whole tree." to the rest of the container tree, which could end in copying the whole tree."