/*******************************************************************************
 * 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 org.eclipse.birt.report.model.activity.SimpleRecord;
import org.eclipse.birt.report.model.api.activity.NotificationEvent;
import org.eclipse.birt.report.model.api.command.StyleEvent;
import org.eclipse.birt.report.model.core.DesignElement;
import org.eclipse.birt.report.model.core.StyleElement;
import org.eclipse.birt.report.model.core.StyledElement;
import org.eclipse.birt.report.model.i18n.MessageConstants;
import org.eclipse.birt.report.model.metadata.ElementRefValue;
import org.eclipse.birt.report.model.util.CommandLabelFactory;

/**
 * Records a change to the style of an element.
 * 
 */

public class StyleRecord extends SimpleRecord
{

	/**
	 * The element to change.
	 */

	protected StyledElement element = null;

	/**
	 * The old style. Can be null.
	 */

	protected Object oldStyle = null;

	/**
	 * The new style. Can be null.
	 */

	// protected StyleElement newStyle = null;
	protected ElementRefValue newStyle = null;

	/**
	 * Constructor.
	 * 
	 * @param obj
	 *            the element to modify.
	 * @param newStyle
	 *            the style to set.
	 */

	public StyleRecord( StyledElement obj, ElementRefValue newStyle )
	{
		assert obj != null;

		element = obj;
		this.newStyle = newStyle;

		if ( element.getStyle( ) != null )
			oldStyle = element.getStyle( );
		else
			oldStyle = element.getStyleName( );

		label = CommandLabelFactory
				.getCommandLabel( MessageConstants.SET_STYLE_MESSAGE );
	}

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

	protected void perform( boolean undo )
	{
		if ( undo )
		{
			if ( oldStyle instanceof String )
				element.setStyleName( (String) oldStyle );
			else
				element.setStyle( (StyleElement) oldStyle );
		}
		else
		{
			StyleElement tmpStyle = newStyle == null
					? null
					: (StyleElement) newStyle.getElement( );
			if ( tmpStyle != null )
			{
				element.setStyle( tmpStyle );
			}
			else
			{
				element.setStyleName( newStyle.getName( ) );
			}
		}
	}

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

	public DesignElement getTarget( )
	{
		return element;
	}

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

	public NotificationEvent getEvent( )
	{
		return new StyleEvent( element );
	}

}
@@ -2350,70 +2350,116 @@
 %09%09%7B%0A
+%0A
 %09%09%09
-StyleE
+if ( newStyle == null )%0A%09%09%09%7B%0A%09%09%09%09e
 lement
- tmp
+.set
 Style
- = newStyle ==
+(
  null
+ );
 %0A%09%09%09
-%09%09? null%0A%09%09%09%09%09:
+%7D%0A%09%09%09else%0A%09%09%09%7B%0A%09%09%09%09StyleElement tmpStyle =
  (St
@@ -2493,24 +2493,25 @@
 ment( );%0A%09%09%09
+%09
 if ( tmpStyl
@@ -2514,32 +2514,33 @@
 Style != null )%0A
+%09
 %09%09%09%7B%0A%09%09%09%09element
@@ -2524,32 +2524,33 @@
 ull )%0A%09%09%09%09%7B%0A%09%09%09%09
+%09
 element.setStyle
@@ -2562,34 +2562,36 @@
 Style );%0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09else%0A
 %09%09%09%7B%0A%09%09%09%09ele
@@ -2570,32 +2570,33 @@
 %0A%09%09%09%09%7D%0A%09%09%09%09else%0A
+%09
 %09%09%09%7B%0A%09%09%09%09element
@@ -2592,52 +2592,170 @@
 %09%09%09%09
-element.setStyleName( newStyle.getName( ) );
+%09if ( newStyle.getName( ) != null )%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09element.setStyleName( newStyle.getName( ) );%0A%09%09%09%09%09%7D%0A%09%09%09%09%09else%0A%09%09%09%09%09%7B%0A%09%09%09%09%09%09element.setStyle( null );%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D
 %0A%09%09%09

Fix Table deletion and other multiple operations return class java.lang.NullPointerException (T51292)