/*******************************************************************************
 * 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.engine.content.impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.eclipse.birt.core.util.IOUtil;
import org.eclipse.birt.report.engine.content.IContent;
import org.eclipse.birt.report.engine.content.IContentVisitor;
import org.eclipse.birt.report.engine.content.ILabelContent;
import org.eclipse.birt.report.engine.ir.LabelItemDesign;

public class LabelContent extends TextContent implements ILabelContent
{

	protected String helpTextKey;
	protected String labelText;
	protected String labelTextKey;
	
	LabelContent(ILabelContent label)
	{
		super(label);
		this.helpText = label.getHelpText( );
		this.labelTextKey = label.getHelpKey( );
		this.helpTextKey = label.getHelpKey( );
		this.labelText = label.getLabelText( );
	}

	public int getContentType( )
	{
		return LABEL_CONTENT;
	}

	LabelContent( ReportContent report )
	{
		super( report );
	}

	LabelContent( IContent content )
	{
		super( content );
	}

	public void setHelpText( String helpText )
	{
		this.helpText = helpText;
	}

	public String getHelpText( )
	{
		if ( helpText == null )
		{
			if ( generateBy instanceof LabelItemDesign )
			{
				return ( (LabelItemDesign) generateBy ).getHelpText( );
			}
		}
		return helpText;
	}

	public void setHelpKey( String helpKey )
	{
		this.helpTextKey = helpKey;
	}

	public String getHelpKey( )
	{
		if ( helpTextKey == null )
		{
			if ( generateBy instanceof LabelItemDesign )
			{
				return ( (LabelItemDesign) generateBy ).getHelpTextKey( );
			}
		}
		return helpTextKey;
	}

	public void setLabelText( String labelText )
	{
		this.labelText = labelText;
	}

	public String getLabelText( )
	{
		if ( labelText == null )
		{
			if ( generateBy instanceof LabelItemDesign )
			{
				return ( (LabelItemDesign) generateBy ).getText( );
			}
		}
		return labelText;
	}

	public void setLabelKey( String labelKey )
	{
		this.labelTextKey = labelKey;
	}

	public String getLabelKey( )
	{
		if ( labelTextKey == null )
		{
			if ( generateBy instanceof LabelItemDesign )
			{
				return ( (LabelItemDesign) generateBy ).getTextKey( );
			}
		}
		return labelTextKey;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.engine.content.impl.AbstractContent#accept(org.eclipse.birt.report.engine.content.IContentVisitor)
	 */
	public Object accept( IContentVisitor visitor, Object value )
	{
		return visitor.visitLabel( this, value );
	}

	static final protected short FIELD_HELPTEXTKEY = 600;
	static final protected short FIELD_LABELTEXT = 601;
	static final protected short FIELD_LABELTEXTKEY = 602;

	protected void writeFields( DataOutputStream out ) throws IOException
	{
		super.writeFields( out );
		if ( helpTextKey != null )
		{
			IOUtil.writeShort( out, FIELD_HELPTEXTKEY );
			IOUtil.writeString( out, helpTextKey );
		}
		if ( labelText != null )
		{
			IOUtil.writeShort( out, FIELD_LABELTEXT );
			IOUtil.writeString( out, labelText );
		}
		if ( labelTextKey != null )
		{
			IOUtil.writeShort( out, FIELD_LABELTEXTKEY );
			IOUtil.writeString( out, labelTextKey );
		}
	}

	public boolean needSave( )
	{
		if ( helpTextKey != null )
		{
			return true;
		}
		if ( labelText != null ||

		labelTextKey != null )
		{
			return true;
		}
		return super.needSave( );
	}

	protected void readField( int version, int filedId, DataInputStream in,
			ClassLoader loader ) throws IOException
	{
		switch ( filedId )
		{
			case FIELD_HELPTEXTKEY :
				helpTextKey = IOUtil.readString( in );
				break;
			case FIELD_LABELTEXT :
				labelText = IOUtil.readString( in );
				break;
			case FIELD_LABELTEXTKEY :
				labelTextKey = IOUtil.readString( in );
				break;
			default :
				super.readField( version, filedId, in, loader );
		}
	}
	
	protected IContent cloneContent()
	{
		return new LabelContent(this);
	}
}

@@ -1175,16 +1175,68 @@
 label);%0A
+%09%09LabelContent originalLabel = (LabelContent)label;%0A
 %09%09this.h
@@ -1249,28 +1249,30 @@
 t = 
-l
+originalL
 abel.
-getH
+h
 elpText
-( )
 ;%0A%09%09
@@ -1295,69 +1295,82 @@
 y = 
-label.getHelpKey( );%0A%09%09this.helpTextKey = label.getHelpKey( )
+originalLabel.labelTextKey;%0A%09%09this.helpTextKey = originalLabel.helpTextKey
 ;%0A%09%09
@@ -1390,26 +1390,31 @@
 t = 
-l
+originalL
 abel.
-getL
+l
 abelText
 ( );
@@ -1409,19 +1409,16 @@
 abelText
-( )
 ;%0A%09%7D%0A%0A%09p

Fix bug 11282 : failed to edit label text in master page.