/*************************************************************************************
 * Copyright (c) 2004 Actuate Corporation and others.
 * 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 implementation.
 ************************************************************************************/

package org.eclipse.birt.report.designer.ui.actions;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.util.Policy;
import org.eclipse.birt.report.designer.internal.ui.util.UIUtil;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.ReportPlugin;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.birt.report.viewer.utilities.WebViewer;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.forms.editor.FormEditor;

/**
 * Our sample action implements workbench action delegate. The action proxy will
 * be created by the workbench and shown in the UI. When the user tries to use
 * the action, this delegate will be created and execution will be delegated to
 * it.
 * 
 * @see IWorkbenchWindowActionDelegate
 */

public class PreviewAction implements IWorkbenchWindowActionDelegate
{

	public static final String PreviewInWebViewerActionId = "org.eclipse.birt.report.designer.ui.actions.PreviewInWebviewerAction"; //$NON-NLS-1$
	public static final String PreviewInHtmlActionId = "org.eclipse.birt.report.designer.ui.actions.PreviewInHtmlAction"; //$NON-NLS-1$
	public static final String PreviewInPDFActionId = "org.eclipse.birt.report.designer.ui.actions.PreviewInPDFAction"; //$NON-NLS-1$

	protected IWorkbenchWindow window = null;

	/**
	 * The constructor.
	 */
	public PreviewAction( )
	{
		super( );
	}

	/**
	 * Selection in the workbench has been changed. We can change the state of
	 * the 'real' action here if we want, but this can only happen after the
	 * delegate has been created.
	 * 
	 * @see IWorkbenchWindowActionDelegate#selectionChanged(IAction,ISelection)
	 */
	public void selectionChanged( IAction action, ISelection selection )
	{
	}

	/**
	 * The action has been activated. The argument of the method represents the
	 * 'real' action sitting in the workbench UI.
	 * 
	 * @see IWorkbenchWindowActionDelegate#run(IAction)
	 */
	public void run( IAction action )
	{
		if ( Policy.TRACING_ACTIONS )
		{
			System.out.println( "Preview action >> Run ..." ); //$NON-NLS-1$
		}
		if ( PreviewAction.PreviewInHtmlActionId.equalsIgnoreCase( action.getId( ) ) )
		{
			preview( WebViewer.HTML, false );
		}
		else if ( PreviewAction.PreviewInPDFActionId.equalsIgnoreCase( action.getId( ) ) )
		{
			doPDFPreview( );
		}
		else
		{
			preview( WebViewer.HTML, true );
		}
	}

	protected void preview( String format, boolean allowPage )
	{
		FormEditor editor = UIUtil.getActiveReportEditor( false );
		ModuleHandle model = SessionHandleAdapter.getInstance( )
				.getReportDesignHandle( );

		if ( editor != null )
		{
			if ( model.needsSave( ) )
			{
				editor.doSave( null );
			}
		}
		Map options = new HashMap( );
		options.put( WebViewer.FORMAT_KEY, format );
		options.put( WebViewer.ALLOW_PAGE_KEY, Boolean.valueOf( allowPage ) );
		options.put( WebViewer.RESOURCE_FOLDER_KEY, ReportPlugin.getDefault( )
				.getResourceFolder( ) );
		WebViewer.display( model.getFileName( ), options );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose( )
	{
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
	 */
	public void init( IWorkbenchWindow window )
	{
		this.window = window;
	}

	protected void doPDFPreview( )
	{
		if ( Platform.getBundle( "com.lowagie.itext" ) != null ) //$NON-NLS-1$
		{
			preview( WebViewer.PDF, false );
		}
		else
		{
			MessageDialog.openError( window.getShell( ),
					Messages.getString( "designer.preview.previewaction.ErrorMessage.title" ), //$NON-NLS-1$
					Messages.getString( "designer.preview.previewaction.ErrorMessage" )//$NON-NLS-1$
			);
		}
	}
}