/*******************************************************************************
 * 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.designer.ui.IReportGraphicConstants;
import org.eclipse.birt.report.designer.ui.views.attributes.IPropertyDescriptor;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.LibraryHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The general attribute page of Library element.
 */

public class LibraryPage extends ModulePage
{

	private Text namespace;
	private Label[] labels;

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

	protected void buildUI( )
	{
		super.buildUI( );

		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.REPORT_DESIGN_ELEMENT,
				ReportDesignHandle.ICON_FILE_PROP,
				3,
				500 );
		
		final Text prvImageText;
		prvImageText = (Text) ( (IPropertyDescriptor) propertiesMap.get( ReportDesignHandle.ICON_FILE_PROP ) ).getControl( );

		Button browse = new Button( this, SWT.PUSH );
		browse.setText( Messages.getString( "ReportPage.text.Browse" ) ); //$NON-NLS-1$
		GridData data = new GridData( );
		data.widthHint = Math.max( browse.computeSize( -1, -1 ).x, 60 );
		browse.setLayoutData( data );

		browse.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				FileDialog dlg = new FileDialog( getShell( ) );
				dlg.setText( Messages.getString( "ReportPage.title.setPrewImg" ) ); //$NON-NLS-1$
				dlg.setFilterNames( IReportGraphicConstants.IMAGE_FILTER_NAMES );
				dlg.setFilterExtensions( IReportGraphicConstants.IMAGE_FILTER_EXTS );

				try
				{
					String fileName = dlg.open( );

					if ( fileName == null )
					{
						return;
					}

					prvImageText.setText( fileName );
					prvImageText.setFocus( );
				}
				catch ( Exception e1 )
				{
					return;
				}

			}
		} );
		
		labels = new Label[3];
		labels[0]=WidgetUtil.createHorizontalLine( this, 5 );

		labels[1]=new Label( this, SWT.NONE );
		labels[1].setText( Messages.getString( "LibraryPage.Label.Namespace" ) ); //$NON-NLS-1$
		namespace = new Text( this, SWT.BORDER | SWT.SINGLE | SWT.READ_ONLY );
		GridData gd = new GridData( );
		gd.widthHint = 500;
		gd.horizontalSpan = 3;
		namespace.setLayoutData( gd );

		labels[2]=WidgetUtil.createGridPlaceholder( this, 1, true );
	}

	protected void refreshValues( Set propertiesSet )
	{
		super.refreshValues( propertiesSet );
		boolean visible = false;
		if ( input.size( ) == 1 && input.get( 0 ) instanceof LibraryHandle )
		{
			LibraryHandle handle = (LibraryHandle) input.get( 0 );
			if ( DEUtil.isIncluded( handle ) )
			{
				namespace.setText( handle.getNamespace( ) );
				visible = true;
			}
		}
		setControlVisible( visible );
	}

	private void setControlVisible( boolean visible )
	{
		namespace.setVisible( visible );
		for ( int i = 0; i < labels.length; i++ )
		{
			labels[i].setVisible( visible );
		}
	}

}
