/*******************************************************************************
 * Copyright (c) 2004 Actuate Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Actuate Corporation  - initial API and implementation
 *******************************************************************************/

package org.eclipse.birt.report.model.command;

import java.util.List;

import org.eclipse.birt.report.model.activity.SimpleRecord;
import org.eclipse.birt.report.model.api.activity.NotificationEvent;
import org.eclipse.birt.report.model.api.command.PropertyEvent;
import org.eclipse.birt.report.model.core.DesignElement;
import org.eclipse.birt.report.model.i18n.MessageConstants;
import org.eclipse.birt.report.model.i18n.ModelMessages;
import org.eclipse.birt.report.model.metadata.PropertyDefn;

/**
 * Records adding or removing a simple value item from a property list.
 * 
 */

public class SimplePropertyListRecord extends SimpleRecord
{

	/**
	 * The element that contains the property list.
	 */

	protected DesignElement element = null;

	/**
	 * Reference to the property list.
	 */

	protected PropertyDefn prop = null;

	/**
	 * The property list itself.
	 */

	protected List list = null;

	/**
	 * The item to add or remove.
	 */

	protected Object value = null;

	/**
	 * The position where the item lies in the list.
	 */

	protected int posn = -1;

	/**
	 * Whether the operation is an add or remove.
	 */

	protected final boolean isAdd;

	/**
	 * Constructor for an add operation.
	 * 
	 * @param obj
	 *            the element that contains the property list
	 * @param prop
	 *            definition to the simple value property list
	 * @param theList
	 *            the property list itself
	 * @param item
	 *            the value item to add
	 * @param posn
	 *            the position where to insert the value item
	 */

	public SimplePropertyListRecord( DesignElement obj, PropertyDefn prop,
			List theList, Object item, int posn )
	{
		assert obj != null;
		assert prop != null;
		assert theList != null;
		assert item != null;
		assert posn >= 0 && posn <= theList.size( );

		assert obj.getPropertyDefn( prop.getName( ) ) == prop;

		this.element = obj;
		this.prop = prop;
		this.list = theList;
		this.value = item;
		this.posn = posn;
		this.isAdd = true;

	}

	/**
	 * Constructor for a remove operation. Removes the item given by the member
	 * reference.
	 * 
	 * @param obj
	 *            the element that contains the property list
	 * @param prop
	 *            definition to the simple value property list
	 * @param theList
	 *            the property list itself
	 * @param posn
	 *            the posn where the item to remove lies in the list
	 */

	public SimplePropertyListRecord( DesignElement obj, PropertyDefn prop,
			List theList, int posn )
	{
		assert obj != null;
		assert prop != null;
		assert theList != null;
		assert posn >= 0 && posn < theList.size( );

		assert obj.getPropertyDefn( prop.getName( ) ) == prop;

		this.element = obj;
		this.prop = prop;
		this.list = theList;
		this.isAdd = false;
		this.posn = posn;

		this.value = list.get( posn );

		label = ModelMessages.getMessage(
				MessageConstants.CHANGE_PROPERTY_MESSAGE, new String[]{prop
						.getDisplayName( )} );

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.design.core.activity.SimpleRecord#perform(boolean)
	 */

	protected void perform( boolean undo )
	{
		boolean doAdd = ( undo && !isAdd || !undo && isAdd );
		if ( doAdd )
		{
			list.add( posn, value );
		}
		else
		{
			list.remove( posn );
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.design.core.activity.AbstractElementRecord#getTarget()
	 */

	public DesignElement getTarget( )
	{
		return element;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.model.design.core.AbstractElementRecord#getEvent()
	 */

	public NotificationEvent getEvent( )
	{
		// Use the same notification for the done/redone and undone states.

		return new PropertyEvent( element, prop.getName( ) );
	}
}
