From 82ba093f3f974a8760535239ab23844a28a1a80c Mon Sep 17 00:00:00 2001 From: Offray Luna Date: Thu, 30 Nov 2017 13:09:12 +0000 Subject: [PATCH] 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. --- .../Grafoscopio/GrafoscopioNode.class.st | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/repository/Grafoscopio/GrafoscopioNode.class.st b/repository/Grafoscopio/GrafoscopioNode.class.st index e78268b..b9f0790 100644 --- a/repository/Grafoscopio/GrafoscopioNode.class.st +++ b/repository/Grafoscopio/GrafoscopioNode.class.st @@ -78,7 +78,7 @@ GrafoscopioNode >> addLink: anUrl [ GrafoscopioNode >> addNode: aNode [ "Adds the given node to the receivers collection of children, and sets this object as the parent of the node" - aNode parent = self ifTrue: [ ^ self ]. + "aNode parent = self ifTrue: [ ^ self ]." self children add: aNode. aNode level: (self level) + 1. aNode parent: self. @@ -302,8 +302,7 @@ GrafoscopioNode >> content: anObject [ { #category : #'add/remove nodes' } GrafoscopioNode >> copyToClipboard [ - self class clipboard: self copy. - self class cleanTreeRootReferences + self class clipboard: self subtreeCopy. ] @@ -845,7 +844,14 @@ GrafoscopioNode >> parent [ { #category : #accessing } GrafoscopioNode >> parent: aNode [ "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. + (aNode children includes: self) + ifFalse: [ aNode addNode: self ] + ] { #category : #'add/remove nodes' } @@ -947,7 +953,21 @@ GrafoscopioNode >> specModelClass [ ^ 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 [ "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."