/*
 * FSButtonColorTransform.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;

/**
 * FSButtonColorTransform defines the colour transform that is applied to each
 * shape that is used to draw a button.
 * 
 * <p>
 * This class is only used in conjunction with FSDefineButton. FSDefineButton2
 * allows colour transforms to be specified in the FSButtonRecord object that
 * identifies each shape that is displayed for a given button state.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>the identifier for the FSDefineButton or FSDefineButton2 object that
 * defines the appearance and behaviour of a button.</td>
 * </tr>
 * 
 * <tr>
 * <td>colorTransform</td>
 * <td>An FSColorTransform object that describes how the colours are changed.</td>
 * </tr>
 * 
 * </table>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSButtonColorTransform class represents the DefineButtonCXForm structure
 * from the Macromedia Flash (SWF) File Format Specification. It was introduced
 * in Flash 2.
 * </p>
 */
public class FSButtonColorTransform extends FSMovieObject
{
	private int identifier = 0;
	private FSColorTransform colorTransform = null;

	/**
	 * Construct an FSButtonColorTransform object, initialising it with values
	 * decoded from an encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSButtonColorTransform(FSCoder coder)
	{
		super(ButtonColorTransform);
		decode(coder);
	}

	/**
	 * Constructs an FSButtonColorTransform object with a colour transform for
	 * the specified button.
	 * 
	 * @param anIdentifier
	 *            the identifier of a button that this FSButtonColorTransform
	 *            applies to.
	 * @param aColorTransform
	 *            an FSColorTransform object that will be applied to the button.
	 */
	public FSButtonColorTransform(int anIdentifier, FSColorTransform aColorTransform)
	{
		super(ButtonColorTransform);
		setIdentifier(anIdentifier);
		setColorTransform(aColorTransform);
	}

	/**
	 * Constructs an FSButtonColorTransform object by copying values from an
	 * existing object.
	 * 
	 * @param obj
	 *            an FSButtonColorTransform object.
	 */
	public FSButtonColorTransform(FSButtonColorTransform obj)
	{
		super(obj);

		identifier = obj.identifier;
		colorTransform = new FSColorTransform(obj.colorTransform);
	}

	/**
	 * Gets the identifier of the button that this FSButtonColorTransform object
	 * applies to.
	 * 
	 * @return the identifier of the button that the transform will be applied
	 *         to.
	 */
	public int getIdentifier()
	{
		return identifier;
	}

	/**
	 * Gets the colour transform that will be applied to the FSDefineButton
	 * object.
	 * 
	 * @return the colour transform that will be applied to the button.
	 */
	public FSColorTransform getColorTransform()
	{
		return colorTransform;
	}

	/**
	 * Sets the identifier of the button that the transform applies to.
	 * 
	 * @param anIdentifier
	 *            the identifier of the FSDefineButton object that the transform
	 *            will be applied to.
	 */
	public void setIdentifier(int anIdentifier)
	{
		identifier = anIdentifier;
	}

	/**
	 * Sets the colour transform.
	 * 
	 * @param aTransform
	 *            the colour transform object that will be applied to the
	 *            button.
	 */
	public void setColorTransform(FSColorTransform aTransform)
	{
		colorTransform = aTransform;
	}

	public Object clone()
	{
		FSButtonColorTransform anObject = (FSButtonColorTransform) super
						.clone();

		anObject.colorTransform = (colorTransform != null) ? (FSColorTransform) colorTransform
						.clone()
						: null;

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSButtonColorTransform typedObject = (FSButtonColorTransform) anObject;

			result = identifier == typedObject.identifier;

			if (colorTransform == null)
				result = result && (typedObject.colorTransform == null);
			else
				result = result
								&& colorTransform
												.equals(typedObject.colorTransform);
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "identifier", identifier);
			Transform.append(buffer, "colorTransform", colorTransform, depth);
			buffer.append("};");
		}
	}

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

		length += 2;
		length += colorTransform.length(coder);

		return length;
	}

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

		coder.writeWord(identifier, 2);
		colorTransform.encode(coder);
		coder.endObject(name());
	}

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

		identifier = coder.readWord(2, false);
		colorTransform = new FSColorTransform(coder);
		coder.endObject(name());
	}
}
