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

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.ibm.icu.text.MessageFormat;

/**
 * Class that handle externalized string used by the Birt viewer.
 * <p>
 */
public class BirtResources
{

	/**
	 * List of resource bundles keyed by the locale, which is cached for the
	 * whole application.
	 */

	private static Map resourceMap = new HashMap( );

	/**
	 * Thread-local variable for the current thread.
	 */

	private static ThreadLocal threadLocal = new ThreadLocal( );


	/**
	 * Sets the locale of current user-thread. This method should be called
	 * before access to any localized message. If the locale is
	 * <code>null</code>, the default locale will be set.
	 * 
	 * @param locale
	 *            locale of the current thread.
	 */

	public static void setLocale( Locale locale )
	{
		if ( locale == null )
			threadLocal.set( Locale.getDefault( ) );
		else
			threadLocal.set( locale );
	}

	/**
	 * Gets the locale of current user-thread.
	 * 
	 * @return the locale of the current thread.
	 */

	public static Locale getLocale( )
	{
		Locale locale = (Locale) threadLocal.get( );
		if ( locale == null )
			locale = Locale.getDefault( );
		return locale;
	}

	/**
	 * Gets the localized message with the resource key.
	 * 
	 * @param key
	 *            the resource key
	 * @return the localized message for that key. Returns the key itself if the
	 *         message was not found.
	 */

	public static String getMessage( String key )
	{
		ViewerResourceHandle resourceHandle = getResourceHandle( );
		if ( resourceHandle != null )
			return resourceHandle.getMessage( key );

		return key;
	}

	/**
	 * Gets the localized message with the resource key and arguments.
	 * 
	 * @param key
	 *            the resource key
	 * @param arguments
	 *            the set of arguments to place the place-holder of message
	 * @return the localized message for that key and the locale set in the
	 *         constructor. Returns the key itself if the message was not found.
	 */

	public static String getMessage( String key, Object[] arguments )
	{
		ViewerResourceHandle resourceHandle = getResourceHandle( );
		if ( resourceHandle != null )
			return resourceHandle.getMessage( key, arguments );

		return key;
	}

	/**
	 * Returns the resource handle with the locale of this thread. The resource
	 * handle will be cached.
	 * 
	 * @return the resource handle with the locale of this thread
	 */

	private static ViewerResourceHandle getResourceHandle( )
	{
		Locale locale = getLocale( );

		ViewerResourceHandle resourceHandle = (ViewerResourceHandle) resourceMap
				.get( locale );
		if ( resourceHandle != null )
			return resourceHandle;

		synchronized ( resourceMap )
		{
			if ( resourceMap.get( locale ) != null )
				return (ViewerResourceHandle) resourceMap.get( locale );

			resourceHandle = new ViewerResourceHandle( locale );
			resourceMap.put( locale, resourceHandle );
		}

		return resourceHandle;
	}
	

}

@@ -1075,17 +1075,16 @@
 al( );%0A%0A
-%0A
 %09/**%0A%09 *
@@ -2815,32 +2815,791 @@
 return key;%0A%09%7D%0A%0A
+%09/**%0A%09 * Returns the string from the plugin's resource bundle, or 'key' if not%0A%09 * found.%0A%09 * %0A%09 * @param key%0A%09 *            resource key%0A%09 * @return resource string%0A%09 * @deprecated see getMessage( String key )%0A%09 */%0A%09public static String getString( String key )%0A%09%7B%0A%09%09return getMessage( key );%0A%09%7D%0A%0A%09/**%0A%09 * Returns the string from the plugin's resource bundle, or 'key' if not%0A%09 * found.%0A%09 * %0A%09 * @param key%0A%09 *            resource key%0A%09 * @param arguments%0A%09 *            list of arguments%0A%09 * @return locale string%0A%09 * @deprecated the getMessage( String key ) will returnt he formated%0A%09 *             message.%0A%09 */%0A%09public static String getFormattedString( String key, Object%5B%5D arguments )%0A%09%7B%0A%09%09return MessageFormat.format( getString( key ), arguments );%0A%09%7D%0A%0A
 %09/**%0A%09 * Returns
@@ -3664,16 +3664,16 @@
 esource%0A
-
 %09 * hand
@@ -4290,12 +4290,10 @@
 le;%0A
+
 %09%7D
-%0A%09
 %0A%0A%7D%0A

Handle the backward compatibility for BirtResources.