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

import org.bodington.applet.components.*;
import org.bodington.applet.data.*;
import java.awt.*;
import java.io.*;
import java.net.*;

import javax.swing.*;
import javax.swing.text.*;
import javax.swing.text.html.*;

public class TransferRemoteFileDialog extends org.bodington.applet.components.TransferDataDialog 
    implements java.lang.Runnable
    {
	File file;
	URL url;
	JEditorPane editor;
	Object describer;
	
	TransferRemoteFileItem[] items;
	int current;
	
	Thread worker=null;
        
	public TransferRemoteFileDialog( JDesktopPane desktop )
	    {
		super( desktop, "Retrieving Published File", "Preparing to Retrieve File", true );
    	}

    public synchronized boolean isInProgress()
	{
	return worker != null;
	}
	
	public void transfer( TransferRemoteFileItem[] items )
		throws IOException
		{
		if ( worker!=null )
			throw new IOException( "Transfer already in progress." );
		
		this.items = items;
		current=0;
		this.editor = null;

		worker = new Thread( this );
		setBusy( true );
		worker.start();
		
		setVisible( true );
		}
		
	public void transfer(  URL url, JEditorPane target, Object desc )
	    throws IOException
	    {
		if ( worker!=null )
			throw new IOException( "Transfer already in progress." );
		
		this.file = null;
		this.editor = target;
		this.describer = desc;
		this.url = url;
		worker = new Thread( this );
		worker.start();
		
		setVisible( true );
	    }

	public void run()
		{
		try
			{
			if ( editor == null )
			    runDownloadToFile();
			else
			    runDownloadToEditor();
		    if ( listener != null )
			listener.progressCompleted( this, false );
   		    }
   	    catch ( Exception ex )
   		    {
   		    ex.printStackTrace();
		    if ( listener != null )
			listener.progressCompleted( this, true );
   		    }
	finally
	    {
	    worker = null;
	    setBusy( false );
	    panel.setCloseEnabled( true );
	    panel.setCancelEnabled( false );
	    }	
		}
    
    private void runDownloadToFile()
        throws IOException
        {
	    for ( current=0; current<items.length; current++ )
		{
		if ( cancelled )
		    {
		    panel.setErrorMessage( "Download cancelled." );
		    return;
		    }
			    
			BufferedOutputStream fout = 
				new BufferedOutputStream( new FileOutputStream( items[current].file ) );

		startGet( items[current].url );
		InputStream in = getResponse();

		byte[] buf = new byte[2048];
			int n;
			while ( (n = in.read( buf )) > 0 )
			    {
			    fout.write( buf, 0, n );
			    if ( cancelled )
				{
				panel.setErrorMessage( "Download cancelled." );
				fout.close();
				in.close();
				items[current].file.delete();
				return;
				}
			    }

			fout.close();
			in.close();

		setMessage( "Transfer Complete" );
		setProgress( 100 );
		}
        }

    private void runDownloadToEditor()
        throws IOException
        {
        startGet( url );
       	InputStreamReader reader = new InputStreamReader( getResponse(), "UTF-8" );

        editor.read( reader, describer );

   		reader.close();
   		
       	setMessage( "Transfer Complete" );
   	    setProgress( 100 );
        }
    }
    
