/*
 *
 * Copyright (C) 05 janv. 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.gui;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.BorderFactory;

import javax.swing.JDialog;
import javax.swing.JTextField;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;


import java.awt.BorderLayout;
import java.awt.Color;


import jxtray.Jxtray;
import jxtray.JxtrayConstants;
import jxtray.Messages;

import jxtray.swing.*;

public class PropertiesDialog extends JDialog implements ActionListener
{
  
	private JTabbedPane pMain;
  
  private JPanel panelEditor;
  private JPanel panelApparence;
  private JPanel panelGeneral;
  private JPanel panelDump;
  
  private JxtComboBox comboLookAndFeel= new JxtComboBox(JxtrayConstants.SUPPORTED_LOOKNFEEL);
  private JxtComboBox comboLanguages = new JxtComboBox(JxtrayConstants.SUPPORTED_LANGUAGES);
  private JxtComboBox comboAppFont = new JxtComboBox(JxtrayConstants.SUPPORTED_FONT);
  private JxtComboBox comboAppFontSize = new JxtComboBox(JxtrayConstants.SUPPORTED_FONTSIZE);
  private JxtComboBox comboSQLFont = new JxtComboBox(JxtrayConstants.SUPPORTED_FONT);
  private JxtComboBox comboSQLFontSize = new JxtComboBox(JxtrayConstants.SUPPORTED_FONTSIZE);  
  private JxtComboBox comboSQLComment = new JxtComboBox(JxtrayConstants.SUPPORTED_SQLCOMMENT);
  
  private JxtCheckBox chkOverWrite = null;
  private JxtCheckBox chkConfirmOnExit = null;
  private JxtCheckBox chkEraseQuery = null;
  
  
  private JTextField txCsvTextDelimitor;
  private JTextField txCsvSeparator;
  
  private JTextField txLogOutput;
  private JTextField txLogError;
  
  public PropertiesDialog(JFrame frame)
  {
    super(frame, Jxtray.getLang("dialog.properties.title"), true);
    try
    {
      jbInit();
      pack();
    }
    catch(Exception ex)
    {
      System.err.println("PropertiesDialog.contructor() : "+ ex );
      ex.printStackTrace();
    }
  }


  void jbInit() throws Exception
  {
    pMain = new JTabbedPane();
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add(pMain,BorderLayout.CENTER);
    JPanel pButton = new JPanel();
    pButton.setBorder(BorderFactory.createLineBorder(Color.black));
    pButton.add(new JxtButton(new ImageIcon(getClass().getResource("/images/save.png")),Jxtray.getLang("action.save"),this,Jxtray.getLang("action.save")), null);
	pButton.add(new JxtButton(new ImageIcon(getClass().getResource("/images/close.png")),Jxtray.getLang("action.close"),this,Jxtray.getLang("action.close")), null);
    
	
	chkConfirmOnExit = new JxtCheckBox(Jxtray.getLang("properties.confirm.exit"),Jxtray.getProperty("confirm.quit"));
	chkOverWrite = new JxtCheckBox(Jxtray.getLang("properties.confirm.overwrite"),Jxtray.getProperty("confirm.overwrite"));
	chkEraseQuery = new JxtCheckBox(Jxtray.getLang("properties.sqleditor.erasequeryafterxecute"),Jxtray.getProperty("sqleditor.erasequeryafterxecute"));
	
	panelGeneral = new JPanel();
	panelGeneral.setLayout(new BoxLayout(panelGeneral,BoxLayout.Y_AXIS));
	panelGeneral.setName(Jxtray.getLang("properties.general"));
	panelGeneral.add(new JxtPropertiesPanelItem(comboLanguages,Jxtray.getLang("properties.lang")));
	
	JPanel pControl = new JPanel();
	pControl.setLayout(new BoxLayout(pControl,BoxLayout.Y_AXIS));
	pControl.setBorder(BorderFactory.createTitledBorder(Jxtray.getLang("properties.control")));

	pControl.add(new JxtPropertiesPanelItem(chkConfirmOnExit,""));
	pControl.add(new JxtPropertiesPanelItem(chkOverWrite,""));
	
	panelGeneral.add(pControl);
	
	JPanel pLOG = new JPanel();
	pLOG.setLayout(new BoxLayout(pLOG,BoxLayout.Y_AXIS));
	pLOG.setBorder(BorderFactory.createTitledBorder(Jxtray.getLang("properties.log")));
	
	txLogOutput = new JTextField();
	txLogError = new JTextField();
	pLOG.add(new JxtPropertiesPanelItem(txLogOutput,Jxtray.getLang("properties.logoutput")));
	pLOG.add(new JxtPropertiesPanelItem(txLogError,Jxtray.getLang("properties.logerror")));
	panelGeneral.add(pLOG);
	
	
	panelEditor = new JPanel();
	panelEditor.setLayout(new BoxLayout(panelEditor,BoxLayout.Y_AXIS));
	panelEditor.setName(Jxtray.getLang("properties.editor"));
	panelEditor.add(new JxtPropertiesPanelItem(chkEraseQuery,Jxtray.getLang("properties.sqleditor.erasequeryafterxecute")));
	panelEditor.add(new JxtPropertiesPanelItem(comboSQLFontSize,Jxtray.getLang("properties.sqlfontsize")));
	panelEditor.add(new JxtPropertiesPanelItem(comboSQLFont,Jxtray.getLang("properties.sqlfont")));
	
	
	panelApparence = new JPanel();
	panelApparence.setLayout(new BoxLayout(panelApparence,BoxLayout.Y_AXIS));
	panelApparence.setName(Jxtray.getLang("properties.apparence"));
	panelApparence.add(new JxtPropertiesPanelItem(comboLookAndFeel,Jxtray.getLang("properties.lookandfeel")));
	panelApparence.add(new JxtPropertiesPanelItem(comboAppFontSize,Jxtray.getLang("properties.appfontsize")));
	panelApparence.add(new JxtPropertiesPanelItem(comboAppFont,Jxtray.getLang("properties.appfont")));
	
	panelDump = new JPanel();
	panelDump.setLayout(new BoxLayout(panelDump,BoxLayout.Y_AXIS));
	panelDump.setName(Jxtray.getLang("properties.dump"));
	
	
	JPanel pCSV = new JPanel();
	pCSV.setLayout(new BoxLayout(pCSV,BoxLayout.Y_AXIS));
	pCSV.setBorder(BorderFactory.createTitledBorder(Jxtray.getLang("properties.csvformat")));
	txCsvSeparator = new JTextField();
	txCsvTextDelimitor = new JTextField();
	pCSV.add(new JxtPropertiesPanelItem(txCsvSeparator,Jxtray.getLang("properties.csvseparator")));
	pCSV.add(new JxtPropertiesPanelItem(txCsvTextDelimitor,Jxtray.getLang("properties.csvtextdelimitor")));
	
	JPanel pSQL = new JPanel();
	pSQL.setLayout(new BoxLayout(pSQL,BoxLayout.Y_AXIS));
	pSQL.setBorder(BorderFactory.createTitledBorder(Jxtray.getLang("properties.sqlformat")));
	pSQL.add(new JxtPropertiesPanelItem(comboSQLComment,Jxtray.getLang("properties.sqlcomment")));
	
		
	panelDump.add(pCSV);
	panelDump.add(pSQL);
	
    comboLookAndFeel.setSelectedItem(Jxtray.getProperty("lookandfeel"));
	comboLanguages.setSelectedItem(Jxtray.getProperty("lang"));
	comboAppFont.setSelectedItem(Jxtray.getProperty("font.name"));
	comboAppFontSize.setSelectedItem(Jxtray.getProperty("font.size"));
	comboSQLFont.setSelectedItem(Jxtray.getProperty("sqleditor.fontname"));
	comboSQLFontSize.setSelectedItem(Jxtray.getProperty("sqleditor.fontsize"));
	comboSQLComment.setSelectedItem(Jxtray.getProperty("sql.comment"));
	txCsvSeparator.setText(Jxtray.getProperty("csv.separator"));
	txCsvTextDelimitor.setText(Jxtray.getProperty("csv.textdelimitor"));
	txLogError.setText(Jxtray.getProperty("log.file.error"));
	txLogOutput.setText(Jxtray.getProperty("log.file.output"));
		
    pMain.add(panelGeneral);
	pMain.add(panelEditor);
	pMain.add(panelApparence);
	pMain.add(panelDump);
    this.getContentPane().add(pButton,  BorderLayout.SOUTH);
    
  }

  public void actionPerformed( ActionEvent e )
  {
        if ( e.getActionCommand().equals(Jxtray.getLang("action.close")) )
        {
              this.setVisible(false);
        }
        else  if ( e.getActionCommand().equals(Jxtray.getLang("action.browse")) )
        {
              this.setVisible(false);
        }
        else if ( e.getActionCommand().equals(Jxtray.getLang("action.save")) )
        {
			Jxtray.setProperty("font.name",(String) comboAppFont.getSelectedItem());
			Jxtray.setProperty("font.size",(String) comboAppFontSize.getSelectedItem());
			Jxtray.setProperty("log.file.error",txLogError.getText());
			Jxtray.setProperty("log.file.output",txLogOutput.getText());
			Jxtray.setProperty("lang",(String) comboLanguages.getSelectedItem());
			Jxtray.setProperty("csv.separator",txCsvSeparator.getText());
			Jxtray.setProperty("csv.textdelimitor",txCsvTextDelimitor.getText());
			Jxtray.setProperty("sqleditor.fontsize",(String) comboSQLFontSize.getSelectedItem());
			Jxtray.setProperty("sqleditor.fontname",(String) comboSQLFont.getSelectedItem());
			Jxtray.setProperty("sql.comment",(String) comboSQLComment.getSelectedItem());
			Jxtray.setProperty("lookandfeel",(String) comboLookAndFeel.getSelectedItem());
			
			if ( chkConfirmOnExit.isSelected())
				Jxtray.setProperty("confirm.quit","true");
			else
				Jxtray.setProperty("confirm.quit","false");
			
			if ( chkOverWrite.isSelected())
				Jxtray.setProperty("confirm.overwrite","true");
			else
				Jxtray.setProperty("confirm.overwrite","false");
				
			if ( chkEraseQuery.isSelected())
				Jxtray.setProperty("sqleditor.erasequeryafterxecute","true");
			else
				Jxtray.setProperty("sqleditor.erasequeryafterxecute","false");				
			
			Jxtray.saveProperties();
			Messages.info(Jxtray.getLang("properties.saved"));
        }
  }
 



}


