/*******************************************************************************
 * 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.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

public abstract class GeneralPage extends AttributePage
{

	private GridLayout layout;
	private Composite contextControl;
	private Text pathText;

	public GeneralPage( Composite parent, int style )
	{
		super( parent, style );
	}

	public void setInput( List elements )
	{
		super.setInput( elements );

		/*
		 * refresh context control,if input elements come from library ,show it,
		 * else hide it.
		 */
		if ( contextControl != null )
		{
			if ( DEUtil.getGroupElementHandle( elements ).isExtendedElements( ) )
			{
				DesignElementHandle handle = (DesignElementHandle) DEUtil.getGroupElementHandle( elements )
						.getElements( )
						.get( 0 );
				try
				{
					File libraryFile = new File( new URL( handle.getExtends( )
							.getRoot( )
							.getFileName( ) ).getFile( ) );
					if ( libraryFile.exists( ) )
						pathText.setText( libraryFile.getAbsolutePath( ) );
				}
				catch ( MalformedURLException e )
				{
					e.printStackTrace( );
				}
				contextControl.setVisible( true );
				GridData data = new GridData( GridData.FILL_HORIZONTAL );
				data.horizontalSpan = 5;
				contextControl.setLayoutData( data );
				this.layout( true );
			}
			else
			{
				contextControl.setVisible( false );
				GridData data = (GridData) contextControl.getLayoutData( );
				data.heightHint = 0;
				contextControl.setLayoutData( data );
				this.layout( true );
			}
		}

	}

	/*
	 * Build Library Control
	 */
	protected void buildUI( )
	{
		setLayout( WidgetUtil.createSpaceGridLayout( 1, 0 ) );

		contextControl = WidgetUtil.buildGridComposite( this, 1, true );
		GridLayout gridLayout = WidgetUtil.createGridLayout( 5 );
		gridLayout.marginHeight = 0;
		gridLayout.marginTop = WidgetUtil.MARGIN_HEIGHT;
		contextControl.setLayout( gridLayout );

		Label pathLabel = new Label( contextControl, SWT.NONE );
		pathLabel.setText( Messages.getString( "GeneralPage.Library.Included" ) );

		pathText = new Text( contextControl, SWT.BORDER | SWT.READ_ONLY );
		GridData gridData = new GridData( );
		gridData.horizontalSpan = 2;
		pathText.setLayoutData( gridData );

		WidgetUtil.createGridPlaceholder( contextControl, 2, true );
		WidgetUtil.createHorizontalLine( contextControl, 5 );

		Composite composite = WidgetUtil.buildGridComposite( this, 1, true );
		composite.setLayout( layout );

		buildContent( composite );

	}

	/**
	 * Builds UI content of this page.
	 * 
	 * @param content
	 *            parent composite.
	 */
	abstract void buildContent( Composite content );

	/**
	 * Set layout of the content.
	 * 
	 * @param layout
	 *            layout of the content.
	 */
	public void setContentLayout( GridLayout layout )
	{
		this.layout = layout;
	}

}
