/*******************************************************************************
 * 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.widget;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.dialogs.ResourceEditDialog;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.page.WidgetUtil;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.ReportPlugin;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.IResourceLocator;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.util.MessageFileUtil;
import org.eclipse.birt.report.model.i18n.ThreadResources;
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.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;

/**
 * The descriptor to control resource keys
 */
public class ResourceKeyDescriptor extends AbstractPropertyDescriptor
{

	private Text text;

	private Button btnBrowse, btnReset;

	private String oldValue;

	/**
	 * The constructor.
	 * 
	 * @param propertyProcessor
	 */
	public ResourceKeyDescriptor( PropertyProcessor propertyProcessor )
	{
		super( propertyProcessor );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.PropertyDescriptor#resetUIData()
	 */
	public void resetUIData( )
	{
		oldValue = getPropertyProcessor( ).getStringValue( getModelList( ) );

		String baseName = SessionHandleAdapter.getInstance( )
				.getReportDesignHandle( )
				.getIncludeResource( );

		btnBrowse.setEnabled( baseName != null );
		text.setEnabled( baseName != null );

		text.setText( DEUtil.resolveNull( oldValue ) );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.extensions.IPropertyDescriptor#createControl(org.eclipse.swt.widgets.Composite)
	 */
	public Control createControl( Composite parent )
	{
		Composite innerParent = new Composite( parent, 0 );
		innerParent.setLayout( new GridLayout( 3, false ) );

		text = new Text( innerParent, SWT.BORDER | SWT.READ_ONLY );
		text.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );

		btnBrowse = new Button( innerParent, SWT.PUSH );
		btnBrowse.setText( "..." ); //$NON-NLS-1$

		btnBrowse.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				String baseName = SessionHandleAdapter.getInstance( )
						.getReportDesignHandle( )
						.getIncludeResource( );

//				String designFileName = SessionHandleAdapter.getInstance( )
//						.getReportDesignHandle( )
//						.getFileName( );

				// String designFilePath = null;
				// File designFile = new File( designFileName );
				// if ( designFile.exists( ) && designFile.isFile( ) )
				// {
				// designFilePath = designFile.getParentFile( )
				// .getAbsolutePath( );
				// }
//				String designFilePath = ReportPlugin.getDefault( )
//						.getResourceFolder( );
				ResourceEditDialog dlg = new ResourceEditDialog( btnBrowse.getShell( ),
						Messages.getString( "ResourceKeyDescriptor.title.SelectKey" ) ); //$NON-NLS-1$

				// Tempopray solution, because bug 155509 not fix
				// did not find resource by locale
				dlg.setResourceURL( MessageFileUtil.findMessageFile( SessionHandleAdapter.getInstance( )
						.getReportDesignHandle( ),
						baseName,
						ThreadResources.getLocale( ) ) );

				if ( dlg.open( ) == Window.OK )
				{
					processAction( (String) dlg.getResult( ) );
				}
			}
		} );

		btnReset = new Button( innerParent, SWT.PUSH );
		btnReset.setText( Messages.getString( "ResourceKeyDescriptor.text.Reset" ) ); //$NON-NLS-1$

		btnReset.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				processAction( null );
			};

		} );

		return innerParent;
	}

	private void processAction( String newValue )
	{
		if ( "".equals( newValue ) )//$NON-NLS-1$
		{
			newValue = null;
		}

		try
		{
			getPropertyProcessor( ).setStringValue( newValue, getModelList( ) );
			text.setText( DEUtil.resolveNull( newValue ) );
		}
		catch ( SemanticException e )
		{
			text.setText( DEUtil.resolveNull( oldValue ) );
			WidgetUtil.processError( text.getShell( ), e );
		}

	}

}