/*
 *
 * Copyright (C) 10-sept.-2003  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.swing;

import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.awt.*;
import java.awt.event.*;

import jxtray.Jxtray;
import jxtray.JxtrayConstants;
import jxtray.connection.JxtConnection;
import jxtray.Messages;

import jxtray.dump.DumperListener;

import java.util.Vector;

public class JxtDumpPanel extends JPanel implements ItemListener, DumperListener
 {
	
		private JxtButton bDump;
		private JPanel pConfig =new JPanel();
		private JPanel pDump = new JPanel();
	 	private JxtEditorPane jedit = new JxtEditorPane();
	 	private JxtDumpPanelListener dumpListener;
	 	private String connectionName;
		private JxtComboBox comboCatalog = new JxtComboBox();
		private JxtComboBox comboSchema = new JxtComboBox();
		private JxtComboBox comboFormat = new JxtComboBox(JxtrayConstants.SUPPORTED_DUMPFORMAT);
		private JxtComboBox comboTable = new JxtComboBox();
	    private JCheckBox chkZip = new JCheckBox();
	    private JxtConnection connection = null;
	    private JxtProgressBar pbar = new JxtProgressBar();
	    //private MemoryViewer memViewer= new MemoryViewer();

		
		public JxtDumpPanel(String conName, JxtConnection connection, String catalog[], String schema[])
		{	
			super(new BorderLayout(),true);
			connectionName = conName; 
			this.connection = connection;
			this.setFont(Jxtray.getFont());
			
			dumpListener = new JxtDumpPanelListener(this);
			pDump.setLayout(new BorderLayout());
			
			chkZip.setFont(Jxtray.getFont());
			chkZip.setText(Jxtray.getLang("action.zip")); 
			
			comboTable.setEditable(true);
			
			comboFormat.setEditable(false);
			comboFormat.addItemListener(this);
			
			if ( catalog.length > 0 )
				comboCatalog = new JxtComboBox(catalog);
			else
			{
				comboCatalog = new JxtComboBox();
				comboCatalog.setVisible(false);
			}
			
			if ( schema.length > 0 )
				comboSchema = new JxtComboBox(schema);
			else
			{
				comboSchema = new JxtComboBox();
				comboSchema.setVisible(false);
			}			
			pConfig.add(comboCatalog);
			pConfig.add(comboSchema);
			pConfig.add(comboTable);
			pConfig.add(new JxtJLabel("format "));
			
			pConfig.add(comboFormat);
		
			bDump = new JxtButton();
			bDump.addActionListener(dumpListener);
			bDump.setActionCommand("dump");
			bDump.setToolTipText(Jxtray.getLang("action.dump"));
			bDump.setIcon(new ImageIcon(getClass().getResource("/images/exec.png")));
			
			pConfig.add(bDump);
			pConfig.add(chkZip);
			
			pbar.setForeground( new Color(54,80,125));
			pbar.setOpaque(true);
			jedit.setEditable(false);
			
			this.add(pConfig, BorderLayout.NORTH);
			
			pDump.add(pbar,BorderLayout.NORTH);
			pDump.add(new JScrollPane(jedit),BorderLayout.CENTER);
			//pDump.add(memViewer,BorderLayout.EAST);
			
			this.add(pDump, BorderLayout.CENTER);
			
			try
			{
				init();
			}
			catch( Exception e )
			{
				Messages.error("JxtStructurePanel.init() : "+ e);
			}		
			
			this.revalidate();
			
		}
		
		public void init() throws Exception
		{
				comboTable.addItemListener(this);
				Vector v = connection.getTables((String)comboCatalog.getSelectedItem(),(String) comboSchema.getSelectedItem());
				for ( int i=0; i<v.size();i++)
				comboTable.addItem(v.elementAt(i));
				comboCatalog.addItemListener(this);
				comboSchema.addItemListener(this);
				
		}
		

		/**
		 * @return JxtComboBox
		 */
		public String getTableName() 
		{
			return (String) comboTable.getSelectedItem();
		}
		
		public JxtComboBox getComboTable() 
		{
			return comboTable;
		}

		

		/**
		 * @return String
		 */
		public String getDumpFormat() 
		{
			return (String) comboFormat.getSelectedItem();
		}
		
		public boolean isCompressChecked()
		{
			return chkZip.isSelected();
		}
		
		public JTextComponent getLogComponent()
		{
			return (JTextComponent) jedit;
		}
		

		/**
		 * @return String
		 */
		public String getDumpResult() 
		{
			return jedit.getText();
		}

		/**
		 * @param res
		 * void
		 */
		public void setDumpResult(String res) 
		{
			this.jedit.setText(res);
		}

		/**
		 * @return String
		 */
		public String getConnectionName() 
		{
			return connectionName;
		}
		
		public void itemStateChanged( ItemEvent e )
		{
				/** Catalog or Schema has been changed */
				if ( e.getSource().equals(comboCatalog) || e.getSource().equals(comboSchema) )
				{
					try
					{
						comboTable.removeAllItems();
						//comboTable.addItem("*");
						Vector v = connection.getTables((String)comboCatalog.getSelectedItem(),(String) comboSchema.getSelectedItem());
						for ( int i=0; i<v.size();i++)
							comboTable.addItem(v.elementAt(i));
						
					}
					catch ( Exception ex )
					{
							Messages.error("JxtStructurePanel.itemStateChanged() : " + ex);
					}
				}
	}
	
	public String getSchemaName()
	{
			if ( comboSchema.getItemCount() > 0 )
					return (String) comboSchema.getSelectedItem();
			else
				return "";
	}
		
	public String getCatalogName()
	{
			if ( comboCatalog.getItemCount() > 0 )
					return (String) comboCatalog.getSelectedItem();
			else
				return "";
	}	
	
	public void dumpLog(String msg)
	{
		jedit.setText(jedit.getText()+msg);
	}
	
	public void dumpEnded()
	{
		pbar.setIndeterminate(false);
		pbar.setValue(pbar.getMaximum());
		pbar.setString(Jxtray.getLang("dump.completed"));
		Jxtray.getMainFrame().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
		jedit.setEnabled(true);
		//memViewer.stop();
	}
	
	public void dumpNextRecord(int value)
	{
		pbar.setString(Jxtray.getLang("dump.current_record")+value);
	}
	
	public void dumpState( String msg)
	{
		pbar.setString(msg);
	}
	
	public void dumpStarted()
	{
		//memViewer.start();
		jedit.setEnabled(false);
		pbar.setStringPainted(true);
		pbar.setIndeterminate(true);
	}
	
	public void dumpError(String msg)
	{
		
		Messages.error(msg);
	}

}
