/*******************************************************************************
 * 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.designer.internal.ui.views.attributes.page;

import java.util.Set;

import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.model.api.TemplateReportItemHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.core.runtime.Platform;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * 
 */

public class TemplateReportItemPage extends AttributePage
{

	private Text descriptionText;

	/**
	 * @param parent
	 * @param style
	 */
	public TemplateReportItemPage( Composite parent, int style )
	{
		super( parent, style );

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#buildUI()
	 */
	protected void buildUI( )
	{
		this.setLayout( WidgetUtil.createGridLayout( 3 ) );

		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.TEMPLATE_REPORT_ITEM,
				TemplateReportItemHandle.NAME_PROP,
				1,
				500 );
		WidgetUtil.createGridPlaceholder( this, 1, true );
		int width;
		if ( Platform.getOS( ).equals( Platform.OS_LINUX ) )
		{
			width = 500;
		}
		else
		{
			width = 480;
		}

		createDescription( this, 1, width );
		WidgetUtil.createGridPlaceholder( this, 1, true );

	}

	private void createDescription( Composite parent, int hSpan, int width )
	{
		Label label = new Label( parent, SWT.NONE );
		GridData gd = new GridData( );
		gd.verticalAlignment = GridData.BEGINNING;
		label.setLayoutData( gd );
		label.setText( Messages.getString( "TemplateReportItemPage.description.Label.Instructions" ) );
		descriptionText = new Text( parent, SWT.MULTI
				| SWT.WRAP
				| SWT.BORDER
				| SWT.H_SCROLL
				| SWT.V_SCROLL );
		gd = new GridData( );
		gd.horizontalSpan = hSpan;
		gd.widthHint = width;
		gd.grabExcessVerticalSpace = true;
		gd.verticalAlignment = GridData.FILL;
		descriptionText.setLayoutData( gd );
		descriptionText.addSelectionListener( new SelectionAdapter( ) {

			public void widgetDefaultSelected( SelectionEvent e )
			{
				setDescription( );
			}
		} );
		descriptionText.addFocusListener( new FocusListener( ) {

			public void focusGained( FocusEvent e )
			{

			}

			public void focusLost( FocusEvent e )
			{
				setDescription( );
			}
		} );

	}

	protected void refreshValues( Set propertiesSet )
	{
		super.refreshValues( propertiesSet );
		if ( descriptionText != null
				&& input.size( ) == 1
				&& input.get( 0 ) instanceof TemplateReportItemHandle )
		{
			TemplateReportItemHandle handle = (TemplateReportItemHandle) input.get( 0 );
			String desc = "";
			desc = ( handle.getDescription( ) == null ) ? ""
					: handle.getDescription( ).trim( );
			descriptionText.setText( desc );
		}
	}

	private void setDescription( )
	{
		if ( descriptionText != null
				&& input.size( ) == 1
				&& input.get( 0 ) instanceof TemplateReportItemHandle )
		{
			TemplateReportItemHandle handle = (TemplateReportItemHandle) input.get( 0 );
			try
			{
				String desc = descriptionText.getText( ).trim( );
				handle.setDescription( desc );
				descriptionText.setText( desc );

			}
			catch ( SemanticException e1 )
			{
				e1.printStackTrace( );
			}
		}
	}

}
