/*
 *
 * Copyright (C)   6 sept. 2003 Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 */
package jxtray.swing;

import org.xml.sax.ContentHandler;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.ImageIcon;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import jxtray.Jxtray;


public class JMenuBarContentHandler implements ContentHandler
{

	private JMenuBar menubar;
	private JxtMenuListener listener;
	
	private JMenu curMenu = null;
	private JMenu curMenuItem = null;
	private Locator locator;
	private Map namespaceMappings;
	
	public JMenuBarContentHandler( JMenuBar menubar, JxtMenuListener listener)
	{
			this.menubar = menubar;
			this.listener = listener;
			this.namespaceMappings = new HashMap();
	}
	
	public void setDocumentLocator( Locator locator) 
	{
			this.locator = locator;
	}
	
	public void startDocument() throws SAXException 
	{
	}
	
	public void endDocument() throws SAXException 
	{
	}
	
	public void processingInstruction ( String target, String data) throws SAXException
	{
		
	}
	
	public void startPrefixMapping(String prefix, String uri) 
	{
			namespaceMappings.put(uri,prefix);
	}
	
	public void endPrefixMapping(String prefix ) 
	{
			for ( Iterator i = namespaceMappings.keySet().iterator(); i.hasNext();)
			{
					String uri = (String) i.next();
					String thisPrefix=  (String) namespaceMappings.get(uri);
					if ( prefix.equals(thisPrefix) )
					{
							namespaceMappings.remove(uri);
							break;
					}
			}
	}
	
	public void startElement ( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
	{
		if ("menubar".equals(localName) ) // if menubar do nothing. Object is already created
		{
		}

		else if ("menu".equals(localName) ) // new menu
		{
				JMenu element = new JMenu( atts.getValue("text"));
				String tIcon = atts.getValue("icon");
				if ( tIcon.length() > 0 )
					element.setIcon( new ImageIcon(getClass().getResource("/images/menu/"+tIcon)));

				element.setFont(Jxtray.getFont());
				menubar.add(element);
				curMenu = element ;
		}
		else if ("menuitem".equals(localName) ) // new menuitem to add to preview menu
		{
					JMenuItem curItem = new JMenuItem(atts.getValue("text"));
					curItem.setFont(Jxtray.getFont());
					curItem.setActionCommand(atts.getValue("action"));
					curItem.addActionListener(listener);
					String tIcon = atts.getValue("icon");
					if ( tIcon.length() > 0 )
						curItem.setIcon( new ImageIcon(getClass().getResource("/images/menu/"+tIcon)));
					curMenu.add(curItem);
		}
			

			
			
			// Traitement des attributs.
		/*	for ( int i=0; i < atts.getLength(); i++)
			{
					DefaultMutableTreeNode attribute = new DefaultMutableTreeNode(atts.getLocalName(i)+" ='"+atts.getValue(i));
					
					String attURI = atts.getURI(i);
					if ( attURI.length() > 0 )
					{
							String attPrefix = (String) namespaceMappings.get(namespaceURI);
							if ( attPrefix.equals("") )
							{
								attPrefix = "[Aucun]";
							}
							DefaultMutableTreeNode attNamespace = new DefaultMutableTreeNode("Escpace de nommage: prefixe= '"+attPrefix+"', URI='"+attURI+"'");
							attribute.add(attNamespace);
					}
					current.add(attribute);
			}*/
	}
	
	public void endElement ( String namespaceURI, String localName, String qName) throws SAXException
	{
		
	}
	
	public void characters ( char[] ch, int start, int length) throws SAXException
	{	
	}
	
	public void ignorableWhitespace ( char[] ch, int start, int length) throws SAXException
	{	
	}
	public void skippedEntity ( String name) throws SAXException
	{	
	}	
	
	
}
