
//Title:        Table of Elements
//Version:      
//Copyright:    Copyright (c) 1999
//Author:       J.Aerts
//Company:      Computer Chemistry Consultancy
//Description:  A Table of Elements project

package JosAertsBeans;

import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.event.*;

public class TableOfElements extends JPanel {
  GridBagLayout gbl = new GridBagLayout();

  String[] elements = {"H ","He","Li","Be","B ","C ","N ","O ","F ","Ne",
   "Na","Mg","Al","Si","P","S","Cl","Ar",
   "K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr",
   "Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe",
   "Cs","Ba","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu",
   "Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn",
   "Fr","Ra","Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lw"
  };

  String[] elementNames = {"Hydrogen","Helium","Lithium","Berillium","Boron",
    "Carbon","Nitrogen","Oxygen","Fluorine","Neon",
    "Sodium","Magnesium","Aluminium","Silicon","Phosphorus","Sulfur","Chlorine","Argon",
    "Potassium","Calcium","Scandium","Titanium","Vanadium","Chromium","Manganese","Iron",
    "Cobalt","Nickel","Copper","Zinc","Gallium","Germanium","Arsenic","Selenium","Bromine","Krypton",
    "Rubidium","Strontium","Yttrium","Zirconium","Niobium","Molybdenum","Technetium","Ruthenium",
    "Rhodium","Palladium","Silver","Cadmium","Indium","Tin","Antimony","Tellurium","Iodine","Xenon",
    "Cesium","Barium","Lanthanum","Cerium","Praseodymium","Neodymium","Promethium","Samarium","Europium",
    "Gadolinum","Terbium","Dysprosium","Holmium","Erbium","Thulium","Ytterbium","Lutetium",
    "Hafnium","Tantalum","Wolfram","Rhenium","Osmium","Iridium","Platinum",
    "Gold","Mercury","Thallium","Pead","Bismuth","Polonium","Astatine","Radon",
    "Francium","Radium","Actinium","Thorium","Profactinium","Uranium","Neptunium","Plutonium","Americium",
    "Curium","Berkelium","Californium","Einsteinium","Fermium","Mendelevium","Nobelium","Lawrencium"
  } ;

  ElementPanel[] ep = new ElementPanel[103];
  boolean ElementNameAsToolTipText = true;
  boolean DisplayElementName = true;
  boolean AllowMultipleSelection = false;
  Color ElementPanelNormalColor = Color.green;
  Color ElementPanelMouseOverColor = Color.yellow;
  Color ElementPanelElementNumberColor = Color.black;
  Color ElementPanelElementSymbolColor = Color.black;
  Color ElementPanelElementNameColor = Color.black;
  java.awt.Font ElementPanelElementNumberFont = new java.awt.Font("Dialog", 1, 12);
  java.awt.Font ElementPanelElementSymbolFont = new java.awt.Font("Dialog", 1, 24);
  java.awt.Font ElementPanelElementNameFont = new java.awt.Font("Dialog", 1, 8);
  private java.awt.Font smallfont = new java.awt.Font("Dialog", 1, 6);


  public TableOfElements() {
    try  {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.addMouseListener(new java.awt.event.MouseAdapter() {

      public void mouseClicked(MouseEvent e) {
        this_mouseClicked(e);
      }
    });
    this.setLayout(gbl);

    // set standard general properties for all the chemical elementspanels
    ElementPanelElementNameFont = smallfont;
  }

  /** Shows the Table of Elements. Use this method after having set/changed
  properties to actually show the Table of Elements */
  public void show() {
    // set infividual properties and add to layout
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; gbc.fill = GridBagConstraints.BOTH;
    ep[0] = new ElementPanel();
    ep[0].setElementNumber(1); ep[0].setElementSymbol(elements[0]); ep[0].setElementName(elementNames[0]);
    setElementPanelGeneralProperties(ep[0]);
    this.add(ep[0],gbc);
    gbc.gridx = 17;
    ep[1] = new ElementPanel();
    ep[1].setElementNumber(2); ep[1].setElementSymbol(elements[1]); ep[1].setElementName(elementNames[1]);
    setElementPanelGeneralProperties(ep[1]);
    this.add(ep[1],gbc);
    gbc.gridx = 0;
    gbc.gridy++;
    int x2 = 0;
    for (int i=2; i<10; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      if(i > 3) x2 = i+8;
      gbc.gridx = x2;
      this.add(ep[i],gbc);
      x2++;
    }
    gbc.gridx = 0; gbc.gridy++;
    x2 = 0;
    for (int i=10; i<18; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      if(i > 11) x2 = i;
      gbc.gridx = x2;
      this.add(ep[i],gbc);
      x2++;
    }
    gbc.gridx = 0; gbc.gridy++;
    for (int i=18; i<36; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    gbc.gridx = 0; gbc.gridy++;
    for (int i=36; i<54; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    gbc.gridx = 0; gbc.gridy++;
    // Attention: Actinides are coming
    for (int i=54; i<57; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    // Actinides
    for (int i=71; i<86; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    // Attention: Lantinides are coming
    gbc.gridx = 0; gbc.gridy++;
    for (int i=86; i<89; i++) {
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    // Add a button to pup-op the Computer Chemistry Consultancy website
    JButton CCCButton = new JButton("Computer Chemistry Consultancy");
    CCCButton.setFont(new java.awt.Font("Dialog", 1, 12));
    CCCButton.setBackground(Color.cyan);
    CCCButton.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) { CCCButton_actionPerformed(e); } });
//    gbc.gridy++;
    gbc.gridx = 0; gbc.gridwidth = 18;
//    this.add(CCCButton,gbc);
    // Lantanides and Actinides
    gbc.gridx = 3; //gbc.gridy++;
    gbc.gridwidth = 1;
    for (int i=57; i<71; i++) {
      if(i == 57) {
        gbc.insets = new Insets(10,10,0,0);
      } else {
        gbc.insets = new Insets(10,0,0,0);
      }
      ep[i] = new ElementPanel();
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);
      setElementPanelGeneralProperties(ep[i]);
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    gbc.gridx = 3; gbc.gridy++;
    for (int i=89; i<103; i++) {
      ep[i] = new ElementPanel();
      setElementPanelGeneralProperties(ep[i]);
      if(i == 89) {
        gbc.insets = new Insets(0,10,0,0);
      } else {
        gbc.insets = new Insets(0,0,0,0);
      }
      ep[i].setElementNumber(i+1);
      ep[i].setElementName(elementNames[i]);
      ep[i].setElementSymbol(elements[i]);;
      this.add(ep[i],gbc);
      gbc.gridx++;
    }
    this.setVisible(true);
  }

  void CCCButton_actionPerformed(ActionEvent e) {
    System.out.println("CCC button pressed");
  }

  /** This method can be used to avoid repeating setting properties
   each time a new ElementPanel is created.
   It is very useful when for each ElementPanel the properties are the same
   (e.g. colors, fonts, ...).
  */
  private void setElementPanelGeneralProperties(ElementPanel ep) {
    ep.setElementNameColor(ElementPanelElementNameColor);
    ep.setElementNumberFont(ElementPanelElementNumberFont);
    ep.setElementSymbolFont(ElementPanelElementSymbolFont);
    ep.setElementNameFont(ElementPanelElementNameFont);
    ep.setElementNumberColor(ElementPanelElementNumberColor);
    ep.setElementSymbolColor(ElementPanelElementSymbolColor);
    ep.setElementNameColor(ElementPanelElementNameColor);
    ep.setNormalColor(ElementPanelNormalColor);
    ep.setMouseOverColor(ElementPanelMouseOverColor);
    ep.setElementNameAsToolTipText(ElementNameAsToolTipText);
    ep.setDisplayElementName(DisplayElementName);
  }

// Setters and getters

  /** Sets whether element names are displayed as tooltip text*/
  public void setElementNameAsToolTipText(boolean b) {
    ElementNameAsToolTipText = b;
  }

  /** Returns whether element names are displayed as tooltip text*/
  public boolean getElementNameAsToolTipText() {
    return(ElementNameAsToolTipText);
  }

  /** Sets whether element names are displayed */
  public void setDisplayElementName(boolean b) {
    DisplayElementName = b;
  }

  /** Returns whether element names are displayed */
  public boolean getDisplayElementName() {
    return(DisplayElementName);
  }

  /** Sets the normal color of the element panels */
  public void setElementPanelNormalColor(Color c) {
    ElementPanelNormalColor = c;
  }

  /** Returns the normal color of the element panels */
  public Color getElementPanelNormalColor() {
    return(ElementPanelNormalColor);
  }

  /** Sets the color displayed when the mouse is over an element panel */
  public void setElementPanelMouseOverColor(Color c) {
    ElementPanelMouseOverColor = c;
  }

  /** Returns the color displayed when the mouse is over an element panel */
  public Color getElementPanelMouseOverColor() {
    return(ElementPanelMouseOverColor);
  }

  /** Sets the color of the element numbers */
  public void setElementPanelElementNumberColor(Color c) {
    ElementPanelElementNumberColor = c;
  }

  /** Returns the color of the element numbers */
  public Color getElementPanelElementNumberColor() {
    return(ElementPanelElementNumberColor);
  }

  /** Sets the color of the element symbols */
  public void setElementPanelElementSymbolColor(Color c) {
    ElementPanelElementSymbolColor = c;
  }

  /** Returns the color of the element symbols */
  public Color getElementPanelElementSymbolColor() {
    return(ElementPanelElementSymbolColor);
  }

  /** Sets the color of the element names */
  public void setElementPanelElementNameColor(Color c) {
    ElementPanelElementNameColor = c;
  }

  /** Returns the color of the element names */
  public Color getElementPanelElementNameColor() {
    return(ElementPanelElementNameColor);
  }

  /** Sets the java.awt.Font of the element numbers */
  public void setElementPanelElementNumberFont(java.awt.Font f) {
    ElementPanelElementNumberFont = f;
  }

  /** Returns the java.awt.Font of the element numbers */
  public java.awt.Font getElementPanelElementNumberFont() {
    return(ElementPanelElementNumberFont);
  }

  /** Sets the java.awt.Font of the element symbols */
  public void setElementPanelElementSymbolFont(java.awt.Font f) {
    ElementPanelElementSymbolFont = f;
  }

  /** Returns the java.awt.Font of the element symbols */
  public java.awt.Font getElementPanelElementSymbolFont() {
    return(ElementPanelElementSymbolFont);
  }

  /** Sets the java.awt.Font of the element names */
  public void setElementPanelElementNameFont(java.awt.Font f) {
    ElementPanelElementNameFont = f;
  }

  /** Returns the java.awt.Font of the element names */
  public java.awt.Font getElementPanelElementNameFont() {
    return(ElementPanelElementNameFont);
  }

  /** Sets whether multiple selection of elements is allowed. Default: true.
  No implementation of "false" yet */
  public void setAllowMultipleSelection(boolean b) {
    AllowMultipleSelection = b;
  }

  /** Returns whether multiple selection of elements is allowed */
  public boolean getAllowMultipleSelection() {
    return(AllowMultipleSelection);
  }

  /** sets an array of element panels as selected (not implemented yet) */
  public void setSelectedElements(ElementPanel[] ep) {
    // implementation not performed yet
  }

  /** Returns a an array of selected element panels */
  public ElementPanel[] getSelectedElements() {
    ElementPanel[] selectedelementpanels = new ElementPanel[103];
    for (int i=0; i<103; i++) {selectedelementpanels[i]  = null;}
    int j = 0;
    for (int i=0; i<103; i++) {
      if(ep[i].getSelected() == true) {
        selectedelementpanels[j] = ep[i];
        j++;
      }
    }
    return(selectedelementpanels);
  }

  /* Mouse clicked somewhere in the table, but not on an ElementPanel.
     Change the background color of the table of the elements.
  */
  void this_mouseClicked(MouseEvent e) {
    Color c = JColorChooser.showDialog(this,"Choose a new Background Color", this.getBackground());
    this.setBackground(c);
  }


}