/*******************************************************************************
 * 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.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.model.api.MasterPageHandle;
import org.eclipse.birt.report.model.api.SimpleMasterPageHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;

/**
 * The general attribute page of MasterPage element.
 */
public class MasterPageGeneralPage extends AttributePage
{

	private Composite widthPane, heightPane;

	private IPropertyDescriptor pageSizeDescriptor;

	/**
	 * @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 MasterPageGeneralPage( 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( )
	{
		setLayout( WidgetUtil.createGridLayout( 5 ) );

		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.MASTER_PAGE_ELEMENT,
				MasterPageHandle.NAME_PROP, 1, false );

		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BACKGROUND_COLOR_PROP, 1, false );
		
		WidgetUtil.createGridPlaceholder( this, 1, true );

		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.SIMPLE_MASTER_PAGE_ELEMENT,
				SimpleMasterPageHandle.HEADER_HEIGHT_PROP, 1, false );
		
		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.SIMPLE_MASTER_PAGE_ELEMENT,
				SimpleMasterPageHandle.FOOTER_HEIGHT_PROP, 1, false );

		WidgetUtil.createGridPlaceholder( this, 1, true );

		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.MASTER_PAGE_ELEMENT,
				MasterPageHandle.ORIENTATION_PROP, 1, false );

		Label separator = new Label( this, SWT.SEPARATOR | SWT.HORIZONTAL );
		GridData data = new GridData( );
		data.horizontalSpan = 5;
		data.grabExcessHorizontalSpace = false;
		data.horizontalAlignment = GridData.FILL;
		separator.setLayoutData( data );

		WidgetUtil.buildGridControl( this, propertiesMap,
				ReportDesignConstants.MASTER_PAGE_ELEMENT,
				MasterPageHandle.TYPE_PROP, 1, false );
		pageSizeDescriptor = (IPropertyDescriptor) propertiesMap
				.get( MasterPageHandle.TYPE_PROP );
		
		WidgetUtil.createGridPlaceholder( this, 3, false );
		
		widthPane = (Composite) WidgetUtil.buildGridControl( this,
				propertiesMap, ReportDesignConstants.MASTER_PAGE_ELEMENT,
				MasterPageHandle.WIDTH_PROP, 1, false );

		heightPane = (Composite) WidgetUtil.buildGridControl( this,
				propertiesMap, ReportDesignConstants.MASTER_PAGE_ELEMENT,
				MasterPageHandle.HEIGHT_PROP, 1, false );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#refreshValues(java.util.Set)
	 */
	protected void refreshValues( Set propertiesSet )
	{
		propertiesSet.add( MasterPageHandle.WIDTH_PROP );
		propertiesSet.add( MasterPageHandle.HEIGHT_PROP );
		super.refreshValues( propertiesSet );

		String value = pageSizeDescriptor.getPropertyProcessor( )
				.getStringValue( input );
		if ( DesignChoiceConstants.PAGE_SIZE_CUSTOM.equals( value ) )
		{
			setSizeEnable( true );
		}
		else
		{
			setSizeEnable( false );
		}
	}

	/**
	 * Sets enable status for PageSize-relative controls.
	 * 
	 * @param enable
	 *            The boolean value for enable status
	 */
	private void setSizeEnable( boolean enable )
	{
		Control[] children = widthPane.getChildren( );
		for ( int i = 0; i < children.length; i++ )
		{
			children[i].setEnabled( enable );
		}
		children = heightPane.getChildren( );
		for ( int i = 0; i < children.length; i++ )
		{
			children[i].setEnabled( enable );
		}
		IPropertyDescriptor descriptor = (IPropertyDescriptor) propertiesMap
				.get( MasterPageHandle.ORIENTATION_PROP );
		descriptor.getControl( ).setEnabled( !enable );
	}
}