/*******************************************************************************
 * 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.views.attributes.page;

import java.util.List;

import org.eclipse.birt.report.designer.core.model.SessionHandleAdapter;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.ColorBuilder;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.StyleCombo;
import org.eclipse.birt.report.designer.internal.ui.views.attributes.widget.StyleComboProvider;
import org.eclipse.birt.report.designer.nls.Messages;
import org.eclipse.birt.report.designer.ui.IReportGraphicConstants;
import org.eclipse.birt.report.designer.ui.ReportPlatformUIImages;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.ChoiceSetFactory;
import org.eclipse.birt.report.designer.ui.views.attributes.providers.PropertyProcessor;
import org.eclipse.birt.report.model.api.CommandStack;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.activity.NotificationEvent;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.ReportDesignConstants;
import org.eclipse.birt.report.model.api.metadata.IChoice;
import org.eclipse.birt.report.model.api.metadata.IChoiceSet;
import org.eclipse.birt.report.model.api.metadata.IColorConstants;
import org.eclipse.birt.report.model.api.util.ColorUtil;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

/**
 * The borders attribute page of DE element.
 */
public class BordersPage extends AttributePage
{

	ColorBuilder builder;

	StyleCombo styleCombo, widthCombo;

	// private Button btnNone, btnFrame;

	private Button btnTop, btnBottom, btnLeft, btnRight;

	private static final String[] styles = {
			DesignChoiceConstants.LINE_STYLE_SOLID,
			DesignChoiceConstants.LINE_STYLE_DOTTED,
			DesignChoiceConstants.LINE_STYLE_DASHED,
			DesignChoiceConstants.LINE_STYLE_DOUBLE};

	private static final String LABEL_STYLE = Messages
			.getString( "BordersPage.Label.Style" ); //$NON-NLS-1$

	private static final String LABEL_COLOR = Messages
			.getString( "BordersPage.Label.Color" ); //$NON-NLS-1$

	private static final String LABEL_WIDTH = Messages
			.getString( "BordersPage.Label.Width" ); //$NON-NLS-1$

	// private static final String LABEL_PRESETS = Messages
	// .getString( "BordersPage.Label.Presets" ); //$NON-NLS-1$

	private static final String LABEL_BORDER = Messages
			.getString( "BordersPage.Label.Borders" ); //$NON-NLS-1$

	/**
	 * Button action listener.only for btnTop, btnBottom, btnLeft and btnRight.
	 */
	private SelectionAdapter listener;

	/**
	 * Constructor
	 * 
	 * @param parent
	 *            A widget which will be the parent of the new instance (cannot
	 *            be null)
	 * @param style
	 *            The style of widget to construct
	 */
	public BordersPage( Composite parent, int style )
	{
		super( parent, style );
	}

	// static class
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.ui.attributes.page.AttributePage#buildUI()
	 */
protected void buildUI( )
	{
		listener = new SelectionAdapter( ) {

			public void widgetSelected( SelectionEvent e )
			{
				CommandStack stack = getActionStack( );
				stack.startTrans( "" );//$NON-NLS-1$
				try
				{
					setBorderProperties( (Button) e.widget );
				}
				catch ( SemanticException e1 )
				{
					stack.rollback( );
					WidgetUtil.processError( getShell( ), e1 );
				}
				stack.commit( );
			}
		};
		setLayout( WidgetUtil.createGridLayout( 6 ) );

		Label label = new Label( this, SWT.NONE );
		label.setText( LABEL_STYLE );
		styleCombo = new StyleCombo( this, SWT.BORDER, new StyleComboProvider( ) );
		styleCombo.setItems( styles );
		styleCombo.select( 0 );

		styleCombo.addSelectionListener( new SelectionListener( ) {

			public void widgetSelected( SelectionEvent e )
			{
				CommandStack stack = getActionStack( );
				stack.startTrans( "" );//$NON-NLS-1$
				try
				{
					setStylePropertyForEnableButton((String) styleCombo.getSelectedItem());
				}
				catch ( SemanticException e1 )
				{
					stack.rollback( );
					WidgetUtil.processError( getShell( ), e1 );
				}
				stack.commit( );
			}

			public void widgetDefaultSelected( SelectionEvent e )
			{
				// TODO Auto-generated method stub
				
			}

		} );

		label = new Label( this, SWT.NONE );
		label.setText( LABEL_COLOR );
		builder = new ColorBuilder( this, SWT.NONE );
		builder.setChoiceSet( ChoiceSetFactory.getElementChoiceSet(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_COLOR_PROP ) );
		builder.setColorValue( IColorConstants.BLACK );

		GridData data = new GridData( );
		data.horizontalAlignment = GridData.FILL;
		builder.setLayoutData( data );
		
		builder.addListener(SWT.Modify,new Listener()
				{

					public void handleEvent( Event event )
					{
						if(event.type == SWT.Modify)
						{
							CommandStack stack = getActionStack( );
							stack.startTrans( "" );//$NON-NLS-1$
							try
							{
								setColorPropertyForEnableButton(getColorBuilderValue());
							}
							catch ( SemanticException e1 )
							{
								stack.rollback( );
								WidgetUtil.processError( getShell( ), e1 );
							}
							stack.commit( );
							
						}
						
					}
			
				});

		WidgetUtil.createGridPlaceholder( this, 2, true );
		
		label = new Label( this, SWT.NONE );
		label.setText( LABEL_WIDTH );
		widthCombo = new StyleCombo( this, SWT.BORDER, new StyleComboProvider( ) );
		IChoiceSet choiceSet = ChoiceSetFactory.getElementChoiceSet(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_WIDTH_PROP );

		String[] widths = getWidths( choiceSet );
		if ( choiceSet != null )
		{
			widthCombo.setItems( widths );
			widthCombo.select( 1 );
		}
		
		widthCombo.addSelectionListener(new SelectionListener(){

			public void widgetSelected( SelectionEvent e )
			{
				CommandStack stack = getActionStack( );
				stack.startTrans( "" );//$NON-NLS-1$
				try
				{
					setWidthPropertyForEnableButton((String) widthCombo.getSelectedItem());
				}
				catch ( SemanticException e1 )
				{
					stack.rollback( );
					WidgetUtil.processError( getShell( ), e1 );
				}
				stack.commit( );				
			}

			public void widgetDefaultSelected( SelectionEvent e )
			{
				// TODO Auto-generated method stub
				
			}
			
		});
		WidgetUtil.createGridPlaceholder( this, 4, true );
		
		// label = new Label( this, SWT.NONE );
		// label.setText( LABEL_PRESETS );
		//
		// Composite container1 = new Composite( this, SWT.NONE );
		// RowLayout rowLayout = new RowLayout( SWT.HORIZONTAL );
		// rowLayout.wrap = false;
		// rowLayout.spacing = WidgetUtil.SPACING;
		// container1.setLayout( rowLayout );
		//
		// btnNone = new Button( container1, SWT.PUSH );
		// btnNone
		// .setToolTipText( Messages
		// .getString( "BordersPage.Tooltip.None" ) ); //$NON-NLS-1$
		// btnNone
		// .setImage( ReportPlatformUIImages
		// .getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_NONE ) );
		// btnNone.addSelectionListener( new SelectionAdapter( ) {
		//
		// public void widgetSelected( SelectionEvent e )
		// {
		// Button[] btns = {btnTop, btnBottom, btnLeft, btnRight,};
		// for ( int i = 0; i < btns.length; i++ )
		// {
		// btns[i].setSelection( false );
		// }
		// CommandStack stack = getActionStack( );
		// stack.startTrans( "" );//$NON-NLS-1$
		// try
		// {
		// for ( int i = 0; i < btns.length; i++ )
		// {
		// setBorderProperties( btns[i] );
		// }
		// }
		// catch ( SemanticException e1 )
		// {
		// stack.rollback( );
		// WidgetUtil.processError( getShell( ), e1 );
		// }
		// stack.commit( );
		// }
		// } );
		//
		// btnFrame = new Button( container1, SWT.PUSH );
		// btnFrame
		// .setToolTipText( Messages.getString( "BordersPage.Tooltip.All" ) );
		// //$NON-NLS-1$
		// btnFrame
		// .setImage( ReportPlatformUIImages
		// .getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_FRAME ) );
		// btnFrame.addSelectionListener( new SelectionAdapter( ) {
		//
		// public void widgetSelected( SelectionEvent e )
		// {
		// Button[] btns = {btnTop, btnBottom, btnLeft, btnRight,};
		// for ( int i = 0; i < btns.length; i++ )
		// {
		// btns[i].setSelection( true );
		// }
		// CommandStack stack = getActionStack( );
		// stack.startTrans( "" );//$NON-NLS-1$
		// try
		// {
		// for ( int i = 0; i < btns.length; i++ )
		// {
		// setBorderProperties( btns[i] );
		// }
		// }
		// catch ( SemanticException e1 )
		// {
		// stack.rollback( );
		// WidgetUtil.processError( getShell( ), e1 );
		// }
		// stack.commit( );
		// }
		// } );

		label = new Label( this, SWT.NONE );
		label.setText( LABEL_BORDER ); //$NON-NLS-1$

		Composite container2 = new Composite( this, SWT.NONE );
		RowLayout rowLayout2 = new RowLayout( SWT.HORIZONTAL );
		rowLayout2.wrap = false;
		rowLayout2.spacing = WidgetUtil.SPACING;
		container2.setLayout( rowLayout2 );

		btnTop = new Button( container2, SWT.TOGGLE );
		btnTop.setToolTipText( Messages.getString( "BordersPage.Tooltip.Top" ) ); //$NON-NLS-1$
		btnTop.setImage( ReportPlatformUIImages
				.getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_TOP ) );
		btnTop.addSelectionListener( listener );

		btnBottom = new Button( container2, SWT.TOGGLE );
		btnBottom.setToolTipText( Messages
				.getString( "BordersPage.Tooltip.Bottom" ) ); //$NON-NLS-1$
		btnBottom
				.setImage( ReportPlatformUIImages
						.getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_BOTTOM ) );
		btnBottom.addSelectionListener( listener );

		btnLeft = new Button( container2, SWT.TOGGLE );
		btnLeft
				.setToolTipText( Messages
						.getString( "BordersPage.Tooltip.Left" ) ); //$NON-NLS-1$
		btnLeft
				.setImage( ReportPlatformUIImages
						.getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_LEFT ) );
		btnLeft.addSelectionListener( listener );

		btnRight = new Button( container2, SWT.TOGGLE );
		btnRight.setToolTipText( Messages
				.getString( "BordersPage.Tooltip.Right" ) ); //$NON-NLS-1$
		btnRight
				.setImage( ReportPlatformUIImages
						.getImage( IReportGraphicConstants.ICON_ATTRIBUTE_BORDER_RIGHT ) );
		btnRight.addSelectionListener( listener );

	}
	private String[] getWidths( IChoiceSet choiceSet )
	{

		IChoice[] choices = choiceSet.getChoices( );

		String[] strChoices = new String[choices.length];

		for ( int i = 0; i < choices.length; i++ )
		{
			strChoices[i] = choices[i].getName( );
		}
		return strChoices;
	}

	/**
	 * Sets properties value that associated with one border.
	 * 
	 * @param btn
	 *            The button that has the selection event.
	 * @throws SemanticException
	 */
	private void setBorderProperties( Button btn ) throws SemanticException
	{
		String styleProperty = null, colorProperty = null, widthProperty = null;
		String styleValue = null, colorValue = null, widthValue = null;
		if ( btn == btnTop )
		{
			styleProperty = StyleHandle.BORDER_TOP_STYLE_PROP;
			colorProperty = StyleHandle.BORDER_TOP_COLOR_PROP;
			widthProperty = StyleHandle.BORDER_TOP_WIDTH_PROP;
		}
		else if ( btn == btnBottom )
		{
			styleProperty = StyleHandle.BORDER_BOTTOM_STYLE_PROP;
			colorProperty = StyleHandle.BORDER_BOTTOM_COLOR_PROP;
			widthProperty = StyleHandle.BORDER_BOTTOM_WIDTH_PROP;
		}
		else if ( btn == btnLeft )
		{
			styleProperty = StyleHandle.BORDER_LEFT_STYLE_PROP;
			colorProperty = StyleHandle.BORDER_LEFT_COLOR_PROP;
			widthProperty = StyleHandle.BORDER_LEFT_WIDTH_PROP;
		}
		else if ( btn == btnRight )
		{
			styleProperty = StyleHandle.BORDER_RIGHT_STYLE_PROP;
			colorProperty = StyleHandle.BORDER_RIGHT_COLOR_PROP;
			widthProperty = StyleHandle.BORDER_RIGHT_WIDTH_PROP;
		}

		
		if ( btn.getSelection( ) )
		{
			styleValue = styleCombo.getSelectedItem( ).toString( );
			widthValue = (String) widthCombo.getSelectedItem( );
			colorValue = getColorBuilderValue();
			
			setStylePropertyForEnableButton( styleValue );
			setColorPropertyForEnableButton( colorValue );
			setWidthPropertyForEnableButton( widthValue );
		}
		else
		{
			styleValue = null;
			widthValue = null;
			colorValue = null;

			setPropertyValue( styleProperty, styleValue );
			setPropertyValue( colorProperty, colorValue );
			setPropertyValue( widthProperty, widthValue );
		}
	}

	private String getColorBuilderValue( )
	{
		RGB rgb = builder.getRGB( );
		String colorValue = null;
		if ( rgb != null )
		{
			int intValue = ColorUtil.formRGB( rgb.red, rgb.green, rgb.blue );
			colorValue = ColorUtil.format( intValue, ColorUtil.INT_FORMAT );
		}
		else
		{
			colorValue = null;
		}
		return colorValue;
	}

	private void setWidthPropertyForEnableButton( String widthValue )
			throws SemanticException
	{
		if ( btnTop.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_TOP_WIDTH_PROP, widthValue );
		}

		if ( btnBottom.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_BOTTOM_WIDTH_PROP, widthValue );
		}

		if ( btnLeft.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_LEFT_WIDTH_PROP, widthValue );
		}

		if ( btnRight.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_RIGHT_WIDTH_PROP, widthValue );
		}
	}

	private void setColorPropertyForEnableButton( String colorValue )
			throws SemanticException
	{
		if ( btnTop.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_TOP_COLOR_PROP, colorValue );
		}

		if ( btnBottom.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_BOTTOM_COLOR_PROP, colorValue );
		}

		if ( btnLeft.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_LEFT_COLOR_PROP, colorValue );
		}

		if ( btnRight.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_RIGHT_COLOR_PROP, colorValue );
		}

	}

	private void setStylePropertyForEnableButton( String styleValue )
			throws SemanticException
	{
		if ( btnTop.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_TOP_STYLE_PROP, styleValue );
		}

		if ( btnBottom.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_BOTTOM_STYLE_PROP, styleValue );
		}

		if ( btnLeft.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_LEFT_STYLE_PROP, styleValue );
		}

		if ( btnRight.getSelection( ) )
		{
			setPropertyValue( StyleHandle.BORDER_RIGHT_STYLE_PROP, styleValue );
		}
	}

	/**
	 * Sets value of a given property.
	 * 
	 * @param property
	 *            The property key.
	 * @param value
	 *            The value to set.
	 * @throws SemanticException
	 */
	private void setPropertyValue( String property, String value )
			throws SemanticException
	{
		PropertyProcessor processor = new PropertyProcessor(
				ReportDesignConstants.STYLE_ELEMENT, property );
		// try
		// {
		processor.setStringValue( value, input );
		// }
		// catch ( SemanticException e )
		// {
		// e.printStackTrace( );
		// WidgetUtil.processError( this.getShell( ), e );
		// }
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.birt.report.designer.internal.ui.views.attributes.page.TabPage#setInput(java.util.List)
	 */
	public void setInput( List elements )
	{
		super.setInput( elements );
		restoreSelection( elements );
	}

	private void restoreSelection( List elements )
	{
		restoreBorderType( elements );
		restoreBorderColor( elements );
		restoreBorderWidth( elements );
		restoreBorderButton( elements );

		return;
	}

	private void restoreBorderButton( List elements )
	{
		PropertyProcessor processor = new PropertyProcessor(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_LEFT_STYLE_PROP );
		String value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			btnLeft.setSelection( true );
		}
		else
		{
			btnLeft.setSelection( false );	
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_RIGHT_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			btnRight.setSelection( true );
		}
		else
		{
			btnRight.setSelection( false );	
		}

		
		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			btnTop.setSelection( true );
		}
		else
		{
			btnTop.setSelection( false );	
		}

		
		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_BOTTOM_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			btnBottom.setSelection( true );
		}
		else
		{
			btnBottom.setSelection( false );	
		}
		
		return;
	}

	private void restoreBorderWidth( List elements )
	{
		PropertyProcessor processor = new PropertyProcessor(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_LEFT_WIDTH_PROP );
		String value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			widthCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_RIGHT_WIDTH_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			widthCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_WIDTH_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			widthCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_BOTTOM_WIDTH_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			widthCombo.setSelectedItem( value );
			return;
		}

		return;

	}

	private void restoreBorderColor( List elements )
	{
		PropertyProcessor processor = new PropertyProcessor(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_LEFT_COLOR_PROP );
		String value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			builder.setColorValue( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_RIGHT_COLOR_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			builder.setColorValue( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_COLOR_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			builder.setColorValue( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_BOTTOM_COLOR_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			builder.setColorValue( value );
			return;
		}
		return;
	}

	private void restoreBorderType( List elements )
	{
		PropertyProcessor processor = new PropertyProcessor(
				ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_LEFT_STYLE_PROP );
		String value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			styleCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_RIGHT_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			styleCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_TOP_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			styleCombo.setSelectedItem( value );
			return;
		}

		processor = new PropertyProcessor( ReportDesignConstants.STYLE_ELEMENT,
				StyleHandle.BORDER_BOTTOM_STYLE_PROP );
		value = processor.getLocalStringValue( elements );
		if ( !"".equals( value ) )
		{
			styleCombo.setSelectedItem( value );
			return;
		}

	}

	/**
	 * Gets the DE CommandStack instance
	 * 
	 * @return CommandStack instance
	 */
	private CommandStack getActionStack( )
	{
		return SessionHandleAdapter.getInstance( ).getCommandStack( );
	}
	
	public void elementChanged( DesignElementHandle focus, NotificationEvent ev )
	{
		super.elementChanged(focus,ev);
		//restoreSelection(input);
	}
}