/*
 * FSDefineObject.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * The FSDefineObject is a base class for the different types of object used to
 * create images, buttons, shapes and sounds. Each such object contains a unique
 * identifier that is used to reference the object when performing operations
 * such as placing it in the display list using the FSPlaceObject class, etc.
 * The FSDefineObject manages the encoding and decoding of this identifier.
 * 
 * Identifiers are unique within a given movie. The FSMovie class is maintains
 * an internal counter which is used to generate the identifiers that are
 * assigned to classes derived from FSDefineObject.
 * 
 * Two accessor methods, getIdentifier() and setIdentifier() are used to
 * retrieve and update the unique identifier assigned to an object.
 */
public abstract class FSDefineObject extends FSMovieObject
{
	protected int identifier = 0;

	FSDefineObject(int type, int anIdentifier)
	{
		super(type);
		setIdentifier(anIdentifier);
	}

	protected FSDefineObject(FSDefineObject obj)
	{
		super(obj);
		identifier = obj.identifier;
	}

	/**
	 * Returns the unique identifier assigned to this Define tag.
	 * 
	 * @return the unique identifier for the object.
	 */
	public int getIdentifier()
	{
		return identifier;
	}

	/**
	 * Sets the identifier used to uniquely identify the object within a given
	 * FSMovie.
	 * 
	 * @param anIdentifier
	 *            a unique identifier for the object.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		Transform.append(buffer, "identifier", identifier);
	}

	public int length(FSCoder coder)
	{
		super.length(coder);

		length += 2;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.writeWord(identifier, 2);
	}

	public void decode(FSCoder coder)
	{
		super.decode(coder);

		identifier = coder.readWord(2, false);
	}
}
