 /*
 * Copyright (C) 24-mar.-2004  Jean-Yves Beaujean 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,  59
 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */
package jxtray.gui;


import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;

import java.awt.*;
import java.awt.event.*;

import java.sql.*;

import jxtray.Jxtray;
import jxtray.Messages;

import jxtray.swing.JxtList;

import jxtray.swing.JxtButton;


import jxtray.table.SQLHistoryTable;

public class SQLHistoryDialog extends JDialog implements ActionListener, ListSelectionListener
{
	
	private JPanel pNorth;
	private JxtList listHist ;
	
	private JxtButton bModify = new JxtButton(Jxtray.getLang("action.modify"),this,Jxtray.getLang("action.modify"));
	private JxtButton bDelete = new JxtButton(Jxtray.getLang("action.delete"),this,Jxtray.getLang("action.delete"));
	
	private JScrollPane scrollData = new JScrollPane();
	

	private Connection connection;
	
	private int mode =0;
	
	private final int ADD = 1;
	private final int MODIFY = 2;
	private final int DELETE = 3;
	private final int OK = 4;

	
	public SQLHistoryDialog(JFrame parent)
	{
		super(parent,Jxtray.getLang("dialog.sqlhistory.title"),true);
		this.getContentPane().setLayout( new BorderLayout());
		this.setSize(700,350);
		this.setResizable(false);	
		
		try
		{
			init();
		}
		catch( Exception e )
		{
			Messages.error("SQLHistoryDialog.init() : error : "+ e );
		}		
		
		pNorth = new JPanel();

		/*pNorth.add(bModify);
		pNorth.add(bDelete);*/

		JPanel panel = new JPanel(new BorderLayout());
		
		TitledBorder border = BorderFactory.createTitledBorder("");
		border.setTitleFont(Jxtray.getFont());		
		
		scrollData = new JScrollPane(new SQLHistoryTable());

		this.getContentPane().add(pNorth,BorderLayout.NORTH);
		this.getContentPane().add(scrollData,BorderLayout.CENTER);
		
		if (listHist.getComponentCount() > 0 )
		{
			if ( Jxtray.getConnectionManager().getAllConnectionName().length > 0 )
				listHist.setSelectedIndex(0);
		}
			
	}
	
	public void init() throws Exception
	{
		listHist = new JxtList(this,Jxtray.getSQLHistoryCache().getAllQueries());
	}
	
	public void valueChanged(ListSelectionEvent e)
	{
			
	}
	
	
	
	
	public void action()
	{
			switch ( mode )
			{
					case DELETE:
						if ( listHist.getSelectedIndex() >=0 )
						{
							if(  Messages.question(Jxtray.getLang("action.delete")+" '"+listHist.getSelectedValue()+"' ?") == JOptionPane.YES_OPTION ) 
							{
								try
								{
									String toRemove = (String)listHist.getSelectedValue();
									Jxtray.getSQLHistoryCache().remove(toRemove);
									if (  listHist.getComponentCount() > 0)
										listHist.removeAll();
									listHist.removeListSelectionListener(this);
									listHist.setListData(Jxtray.getSQLHistoryCache().getAllQueries());
									listHist.addListSelectionListener(this);
									Jxtray.getSQLHistoryCache().writeXMLFile();
									
									
								}
								catch ( Exception e ) 
								{
										System.err.println("JxtConnectionDialog.action()-delete : exception : " + e );
								}
							}
						}
						mode =0;
						break;
					case MODIFY:
						break;
					default:
						break;
			}
			
	}	
	public void actionPerformed( ActionEvent e )
	{
		if ( e.getActionCommand().equals(Jxtray.getLang("action.modify")) )
		{
			mode = MODIFY;
			action();
		}
		else if ( e.getActionCommand().equals(Jxtray.getLang("action.delete")) )
		{
			mode = DELETE;
			action();
		}

	}

	

	
}
