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

/**
 * FSMorphSolidLine defines the width and colour of a line drawn for a morphing
 * shape.
 * 
 * <p>
 * It specifies the width and colour of the line at the start and end of the
 * morphing process. The transparency value for the colour should also be
 * specified. As with the FSSolidLine class all lines are drawn with rounded
 * corners and end caps. Different join and line end styles can be created by
 * drawing very narrow shapes.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSMorphSolidLine_0">startWidth</a></td>
 * <td>The width of the line at the start of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSMorphSolidLine_1">startColor</a></td>
 * <td>The colour applied to the line at the start of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSMorphSolidLine_2">endWidth</a></td>
 * <td>The width of the line at the end of the morphing process.</td>
 * </tr>
 * 
 * <tr>
 * <td><a name="FSMorphSolidLine_3">endColor</a></td>
 * <td>The colour applied to the line at the end of the morphing process.</td>
 * </tr>
 * </table>
 * 
 * <p>
 * The FSDefineMorphShape object support transparent colours so alpha channel
 * information must be specified in the colour objects.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSMorphSolidLine class represents the MorphLineStyle structure from the
 * Macromedia Flash (SWF) File Format Specification. Shape morphing was
 * introduced in Flash 3.
 * </P>
 */
public class FSMorphSolidLine extends FSLineStyle
{
	private int startWidth = 0;

	private int endWidth = 0;

	private FSColor startColor = null;

	private FSColor endColor = null;

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

	/**
	 * Constructs an FSMorphLineStyle object specifying the starting and ending
	 * widths and colours.
	 * 
	 * @param aStartWidth
	 *            the width of the line at the start of the morphing process.
	 * @param anEndWidth
	 *            the width of the line at the end of the morphing process.
	 * @param aStartColor
	 *            the colour of the line at the start of the morphing process.
	 * @param anEndColor
	 *            the colour of the line at the end of the morphing process.
	 */
	public FSMorphSolidLine(int aStartWidth, int anEndWidth,
							FSColor aStartColor, FSColor anEndColor)
	{
		setStartWidth(aStartWidth);
		setEndWidth(anEndWidth);
		setStartColor(aStartColor);
		setEndColor(anEndColor);
	}

	/**
	 * Constructs an FSMorphSolidLine object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSMorphSolidLine object.
	 */
	public FSMorphSolidLine(FSMorphSolidLine obj)
	{
		startWidth = obj.startWidth;
		endWidth = obj.endWidth;
		startColor = new FSColor(obj.startColor);
		endColor = new FSColor(obj.endColor);
	}

	/**
	 * Gets the width of the line at the start of the morphing process.
	 * 
	 * @return the starting width of the line.
	 */
	public int getStartWidth()
	{
		return startWidth;
	}

	/**
	 * Gets the width of the line at the end of the morphing process.
	 * 
	 * @return the ending width of the line.
	 */
	public int getEndWidth()
	{
		return endWidth;
	}

	/**
	 * Gets the colour of the line at the start of the morphing process.
	 * 
	 * @return the starting colour of the line.
	 */
	public FSColor getStartColor()
	{
		return startColor;
	}

	/**
	 * Gets the colour of the line at the end of the morphing process.
	 * 
	 * @return the ending colour of the line.
	 */
	public FSColor getEndColor()
	{
		return endColor;
	}

	/**
	 * Sets the width of the line at the start of the morphing process.
	 * 
	 * @param aNumber
	 *            the starting width of the line.
	 */
	public void setStartWidth(int aNumber)
	{
		startWidth = aNumber;
	}

	/**
	 * Sets the width of the line at the end of the morphing process.
	 * 
	 * @param aNumber
	 *            the ending width of the line.
	 */
	public void setEndWidth(int aNumber)
	{
		endWidth = aNumber;
	}

	/**
	 * Gets the colour of the line at the start of the morphing process.
	 * 
	 * @param aColor
	 *            the starting colour of the line.
	 */
	public void setStartColor(FSColor aColor)
	{
		startColor = aColor;
	}

	/**
	 * Sets the colour of the line at the end of the morphing process.
	 * 
	 * @param aColor
	 *            the ending colour of the line.
	 */
	public void setEndColor(FSColor aColor)
	{
		endColor = aColor;
	}

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

		anObject.startColor = (startColor != null) ? (FSColor) startColor
						.clone() : null;
		anObject.endColor = (endColor != null) ? (FSColor) endColor.clone()
						: null;

		return anObject;
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

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

			result = startWidth == typedObject.startWidth;
			result = result && endWidth == typedObject.endWidth;

			if (startColor != null)
				result = result && startColor.equals(typedObject.startColor);
			else
				result = result && startColor == typedObject.startColor;

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

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "startWidth", startWidth);
			Transform.append(buffer, "endWidth", endWidth);
			Transform.append(buffer, "startColor", startColor, depth);
			Transform.append(buffer, "endColor", endColor, depth);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int length = 4;

		length += startColor.length(coder);
		length += endColor.length(coder);

		return length;
	}

	public void encode(FSCoder coder)
	{
		coder.writeWord(startWidth, 2);
		coder.writeWord(endWidth, 2);
		startColor.encode(coder);
		endColor.encode(coder);
	}

	public void decode(FSCoder coder)
	{
		startWidth = coder.readWord(2, false);
		endWidth = coder.readWord(2, false);
		startColor = new FSColor(coder);
		endColor = new FSColor(coder);
	}
}