/*******************************************************************************
 * 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 org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.preview.Activator;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.ReportEngine;
import org.eclipse.birt.report.viewer.utilities.WebViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.IWorkbenchWindowPulldownDelegate;

public class PreviewToolbarMenuAction extends PreviewAction implements
		IWorkbenchWindowPulldownDelegate
{

	private Image previewIcon = Activator.getImageDescriptor( "icons/etool16/preview.gif" )
			.createImage( );
	private Image previewPDFIcon = Activator.getImageDescriptor( "icons/etool16/preview_pdf.gif" )
			.createImage( );

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

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.actions.PreviewAction#dispose()
	 */
	public void dispose( )
	{
		previewIcon.dispose( );
		previewPDFIcon.dispose( );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWindowPulldownDelegate#getMenu(org.eclipse.swt.widgets.Control)
	 */
	public Menu getMenu( Control parent )
	{
		ReportEngine engine = new ReportEngine( new EngineConfig( ) );
		String[] supportedFormats = engine.getSupportedFormats( );

		Menu menu = new Menu( parent );

		MenuItem previewWebViewer = new MenuItem( menu, SWT.PUSH );
		previewWebViewer.setText( Messages.getString( "designer.preview.previewaction.label.webviewer" ) ); //$NON-NLS-1$
		previewWebViewer.setImage( previewIcon ); //$NON-NLS-1$		
		previewWebViewer.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				preview( WebViewer.HTML, true );
			}
		} );

		for ( int i = 0; i < supportedFormats.length; i++ )
		{
			final String format = supportedFormats[i];
			MenuItem previewOption = new MenuItem( menu, SWT.PUSH );
			previewOption.setText( "View report as " + format.toUpperCase( ) );
			if ( format.equals( "pdf" ) )
			{
				previewOption.setImage( previewPDFIcon );
			}
			else
			{
				previewOption.setImage( previewIcon );
			}
			previewOption.addSelectionListener( new SelectionAdapter( ) {

				public void widgetSelected( SelectionEvent e )
				{
					preview( format, false );
				}
			} );
		}

		return menu;
	}
}