// file: $ISIP_IFC/class/java/ConfigPanel/Configuration.java
// version: $Id: Configuration.java 10227 2005-09-09 19:07:49Z stanley $ 
//

// import necessary java libraries
//
import java.net.*;
import java.io.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import javax.swing.border.*;

/**
 *  A configuration that is shown inside a dialog.
 */
public class Configuration extends JPanel {  

    //-------------------------------------------------------------------------
    //
    // public constants
    //
    //-------------------------------------------------------------------------


    //------------------------------------------------------------------------
    //
    // protected data
    //
    //------------------------------------------------------------------------

    /**
     * ConfigTabPanel object.
     */
    protected ConfigTabPanel cfg_tab_d;

    /**
     * Apply button.
     */
    protected JButton applyButton;

    /**
     * OK button.
     */
    protected JButton okButton;

    /**
     * Cancel button.
     */
    protected JButton cancelButton;

    /**
     * Reset button.
     */
    protected JButton resetButton;

    /**
     * Boolean value ok to represent the status of the config.
     */
    protected boolean ok;

    /**
     * Dialog box to hold the configution.
     */
    protected JDialog dialog;
    
    /**
     * Constructor.
     */
    public Configuration() {  
	
	setLayout(new BorderLayout());
	
	// construct a panel with user name and password fields
	
	cfg_tab_d = new ConfigTabPanel(); 
	
	add(cfg_tab_d, BorderLayout.CENTER);
	
	// create apply buttons that verify the input dat
	//
	applyButton = new JButton("Apply");
	applyButton.addActionListener(new
				      ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    cfg_tab_d.apply();
		}
	    });
	
	// create Ok buttons that terminate the dialog
	//
	okButton = new JButton("OK");
	okButton.addActionListener(new
				   ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    if (cfg_tab_d.apply()) {
	  
			ok = true;
			if (dialog != null)
			    dialog.setVisible(false);
		    }
		}
	    });
	
	// create reset buttons that reset all values to its default
	//
	resetButton = new JButton("Reset");
	resetButton.addActionListener(new
				      ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    cfg_tab_d.reset();

		}
	    });

	// create Cancel buttons that terminate the dialog
	//
	cancelButton = new JButton("Cancel");
	cancelButton.addActionListener(new
				       ActionListener() {
		public void actionPerformed(ActionEvent event) {
		    if (dialog != null)
			dialog.setVisible(false);
		}
	    });

	
	// add buttons to southern border
	//
	JPanel buttonPanel = new JPanel();
	buttonPanel.add(applyButton);
	buttonPanel.add(okButton);
	buttonPanel.add(resetButton);
	buttonPanel.add(cancelButton);
	add(buttonPanel, BorderLayout.SOUTH);
    }
    
    /**
     * Gets the dialog entries.
     * 
     * @return a ConfigTabPanel object whose state represents the
     * dialog entries.
     */
    public ConfigTabPanel getPanel() {
	return cfg_tab_d;
    }
    
    /**
     * Show the chooser panel in a dialog
     *
     * @param parent a component in the owner frame or null
     * @param title the dialog window title
     *
     * @return  a boolean value indicating status.
     */
    public boolean showDialog(Component parent, String title) {
	
	ok = false;
	
	// locate the owner frame
	//
	Frame owner = null;
	if (parent instanceof Frame)
	    owner = (Frame) parent;
	else 
	    owner = (Frame)SwingUtilities.getAncestorOfClass(
							     Frame.class, parent);
	
	// if first time, or if owner has changed, make new dialog
	//
	if (dialog == null || dialog.getOwner() != owner)  {
	    
	    dialog = new JDialog(owner, true);
	    dialog.getContentPane().add(this);
	    dialog.getRootPane().setDefaultButton(okButton);
	    dialog.pack();
	}
	
	// set title and show dialog
	//
	dialog.setTitle(title);

	Toolkit tk = Toolkit.getDefaultToolkit();
	Dimension d = tk.getScreenSize();
	int screenHeight = d.height;
	int screenWidth = d.width;

	dialog.setLocation( screenWidth / 4, screenHeight / 4);

	dialog.setVisible(true);

	return ok;
    }


 /**
  * Get the name of the demo.
  *
  * @return a String variable containing the demo
  * name
  */
    public String getDemoName() {
	return cfg_tab_d.getDemoName();
    }

    /**
     * Set the name of the demo.
     *
     * @return a boolean value
     */
    public boolean setDemoName(String demo_name_a) {
	
	cfg_tab_d.setDemoName(demo_name_a);
	
	return true;
    }

    /** 
    * Main driver program. Used to test this class separately.
    *
    * @param args arguments from the command line.
    */
    public static void main(String args[]) {
	
        final Configuration splot = new Configuration();
	
        WindowListener l = new WindowAdapter() {
                public void windowClosing(WindowEvent e) { System.exit(0); }
            };
	
        JFrame f = new JFrame("Configuration");

	f.addWindowListener(l);
        f.getContentPane().add("Center", splot);
        f.pack();
        f.setVisible(true);

    }

}

