/*
 * @(#) 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.*;


/**
 * This class handles the symboly dialog box actions
 */
public class UISymbolDialog extends JDialog implements ActionListener,
						       ListSelectionListener,
						       ChangeListener{

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

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

    // must init outside of the dialog
    //
    public Vector<String> level_symbols_d = new Vector<String>();
    public String level_name_d = new String();
    public String selected_string_d = new String();

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

    // four panels on the main pane
    //
    JPanel jTopPane = new JPanel();
    JPanel jSymbolPane = new JPanel();
    JPanel jCheckPane = new JPanel();
    JPanel jBottomPane = new JPanel();

    // components on the top pane
    //
    JLabel jLevelName = new JLabel();

    // components on the symbol pane
    //
    LevelSymbolModel symbols_in_level_d = new LevelSymbolModel();
    JTable jLevelSymbols = new JTable( symbols_in_level_d );
    JScrollPane jScrollPane1 = new JScrollPane();
    JPanel jTablePanel = new JPanel();

    // components on the check pane
    //
    JCheckBox jCheckBoxNonSpeech = new JCheckBox();
    JCheckBox jCheckBoxContextLess = new JCheckBox();
    JCheckBox jCheckBoxDummy = new JCheckBox();
    JCheckBox jCheckBoxSkip = new JCheckBox();
    JCheckBox jCheckBoxExclude = new JCheckBox();
    JCheckBox jCheckBoxST = new JCheckBox();
    JCheckBox jCheckBoxNonSpeechBoundary = new JCheckBox();
    JCheckBox jCheckBoxNonSpeechInternal = new JCheckBox();
    JCheckBox jCheckBoxSPenaltyExclude = new JCheckBox();
    
    JPanel jSymbolPropPane = new JPanel();

    // components on the bottom pane
    //
    JTextField jTextFieldAddedSymbol = new JTextField();
    JButton jButtonAdd = new JButton();
    JButton jButtonDelete = new JButton();
    JButton jButtonApply = new JButton();
    JButton jButtonOK = new JButton();
    JButton jButtonCancel = new JButton();
    GridBagLayout gridBagLayout1 = new GridBagLayout();

    // borders
    //
    Border border2;  
    Border border3;
   
    /**
     * main 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 UISymbolDialog(Frame frame, String title, boolean modal) {
	super(frame, title, modal);
    }

    /**
     * default class constructor, calls main constructor with null values
     */
    public UISymbolDialog() {
	this(null, "", false);
    }

    /**
     * initializes 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(370, 470));
	jMainPane.setLayout(gridBagLayout1);

	jTopPane.setPreferredSize(new Dimension(350, 50));
	jSymbolPane.setPreferredSize(new Dimension(175, -1));
	jCheckPane.setPreferredSize(new Dimension(200, -1));
	jBottomPane.setPreferredSize(new Dimension(350, 70));
	
	// add four panels
	//
	jMainPane.add(jTopPane, 
		      new GridBagConstraints(0, 0, 2, 1, 1.0, 0.0,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(0, 0, 5, 0), 0, 0));

	jMainPane.add(jSymbolPane, 
		      new GridBagConstraints(0, 1, 1, 1, 0.5, 0.65,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(0, 5, 0, 5), 0, 0));

	jMainPane.add(jCheckPane, 
 		      new GridBagConstraints(1, 1, 1, 1, 0, 0.65,
					     GridBagConstraints.CENTER, 
					     GridBagConstraints.BOTH, 
					     new Insets(0, 5, 0, 5), 0, 0));

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

	// add components on top panel
	//
	jTopPane.setLayout(new FlowLayout(FlowLayout.LEFT));
	jTopPane.add(jLevelName);

	String curr_level_name = "Level Name: ";
	jLevelName.setText(curr_level_name + level_name_d);
	
	// add components on the symbol panel
	//
	jSymbolPane.setLayout(new BorderLayout());
	jSymbolPane.add(jTablePanel);
	//jLevelSymbols.setPreferredSize(new Dimension(175, -1));

	border3 = BorderFactory.createCompoundBorder(new TitledBorder(BorderFactory.createLineBorder(Color.black,1),"Symbol Table"),BorderFactory.createEmptyBorder(1,1,1,1));

	jTablePanel.setBorder(border3);
	jTablePanel.setLayout(new BorderLayout());
	jTablePanel.add(jScrollPane1, null);
	jScrollPane1.getViewport().add(jLevelSymbols, null);

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

	border2 = BorderFactory.createCompoundBorder(new TitledBorder(BorderFactory.createLineBorder(Color.black,1),"Symbol Type"),BorderFactory.createEmptyBorder(1,1,1,1));

	jCheckPane.setBorder(border2);
        
	jCheckBoxNonSpeech.setText("non-speech");
	jCheckBoxContextLess.setText("context-less   ");
	jCheckBoxDummy.setText("dummy");
	jCheckBoxSkip.setText("skip");
	jCheckBoxExclude.setText("exclude");
	jCheckBoxST.setText("attach statistical");
	jCheckBoxNonSpeechBoundary.setText("non-speech boundary");
        jCheckBoxNonSpeechInternal.setText("non-speech internal");
	jCheckBoxSPenaltyExclude.setText("spenalty exclude");
	// add listener
	//
	jCheckBoxNonSpeech.addChangeListener(this);
	jCheckBoxContextLess.addChangeListener(this);
	jCheckBoxDummy.addChangeListener(this);
	jCheckBoxSkip.addChangeListener(this);
	jCheckBoxExclude.addChangeListener(this);
	jCheckBoxST.addChangeListener(this);
	jCheckBoxNonSpeechBoundary.addChangeListener(this);
	jCheckBoxNonSpeechInternal.addChangeListener(this);
	jCheckBoxSPenaltyExclude.addChangeListener(this);
	Box b = Box.createVerticalBox();
	b.setAlignmentX((float)0.5);
	b.setAlignmentY((float)0.5);
	//b.setPreferredSize(new Dimension(150, 200));

	jCheckPane.add(b);
	
	b.add(jCheckBoxNonSpeech);
	b.add(jCheckBoxContextLess);
	b.add(jCheckBoxDummy);
	b.add(jCheckBoxSkip);
	b.add(jCheckBoxExclude);
	b.add(jCheckBoxST);
	b.add(jCheckBoxNonSpeechBoundary);
        b.add(jCheckBoxNonSpeechInternal);
	b.add(jCheckBoxSPenaltyExclude);
// add components on the check panel
	//
	jBottomPane.setLayout(new FlowLayout(FlowLayout.CENTER));

	jButtonAdd.setText("Add");
	jButtonDelete.setText("Delete");
	jButtonApply.setText("Apply");
	jButtonOK.setText("OK");
	jButtonCancel.setText("Cancel");

	jBottomPane.add(jTextFieldAddedSymbol);
	jBottomPane.add(jButtonAdd);
	jBottomPane.add(jButtonDelete);
	jBottomPane.add(jButtonApply);
	jBottomPane.add(jButtonOK);
	jBottomPane.add(jButtonCancel);

	jButtonAdd.addActionListener(this);
	jButtonDelete.addActionListener(this);
	jButtonApply.addActionListener(this);
	jButtonOK.addActionListener(this);
	jButtonCancel.addActionListener(this);

    	jTextFieldAddedSymbol.setPreferredSize(new Dimension(120, 24));
	jButtonAdd.setPreferredSize(new Dimension(80, 24));
	jButtonDelete.setPreferredSize(new Dimension(80, 24));
	jButtonApply.setPreferredSize(new Dimension(80, 24));
	jButtonOK.setPreferredSize(new Dimension(80, 24));
	jButtonCancel.setPreferredSize(new Dimension(80, 24));

	// add listener
	//
	ListSelectionModel model = jLevelSymbols.getSelectionModel();
	model.addListSelectionListener(this);

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

    }

    /**
     * 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() == jButtonAdd) {
	    String added_text = jTextFieldAddedSymbol.getText();
	    if ( added_text.length() != 0){
		if ( level_symbols_d.indexOf(added_text) < 0){
		    level_symbols_d.add( added_text ); 
		    symbols_in_level_d.fireTableDataChanged();
		}
	    }
	}
	if(e.getSource() == jButtonDelete) {
	    int i = jLevelSymbols.getSelectedRow();
	    int j = jLevelSymbols.getSelectedColumn();
	    String selected_string = (String)jLevelSymbols.getValueAt(i , j);
	    if ( selected_string != null){
		level_symbols_d.remove( selected_string ); 
		symbols_in_level_d.fireTableDataChanged();
	    }
	}
	if(e.getSource() == jButtonOK) {

	    SearchLevel curr_level = CData.getCurrLevel();
	    curr_level.symbols_d = level_symbols_d;
	    curr_level.level_name_d = level_name_d;
	    dispose();
	    CData.symbol_dlg_d = null;
	}
	if(e.getSource() == jButtonApply) {

	    SearchLevel curr_level = CData.getCurrLevel();
	    curr_level.symbols_d = level_symbols_d;
	    curr_level.level_name_d = level_name_d;
	}
	if(e.getSource() == jButtonCancel) {
	    dispose();
	    CData.symbol_dlg_d = null;
	}
	
	CData.getCurrLevel().sortSymbols();
    }

    /**
     * handle the event on check boxes
     * 
     * @param e the change event
     */
    public void stateChanged(ChangeEvent e) {
	
	// if  the user click on the symbol list
	//
	int i = jLevelSymbols.getSelectedRow();
	int j = jLevelSymbols.getSelectedColumn();
	int count = jLevelSymbols.getSelectedRowCount();
	
	//if ( i >= 0 && j >= 0 ){

	for (int end = i + count; i >= 0 && j >= 0 && i < end ; i++ ){

	    String selected_string = (String)jLevelSymbols.getValueAt(i,j);
	    SearchLevel curr_level = CData.getCurrLevel();
	    
	    // non-speech symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxNonSpeech ){
		if ( curr_level.isNonSpeechSymbol(selected_string) ){
		    if ( !jCheckBoxNonSpeech.isSelected() ){
			curr_level.non_speech_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxNonSpeech.isSelected() ){
			curr_level.non_speech_d.add(selected_string);
		    }
		}
	    }
 if ( (JCheckBox)e.getSource() == jCheckBoxNonSpeech ){
		if ( curr_level.isNonSpeechSymbol(selected_string) ){
		    if ( !jCheckBoxNonSpeech.isSelected() ){
			curr_level.non_speech_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxNonSpeech.isSelected() ){
			curr_level.non_speech_d.add(selected_string);
		    }
		}
	    }
	    


	    // context less symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxContextLess ){
		if ( curr_level.isContextLessSymbol(selected_string) ){
		    if ( !jCheckBoxContextLess.isSelected() ){
			curr_level.contextless_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxContextLess.isSelected() ){
			curr_level.contextless_d.add(selected_string);
		    }
		}
	    }


	    // non speech boundary symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxNonSpeechBoundary ){
		if ( curr_level.isNonSpeechBoundarySymbol(selected_string) ){
		    if ( !jCheckBoxNonSpeechBoundary.isSelected() ){
			curr_level.nonspeechboundary_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxNonSpeechBoundary.isSelected() ){
			curr_level.nonspeechboundary_d.add(selected_string);
		    }
		}
	    }
	    
  // non speech Internal symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxNonSpeechInternal ){
		if ( curr_level.isNonSpeechInternalSymbol(selected_string) ){
		    if ( !jCheckBoxNonSpeechInternal.isSelected() ){
			curr_level.nonspeechinternal_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxNonSpeechInternal.isSelected() ){
			curr_level.nonspeechinternal_d.add(selected_string);
		    }
		}
	    }
	    
  // spenalty exclude symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxSPenaltyExclude ){
		if ( curr_level.isSPenaltyExcludeSymbol(selected_string) ){
		    if ( !jCheckBoxSPenaltyExclude.isSelected() ){
			curr_level.spenaltyexclude_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxSPenaltyExclude.isSelected() ){
			curr_level.spenaltyexclude_d.add(selected_string);
		    }
		}
	    }
	    
	    // skip symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxSkip ){
	    if ( curr_level.isSkipSymbol(selected_string) ){
		if ( !jCheckBoxSkip.isSelected() ){
		    curr_level.skip_d.remove(selected_string);
		}
	    }
	    else {
		if ( jCheckBoxSkip.isSelected() ){
		    curr_level.skip_d.add(selected_string);
		}
	    }
	    }
	    // exclude symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxExclude ){
	    if ( curr_level.isExcludeSymbol(selected_string) ){
		if ( !jCheckBoxExclude.isSelected() ){
		    curr_level.exclude_d.remove(selected_string);
		}
	    }
	    else {
		if ( jCheckBoxExclude.isSelected() ){
		    curr_level.exclude_d.add(selected_string);
		}
	    }
	    }
	    // dummy symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxDummy ){
		
		if ( curr_level.isDummySymbol(selected_string) ){
		    if ( !jCheckBoxDummy.isSelected() ){
			curr_level.dummy_d.remove(selected_string);
	
		    }
		}
		
		else {
		    if ( jCheckBoxDummy.isSelected()) {
			curr_level.dummy_d.add(selected_string);
		
			/*
			// CData.curr_graph_index_d = 1;
			System.out.println(CData.curr_level_index_d);
			
			
			// delete the subgraph for a symbol if it is declared
			//search_dummy_symbol 
			CData.curr_level_index_d = CData.curr_level_index_d + 1;
			SearchLevel curr_level2 = 	    
			(SearchLevel)CData.getCurrLevel();
		        CData.curr_level_index_d = CData.curr_level_index_d - 1;
			//CData.curr_level_index_d = 0;
			curr_level2.graphs_d.remove(selected_string);
			curr_level2.graph_names_d.remove(selected_string);
			System.out.println(CData.curr_graph_index_d);
			//CData.curr_graph_index_d --;
			
			if ( CData.curr_graph_index_d < 0 ){
			    
			}	   
			CData.main_frame_d.updateAll();
			//CData.curr_graph_index_d = 1;
			//CData.curr_level_index_d = 
			*/
			}
		}
		
	    }
	    //ST symbols
	    //
	    if ( (JCheckBox)e.getSource() == jCheckBoxST ){
		if ( curr_level.isSTSymbol(selected_string) ){
		    if ( !jCheckBoxST.isSelected() ){
			curr_level.ST_d.remove(selected_string);
		    }
		}
		else {
		    if ( jCheckBoxST.isSelected() ){
			curr_level.ST_d.add(selected_string);
		    }
		}
	    }

	}
    }
    
    /**
     * handles the value changes
     * 
     * @param e the list selection event called
     */

    public void valueChanged(ListSelectionEvent e) {
	
	// if  the user click on the symbol list
	//
	int i = jLevelSymbols.getSelectedRow();
	int j = jLevelSymbols.getSelectedColumn();

	if ( i >= 0 && j >= 0 ){
	//for (int end = j + count; i >= 0 && j >= 0 && (j < end); j++ ){

	    String selected_string = (String)jLevelSymbols.getValueAt(i,j);
	    SearchLevel curr_level = CData.getCurrLevel();
	    
	    // non-speech symbols
	    //
	    if ( curr_level.isNonSpeechSymbol(selected_string) ){
		jCheckBoxNonSpeech.setSelected(true);
	    }
	    else {
		jCheckBoxNonSpeech.setSelected(false);
	    }
	    // ContextLess symbols
	    //
	    if ( curr_level.isContextLessSymbol(selected_string) ){
		jCheckBoxContextLess.setSelected(true);
	    }
	    else {
		jCheckBoxContextLess.setSelected(false);
	    }
	    
	    // nonspeech boundary symbols
	    //
	    if ( curr_level.isNonSpeechBoundarySymbol(selected_string) ){
		jCheckBoxNonSpeechBoundary.setSelected(true);
	    }
	    else {
		jCheckBoxNonSpeechBoundary.setSelected(false);
	    }
	    
	    // nonspeech internal  symbols
	    //
	    if ( curr_level.isNonSpeechInternalSymbol(selected_string) ){
		jCheckBoxNonSpeechInternal.setSelected(true);
	    }
	    else {
		jCheckBoxNonSpeechInternal.setSelected(false);
	    }
	    
	    
	    // spenalty exclude symbols
	    //
	    if ( curr_level.isSPenaltyExcludeSymbol(selected_string) ){
		jCheckBoxSPenaltyExclude.setSelected(true);
	    }
	    else {
		jCheckBoxSPenaltyExclude.setSelected(false);
	    }
	    


	    // skip symbols
	    //
	    if ( curr_level.isSkipSymbol(selected_string) ){
		jCheckBoxSkip.setSelected(true);
	    }
	    else {
		jCheckBoxSkip.setSelected(false);
	    }
	    // exclude symbols
	    //
	    if ( curr_level.isExcludeSymbol(selected_string) ){
		jCheckBoxExclude.setSelected(true);
	    }
	    else {
		jCheckBoxExclude.setSelected(false);
	    }
	    // dummy symbols
	    //
	    if ( curr_level.isDummySymbol(selected_string) ){
		jCheckBoxDummy.setSelected(true);
	    }
	    else {
		jCheckBoxDummy.setSelected(false);
	    }
	    // ST symbols
	    //
	    if ( curr_level.isSTSymbol(selected_string) ){
		jCheckBoxST.setSelected(true);
	    }
	    else {
		jCheckBoxST.setSelected(false);
	    }

	}
    }
    
    // define a inner class NodeSymbolModel
    //
    class LevelSymbolModel extends AbstractTableModel {
        final String[] columnNames = {"Symbol Name"};

        public int getColumnCount() {
            return columnNames.length;
        }

        public int getRowCount() {
	    return level_symbols_d.size(); 
        }

        public String getColumnName(int col) {
            return columnNames[col];
        }

        public Object getValueAt(int row, int col) {
	    return level_symbols_d.get(row);
        }

        public void setValueAt(Object aValue, int row, int col) {
	    level_symbols_d.set(row, aValue.toString());
        }

        public Class getColumnClass(int c) {
            return getValueAt(0, c).getClass();
        }

        public boolean isCellEditable(int row, int col) {
	    return false;
        }
    } // end of class LevelSymbolModel
}






















