/* ======================================================================
   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.*;
import javax.swing.*;
import java.beans.PropertyVetoException;
import java.awt.*;
import java.awt.event.*;

public class ProgressDialog extends javax.swing.JInternalFrame implements ActionListener
	{
	ProgressPanel panel;
	ProgressListener listener=null;
	boolean cancelled;
	boolean busy;
	
	public ProgressDialog( JDesktopPane desktop, String title, String message, boolean can_cancel )
		{
		super( title, false, false, false, false );
		
		setVisible(false);
		
		panel = new ProgressPanel();
		panel.setCancelEnabled( can_cancel );
		panel.setMessage( message );
		JProgressBar bar = panel.getProgressBar();
		bar.setString( message );
		bar.setValue(50);
		panel.addActionListener( this );
		getContentPane().add( panel );
		desktop.add( this );
		
		
		
		try
		    {
		    setMaximum( true );
		    }
		catch ( PropertyVetoException pvex )
		    {
		    }
		
		cancelled=false;
		busy=false;
		}

	public void setVisible( boolean vis )
	    {
	    JDesktopPane desktop = BodingtonDesktopPane.findInstance( this );
	    
	    if ( desktop != null )
		{
		JInternalFrame[] list = desktop.getAllFrames();
		for ( int i=0; i<list.length; i++ )
		    {
		    if ( list[i] == this )
			{
			setLocation( 
				(desktop.getWidth()/2)-(getWidth()/2), 
				(desktop.getHeight()/2)-(getHeight()/2) );
			}
		    else
			list[i].setEnabled( !vis );
		    }
		}
	    super.setVisible( vis );
	    }
		
		



	void ProgressDialog_WindowClosing(java.awt.event.WindowEvent event)
		{
		dispose();
		}
		

	public void dispose()
	    {
	    Exception e = new Exception( "Test Exception" );
	    e.printStackTrace();
	    super.dispose();
	    }

	public void actionPerformed(java.awt.event.ActionEvent event)
		{
		if ( event.getActionCommand().equalsIgnoreCase( "cancel" ) )
		    doCancel();
		if ( event.getActionCommand().equalsIgnoreCase( "close" ) )
		    {
		    setVisible( false );
		    dispose();
		    if ( listener != null )
			listener.progressCompleted( this, false );
		    }
		}
	
	public boolean isCancelled()
	    {
	    return cancelled;
	    }
	    
	public synchronized boolean isBusy()
	    {
	    return busy;
	    }

	public synchronized void setBusy( boolean b )
	    {
	    busy = b;
	    }
	
	public synchronized void doCancel()
		{
		cancelled=true;
		panel.setCancelEnabled( false );
		if ( busy )
		    {
		    panel.setErrorMessage( "Transfer will stop after current file..." );
		    }
		else
		    {
		    panel.setErrorMessage( "Stopping..." );
		    panel.setCloseEnabled( true );
		    }
		}


	public void setMessage( String message )
		{
		JProgressBar bar = panel.getProgressBar();
		bar.setString( message );
		}

	public void setErrorMessage( String message )
		{
		panel.setErrorMessage( message );
		}

	public void setProgress( int percent )
		{
		System.err.println( "Progress bar to " + percent );
		JProgressBar bar = panel.getProgressBar();
		if ( percent <0 )
			bar.setValue( 0 );
		else if ( percent > 100 )
			bar.setValue( 100 );
		else
			bar.setValue( percent );
		}
		
	public void setProgressListener( ProgressListener listener )
	    {
	    this.listener = listener;
	    }
	}
	
