
package jp.riken.brain.ni.samuraigraph.base;

import java.awt.Color;
import java.util.ArrayList;


/**
 *
 */

public abstract class SGDrawingElement
	implements SGIDrawingElementConstants
{

	/**
	 * zoom‚Ì”{—¦
	 */
	protected float mMagnification = 1.0f;


	/**
	 *
	 */
	protected ArrayList mColorList = new ArrayList();



	/**
	 * 
	 */
	protected boolean mVisibleFlag = true;



	/**
	 *
	 */
	public SGDrawingElement()
	{
	}


	/**
	 *
	 */
	public abstract boolean contains( final int x, final int y );



	/**
	 *
	 */
	public boolean zoom( final float ratio )
	{
		this.setMagnification( ratio );
		return true;
	}



	/**
	 * 
	 * @return
	 */
	public boolean isVisible()
	{
		return this.mVisibleFlag;
	}


	/**
	 * 
	 * @param b
	 */
	public void setVisible( final boolean b )
	{
		this.mVisibleFlag = b;
	}



	/**
	 * 
	 */
	public boolean setMagnification( final float mag )
	{
		mMagnification = mag;
		return true;
	}



	/**
	 * 
	 */
	public boolean setColorList( final ArrayList colorList )
	{
		mColorList = new ArrayList(colorList);
		return true;
	}


	/**
	 * 
	 */
	public boolean setColor( final Color color )
	{
		mColorList = new ArrayList();
		mColorList.add(color);
		return true;
	}


	/**
	 * 
	 * @return
	 */
	public boolean addColor( final Color color )
	{
		if( mColorList == null )
		{
			mColorList = new ArrayList();
		}
		mColorList.add(color);
		return true;
	}



	/**
	 * 
	 * @return
	 */
	public ArrayList getColorList()
	{
		return mColorList;
	}


	/**
	 * 
	 */
	public Color getColor( final int index )
	{
		if( mColorList == null )
		{
			return null;
		}

		if( index >= mColorList.size() )
		{
			return null;
		}

		return (Color)mColorList.get(index);
	}


	/**
	 * 
	 */
	public float getMagnification()
	{
		return this.mMagnification;
	}



	/**
	 * 
	 */
	public boolean setProperty( final SGDrawingElement element )
	{
		this.setVisible( element.isVisible() );
		this.setMagnification( element.getMagnification() );
		this.setColorList( element.getColorList() );

		return true;
	}




	/**
	 * 
	 */
	public boolean setProperties( SGProperties p )
	{
//System.out.println("setProperties");

		if( ( p instanceof DrawingElementProperties ) == false ) return false;

		DrawingElementProperties dp = (DrawingElementProperties)p;

		final Boolean b = dp.isVisible();
		if( b==null )
		{
			return false;
		}
		this.setVisible( b.booleanValue() );


		final ArrayList cList = dp.getColorList();
		if( cList==null )
		{
			return false;
		}
		this.setColorList( cList );


		return true;
	}



	/**
	 * 
	 * @return
	 */
	public SGProperties getProperties()
	{
		final DrawingElementProperties p = new DrawingElementProperties();
		if( this.getProperties(p) == false )
		{
			return null;
		}
		return p;
	}



	/**
	 * 
	 * @param p
	 * @return
	 */
	public boolean getProperties( final SGProperties p )
	{
		if( ( p instanceof DrawingElementProperties ) == false )
		{
			return false;
		}
		
		final DrawingElementProperties dp = (DrawingElementProperties)p;
		dp.setVisible( this.isVisible() );
		dp.setColorList( this.getColorList() );

		return true;
	}





	/**
	 * Property of Drawing Element.
	 */
	public static class DrawingElementProperties extends SGProperties
	{

		public static final String[] keys = {
			KEY_DRAWING_ELEMENT_VISIBLE, KEY_DRAWING_ELEMENT_COLORS
		};


		/**
		 * 
		 */
		public DrawingElementProperties()
		{
			super();
		}


		/**
		 * 
		 */
		public boolean equals( final Object obj )
		{

			if( ( obj instanceof DrawingElementProperties ) == false )
			{
				return false;
			}

			DrawingElementProperties p = (DrawingElementProperties)obj;

			for( int ii=0; ii<keys.length; ii++ )
			{
				if( this.getProperty(keys[ii]).equals(p.getProperty(keys[ii])) == false )
				{
					return false;
				}
			}

			return true;
		}


		public Boolean isVisible()
		{
			final String value = this.getProperty(KEY_DRAWING_ELEMENT_VISIBLE);
			return SGUtilityText.getBoolean(value);
		}

		public ArrayList getColorList()
		{
			final String value = this.getProperty(KEY_DRAWING_ELEMENT_COLORS);
			return SGUtilityText.getColorList(value);
		}

		public boolean setVisible( final boolean b )
		{
			this.setProperty( KEY_DRAWING_ELEMENT_VISIBLE, new Boolean(b).toString() );
			return true;
		}


		public boolean setColor( final Color cl )
		{
			if( cl==null )
			{
				new IllegalArgumentException("cl==null");
			}
			ArrayList list = new ArrayList();
			list.add(cl);
			String str = SGUtilityText.getColorListString(list);
			if( str==null )
			{
				new IllegalArgumentException("str==null");
			}
			this.setProperty( KEY_DRAWING_ELEMENT_COLORS, str );
			return true;
		}

		public boolean setColorList( final ArrayList list )
		{
			if( list==null )
			{
				new IllegalArgumentException("list==null");
			}
			if( list.size()==0 )
			{
				new IllegalArgumentException("list.size()==0");
			}
			String str = SGUtilityText.getColorListString(list);
			if( str==null )
			{
				new IllegalArgumentException("str==null");
			}
			this.setProperty( KEY_DRAWING_ELEMENT_COLORS, str );
			return true;
		}

	}

}
