/** 
  * Equation Selector for CtrlPanl.class re DifEqnAp.class
  * @author Robert J Morton UK-YE572246C
  * @version 27 November 1997 modified 07 August 2009, 18 April 2012 */

/*  This class contains a pair of radio buttons for
    choosing which of the two difference equations
    to iterate: x = x**2 + c or x = cx(1 - x) */

import java.awt.*;        // for graphics operations (GUI)
import java.awt.event.*;  // for the new-fangled 1.1 event handling
import javax.swing.*;     // library for constructing Swing GUI

public class seleqn extends JPanel {
  private cval sc;            // reference to the c value text entry panel
  private timegr tg;          // reference to time-graph instance
  private bouncegr bg;        // reference to bounce-graph instance
  private formula f;          // instance reference to formula class
  private JLabel L;           // reference for the title label
  private ButtonGroup G;      // reference for the checkbox group
  private JRadioButton a, b;  // references for the two buttons

  private boolean 
    Flags[] = {false, true},  // the two flag options
    EqnFlag = false;          /* false means x = cx(1 - x) is selected
                                 true means x = x**2 + c is selected */

  seleqn(cval sc, timegr tg, bouncegr bg, formula f, Color pc) {
    this.sc = sc;  // set instance ref. of cval class to local variable
    this.tg = tg;  // set instance ref. of timegr class to local variable
    this.bg = bg;  // set instance ref. of bouncegr class to local variable
    this.f = f;    // set instance ref. of formula class to local variable

    setBackground(pc);  // set panel colour

    // Lay out the 3 objects one beneath the next.
    setLayout(new GridLayout(3, 0));

    // Create equation selector panel label and add it to this panel.
    L = new JLabel("Equation:", Label.LEFT); add(L);

    G = new ButtonGroup();  // group container for radio buttons

    /* Create an x = cx(1 - x) radio button, add it to the button-
    group, add it to this panel and create Item Listener for it. */

    a = new JRadioButton("x = cx(1 - x)", true); G.add(a); add(a);
    a.addItemListener(new eqnbut(0, this));

    /* Create an x = x * x + c radio button, add it to the button-
    group, add it to this panel and create Item Listener for it. */

    b = new JRadioButton(
      "x = x * x + c", false  // x = x**2 + c checkbox
    );
    G.add(b);                 // add it to the button-group
    add(b);                   // add it to this panel
    b.addItemListener(
      new eqnbut(1, this)     // create Item Listener for it
    );
  }


  void selectEquation(int i) {
    EqnFlag = Flags[i];  // set the new state of the equation selector

    /* Set the equation-flag state in the formula,
    time-graph, bounce-graph and C-value classes. */

    f.setEF(EqnFlag); tg.setEF(EqnFlag); 
    bg.setEF(EqnFlag); sc.setEF(EqnFlag);

    sc.initCval();  // reset c to its default value for selected equation
  }
}


// LISTENS FOR EVENTS FROM EQUATION SELECTION BUTTONS
class eqnbut implements ItemListener {
  int id;     // one of the above events
  seleqn se;  // the application that called: always the above class

  /* Constructor for a new checkbox event: sets 'id' number
  pertaining to this instance and reference to the instance
  of above class from which it came. */

  public eqnbut(int id, seleqn se) { this.id = id; this.se = se; }

  // An event has occurred from checkbox 'id'
  public void itemStateChanged(ItemEvent e) {
    switch(id) {                            // selected equation:
      case 0: se.selectEquation(0); break;  // x = cx(1 - x)
      case 1: se.selectEquation(1); break;  // x = x**2 + c
    }
  }
}