/*******************************************************************************
 * 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.List;
import java.util.Set;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.dialogs.HyperlinkBuilder;
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.ActionHandle;
import org.eclipse.birt.report.model.api.CommandStack;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.metadata.IChoiceSet;
import org.eclipse.jface.dialogs.Dialog;
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;
import org.eclipse.ui.PlatformUI;

/**
 * The HyperLink attribute page of DE element.
 */
public class HyperLinkPage extends AttributePage
{

	private static final String LABEL_LINK_TO = Messages.getString( "HyperLinkPage.Label.LnikTo" ); //$NON-NLS-1$

	private static final String LABEL_NONE = Messages.getString( "HyperLinkPage.Label.None" ); //$NON-NLS-1$

	private Text previewText;

	private boolean needRefresh = true;

	// private List actions;

	protected IChoiceSet choiceSet;

	/**
	 * @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 HyperLinkPage( 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( 3 ) );

		Label label = new Label( this, SWT.NONE );
		label.setText( LABEL_LINK_TO );

		previewText = new Text( this, SWT.BORDER | SWT.READ_ONLY );
		GridData gd = new GridData( );
		gd.widthHint = 250;
		previewText.setLayoutData( gd );

		Button builder = new Button( this, SWT.PUSH );
		builder.setText( "..." ); //$NON-NLS-1$
		builder.setToolTipText(Messages
				.getString("HyperLinkPage.toolTipText.Button"));
		builder.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				HyperlinkBuilder dialog = new HyperlinkBuilder( PlatformUI.getWorkbench( )
						.getDisplay( )
						.getActiveShell( ) );
				getActionStack( ).startTrans( Messages.getString( "HyperLinkPage.Menu.Save" ) ); //$NON-NLS-1$
				ActionHandle handle = getActionHandle( );
				if ( handle == null )
				{
					try
					{
						handle = DEUtil.setAction( (ReportItemHandle) input.get( 0 ),
								StructureFactory.createAction( ) );
					}
					catch ( SemanticException e1 )
					{
						getActionStack( ).rollback( );
						ExceptionHandler.handle( e1 );
						return;
					}
				}
				dialog.setInput( handle );
				needRefresh = false;
				boolean isOK = dialog.open( ) == Dialog.OK;
				needRefresh = true;
				if ( isOK )
				{
					getActionStack( ).commit( );
					refreshValues( null );
				}
				else
				{
					getActionStack( ).rollback( );
				}

			}
		} );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.TabPage#setInput(java.util.List)
	 */
	public void setInput( List elements )
	{
		deRegisterListeners( );
		input = elements;

		Object[] descriptors = propertiesMap.values( ).toArray( );
		for ( int i = 0; i < descriptors.length; i++ )
		{
			IPropertyDescriptor descriptor = (IPropertyDescriptor) descriptors[i];
			descriptor.setModelList( elements );
		}
		refreshValues( null );
		registerListeners( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#refreshValues(java.util.Set)
	 */

	protected void refreshValues( Set propertiesSet )
	{
		if ( needRefresh )
		{
			doLoad( getActionHandle( ) );
		}
	}

	private ActionHandle getActionHandle( )
	{
		return DEUtil.getActionHandle( (ReportItemHandle) input.get( 0 ) );
	}

	/**
	 * 
	 */
	private void doLoad( ActionHandle handle )
	{
		if ( handle != null )
		{
			String previewString = null;
			if ( DesignChoiceConstants.ACTION_LINK_TYPE_HYPERLINK.equals( handle.getLinkType( ) ) )
			{
				previewString = handle.getURI( );
			}
			else if ( DesignChoiceConstants.ACTION_LINK_TYPE_BOOKMARK_LINK.equals( handle.getLinkType( ) ) )
			{
				previewString = handle.getTargetBookmark( );
			}
			else if ( DesignChoiceConstants.ACTION_LINK_TYPE_DRILL_THROUGH.equals( handle.getLinkType( ) ) )
			{
				previewString = handle.getReportName( );
				if ( handle.getTargetBookmark( ) != null )
				{
					previewString += ":" + handle.getTargetBookmark( ); //$NON-NLS-1$
				}
			}
			if ( previewString == null )
			{
				previewString = LABEL_NONE;
			}
			previewText.setText( previewString );
		}
		else
		{
			setDefaultUI( );
		}
	}

	protected void setDefaultUI( )
	{
		previewText.setText( LABEL_NONE ); //$NON-NLS-1$
	}

	/**
	 * Gets the DE CommandStack instance
	 * 
	 * @return CommandStack instance
	 */
	private CommandStack getActionStack( )
	{
		return SessionHandleAdapter.getInstance( ).getCommandStack( );
	}
}