/*******************************************************************************
 * 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.ui.lib.explorer.action;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
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.lib.explorer.resource.ReportResourceEntry;
import org.eclipse.birt.report.designer.ui.lib.explorer.resource.ResourceEntryWrapper;
import org.eclipse.birt.report.designer.ui.util.ExceptionUtil;
import org.eclipse.birt.report.model.api.LibraryHandle;
import org.eclipse.birt.report.model.api.ModuleHandle;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredViewer;

/**
 * The action used to add library to a report design
 */

public class AddSelectedLibToCurrentReportDesignAction extends Action
{

	private StructuredViewer viewer;

	private static final String ACTION_TEXT = Messages.getString( "UseLibraryAction.Text" ); //$NON-NLS-1$

	public AddSelectedLibToCurrentReportDesignAction( StructuredViewer viewer )
	{
		super( ACTION_TEXT );
		this.viewer = viewer;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#isEnabled()
	 */
	public boolean isEnabled( )
	{
		LibraryHandle library = getSelectedLibrary( );
		ModuleHandle moduleHandle = SessionHandleAdapter.getInstance( )
				.getReportDesignHandle( );

		boolean enabled = library != null
				&& moduleHandle != null
				&& !moduleHandle.isInclude( library )
				&& ( library.getFileName( ) != null && !library.getFileName( )
						.equals( moduleHandle.getFileName( ) ) );
		if ( library != null )
			library.close( );
		return enabled;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.jface.action.Action#run()
	 */
	public void run( )
	{
		if ( isEnabled( ) )
		{
			LibraryHandle library = getSelectedLibrary( );
			try
			{
				UIUtil.includeLibrary( library );
			}
			catch ( Exception e )
			{
				ExceptionUtil.handle( e );
			}
		}
	}

	private LibraryHandle getSelectedLibrary( )
	{
		IStructuredSelection selection = (IStructuredSelection) viewer.getSelection( );
		if ( selection != null && selection.size( ) == 1 )
		{
			Object selected = selection.getFirstElement( );

			if ( selected instanceof LibraryHandle )
			{
				return (LibraryHandle) selected;
			}
			else if ( selected instanceof ReportResourceEntry
					&& ( (ReportResourceEntry) selected ).getReportElement( ) instanceof LibraryHandle )
			{
				return (LibraryHandle) ( (ReportResourceEntry) selected ).getReportElement( );
			}
			else if ( selected instanceof ResourceEntryWrapper
					&& ( (ResourceEntryWrapper) selected ).getType( ) == ResourceEntryWrapper.LIBRARY )
			{
				return (LibraryHandle) ( (ResourceEntryWrapper) selected ).getAdapter( LibraryHandle.class );
			}
		}
		return null;
	}

}

@@ -659,24 +659,86 @@
 dleAdapter;%0A
+import org.eclipse.birt.report.designer.data.ui.util.Utility;%0A
 import org.e
@@ -786,24 +786,24 @@
 til.UIUtil;%0A
-
 import org.e
@@ -1076,24 +1076,79 @@
 eptionUtil;%0A
+import org.eclipse.birt.report.model.api.CommandStack;%0A
 import org.e
@@ -2281,24 +2281,76 @@
 ame( ) ) );%0A
+%0A%09%09if ( enabled )%0A%09%09%09enabled = testRun( library );%0A%0A
 %09%09if ( libra
@@ -2383,16 +2383,17 @@
 ose( );%0A
+%0A
 %09%09return
@@ -2709,16 +2709,16 @@
 %09%09%7D%0A
-
 %09%09%7D%0A%09%7D%0A%0A
 %09pri
@@ -2713,16 +2713,432 @@
 %09%09%7D%0A%09%7D%0A%0A
+%09/*%0A%09 * Need model to provide a new api to support it.%0A%09 */%0A%09@Deprecated%0A%09private boolean testRun( LibraryHandle library )%0A%09%7B%0A%09%09boolean enabled = false;%0A%09%09CommandStack commandStack = Utility.getCommandStack( );%0A%09%09commandStack.startTrans( %22%22 );%0A%09%09try%0A%09%09%7B%0A%09%09%09UIUtil.includeLibrary( library );%0A%09%09%09enabled = true;%0A%09%09%7D%0A%09%09catch ( Exception e )%0A%09%09%7B%0A%09%09%09enabled = false;%0A%09%09%7D%0A%09%09commandStack.rollback( );%0A%09%09return enabled;%0A%09%7D%0A%0A
 %09private

Make AddSelectedLibToCurrentReportDesignAction more robust by testing if action works before enabling it.