/* ======================================================================
   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 java.io.*;
import javax.swing.tree.*;

public class Menu extends javax.swing.tree.DefaultTreeModel
	{
	boolean automatic;
	
	public Menu( TreeNode root )
		{
		super( root );
		automatic = false;
		}

	public Menu( TreeNode root, boolean asksAllowsChildren )
		{
		super( root, asksAllowsChildren );
		automatic = false;
		}
	
	public void setAutomatic( boolean b )
		{
		automatic = b;
		}

	public boolean isAutomatic()
		{
		return automatic;
		}
		
	public void emit( OutputStream out )
	    throws IOException
	    {
	    Writer writer = new OutputStreamWriter( out, "UTF8" );
	    emit( writer, "utf-8" );
	    writer.flush();
	    }
	    
	public void emit( Writer writer, String encoding )
	    throws IOException
	    {
	    PrintWriter pwriter = new PrintWriter( writer );
	           
		pwriter.print( "<?xml version=\"1.0\" encoding=\"" );
		pwriter.print( encoding );
		pwriter.println( "\" ?>" );
		pwriter.println( "<menu automatic=\"false\">" );
		
		MenuItem item = (MenuItem)getRoot();
		MenuItem child;
		for ( int i=0; i<item.getChildCount(); i++ )
		    {
		    child = (MenuItem)item.getChildAt( i );
		    child.emit( pwriter );
		    }
		pwriter.println( "</menu>" );
	    pwriter.flush();
	    }

	}


