view src/treecms/proto/edit/EditableTreeBuilder.java @ 26:9b91329e8a04

commit for zemi , doent move
author ShoshiTAMAKI
date Tue, 02 Nov 2010 18:44:21 +0900
parents cce963b8a4fd
children
line wrap: on
line source

package treecms.proto.edit;

import java.util.LinkedList;
import java.util.List;
import treecms.proto.api.Node;
import treecms.proto.api.TreeBuilder;
import treecms.proto.simple.SimpleTreeBuilder;

public class EditableTreeBuilder extends SimpleTreeBuilder
{
	private Node m_newRoot;
	private Node m_target; //node that wanted to edit.
	
	public EditableTreeBuilder(Node _target,TreeBuilder _builder)
	{
		//search path
		LinkedList<Node> path = findPath(_builder.getContents(),_target);
		
		//clone root node.
		m_newRoot = cloneNode(path.poll());
		
		cloneTree(path,m_newRoot.getChildList());
	}
	
	public Node getTargetNode()
	{
		return m_target;
	}
	
	private void cloneTree(LinkedList<Node> _path,List<Node> _children)
	{
		Node target = _path.poll();
		for(int i = 0;i < _children.size();i ++){
			Node _child = _children.get(i);
			if(_child == target){
				//clone node
				Node newNode = cloneNode(target);
				m_target = newNode; // look out!!
				
				//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)
	{
		return findPath(_root,_child);
	}
	
	private LinkedList<Node> findPath(Node _root,Node _child)
	{
		LinkedList<Node> list = new LinkedList<Node>();
		findPath(_root,_child,list);
		list.addFirst(_root);
		return list;
	}
	
	private boolean findPath(Node _root,Node _child,LinkedList<Node> _list)
	{
		if(_root == _child){
			return true;
		}
		
		for(Node child : _root.getChildList()){
			if(findPath(child,_child,_list)){
				_list.addFirst(child);
				return true;
			}
		}
		return false;
	}

	@Override
	public Node getContents()
	{
		// TODO Auto-generated method stub
		return m_newRoot;
	}
}