package org.fmunch.swing;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Dimension;
import java.util.ResourceBundle;
import java.util.Locale;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Map;
import java.util.HashMap;
import javax.swing.JComponent;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.net.URL; import java.net.URLClassLoader;
import java.util.PropertyResourceBundle;

public class LangChanger
{


	private static PropertyResourceBundle reloadPropertyResourceBundle(URL url,Locale local) throws Exception {
		PropertyResourceBundle prb = null;
		try {
		ClassLoader loader = URLClassLoader.newInstance(new URL[] {url});
		prb = (PropertyResourceBundle) PropertyResourceBundle.getBundle("propertyFileName", local, loader);
		}
		catch (java.util.MissingResourceException mre) {
			System.err.println("Problem on realoding");
		}
		return prb;
	} 


	Map<String,List<JComponent>> componentsmap;
	String bundlename;
	public LangChanger(String bundlename)
	{
		this.bundlename = bundlename;
		locale = Locale.getDefault();
		bundle.clearCache();
//		try
//		{
			bundle = ResourceBundle.getBundle(bundlename,locale);
//		}catch(java.lang.Exception e)
//		{
//			System.err.println("ex");
//		}
		componentsmap = new HashMap<String,List<JComponent>>();
	}

	void build(java.awt.Component jc)
	{
		if(jc instanceof JFrame)
		{
			java.awt.Container cont = ((javax.swing.JFrame)jc).getContentPane();
			int count = cont.getComponentCount();
			for(int i  = 0 ; i < count ; i++)
			{
				build(  cont.getComponent(i) );
			}
		}

		// other work before in case of JFrame
		if(jc instanceof JPanel || jc instanceof JFrame)
		{
			int count = ((java.awt.Container)jc).getComponentCount();
			for(int i  = 0 ; i < count ; i++)
			{
				build(  ((java.awt.Container)jc).getComponent(i) );
			}
			return;
		}

		if(jc instanceof JComponent)
		{
			String text=null;
			if(jc instanceof JLabel)
			{
				text = ((JLabel)jc).getText();
				//System.out.println("add a JLabel with text = "+text);
			}
			if(jc instanceof JButton)
			{
				text = ((JButton)jc).getText();
				//System.out.println("add a JButton with text = "+text);
			}
			if(text == null ) { return ; }
			if(!componentsmap.containsKey(text))
			{
				componentsmap.put(text,new ArrayList<JComponent>());
			}
			componentsmap.get(text).add((JComponent)jc);	
		}
	}

	Locale locale;
	ResourceBundle bundle;
//	String [] langlist = { "en_IE","fr_FR"};
	Locale [] langlist = { Locale.ENGLISH, Locale.FRENCH };
	int langid=0;
	
	void changLang(int id)
	{
		locale = langlist[id];
		//ResourceBundle.clearCache();
		bundle = ResourceBundle.getBundle(bundlename,locale);
		reload();
	}
	void reload()
	{
		//System.out.println("Loading another language to : "  + locale);
		Iterator<String> key_it = componentsmap.keySet().iterator();
		while( key_it.hasNext() )
		{
			String key = key_it.next();
			Iterator<JComponent> it = componentsmap.get(key).iterator();
			String text = bundle.getString(key);
			while( it.hasNext() )
			{
				JComponent comp = it.next();
				if(comp instanceof JLabel)
				{
					//System.out.println("JL to " + text);
					((JLabel)comp).setText(text);
				}
				if(comp instanceof JButton)
				{
					//System.out.println("JB to " + text);
					((JButton)comp).setText(text);
				}

			}
		}
	}
	void nextLang()
	{
		langid ++;
		langid %= langlist.length;
		changLang(langid);
	}

	public static void main(String []args)
	{
		final LangChanger lc=new LangChanger("org.fmunch.swing.LangChanger");
		JFrame jf=new JFrame("Testing Frame");
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jf.setPreferredSize(new Dimension(400,300));
		JLabel label = new JLabel("this_is_a_label");
		JButton jb = new JButton("Change_language");
		jb.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent evt)
			{
				lc.nextLang();
			}
		});
		jf.getContentPane().setLayout(new java.awt.FlowLayout());
		jf.getContentPane().add(label);
		jf.getContentPane().add(jb);
		lc.build(jf);
		lc.nextLang();
		jf.setVisible(true);
		jf.pack();
	}
}


