/*******************************************************************************
 * 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 org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.PropertyDescriptorFactory;
import org.eclipse.birt.report.designer.ui.ReportPlatformUIImages;
import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.MasterPageHandle;
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.Label;

/**
 * The margins attribute page of Marster-Page element.
 */
public class MarginsPage extends AttributePage
{

	/**
	 * 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 MarginsPage( 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( 8 ) );

		String[] properties = {MasterPageHandle.LEFT_MARGIN_PROP,
				MasterPageHandle.TOP_MARGIN_PROP, MasterPageHandle.RIGHT_MARGIN_PROP,
				MasterPageHandle.BOTTOM_MARGIN_PROP};

		for ( int i = 0; i < properties.length; i++ )
		{
			Label imageLabel = new Label( this, SWT.NONE );
			imageLabel.setImage( ReportPlatformUIImages
					.getImage( properties[i] ) );
			GridData data = new GridData( );
			data.verticalSpan = 3;
			imageLabel.setLayoutData( data );
			IPropertyDescriptor descriptor = PropertyDescriptorFactory
					.getPropertyDescriptor(
							ReportDesignConstants.MASTER_PAGE_ELEMENT,
							properties[i] );
			propertiesMap.put( properties[i], descriptor );
			Label label = new Label( this, SWT.NONE );
			label.setText( getDisplayName( descriptor ) );
		}
		Object[] descriptors = new Object[properties.length];
		for ( int i = 0; i < properties.length; i++ )
		{
			descriptors[i] = propertiesMap.get( properties[i] );
		}
		for ( int i = 0; i < descriptors.length; i++ )
		{
			IPropertyDescriptor descriptor = (IPropertyDescriptor) descriptors[i];
			descriptor.createControl( this );
		}
	}

	private String getDisplayName( IPropertyDescriptor descriptor )
	{
		String name = null;
		PropertyProcessor processor = null;
		if ( descriptor != null )
			processor = descriptor.getPropertyProcessor( );
		if ( processor != null )
			name = processor.getDisplayName( );
		return name == null ? "" : name;//$NON-NLS-1$

	}
}