
/*
 * Copyright (C) 2002 - 2007 Lehrstuhl Grafische Systeme, BTU Cottbus
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package de.grogra.c4d.model;


import de.grogra.c4d.BaseObject;
import de.grogra.reflect.Type;
import de.grogra.xl.impl.base.EdgeIterator;
import de.grogra.xl.query.EdgeDirection;
import de.grogra.xl.util.ObjectList;

public class RuntimeModel extends de.grogra.xl.impl.base.RuntimeModel
{
	public static final int SIBLING = MIN_USER_EDGE;
	static final int CHILD = BRANCH_EDGE | SUCCESSOR_EDGE;

	@Override
	public Class getNodeType ()
	{
		return BaseObject.class;
	}


	@Override
	public void addEdgeBits (Object source, Object target, int bits)
	{
		if (!C4D.addEdgeBits((BaseObject) source, (BaseObject) target, bits))
		{
			System.out.println ("Cannot make " + source + " and " + target + " siblings");
		}
	}


	@Override
	public int getEdgeBits (Object source, Object target)
	{
		return C4D.getEdgeBits((BaseObject) source, (BaseObject) target);
	}


	public boolean isWrapperFor (Object object, Type type)
	{
		return false;
	}

	public Object unwrapObject (Object wrapper)
	{
		return null;
	}

	final ObjectList iterators = new ObjectList ();
	
	static final int START = -1;
	static final int PARENT = 0;
	static final int PREV = 1;
	static final int NEXT = 2;
	static final int CHILDREN = 3;

	private final class Iterator extends EdgeIterator
	{
		BaseObject node;
		private int state;
		private boolean children;
		private BaseObject child;
		private EdgeDirection direction;
		
		@Override
		public void moveToNext ()
		{
			while (true)
			{
				switch (++state)
				{
					case PARENT:
						if (direction.contains (EdgeDirection.BACKWARD))
						{
							source = node.getUp ();
							if (source != null)
							{
								target = node;
								edgeBits = RuntimeModel.CHILD;
								return;
							}
						}
						break;
					case PREV:
						if (direction.contains (EdgeDirection.BACKWARD))
						{
							source = node.getPred ();
							if (source != null)
							{
								target = node;
								edgeBits = RuntimeModel.SIBLING;
								return;
							}
						}
						break;
					case NEXT:
						if (direction.contains (EdgeDirection.FORWARD))
						{
							target = node.getNext ();
							if (target != null)
							{
								source = node;
								edgeBits = RuntimeModel.SIBLING;
								return;
							}
						}
						break;
					default:
						if (children)
						{
							if (child == null)
							{
								child = node.getDown ();
								source = node;
								edgeBits = RuntimeModel.CHILD;
							}
							else
							{
								child = child.getNext();
							}
							if (child != null)
							{
								target = child;
								return;
							}
							children = false;
						}
						return;
				}
			}
		}
		
		
		@Override
		public boolean hasEdge ()
		{
			return (state < CHILDREN) || children; 
		}
		
		
		@Override
		public void dispose ()
		{
			child = null;
			source = null;
			target = null;
			synchronized (RuntimeModel.this)
			{
				node = null;
				iterators.push (this);
			}
		}
	}

	@Override
	public synchronized EdgeIterator createEdgeIterator (Object node, EdgeDirection dir)
	{
		Iterator i = iterators.isEmpty () ? new Iterator () : (Iterator) iterators.pop ();
		i.node = (BaseObject) node;
		i.state = START;
		i.children = dir.contains (EdgeDirection.FORWARD);
		i.child = null;
		i.direction = dir;
		i.moveToNext ();
		return i;
	}

}
