/*
 * @(#) UIMainFrame.java  1.10 10/09/02
 *
 * Copyright ***,  All Rights Reserved.
 * 
 * This software is the proprietary information of ********  
 * Use is subject to license terms.
 * 
 */

// import java packages
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.AbstractButton.*;
import java.util.*;
import java.io.*;


/**
 * This class handles the save all dialog box
 */
public class UISaveAllDialog extends JDialog implements ActionListener{

    //-----------------------------------------------------------------
    //
    // static data members
    //
    //-----------------------------------------------------------------

    //-----------------------------------------------------------------
    //
    // instance data members
    //
    //-----------------------------------------------------------------

    // the main panel
    //
    JPanel jMainPane = new JPanel();

    // four panels on the main pane
    //
    JPanel jLMPane = new JPanel();
    JPanel jSMPane = new JPanel();
    JPanel jBottomPane = new JPanel();

    // components on the top pane
    //
    JButton jButtonOK = new JButton();
    JButton jButtonCancel = new JButton();
    JButton jButtonBrowseLM = new JButton();
    JButton jButtonBrowseSM = new JButton();

    JLabel jLMName = new JLabel();
    JTextField jLMText = new JTextField();

    JLabel jSMName = new JLabel();
    JTextField jSMText = new JTextField();

    // components on the bottom pane
    //
    GridBagLayout gridBagLayout1 = new GridBagLayout();

    // borders
    //
    Border border2;  
    Border border3;

    /**
     * default class constructor
     *
     * @param frame the Frame from which the dialog is displayed
     * @param title the String to display in the dialog's title bar
     * @param modal true for a modal dialog, false for one that allows other
     *     windows to be active at the same time 
     */
    public UISaveAllDialog(Frame frame, String title, boolean modal) {
	super(frame, title, modal);
    }

    public UISaveAllDialog() {
	this(null, "", false);
    }

    /**
     * init everything for a dialog
     * 
     */
    public void initAll(){
	try  {
	    jbInit();
	    pack();
	}
	catch(Exception ex) {
	    ex.printStackTrace();
	}
    }

    private void jbInit() throws Exception {

	// add the main panel
	//
	jMainPane.setPreferredSize(new Dimension(500, 120));
	jMainPane.setLayout(gridBagLayout1);

	jLMPane.setPreferredSize(new Dimension(500, 40));
	jSMPane.setPreferredSize(new Dimension(500, 40));
	jBottomPane.setPreferredSize(new Dimension(500, 30));
	
	// add four panels
	//
	jMainPane.add(jLMPane, 
		      new GridBagConstraints(0, 0, 2, 1, 1.0, 0.5,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(5, 0, 5, 0), 0, 0));

	jMainPane.add(jSMPane, 
		      new GridBagConstraints(0, 1, 2, 1, 1.0, 0.5,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(5, 0, 5, 0), 0, 0));

	jMainPane.add(jBottomPane, 
		      new GridBagConstraints(0, 2, 1, 1, 1.0, 0.1,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(5, 0, 5, 0), 0, 0));

	// add components on top panel
	//
	jLMPane.setLayout(new FlowLayout(FlowLayout.LEFT));
	jLMPane.add(jLMName);

	String lm_name = "Language Model: ";
	jLMName.setText(lm_name);
	jLMPane.add(jLMText);
	jLMPane.add(jButtonBrowseLM);
	jButtonBrowseLM.setText("Browse");
	jButtonBrowseLM.addActionListener(this);

	jLMName.setPreferredSize(new Dimension(120, 20));
	jLMText.setPreferredSize(new Dimension(300, 20));
	jButtonBrowseLM.setPreferredSize(new Dimension(40, 20));

	// add components on top panel
	//
	jSMPane.setLayout(new FlowLayout(FlowLayout.LEFT));
	jSMPane.add(jSMName);

	String sm_name = "Statistical Model: ";
	jSMName.setText(sm_name);
	jSMPane.add(jSMText);
	jSMPane.add(jButtonBrowseSM);
	jButtonBrowseSM.setText("Browse");
	jButtonBrowseSM.addActionListener(this);
	
	jSMName.setPreferredSize(new Dimension(120, 20));
	jSMText.setPreferredSize(new Dimension(300, 20));
	jButtonBrowseSM.setPreferredSize(new Dimension(40, 20));

	// add components on the check panel
	//
	jBottomPane.setLayout(new FlowLayout(FlowLayout.CENTER));

	jButtonOK.setText("OK");
	jButtonCancel.setText("Cancel");

	jBottomPane.add(jButtonOK);
	jBottomPane.add(jButtonCancel);

	jButtonOK.addActionListener(this);
	jButtonCancel.addActionListener(this);

	jButtonOK.setPreferredSize(new Dimension(80, 24));
	jButtonCancel.setPreferredSize(new Dimension(80, 24));

	// add the main pain
	//
	getContentPane().add(jMainPane);

	// set the file names
	//
	jLMText.setText(CData.DEFAULT_LM_FILE);
	if( CData.curr_file_d != null){
	    jLMText.setText(CData.curr_file_d);
	}
	jSMText.setText(CData.DEFAULT_SM_FILE);
	if( CData.curr_sm_file_d != null){
	    jSMText.setText(CData.curr_sm_file_d);
	}
	else if ( CData.curr_file_d != null){
	    File curr_file = new File(CData.curr_file_d);
	    String curr_dir = curr_file.getParent();
	    jSMText.setText(curr_dir + File.separator + 
			    CData.DEFAULT_SM_FILE);
	}
    }

    /**
     * handle the event when a button is clicked
     * 
     * @param e the button event
     */
    public void actionPerformed(ActionEvent e) {
	
	// decide which button is clicked
	//
	if(e.getSource() == jButtonBrowseLM) {

	    // set current description 
	    //
	    CData.open_lm_file_description_d = CData.lm_file_description_d;

	    // select a file
	    //
	    String select_file = CData.chooseLMFile(CData.curr_file_d);
	    if ( select_file != null ){
		jLMText.setText(select_file);
	    }
	}
	if(e.getSource() == jButtonBrowseSM) {

	    // select a file
	    //
	    String select_file = CData.chooseSMFile(CData.curr_sm_file_d);
	    if ( select_file != null ){
		jSMText.setText(select_file);
	    }
	}
	if(e.getSource() == jButtonOK) {
	    
	    // test if files are valid
	    //
	    if ( jSMText.getText().equals("") || 
		 jLMText.getText().equals("")){
		JOptionPane.showMessageDialog(this, "Invalid File Name");
	    }
	    else {
		
		// set model files 
		//
		CData.curr_file_d = jLMText.getText();
		CData.curr_sm_file_d = jSMText.getText();

		// set current description 
		//
		CData.lm_file_description_d = CData.open_lm_file_description_d;

		// save the lm file
		//
		CData.store2(false,true);
		
		// close the box 
		//
		dispose();
	    }

	}
	if(e.getSource() == jButtonCancel) {
	    dispose();
	}
    }
}
