/***********************************************************************
 * Copyright (c) 2006 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.ui.swt.composites;

import java.util.HashMap;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * Custom text combo
 */
public class CTextCombo extends CustomChooserComposite
{

	private Font fontBold = new Font( Display.getCurrent( ), "arial", //$NON-NLS-1$
			9,
			SWT.BOLD );

	private HashMap choiceMarkerMap = new HashMap( 10 );

	private class TextComboChoice extends TextComboChoiceCanvas
			implements
				ICustomChoice
	{

		TextComboChoice( Composite parent, int iStyle, String comboText )
		{
			super( parent, iStyle, comboText );
		}

		public Object getValue( )
		{
			return getText( );
		}

		public void setValue( Object value )
		{
			setText( (String) value );
			if ( choiceMarkerMap.containsKey( value ) )
			{
				setTextFont( fontBold );
			}
			else
			{
				setTextFont( null );
			}
		}

	}

	public CTextCombo( Composite parent, int style )
	{
		super( parent, style );
		addDisposeListener( new DisposeListener( ) {

			public void widgetDisposed( DisposeEvent e )
			{
				fontBold.dispose( );
				choiceMarkerMap.clear( );
			}
		} );
	}

	protected ICustomChoice createChoice( Composite parent, Object choiceValue )
	{
		if ( choiceValue == null )
		{
			choiceValue = ""; //$NON-NLS-1$
		}
		TextComboChoice choice = new TextComboChoice( parent,
				SWT.NONE,
				(String) choiceValue );
		if ( choiceMarkerMap.containsKey( choiceValue ) )
		{
			choice.setTextFont( fontBold );
		}
		else
		{
			choice.setTextFont( null );
		}
		return choice;
	}

	public void setText( String text )
	{
		setChoiceValue( text );
	}

	public String getText( )
	{
		return (String) getChoiceValue( );
	}

	public void markSelection( String text )
	{
		choiceMarkerMap.put( text, Boolean.TRUE );
	}

	public void unmarkSelection( String text )
	{
		choiceMarkerMap.remove( text );
	}

}
