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

import org.apache.log4j.Logger;

import java.util.*;
import javax.swing.tree.*;

public class PackageFileSystemModel extends javax.swing.tree.DefaultTreeModel
	{
    
    private static Logger log = Logger.getLogger(PackageFileSystemModel.class);
        
	private Package ims_package;
	
	public PackageFileSystemModel( Package p )
		{
		// dummy node to start with...
		super( new DefaultMutableTreeNode() );
		
		ims_package = p;
		setRoot( new PackageFileSystemNode( null, "", true ) );
		syncWithPackage();
		}
		
		
	public void addFile( PackageFile file, String href, boolean folder )
		{
		StringTokenizer tokenizer;
		int i, comparison;
		String part;
        PackageFile pf=null;
        
		if ( href.startsWith( "/" ) )
			throw new IllegalArgumentException( "File href must not start with slash" );

		tokenizer = new StringTokenizer( href, "/" );
		
		PackageFileSystemNode child;
		PackageFileSystemNode current = (PackageFileSystemNode)getRoot();
		boolean f;
		while ( tokenizer.hasMoreTokens() )
			{
			part = tokenizer.nextToken();
			if ( !tokenizer.hasMoreTokens() )
			    pf = file;
			    
			f = tokenizer.hasMoreTokens() || folder;

			comparison=-1;
			child = null;
			for ( i=0; i<current.getChildCount(); i++ )
				{
				child = (PackageFileSystemNode)current.getChildAt( i );
				comparison =  child.getHref().compareTo( part );
				if ( comparison >=0 )
					break;
				}

			if ( comparison == 0 )
				{
				if ( child.isFolder() != f )
					throw new IllegalArgumentException( "File/folder name clash." );
				}
					
			if ( comparison > 0 )
				{
				child = new PackageFileSystemNode( pf, part, f );
				current.insert( child, i );
				}

			if ( comparison < 0 )
				{
				child = new PackageFileSystemNode( pf, part, f );
				current.add( child );
				}

			current = child;
			}
		}

	private void syncWithPackageNode( TreeNode node )
		{
		Enumeration enumeration;
		TreeNode child;
		String href;
		PackageFile file;
		
		log.debug( node.toString() );
		
		if ( node instanceof PackageManifest ||
		     node instanceof PackageResources ||
		     node instanceof PackageResource )
			{
			enumeration = node.children();
			while ( enumeration.hasMoreElements() )
				{
				child = (TreeNode) enumeration.nextElement();
				syncWithPackageNode( child );
				}
			return;
			}

		if ( node instanceof PackageFile )
			{
			file = (PackageFile)node;
			href = file.getHref();
			
			addFile( (PackageFile)node, href, false );
			}
		}
		
	private void syncWithPackage()
		{
		TreeNode node = (TreeNode)ims_package.getRoot();
		syncWithPackageNode( node );
		}
	}
