/*******************************************************************************
 * 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.ide.propertyeditor;

import org.eclipse.birt.report.designer.internal.ui.ide.util.ClassFinder;
import org.eclipse.birt.report.designer.internal.ui.util.ExceptionHandler;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.page.WidgetUtil;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.AttributeConstant;
import org.eclipse.birt.report.designer.util.DEUtil;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.GroupPropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
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;

/**
 * 
 */

public class HandlerPage extends AttributePage
{

	/**
	 * @param parent
	 * @param style
	 */
	public HandlerPage( Composite parent, int style )
	{
		super( parent, style );
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.AttributePage#buildUI()
	 */
	protected void buildUI( )
	{
		this.setLayout( WidgetUtil.createGridLayout( 5 ) );

		WidgetUtil.buildGridControl( this,
				propertiesMap,
				ReportDesignConstants.REPORT_DESIGN_ELEMENT,
				ReportDesignHandle.EVENT_HANDLER_CLASS_PROP,
				1,
				400 );

		Button browse = new Button( this, SWT.PUSH );
		browse.setText( Messages.getString( "EventHandlerPage.Browse" ) );
		browse.setLayoutData( new GridData( ) );
		browse.addSelectionListener( new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				ClassFinder finder = new ClassFinder( );
				String className = null;
				if ( input != null && input.size( ) > 0 )
				{
//					if ( input.get( 0 ) instanceof ExtendedItemHandle
//					{
//						className = (String) ( EventHandlerWrapper.get( AttributeConstant.EVENT_HANDLER_CLASS_PROPERTY_KEY ) );
//					}
//					else 
						if ( input.get( 0 ) instanceof DesignElementHandle )
					{
						className = EventHandlerWrapper.getEventHandlerClassName( (DesignElementHandle) input.get( 0 ) );

					}
				}
				if ( className != null )
				{
					finder.setParentClassName( className );
					GroupPropertyHandle handle = DEUtil.getMultiSelectionHandle( input )
							.getPropertyHandle( ReportDesignHandle.EVENT_HANDLER_CLASS_PROP );
					try
					{
						handle.setStringValue( finder.getFinderClassName( ) );
					}
					catch ( SemanticException e1 )
					{
						ExceptionHandler.handle( e1 );
					}
				}
			}

		} );
		WidgetUtil.createGridPlaceholder( this, 2, true );

	}

}

@@ -3355,24 +3355,169 @@
 %09try%0A%09%09%09%09%09%7B%0A
+%09%09%09%09%09%09String finderClassName = finder.getFinderClassName( );%0A%09%09%09%09%09%09if(finderClassName != null && finderClassName.trim( ).length( ) %3E 0)%0A%09%09%09%09%09%09%7B%0A%09
 %09%09%09%09%09%09handle
@@ -3535,34 +3535,24 @@
 alue( finder
-.getFinder
 ClassName( )
@@ -3548,23 +3548,42 @@
 lassName
+.trim
 ( ) );%0A
+%09%09%09%09%09%09%7D%09%09%09%09%09%09%0A
 %09%09%09%09%09%7D%0A%09

clear existing entry when click on Cancel of Class Selection dialog