package voi.ui;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.awt.event.*;

/**
The GuiController class is responsible for constructing the GUI Framework for the 2dfQSO system
Its split in a number of panes. 

There are two splitpanes 
1) splitPane - This is the pane which splits the window in two panes
	a) query pane on the left (leftPane)
	b) outputpane on the right (RightPane)
	
2) The rigthPane adds the following splitPane
   splitOutput - This splitpanes splits the pane in two sections
	a) Pane to describe the output results in table form
	b) pane to perfrom operations on the data results like Save, Vo-Plot etc
	
Bugs : Need to be able to resize the object


*/

class GuiController
{	
	/**
	The main function creates the GuiController object called theApp and invokes init ()
	**/
	public static void main (String[] args)
	{
		GuiController theApp = new GuiController ();
		theApp.init();
	}
	
	/**
	init creates the framework for the UI of the system as described above in the class description
	
	
	**/
	public void init()
	{
		JFrame.setDefaultLookAndFeelDecorated(true);
		window = new JFrame ("2dfQSO Database System");		
		
		Toolkit theKit = window.getToolkit();          // Get the window toolkit
		wndSize = theKit.getScreenSize();    // Get screen size

	    	window.setBounds (0,0, wndSize.width - 20, wndSize.height - 60);  // Set Size for the main Frame
		window.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); 		

	
		leftPane = new JPanel (new BorderLayout());	// creates the left and right pane
		rightPane = new JPanel (new BorderLayout());	// The left pane is for the selecting the query
								// the right one is to display/manipulate the output	
		tdfInterface = new TdfInterface (this, (int) wndSize.width/4, wndSize.height);  // create the form query module
		sqlQueryInterface = new SqlQueryInterface (this, (int) wndSize.width/4, wndSize.height);  // create the SQL query module
		
		leftPane.add (tdfInterface, BorderLayout.CENTER);	// create a left pane for selecting data
		
		JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,leftPane,rightPane);  // splitPane splits
		splitPane.setDividerLocation((int)wndSize.width/4);  					     // the main window in the
		splitPane.setOneTouchExpandable (true);						    	     // left and right pane																	
				
		dataPane = new JPanel (new BorderLayout ());	// this outputs the query results/data
		toolBar = new JPanel (new BorderLayout ());	// this performs operations on the data
			
		rightPane.add (dataPane, BorderLayout.CENTER);
		rightPane.add (toolBar, BorderLayout.EAST);
		tdfToolBar = new TdfToolBar (this);				// Creates the tool bar
		tdfToolBar.createIcons ();					// and adds the icons to it
		tdfToolBar.enableAll (false);					// initialy keep the icons diabled 
		toolBar.add (tdfToolBar, BorderLayout.CENTER);			// finally adding it to the theApp object
		
		int minWidth =  (3 * wndSize.width)/4;				// This setting is done to allow some movement
		int minHeight = (3 * wndSize.height)/4;				// to the toolbar and the output
	
		window.getContentPane().add(splitPane, BorderLayout.CENTER);	
	
		window.setVisible (true);

		window.addComponentListener(new ComponentAdapter() 
		{
			public void componentResized(ComponentEvent e) 
			{
			}
		});
	}
	
	/**
	This function adds the JScrollPane which contains the query results to the main dataPane
	**/
	public void showResults (JScrollPane results)
        {
                dataPane.add (results, BorderLayout.CENTER);
                window.setVisible (true);
        }

	/**
	This function removes the previos results (if any) and clears the output pane
	**/
	public void removeComponents ()
	{
		dataPane.removeAll ();
		dataPane.updateUI ();
	}
	
	/**
	This function returns the TdfInterface object. 
	It is required by the TdfToolBar object because it needs the query results and query Flags
	**/
	public TdfInterface getTdfInterface ()
	{
		return tdfInterface;	
	}
	
	/**
	This module returns the toolBar for other classes to which theApp is passed as a parameter
	*/
	public TdfToolBar getToolBar ()
	{
		return tdfToolBar;
	}
	
	/**
	This module switched from Form mode to Query mode 
	*/
	public void switchToQueryMode ()
	{
		switchTo (sqlQueryInterface);		
	}
	
	/**
	This module switched from Query mode to form mode
	*/
	public void switchToFormMode ()
	{
		switchTo (tdfInterface);
	}
	
	public void switchTo (JPanel currentPane)
	{
		dataPane.removeAll ();
		leftPane.removeAll ();
		leftPane.add (currentPane, BorderLayout.CENTER);
		leftPane.invalidate();
		leftPane.validate();
		leftPane.updateUI ();
		
		window.setBounds (0,0, wndSize.width - 20, wndSize.height - 60);  // Set Size for the main Frame
		dataPane.updateUI ();
		getToolBar().enableAll (false);		
	}
	
	/**
	*
	*/
	private GuiController theApp; 
	private JFrame window;
	private JPanel leftPane,rightPane, dataPane, toolBar;
	private TdfInterface tdfInterface;
	private TdfToolBar tdfToolBar;
	private SqlQueryInterface sqlQueryInterface ;
	private Dimension wndSize;
}
