changeset 21:cce963b8a4fd

EditableTreeBuilderTest1
author ShoshiTAMAKI
date Sun, 17 Oct 2010 13:56:47 +0900
parents e950264f82d3
children 92637ef791b7
files src/treecms/proto/api/Node.java src/treecms/proto/edit/EditableTreeBuilder.java src/treecms/proto/simple/SimpleNode.java src/treecms/proto/test/EditableTreeBuilderTest1.java src/treecms/proto/test/PreOrderTreeWalkerRecurisiveTest1.java src/treecms/proto/test/Test2.java src/treecms/proto/test/Test3.java
diffstat 7 files changed, 178 insertions(+), 139 deletions(-) [+]
line wrap: on
line diff
--- a/src/treecms/proto/api/Node.java	Sun Oct 17 00:29:56 2010 +0900
+++ b/src/treecms/proto/api/Node.java	Sun Oct 17 13:56:47 2010 +0900
@@ -20,4 +20,5 @@
 	void setTitle(String _title);
 	String getClassName();
 	String getTitle();
+	String getID();
 }
\ No newline at end of file
--- a/src/treecms/proto/edit/EditableTreeBuilder.java	Sun Oct 17 00:29:56 2010 +0900
+++ b/src/treecms/proto/edit/EditableTreeBuilder.java	Sun Oct 17 13:56:47 2010 +0900
@@ -12,30 +12,61 @@
 	private Node m_newRoot;
 	private Node m_oldRoot;
 	
+	private Node m_target; //node that wanted to edit.
+	
 	public EditableTreeBuilder(Node _target,TreeBuilder _builder)
 	{
 		m_builder = _builder;
 		
 		//search path
 		m_oldRoot = _builder.getContents();
-		List<Node> path = findPath(m_oldRoot,_target);
+		LinkedList<Node> path = findPath(m_oldRoot,_target);
+		
+		/*
+		for(Node node : path){
+			System.out.println(node.getTitle());
+		}
+		*/
 		
 		//clone tree
-		List<Node> clonedTree = cloneTree(path);
-		m_newRoot = clonedTree.get(0);
+		
+		//clone root node.
+		m_newRoot = m_builder.createNode();
+		Node oldRoot = path.poll();
+		m_newRoot.setClassName(oldRoot.getClassName());
+		m_newRoot.setTitle(oldRoot.getTitle());
+		m_newRoot.getChildList().addAll(oldRoot.getChildList());
+		
+		cloneTree(path,m_newRoot.getChildList());
 	}
 	
-	private List<Node> cloneTree(List<Node> _path)
+	public Node getTargetNode()
+	{
+		return m_target;
+	}
+	
+	private void cloneTree(LinkedList<Node> _path,List<Node> _children)
 	{
-		List<Node> clonedTree = new LinkedList<Node>();
-		
-		Node origRoot = _path.get(0);
-		Node clonedRoot = m_builder.createNode();
-		clonedRoot.setTitle(origRoot.getTitle());
-		clonedRoot.setClassName(origRoot.getClassName());
-		
-		
-		return clonedTree;
+		Node target = _path.poll();
+		for(int i = 0;i < _children.size();i ++){
+			Node _child = _children.get(i);
+			if(_child == target){
+				//clone node
+				Node newNode = m_builder.createNode();
+				newNode.setClassName(target.getClassName());
+				newNode.setTitle(target.getTitle());
+				newNode.getChildList().addAll(target.getChildList());
+				m_target = newNode; //oooooooooooooo.............. :(
+				
+				//remove old node from clonedTree
+				_children.add(i,newNode);
+				_children.remove(target);
+				
+				cloneTree(_path,newNode.getChildList());
+				
+				break;
+			}
+		}
 	}
 	
 	public List<Node> findPathTest(Node _root,Node _child)
@@ -43,7 +74,7 @@
 		return findPath(_root,_child);
 	}
 	
-	private List<Node> findPath(Node _root,Node _child)
+	private LinkedList<Node> findPath(Node _root,Node _child)
 	{
 		LinkedList<Node> list = new LinkedList<Node>();
 		findPath(_root,_child,list);
--- a/src/treecms/proto/simple/SimpleNode.java	Sun Oct 17 00:29:56 2010 +0900
+++ b/src/treecms/proto/simple/SimpleNode.java	Sun Oct 17 13:56:47 2010 +0900
@@ -100,5 +100,10 @@
 		// TODO Auto-generated method stub
 		return m_childs.iterator();
 	}
+	
+	public String getID()
+	{
+		return this.m_uuid;
+	}
 
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/test/EditableTreeBuilderTest1.java	Sun Oct 17 13:56:47 2010 +0900
@@ -0,0 +1,68 @@
+package treecms.proto.test;
+
+import java.util.Iterator;
+
+import treecms.proto.edit.*;
+import treecms.proto.api.*;
+import treecms.proto.simple.*;
+
+public class EditableTreeBuilderTest1 
+{
+	public static void main(String _arg[])
+	{
+		TreeBuilder builder = new SimpleTreeBuilder();
+		Node root = builder.getContents();
+		root.setTitle("root");
+		
+		Node child1 = builder.createNode();
+		child1.setTitle("+-child1");
+		Node child2 = builder.createNode();
+		child2.setTitle("+-child2");
+		
+		root.addChild(child1);
+		root.addChild(child2);
+		
+		Node child11 = builder.createNode(); 
+		child11.setTitle(" +-child11");
+		Node child12 = builder.createNode();
+		child12.setTitle(" +-child12");
+		Node child13 = builder.createNode();
+		child13.setTitle(" +-child13");
+		
+		child1.addChild(child11);
+		child1.addChild(child12);
+		child1.addChild(child13);
+		
+		Node child21 = builder.createNode();
+		child21.setTitle(" +-child21");
+		
+		child2.addChild(child21);
+		
+		Node child211 = builder.createNode();
+		child211.setTitle("  +-child211");
+		Node child212 = builder.createNode();
+		child212.setTitle("  +-child212");
+		
+		child21.addChild(child211);
+		child21.addChild(child212);
+	
+		//i want to edit child22
+		
+		System.out.println("original");
+		PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder.getContents());
+		for(Node node : walker){
+			System.out.println(node.getTitle() + "\t\tID:" + node.getID());
+		}
+		
+		
+		System.out.println("clonedtree");
+		
+		//check that tree correctly copied.
+		EditableTreeBuilder builder2 = new EditableTreeBuilder(child212,builder);
+		builder2.getTargetNode().setTitle("cloned child212");
+		walker = new PreOrderTreeWalkerRecursive(builder2.getContents());
+		for(Node node : walker){
+			System.out.println(node.getTitle() + "\t\tID:" + node.getID());
+		}
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/treecms/proto/test/PreOrderTreeWalkerRecurisiveTest1.java	Sun Oct 17 13:56:47 2010 +0900
@@ -0,0 +1,59 @@
+package treecms.proto.test;
+
+
+import treecms.proto.api.*;
+import treecms.proto.simple.*;
+import treecms.proto.edit.EditableTreeBuilder;
+import treecms.proto.edit.PreOrderTreeWalkerRecursive;
+
+public class PreOrderTreeWalkerRecurisiveTest1
+{
+	public static void main(String _arg[])
+	{
+		//create test tree.
+		TreeBuilder builder = new SimpleTreeBuilder();
+		Node root = builder.getContents();
+		root.setTitle("root");
+		
+		Node child1 = builder.createNode();
+		child1.setTitle("child1");
+		Node child2 = builder.createNode();
+		child2.setTitle("child2");
+		
+		root.addChild(child1);
+		root.addChild(child2);
+		
+		Node child11 = builder.createNode(); 
+		child11.setTitle("child11");
+		Node child12 = builder.createNode();
+		child12.setTitle("child12");
+		Node child13 = builder.createNode();
+		child13.setTitle("child13");
+		
+		child1.addChild(child11);
+		child1.addChild(child12);
+		child1.addChild(child13);
+		
+		Node child21 = builder.createNode();
+		child21.setTitle("child21");
+		
+		child2.addChild(child21);
+		
+		Node child211 = builder.createNode();
+		child211.setTitle("child211");
+		Node child212 = builder.createNode();
+		child212.setTitle("child212");
+		
+		child21.addChild(child211);
+		child21.addChild(child212);
+		
+		
+		//create editable tree.
+		EditableTreeBuilder builder1 = new EditableTreeBuilder(child212,builder);
+		PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder1.getContents());
+		
+		for(Node node : walker){
+			System.out.println(node.getTitle() + "\t\tID:" + node.getID());
+		}
+	}
+}
--- a/src/treecms/proto/test/Test2.java	Sun Oct 17 00:29:56 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,62 +0,0 @@
-package treecms.proto.test;
-
-import java.util.Iterator;
-import java.util.List;
-
-import treecms.proto.api.*;
-import treecms.proto.simple.*;
-import treecms.proto.edit.EditableTreeBuilder;
-import treecms.proto.edit.PreOrderTreeWalkerRecursive;
-
-public class Test2
-{
-	public static void main(String _arg[])
-	{
-		TreeBuilder builder = new SimpleTreeBuilder();
-		Node root = builder.getContents();
-		root.setTitle("root");
-		
-		Node child1 = builder.createNode();
-		child1.setTitle("child1");
-		Node child2 = builder.createNode();
-		child2.setTitle("child2");
-		
-		root.addChild(child1);
-		root.addChild(child2);
-		
-		Node child11 = builder.createNode(); 
-		child11.setTitle("child11");
-		Node child12 = builder.createNode();
-		child12.setTitle("child12");
-		Node child13 = builder.createNode();
-		child13.setTitle("child13");
-		
-		child1.addChild(child11);
-		child1.addChild(child12);
-		child1.addChild(child13);
-		
-		Node child21 = builder.createNode();
-		child21.setTitle("child21");
-		
-		child2.addChild(child21);
-		
-		Node child211 = builder.createNode();
-		child211.setTitle("child211");
-		Node child212 = builder.createNode();
-		child212.setTitle("child212");
-		
-		child21.addChild(child211);
-		child21.addChild(child212);
-		
-		EditableTreeBuilder builder1 = new EditableTreeBuilder(child212,builder);
-		PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder1.getContents());
-		
-		//Iterator<Node> itr = walker.iterator();
-		List<Node> list = builder1.findPathTest(root,child212);
-		Iterator<Node> itr = list.iterator();
-		while(itr.hasNext()){
-			Node node = itr.next();
-			System.out.println(node.getTitle());
-		}
-	}
-}
--- a/src/treecms/proto/test/Test3.java	Sun Oct 17 00:29:56 2010 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,63 +0,0 @@
-package treecms.proto.test;
-
-import java.util.Iterator;
-
-import treecms.proto.edit.*;
-import treecms.proto.api.*;
-import treecms.proto.simple.*;
-
-public class Test3 
-{
-	public static void main(String _arg[])
-	{
-		TreeBuilder builder = new SimpleTreeBuilder();
-		Node root = builder.getContents();
-		root.setTitle("root");
-		
-		Node child1 = builder.createNode();
-		child1.setTitle("+-child1");
-		Node child2 = builder.createNode();
-		child2.setTitle("+-child2");
-		
-		root.addChild(child1);
-		root.addChild(child2);
-		
-		Node child11 = builder.createNode(); 
-		child11.setTitle(" +-child11");
-		Node child12 = builder.createNode();
-		child12.setTitle(" +-child12");
-		Node child13 = builder.createNode();
-		child13.setTitle(" +-child13");
-		
-		child1.addChild(child11);
-		child1.addChild(child12);
-		child1.addChild(child13);
-		
-		Node child21 = builder.createNode();
-		child21.setTitle(" +-child21");
-		
-		child2.addChild(child21);
-		
-		Node child211 = builder.createNode();
-		child211.setTitle("  +-child211");
-		Node child212 = builder.createNode();
-		child212.setTitle("  +-child212");
-		
-		child21.addChild(child211);
-		child21.addChild(child212);
-	
-		//i want to edit child22
-		System.out.println("Path of Root to Target --------------");
-		EditableTreeBuilder builder2 = new EditableTreeBuilder(child212,builder);
-		
-		System.out.println("-----------------------------");
-		
-		//check that tree correctly copied.
-		PreOrderTreeWalkerRecursive walker = new PreOrderTreeWalkerRecursive(builder2.getContents());
-		Iterator<Node> itr = walker.iterator();
-		while(itr.hasNext()){
-			Node node = itr.next();
-			System.out.println(node.getTitle());
-		}
-	}
-}