
//Title:        Table of Elements Dialog
//Version:      
//Copyright:    Copyright (c) 2001
//Author:       J.Aerts
//Company:      Computer Chemistry Consultancy
//Description:  A Table of Elements dialog

package TableOfElements;

import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.beans.*;  // Property change stuff
// Import of the TableOfElements java bean
import JosAertsBeans.*;

public class TableOfElementsDialog extends JDialog {
  TableOfElements toe;
  JPanel panel1 = new JPanel();
  BorderLayout borderLayout1 = new BorderLayout();
  TitledBorder titledBorder1;
  JButton OK_Button = new JButton("OK");
  JButton Cancel_Button = new JButton("Cancel");
  JPanel buttonPanel = new JPanel();
  private JOptionPane optionPane;
  final String btnString1 = "OK";
  final String btnString2 = "Cancel";
  Object[] options = {btnString1, btnString2};
  // Things to return
  int ConfirmDialogOption = -1;
  int numberOfSelectedElements = 0;
  int[] selectedElementNumbers = new int[103];
  String[] selectedElementSymbols = new String[103];
  String[] selectedElementNames = new String[103];

  public TableOfElementsDialog(Frame frame, String title, boolean modal) {
    super(frame, title, modal);
    try  {
      jbInit();
      pack();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  public TableOfElementsDialog() {
    this(null, "", false);
  }

  void jbInit() throws Exception {
    titledBorder1 = new TitledBorder("");
    panel1.setLayout(borderLayout1);
    toe = new TableOfElements();
    toe.setDisplayElementName(false);
    toe.setBorder(BorderFactory.createLoweredBevelBorder());
    toe.show();
    panel1.setBorder(BorderFactory.createLoweredBevelBorder());

    /** The classic way: A JOptionPane
    // Make an option pane
    // Is it possible to omit the QUESTION_MESSAGE (could not fina a way yet)
    optionPane = new JOptionPane(toe,JOptionPane.QUESTION_MESSAGE,
      JOptionPane.OK_CANCEL_OPTION, null, options, options[0]);
    setContentPane(optionPane);
    // add a listener
    optionPane.addPropertyChangeListener(new PropertyChangeListener() {
      public void propertyChange(PropertyChangeEvent e) {
      optionPane_propertyChanged(e);}
    });
    // Window closing event (necessary ???)
    addWindowListener (new WindowAdapter() {
      public void WindowClosing(WindowEvent we) {
        optionPane.setValue(new Integer(JOptionPane.CLOSED_OPTION));
        ConfirmDialogOption = JOptionPane.CLOSED_OPTION;
      }
    });


  // This JDialog has seen a property change:
  // Opened, OK-button, Cancel-button
  void optionPane_propertyChanged(PropertyChangeEvent e) {
    String prop = e.getPropertyName();
    // OK and Cancel buttons both return "value" as property name
    if(isVisible() && (e.getSource() == optionPane) &&
      ( prop.equals(JOptionPane.VALUE_PROPERTY)) ) {
      Object value = optionPane.getValue();
      // Possible object values are "OK" and "Cancel"
      if (value.equals(btnString1)) {     // OK-option
        // get the selected chemical elements
        ElementPanel[] ep = new ElementPanel[103];
        ep = toe.getSelectedElements();
        for (int i=0; i<103; i++) {
          if (ep[i] != null) {
          selectedElementNumbers[numberOfSelectedElements] = ep[i].getElementNumber();
          selectedElementSymbols[numberOfSelectedElements] = ep[i].getElementSymbol();
          selectedElementNames[numberOfSelectedElements] = ep[i].getElementName();
          numberOfSelectedElements++;
          }
        }
        setVisible(false);
        ConfirmDialogOption = JOptionPane.OK_OPTION;
      } else { // Cancel-option: close the window without doing anything else
        setVisible(false);
        ConfirmDialogOption = JOptionPane.CANCEL_OPTION;
      }
    }
  }

  */

  // Alternative: make your own buttons and add them (you can e.g. color them in that case)
    // Add buttons
    OK_Button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        OK_Button_actionPerformed(e);
      }
    });
    Cancel_Button.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        Cancel_Button_actionPerformed(e);
      }
    });
    // Change background color of "OK - Cancel" panel
    buttonPanel.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        buttonPanel_mouseClicked(e);
      }
    });
    panel1.add(toe,BorderLayout.CENTER);
    getContentPane().add(panel1);
    FlowLayout fl = new FlowLayout();
    buttonPanel.setLayout(fl);
    OK_Button.setFont(new java.awt.Font("Dialog", 1, 12));
    OK_Button.setBackground(Color.cyan);
    Cancel_Button.setFont(new java.awt.Font("Dialog", 1, 12));
    Cancel_Button.setBackground(Color.red);
    buttonPanel.add(OK_Button);
    buttonPanel.add(Cancel_Button);
    panel1.add(buttonPanel, BorderLayout.SOUTH);  //
  }

// Actions for the alternative (own buttons)
  void OK_Button_actionPerformed(ActionEvent e) {
    // Is there a chemical element selected ?
    ElementPanel[] ep = new ElementPanel[103];
    ep = toe.getSelectedElements();
    for (int i=0; i<103; i++) {
      if (ep[i] != null) {
        selectedElementNumbers[numberOfSelectedElements] = ep[i].getElementNumber();
        selectedElementSymbols[numberOfSelectedElements] = ep[i].getElementSymbol();
        selectedElementNames[numberOfSelectedElements] = ep[i].getElementName();
        numberOfSelectedElements++;
      }
      setVisible(false);
      ConfirmDialogOption = JOptionPane.OK_OPTION;
    }
  }

  void Cancel_Button_actionPerformed(ActionEvent ec) {
    setVisible(false);
    ConfirmDialogOption = JOptionPane.CANCEL_OPTION;
  }
  //

  /** Returns which option ("OK" or "Cancel" was chosen) */
  public int showConfirmDialog() {
    return(ConfirmDialogOption);
  }

  /** Returns the number of elements that was selected */
  public int getNumberOfSelectedElements() {
    return(numberOfSelectedElements);
  }

  /** Returns the selected element numbers */
  public int[] getSelectedElementNumbers() {
    return(selectedElementNumbers);
  }

  /** Returns the selected element symbols */
  public String[] getSelectedElementSymbols() {
    return(selectedElementSymbols);
  }

  /** Returns the selected element names */
  public String[] getSelectedElementNames() {
    return(selectedElementNames);
  }

  void buttonPanel_mouseClicked(MouseEvent e) {
    Color c = JColorChooser.showDialog(this,"Choose a new Background Color", this.getBackground());
    buttonPanel.setBackground(c);
  }
  
}
 