/*******************************************************************************
 * 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.io.ByteArrayOutputStream;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.ui.dialogs.ThumbnailBuilder;
import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

/**
 * The general attribute page of Report element.
 */
public class ReportPage extends ModulePage
{

	/**
	 * Constructor
	 * 
	 * @param parent
	 *            A widget which will be the parent of the new instance (cannot
	 *            be null)
	 * @param style
	 *            The style of widget to construct
	 */
	public ReportPage( 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( )
	{

		super.buildUI( );

		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.REPORT_DESIGN_ELEMENT,
				ReportDesignHandle.LAYOUT_PREFERENCE_PROP,
				3,
				480 );
		
		WidgetUtil.createGridPlaceholder(this, 1, true);
		
		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.REPORT_DESIGN_ELEMENT,
				ReportDesignHandle.ICON_FILE_PROP,
				1,
				330 );

		final Text prvImageText;
		prvImageText = (Text) ( (IPropertyDescriptor) propertiesMap.get( ReportDesignHandle.ICON_FILE_PROP ) ).getControl( );

		Button thumbnail = new Button( this, SWT.PUSH );
		thumbnail.setText( "..." ); //$NON-NLS-1$
		GridData data = new GridData( );
		data.widthHint = Math.max( thumbnail.computeSize( -1, -1 ).x, 60 );
		data.horizontalAlignment = GridData.END;
		thumbnail.setLayoutData( data );
		thumbnail.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				ThumbnailBuilder dialog = new ThumbnailBuilder( );
				dialog.setImageName( prvImageText.getText( ) );
				ReportDesignHandle handle = (ReportDesignHandle) SessionHandleAdapter.getInstance( )
						.getReportDesignHandle( );
				dialog.setReportDesignHandle( handle );
				if ( dialog.open( ) != Dialog.OK )
				{
					Image image = dialog.getImage( );
					if ( image != null )
					{
						image.dispose( );
						image = null;
					}
					return;
				}
				if ( dialog.shouldSetThumbnail( ) )
				{
					Image image = dialog.getImage( );
					ImageData imageData = image.getImageData( );
					ImageLoader imageLoader = new ImageLoader( );
					imageLoader.data = new ImageData[1];
					imageLoader.data[0] = imageData;
					ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
					imageLoader.save( outputStream, SWT.IMAGE_BMP );
					try
					{
						handle.setThumbnail( outputStream.toByteArray( ) );
					}
					catch ( SemanticException e1 )
					{
						ExceptionHandler.handle( e1 );
					}
					if ( image != null )
					{
						image.dispose( );
						image = null;
					}
					prvImageText.setText( dialog.getImageName( ) );
					prvImageText.setFocus( );

				}
				else
				{

					if ( handle.getThumbnail( ) != null
							&& handle.getThumbnail( ).length != 0 )
					{
						try
						{
							handle.deleteThumbnail( );
						}
						catch ( SemanticException e1 )
						{
							ExceptionHandler.handle( e1 );
						}

					}
					prvImageText.setText( "" );
					prvImageText.setFocus( );
				}
			}
		} );

	}
}