/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.server.resources;

import org.bodington.xml.XMLUtils;
import java.io.*;
import javax.swing.tree.*;

public class MenuItem extends javax.swing.tree.DefaultMutableTreeNode
	{
	public static final int TYPE_UNKNOWN			= 0;
	public static final int TYPE_CONTAINER			= 1;
	public static final int TYPE_RESOURCE			= 2;
	public static final int TYPE_FILE				= 3;
	public static final int TYPE_INTERNAL_LINK	= 4;
	public static final int TYPE_EXTERNAL_LINK	= 5;
	
	private static final String[] TYPE_NAMES = { "unknown", "container", "interactive", "file", "inlink", "exlink" };
	
	private String title;
	private String href;
	private int type;
    private long size;
	
	public MenuItem( String t, String h, String ty, long size )
		{
		super();
		title = t;
		href = h;
		if ( ty!=null )
			setType( ty );
        this.size = size;
		}

	public String getTitle()
		{
		return title;
		}

	public void setTitle( String t )
		{
		title = t;
		}
		
	public String getHRef()
		{
		return href;
		}

	public void setHRef( String h )
		{
		href = h;
		}
		
	public String getType()
		{
		return TYPE_NAMES[type];
		}

	public void setType( String t )
		{
		type = TYPE_UNKNOWN;
		for ( int i=0; i< TYPE_NAMES.length; i++ )
			if ( TYPE_NAMES[i].equals( t ) )
				type = i;
		}

    public long getSize()
        {
        return size;
        }
    
    public void setSize(long size)
        {
        this.size = size;
        }
        
    
	public String toString()
		{
		if ( title == null )
			{
			if ( href == null || href.length() == 0 )
				return "";
			return "untitled menu item";
			}
		else
			return title;
		}

	public void emit( OutputStream out )
	    throws IOException
	    {
	    Writer writer = new OutputStreamWriter( out, "UTF8" );
	    emit( writer, "utf-8" );
	    }
	    
	public void emit( Writer writer, String encoding )
	    throws IOException
	    {
	    PrintWriter pwriter = new PrintWriter( writer );
	    emit( pwriter );
	    }

	public void emit( PrintWriter writer )
	    throws IOException
	    {
		writer.print( "<item" );
		
		if ( getChildCount() == 0 )
		    writer.print( " type=\"file\"" );
		else
		    writer.print( " type=\"container\"" );

		if ( href!=null && href.length()>0 )
		    {
		    writer.print( " href=\"" );
		    writer.print( href );
		    writer.print( "\"" );
		    }
        writer.println( ">" );
        if ( title!=null )
            {
            writer.print( "<title>" );
            writer.print( XMLUtils.toPCData( title ) );
            writer.println( "</title>" );
            }
            
		MenuItem child;
		for ( int i=0; i<getChildCount(); i++ )
		    {
		    child = (MenuItem)getChildAt( i );
		    child.emit( writer );
		    }
		    
		writer.println( "</item>" );
	    }
	}
	
