/*******************************************************************************
 * 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.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.dialogs.ImageBuilder;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.CommandStack;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.jface.window.Window;
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.Label;
import org.eclipse.swt.widgets.Text;

/**
 * The reference attribute page of Image element.
 */
public class ReferencePage extends AttributePage
{

	private Text text;

	private Button button;

	private PropertyProcessor processor, processorURI, processorEmbed,
			processorExpr;

	/**
	 * @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 ReferencePage( Composite parent, int style )
	{
		super( parent, style );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.attributes.page.CategoryTabPage#buildUI()
	 */
	protected void buildUI( )
	{
		setLayout( WidgetUtil.createGridLayout( 2 ) );

		Label sourceLab = new Label( this, SWT.NONE );
		GridData data = new GridData( );
		data.horizontalSpan = 2;
		sourceLab.setLayoutData( data );

		processorEmbed = new PropertyProcessor( ReportDesignConstants.IMAGE_ITEM,
				ImageHandle.IMAGE_NAME_PROP );
		processorURI = new PropertyProcessor( ReportDesignConstants.IMAGE_ITEM,
				ImageHandle.URI_PROP );
		processorExpr = new PropertyProcessor( ReportDesignConstants.IMAGE_ITEM,
				ImageHandle.VALUE_EXPR_PROP );

		sourceLab.setText( Messages.getString( "ReferencePage.Label.Source" ) );// processor.getDisplayName(
		// //$NON-NLS-1$
		// ) );
		// //$NON-NLS-1$

		text = new Text( this, SWT.BORDER | SWT.READ_ONLY );
		data = new GridData( );
		data.widthHint = 400;
		text.setLayoutData( data );

		button = new Button( this, SWT.PUSH );
		button.setText( Messages.getString( "ReferencePage.Button.Edit" ) ); //$NON-NLS-1$

		button.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				CommandStack stack = SessionHandleAdapter.getInstance( )
						.getCommandStack( );
				ImageBuilder dialog = new ImageBuilder( button.getShell( ),
						ImageBuilder.DLG_TITLE_EDIT );
				dialog.setInput( input.get( 0 ) );
				stack.startTrans( Messages.getString( "ImageEditPart.trans.editImage" ) ); //$NON-NLS-1$
				if ( Window.OK == dialog.open( ) )
				{
					stack.commit( );
				}
				else
				{
					stack.rollback( );
				}
			}
		} );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#refreshValues(java.util.Set)
	 */
	protected void refreshValues( Set propertiesSet )
	{
		String source = ( (ImageHandle) input.get( 0 ) ).getSource( );
		if ( source.equals( DesignChoiceConstants.IMAGE_REF_TYPE_EMBED ) )
		{
			processor = processorEmbed;
		}
		else if ( source.equals( DesignChoiceConstants.IMAGE_REF_TYPE_EXPR ) )
		{
			processor = processorExpr;
		}
		else
		{
			processor = processorURI;
		}
		String value = processor.getStringValue( input );
		button.setEnabled( value != null );
		text.setText( value == null ? "" : value ); //$NON-NLS-1$
	}
}