import java.awt.*;
import java.awt.event.*;
import com.perisic.ring.*;

/**
 * An example for mapping of Strings into a selection of rings.
 * Last Change: 16.01.2003, Universal Polynomial Ring introduced.
 */


public class RingAppletExample extends java.applet.Applet
    implements ActionListener, ItemListener {
       private class SQRT2 extends ModularRing {
          public SQRT2() {
                super((new PolynomialRing(Ring.Q,"a")).map("a^2 - 2"));
                hideMod();
                }
          public boolean isField() {
               return true;
               }
          }
    Ring [] rings;
    String [] shortRingDescription;
    String [] longRingDescription;
    TextArea output = new TextArea(8, 60);
    List ringList = new List(3, false);
    Button clearInput = new Button("Clear Input");
    Button clearOutput = new Button("Clear Output");
    Button enter = new Button("Enter");
    TextField input = new TextField("(a + b)^2 - a * b", 30);
    Label whatRing = new Label("(please select a ring)");

    int actionCount;

    /** Builds the interface. */
    public void init() {
    Panel topPanel = new Panel();
    topPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    Panel secondPanel = new Panel();
    secondPanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,5));
/*
    Choice colorChoice = new Choice();
    colorChoice.add("red");
    colorChoice.add("green");
    colorChoice.add("blue");
*/


    try {
       CyclotomicField C12 = new CyclotomicField(12,"a");
       C12.hideMod();
       Ring P1 = new PolynomialRing( Ring.Z, "a,b");
       Ring [] r = {
                     P1,
                     new QuotientField( P1 ),
                     // new QuotientField(new
                     // UniversalPolynomialRing(Ring.Z))
                     new PolynomialRing( Ring.F2, "a,b,c,d"),
                     new PolynomialRing( Ring.C, "d,c,b,a"),
                     new PolynomialRing( Ring.Q, "a,b,c,d,e,f"),
                     new PolynomialRing( new SQRT2(), "b"),
                     new QuotientField(new PolynomialRing(Ring.R, "a,b")),
                     new PolynomialRing( C12, "b"),
                     new PolynomialRing( new ModularIntegerRing(10), "c,a,d,b")
                     };
       String [] tmpStr  = {
            "Polynomial ring in the variables a and b over integers",
            "Rational functions in a and b over integers",
            // "Rational functions over arbitrary variables",
            "Polynomials in a,b,c and d over the field of two elements",
            "Polynomials in d,c,b and a over complex numbers",
             "Polynomial ring in da,b,c,d,e and f over rationals",
            "Polynomial ring in b over the algebraic extensions Q(a) with a^2 = 2",
            "Rational functions in a and b with real coefficients",
            "Polynomial ring in b over the 12th cyclotomic field Q(a) with a^12 = 1",
            "Polynomial ring in c,a,d and b over integers modulo 10"
            };
       longRingDescription = tmpStr;
       rings = r;
       } catch (Throwable t) {
             output.append("Error in initializing. Please report to "+
                       "marc@perisic.com\n");
             output.append(t.toString());
             }
    shortRingDescription = new String [rings.length];

    // Setting Default values for the description.
    for( int i = 0; i < rings.length; i++) {
        shortRingDescription[i] = rings[i].toString();
        }
    shortRingDescription[1] = "Z(a)(b)";
    shortRingDescription[8] = "Z/10Z[c,a,d,b]"; //
    shortRingDescription[7] = "CYC(12)[b]";
    shortRingDescription[6] = "R(a)(b)";
    shortRingDescription[5] = "(Q[a]/(a^2 - 2))[b]";
    for( int i = 0; i < rings.length; i++ ) {
                  ringList.add(longRingDescription[i]);
                  }
    ringList.select(0);

    clearInput.addActionListener(this);
    clearOutput.addActionListener(this);
    input.addActionListener(this);
    enter.addActionListener(this);
    ringList.addActionListener(this);
    ringList.addItemListener(this);
    topPanel.add(clearOutput);
    topPanel.add(clearInput);

    topPanel.add(input);
    topPanel.add(enter);

    secondPanel.add(ringList);

     secondPanel.add(whatRing);


/*
    Panel p = new Panel();
    p.setLayout(new GridLayout(2, 0));
    p.add(new Checkbox("Use Color"));
    p.add(colorChoice);
    topPanel.add(p);
*/
    setLayout(new BorderLayout());
    add("North", topPanel);
    add("Center", secondPanel);
    add("South", output);
    output.setEditable(false);

    }

    /**
     * Handles action events for the applet -- either events
     * directed to the applet or events passed up to the
     * applet from components inside it.
     */
    public void actionPerformed(ActionEvent evt) {
    Object what = evt.getSource();
    ++actionCount;
        if( what == clearInput) {
              input.setText("");
              }
        else if( what == clearOutput ) {
              output.setText("");
              }
        else if( what == enter || what == input || what == ringList ) {
              String str = "";
              try {
              Ring S = rings[ringList.getSelectedIndex()];
              str = S.map(input.getText().trim()).toString()+"\n";
              } catch ( Throwable t ) {
                str = "Error, cannot map "+input.getText()+" into "+
                    shortRingDescription[ringList.getSelectedIndex()]+"\n"
                 + "    (because of: "+t.toString()+")\n";
                }
              output.append("#"+actionCount+"> "+str);
              }
      /*
        output.append("Action event #" +
                      actionCount + ", what = " +
                      what + "\n");
                      */

    }
     public void itemStateChanged(ItemEvent evt) {
          Object what = evt.getSource();
          if( what == ringList ) {
              try {
             whatRing.setText(
              shortRingDescription[ringList.getSelectedIndex()] );
              } catch (Throwable t) {
                  output.append(t.toString());
                  }
             }

    }


}

