
//Title:        Table of Elements - Panel for one element
//Version:      
//Copyright:    Copyright (c) 2001
//Author:       J.Aerts
//Company:      Computer Chemistry Consultancy
//Description:  An Elements panel

package JosAertsBeans;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class ElementPanel extends JPanel {
  BorderLayout borderLayout1 = new BorderLayout();
  String ElementSymbol;
  String ElementName;
  int ElementNumber;
  JLabel jLabelElementNumber = new JLabel();
  JLabel jLabelElementSymbol = new JLabel();
  JLabel jLabelElementName = new JLabel();
  Color NormalColor = Color.green;
  Color MouseOverColor = Color.yellow;
  Color ElementNumberColor = Color.black;
  Color ElementSymbolColor = Color.black;
  Color ElementNameColor = Color.black;
  Color SelectedColor = Color.red;
  java.awt.Font ElementNumberFont = new java.awt.Font("Dialog", 1, 12);
  java.awt.Font ElementSymbolFont = new java.awt.Font("Dialog", 1, 24);
  java.awt.Font ElementNameFont = new java.awt.Font("Dialog", 1, 8);
  boolean ElementNameAsToolTipText = true;
  boolean DisplayElementName = true;
  boolean selected = false;

  public ElementPanel() {
    try  {
      jbInit();
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }

  private void jbInit() throws Exception {
    this.setBackground(NormalColor);
    // Element Number
    jLabelElementNumber.setFont(ElementNumberFont);
    jLabelElementNumber.setHorizontalAlignment(JLabel.RIGHT);
    jLabelElementNumber.setForeground(ElementNumberColor);
    // Element Symbol
    jLabelElementSymbol.setFont(ElementSymbolFont);
    jLabelElementSymbol.setForeground(ElementSymbolColor);
    // Element Name
    jLabelElementName.setFont(ElementNameFont);
    jLabelElementName.setForeground(ElementNameColor);
    if (ElementNameAsToolTipText) this.setToolTipText(ElementName);
    // Putting it all together
    this.setLayout(borderLayout1);
    this.setBorder(BorderFactory.createEtchedBorder());
    this.addMouseListener(new java.awt.event.MouseAdapter() {
      public void mouseEntered(MouseEvent e) {
      this_mouseEntered(e); }
      public void mouseExited(MouseEvent e) {
        this_mouseExited(e); }
      public void mouseClicked(MouseEvent e) {
        this_mouseClicked(e);
      } });
    this.add(jLabelElementNumber, BorderLayout.NORTH);
    this.add(jLabelElementSymbol, BorderLayout.CENTER);
    this.add(jLabelElementName, BorderLayout.SOUTH);
    this.setVisible(true);
  }


  void this_mouseEntered(MouseEvent e) {
    if(selected == false) this.setBackground(MouseOverColor);
    if(ElementNameAsToolTipText) this.setToolTipText(ElementName);
  }

  void this_mouseExited(MouseEvent e) {
    if(selected == false) this.setBackground(NormalColor);
  }

  void this_mouseClicked(MouseEvent e) {
    // check the state
    if(selected == false) {
      this.setBackground(SelectedColor);
      // mark as selected
      selected = true;
    } else {   // deselect
      this.setBackground(NormalColor);
      selected = false;
    }
    // Is it possible to make a setter and getter to pass an action to this class ?
    // This action is then performed when this is clicked
  }

// All getters and setters of this Bean

/** Sets the chemical element number */
  public void setElementNumber(int en) {
    ElementNumber = en;
    String s = "" + en;
    jLabelElementNumber.setText(s);
  }

/** Returns the chemical element number */
  public int getElementNumber() {
/*    int en = 0;
    try {
      en = Integer.parseInt(jLabelElementNumber.getText());
    } catch (Exception e) {
      System.out.println("String to Number Conversion failed");
    }
*/
    return (ElementNumber);
  }


/** Sets the name of the chemical element */
  public void setElementName(String s) {
    ElementName = s;
    jLabelElementName.setText(s);
  }

/** Returns the name of the chemical element */
  public String getElementName() {
//    return(jLabelElementName.getText());
    return(ElementName);
  }

/** Sets the chemical element symbol */
  public void setElementSymbol(String s) {
    ElementSymbol = s;
    jLabelElementSymbol.setText(s);
  }

/** Returns the chemical element symbol */
  public String getElementSymbol() {
//    return(jLabelElementSymbol.getText());
    return(ElementSymbol);
  }

/** Sets the normal background color (default: Color.green) */
  public void setNormalColor(Color c) {
    NormalColor = c;
    this.setBackground(c);
  }

/** Returns the normal background color */
  public Color getNormalColor() {
    return(NormalColor);
  }

/** Sets the color when the mouse is over the panel (default: Color.yellow) */
  public void setMouseOverColor(Color c) {
    MouseOverColor = c;
  }

/** Returns the color when the mouse is over the panel */
  public Color getMouseOverColor() {
    return(MouseOverColor);
  }

/** Sets the font for the chemical element number (java.awt.Font), default: "Dialog", 1, 12 */
  public void setElementNumberFont (java.awt.Font f) {
    ElementNumberFont = f;
    jLabelElementNumber.setFont(f);
  }

/** Returns the font for the chemical element number (java.awt.Font) */
  public java.awt.Font getElementNumberFont() {
    return(ElementNumberFont);
  }

/** Sets the font for the chemical element symbol (java.awt.Font), default: "Dialog", 1, 24 */
  public void setElementSymbolFont (java.awt.Font f) {
    ElementSymbolFont = f;
    jLabelElementSymbol.setFont(f);
  }

/** Returns the font for the chemical element symbol (java.awt.Font) */
  public java.awt.Font getElementSymbolFont() {
    return(ElementSymbolFont);
  }

/** Sets the font for the chemical element name (java.awt.Font) default: "Dialog", 1, 8 */
  public void setElementNameFont (java.awt.Font f) {
    ElementNameFont = f;
    jLabelElementName.setFont(f);
  }

/** Returns the font for the chemical element number (java.awt.Font) */
  public java.awt.Font getElementNameFont() {
    return(ElementNameFont);
  }

/** Sets the chemical element number color (default: Color.black) */
  public void setElementNumberColor (Color c) {
    ElementNumberColor = c;
    jLabelElementNumber.setForeground(c);
  }

/** Returns the chemical element number color */
  public Color getElementNumberColor (Color c) {
    return(ElementNumberColor);
  }

/** Sets the chemical element symbol color (default: Color.black) */
  public void setElementSymbolColor (Color c) {
    ElementSymbolColor = c;
    jLabelElementSymbol.setForeground(c);
  }

/** Returns the chemical element symbol color */
  public Color getElementSymbolColor (Color c) {
    return(ElementSymbolColor);
  }

/** Sets the chemical element name color (default: Color.black) */
  public void setElementNameColor (Color c) {
    ElementNameColor = c;
    jLabelElementName.setForeground(c);
  }

/** Returns the chemical element name color */
  public Color getElementNameColor (Color c) {
    return(ElementNameColor);
  }

/** Sets whether the element name is used for the tooltiptext */
  public void setElementNameAsToolTipText(boolean b) {
    ElementNameAsToolTipText = b;
  }

/** Returns whether the element name is used for the tooltiptext */
  public boolean getElementNameAsToolTipText() {
    return(ElementNameAsToolTipText);
  }

/** Sets whether the element name is displayed. Not displaying the
  element name gives a much compacter Table of Elements */
  public void setDisplayElementName(boolean b) {
    DisplayElementName = b;
    jLabelElementName.setVisible(b);
  }

/** Returns whether the element name is displayed. Not displaying the
  element name gives a much compacter Table of Elements */
  public boolean getDisplayElementName() {
    return(DisplayElementName);
  }
/** Sets this chemical element as selected*/
  public void setSelected(boolean b) {
    selected = true;
    this.setBackground(SelectedColor);
  }

/** Returns whether this chemical element is selected  */
  public boolean getSelected() {
    return(selected);
  }

// STILL TO DO: Only one chemical element can be selected at a time

}
