import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Properties;

/**
 * This class creates a configuration window based on the Properties
 * passed to it, then returns these values to the caller if OK is clicked.
 * Cancel closes the window.
 *
 * @author      Simon Pollard
 * @see         ConfigProperties
 * @see         java.util.Properties
 */

public class CfgWindow implements ActionListener
{
  private JFrame cfgFrame;
  private JButton okButton, cancelButton;
  private ConfigProperties myParent;
  private Box propData;

  /**
   * The constructor creates the configuration window given the passed
   * parameters.
   *
   * @param     newProperties the set of Properties to be edited
   * @param     propertyOrder the order in which the Properties should appear in the window
   * @param     parent the calling class (which must implement ConfigProperties)
   * @see       ConfigProperties
   * @see       java.util.Properties
   */
  public CfgWindow(Properties newProperties, String propertyOrder[], ConfigProperties parent) {
    int i = 0, propCount;
    JTextField tmpField;

    myParent = parent;
    propCount = propertyOrder.length;

    // Add the properties and values to boxes
    Box propLabels = new Box(BoxLayout.Y_AXIS);
    propData = new Box(BoxLayout.Y_AXIS);

    for(i = 0; i < propCount; i++) {
      propLabels.add(new JLabel(propertyOrder[i]));
      propLabels.add(Box.createVerticalGlue());

      tmpField = new JTextField(newProperties.getProperty(propertyOrder[i]), 40);
      tmpField.setName(propertyOrder[i]);
      propData.add(tmpField);
    }

    // OK and Cancel buttons
    okButton = new JButton("OK");
    okButton.setMnemonic('O');
    okButton.addActionListener(this);
    cancelButton = new JButton("Cancel");
    cancelButton.setMnemonic('C');
    cancelButton.addActionListener(this);

    JPanel buttonPanel = new JPanel(new GridLayout(1, 2, 5, 5));
    buttonPanel.add(okButton);
    buttonPanel.add(cancelButton);

    // Fill in the frame
    cfgFrame = new JFrame("Configuration Options");
    cfgFrame.getContentPane().setLayout(new BorderLayout());
    cfgFrame.getContentPane().add(BorderLayout.WEST, propLabels);
    cfgFrame.getContentPane().add(BorderLayout.EAST, propData);
    cfgFrame.getContentPane().add(BorderLayout.SOUTH, buttonPanel);
    cfgFrame.pack(); cfgFrame.setVisible(true);
  }

  /**
   * Called when the OK or Cancel buttons are clicked.  The OK button
   * passes the Properties currently displayed back to the calling class,
   * and the Cancel button simply closes the window.
   *
   * @param     e the ActionEvent that has occurred
   * @see       java.awt.event.ActionListener
   */
  public void actionPerformed(ActionEvent e) {
    if(e.getSource() == cancelButton) {
      cfgFrame.dispose();
    } else if(e.getSource() == okButton) {
      Properties retProperties = new Properties();
      Component cfgComponents[] = propData.getComponents();
      JTextField thisField;

      for(int i = 0; i < propData.getComponentCount(); i++) {
        thisField = (JTextField) cfgComponents[i];

        retProperties.setProperty(thisField.getName(), thisField.getText());
      }

      myParent.setCfgProperties(retProperties);
      cfgFrame.dispose();
    }
  }
}
