/*******************************************************************************
 * 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.chart.reportitem.ui;

import org.eclipse.birt.chart.model.Chart;
import org.eclipse.birt.chart.reportitem.ChartReportItemImpl;
import org.eclipse.birt.chart.reportitem.ui.i18n.Messages;
import org.eclipse.birt.core.ui.frameworks.taskwizard.WizardBase;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.extension.ExtendedElementException;
import org.eclipse.birt.report.model.api.extension.IReportItem;
import org.eclipse.jface.action.Action;

/**
 * Action set to open chart task.
 */

public class OpenChartTaskAction extends Action
{

	private ExtendedItemHandle eih;

	private ChartReportItemBuilderImpl uiServiceProvider;

	public OpenChartTaskAction( ExtendedItemHandle handle, String taskId,
			String text )
	{
		super( text );
		this.eih = handle;
		this.uiServiceProvider = new ChartReportItemBuilderImpl( taskId );
	}

	public void run( )
	{
		uiServiceProvider.open( eih );
	}

	protected boolean isBlankChart( )
	{
		IReportItem item = null;
		try
		{
			item = eih.getReportItem( );
			if ( item == null )
			{
				eih.loadExtendedElement( );
				item = eih.getReportItem( );
			}
		}
		catch ( ExtendedElementException exception )
		{
			WizardBase.displayException( exception );
		}

		if ( item == null )
		{
			return true;
		}
		ChartReportItemImpl crii = ( (ChartReportItemImpl) item );
		return (Chart) crii.getProperty( "chart.instance" ) == null; //$NON-NLS-1$
	}
}

class OpenTaskSelectTypeAction extends OpenChartTaskAction
{

	public OpenTaskSelectTypeAction( ExtendedItemHandle handle )
	{
		super( handle, "org.eclipse.birt.chart.ui.swt.wizard.TaskSelectType", //$NON-NLS-1$
				Messages.getString( "OpenChartTaskAction.Text.SelectChartType" ) ); //$NON-NLS-1$
	}
}

class OpenTaskSelectDataAction extends OpenChartTaskAction
{

	public OpenTaskSelectDataAction( ExtendedItemHandle handle )
	{
		super( handle, "org.eclipse.birt.chart.ui.swt.wizard.TaskSelectData", //$NON-NLS-1$
				Messages.getString( "OpenChartTaskAction.Text.SelectData" ) ); //$NON-NLS-1$
		setEnabled( !isBlankChart( ) );
	}
}

class OpenTaskFormatChartAction extends OpenChartTaskAction
{

	public OpenTaskFormatChartAction( ExtendedItemHandle handle )
	{
		super( handle, "org.eclipse.birt.chart.ui.swt.wizard.TaskFormatChart", //$NON-NLS-1$
				Messages.getString( "OpenChartTaskAction.Text.FormatChart" ) ); //$NON-NLS-1$
		setEnabled( !isBlankChart( ) );
	}
}
