import javax.swing.SwingUtilities;
import java.util.Vector;
import dataccesstool.application.Logger;

/**
  * This class implements a standard way to execute time consuming tasks
  * that would normally be executed in the Event Dispatcher Thread.
  * In Java such tasks would lock the GUI and prevent normal painting and
  * event handling from occurring.  This class provides a mechanism for
  * handling such tasks so that they do NOT lock the GUI up.
  * Any class which needs to complete a time consuming task but not do
  * it on the EVD Thrd should instantiate a DaSwingWorkerThread and in 
  * the constructor call include a reference to itself and a string name
  * for the event being handled.
  * This class will then launch another thread and execute the construct
  * method.  Our implementation for construct is to set the variables for
  * the calling object and the event string.  Then based on the
  * values of those variables the construct will execute a specialized 
  * method per the "if" statement in the construct method.
  * Once the construct is completely finished the class then automatically
  * executes the finished() method ON THE EVENT DISPATCHER THREAD, so that
  * any GUI updates needed to reflect the result of our work can be made.
  *
  * @author Randy Curnutt, Solutions Plus, Inc.
  *
  * @version 2.10
  */
public class DaSwingWorkerThread extends SwingWorker {
	protected String callingEventString;
	protected Object callingObject;
	protected Vector parmObjects;
	
public DaSwingWorkerThread(Object aCallingObject, String anEventString)
{
	this.callingObject = aCallingObject;
	this.callingEventString = anEventString;
	//call the default constructor for SwingWorker class
	//note, since a call to the constructor would have to be the 
	//first line in this method and we want to init the vars prior
	//to running the constructor the lines of code are copied here 
	//instead of executing the super() method.  This allows us to
	//init our vars before executing the super code.

	if (Debug.isDEBUG())
	{
		Logger.LogTraceStatement(" ");
		Logger.LogTraceStatement(" DaSwingWorkerThread callingobject = " + callingObject);
		Logger.LogTraceStatement(" DaSwingWorkerThread callingEventString = " + callingEventString);
		Logger.LogTraceStatement(" ");
	}
	final Runnable doFinished = new Runnable()
	{
		public void run()
		{
			finished();
		}
	};
	Runnable doConstruct = new Runnable()
	{
		public void run()
		{
			synchronized (DaSwingWorkerThread.this)
			{
				if (Debug.isDEBUG())
					Logger.LogTraceStatement("start DaSWT.constr ... ");
				value = construct();
				if (Debug.isDEBUG())
					Logger.LogTraceStatement("DaSWT.constr done,value = " + value);
				thread = null;
			}
			if (Debug.isDEBUG())
				Logger.LogTraceStatement("start DaSWT.doFinished ... ");
			SwingUtilities.invokeLater(doFinished);
		}
	};
	thread = new Thread(doConstruct);
	//Logger.LogTraceStatement("DaSwingWorkerThread.start() ") ;

	thread.start();
}
public DaSwingWorkerThread(Object aCallingObject, String anEventString, Vector aVectorOfParms)
{
	this(aCallingObject, anEventString);
	this.parmObjects = aVectorOfParms;
}
protected boolean completedSubsetDlgOkay(SubsetDatasetDialog anSdd)
{
	class DoShowSdd implements Runnable
	{
		boolean proceedConfirmed;
		private final SubsetDatasetDialog sdd;
		public DoShowSdd(SubsetDatasetDialog anSdd)
		{
			sdd = anSdd;
		}
		public void run()
		{
			sdd.setEnabled(true);
			sdd.setVisible(true);
			int status = sdd.getStatus();
			proceedConfirmed = (status == SubsetDatasetDialog.ACTION_OK);
		} //end run method
	} // end class definition of DoShowSdd

	DoShowSdd anSddDialog = new DoShowSdd(anSdd);
	/** end code from showSdd ***/
	try
	{
		SwingUtilities.invokeAndWait(anSddDialog);
	}
	catch (java.lang.reflect.InvocationTargetException e)
	{
		Logger.LogException(e);
		e.printStackTrace();
	}
	catch (java.lang.InterruptedException e)
	{
		Logger.LogException(e);
		e.printStackTrace();
	}
	
	return anSddDialog.proceedConfirmed;
}
	public Object construct()
	{   
		if (callingEventString.equalsIgnoreCase("pfGetLibComboData")) 
		{
			return this.doWorkPfGetLibComboData();    
		} 
		
		return null;        
	}
/****************************************************************
** 2/9/98 Randy Curnutt
** This method has been modified to work with the DaSwingWorker
** Thread model and not tie up the EventDispatchingThread while
** querying MVS for variable info.  The tricky issue with this 
** method is that we must iterate through displaying MANY 
** SubsetDatasetDialogs.  So in our loop we: iterate to next,
** get var info, show MODAL DIALOG and BLOCK!, after response
** then dispose of the SDD and start the loop all over.  After
** all iterations needed are done the finish method executes
** the code to reenable the DATAccessFrame.
/***************************************************************/
private Object doWorkPfGetLibComboData()
{
	ProfileFrame theProfileFrame = (ProfileFrame) callingObject;
	return theProfileFrame.getLibraryComboData();
}
public void finishDafHandleHostComboAction()
{

//	DATAccessApplication.ClearSystemStatusMessage();  //no message was set anyway
	DATAccessFrame daf = (DATAccessFrame) callingObject;
	daf.setEnabled(true);
	daf.setDefaultCursor();	
	
	
}
public void finishDafHandleProjCode()
{

/* TODO delete when sure not needed; 01/11/00
	
	DATAccessFrame daf = (DATAccessFrame) callingObject;
	//        Logger.LogTraceStatement("Thread: " + Thread.currentThread().getName());
	if (((Boolean) value).booleanValue())
	{
		daf.getAndDisplayDataSourceTree();
		daf.setDataLibraryViaProfile();
	}
	daf.setEnabled(true);
	daf.setVisible(true);
	daf.setDefaultCursor();
	*/
}
public void finishDafShowSubsetDialog()
{
	DATAccessFrame daf = (DATAccessFrame) callingObject;
	daf.setEnabled(true);
	daf.setDefaultCursor();
	daf.setVisible(true);
}
	public void finished() {
		if (callingEventString.equalsIgnoreCase("pfGetLibComboData")) {
			this.finishPfGetLibComboData();    
		} else if (callingEventString.equalsIgnoreCase(
			"pfGetLibComboDataAndInit")) {
			this.finishPfGetLibComboDataAndInit();
		} else  if (callingEventString.equalsIgnoreCase(
			"dafShowSubsetDialog")) {
			this.finishDafShowSubsetDialog(); 
		} else  if (callingEventString.equalsIgnoreCase(
			"dafHandleProjCode")) {
			this.finishDafHandleProjCode(); 
		} else  if (callingEventString.equalsIgnoreCase(
			"dafHandleHostComboAction")) {
			this.finishDafHandleHostComboAction(); 
		}
		
	}
public void finishPfGetLibComboData()
{
	ProfileFrame theProfileFrame = (ProfileFrame) callingObject;
	theProfileFrame.initLibraryComboData();
	theProfileFrame.setDefaultCursor();
}
}
