package jas2.util.pluginmanager;

import javax.swing.border.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import jas2.util.*;
import java.util.*;
import java.io.*;

public class PluginDownloadProgress extends JASDialog
{	
	private static JFrame frame;
	private PluginDownloadThread thread;
	private JLabel message;
	private JProgressBar progress;
	private int count;
	private int done = 1;
	private boolean canceled;
	private Object[] plugins;
	private String path;
	private Container c;

	public PluginDownloadProgress(Object[] plugins, String path)
	{
		super(frame, "Download Progress", true,JASDialog.CANCEL_BUTTON);
		this.plugins =plugins;
		this.path = path;
		
		
		c = getContentPane();
		c.removeAll();
		c.setLayout(new BorderLayout());
		count = plugins.length;

		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10,10,10,10));
		
		message = new JLabel("Hello World Hello World Hello World");
		message.setBorder(new EmptyBorder(10,10,10,10));
		panel.add(BorderLayout.NORTH,message);

		progress = new JProgressBar();
		progress.setStringPainted(true);
		panel.add(BorderLayout.CENTER,progress);

		setCancelLabel("Stop");
		this.setCancelMnemonic('S');
		
		c.add(BorderLayout.CENTER,panel);
		addWindowListener(new WindowHandler());
		
		thread = new PluginDownloadThread(this,plugins,path);

		pack();
		show();
	}

	public void downloading(String plugin)
	{
		showMessage("downloading: "+plugin);
	
	}

	public void setMaximum(final int total)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				progress.setMaximum(total);
			}
		});
	}

	public void setValue(final int value)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				progress.setValue(value);
			}
		});
	}

	public void done()
	{
				
		if(canceled)return;
		
		if(done == count)
		{
			SwingUtilities.invokeLater(new Runnable()
			{
				public void run()
				{
					if(canceled) return;
					onSuccesfulCompletion();
					dispose();
				}
			});
		}
		else
		{
			SwingUtilities.invokeLater(new Runnable()
			{
				public void run()
				{
					progress.setValue(0);
				}
			});
			done++;
		}
	}
	
	
	private void showMessage(final String msg)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				message.setText(msg);
			}
		});
	}

	

	class WindowHandler extends WindowAdapter
	{
		public void windowClosing(WindowEvent evt)
		{
			thread.stopRequest(); //not need as calls onCancel any way
			dispose();
		}
	}
	
	
	public void onCancel(){
		thread.stopRequest();
		canceled = true;
		long i= System.currentTimeMillis();
		try{
			thread.join(5000);
		}catch(InterruptedException ie){
		
		}
		
		
		onCancelFileClean();
		dispose();
	}
	
	private void onCancelFileClean(){
		
		String[] options ={"OK"};
		PluginProperties dummyplug;
		String pluginstring="";
		File removefile=null;
		int i=0;
		while(i 

Ads help maintain this website.