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

/**
 * An example for mapping of Strings into a selection of rings.
 */
 
 
public class PolDemo extends java.applet.Applet 
    implements ActionListener, ItemListener {
 
    String [] vars =  {"a","b","c","d","e","f","g","h","i","j" };
    
    RingElt lastResult = null;
    Ring ringFrom = null;
    Ring ringTo = null;
    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");
    Button shuffle = new Button ("Shuffle Variables");
    TextField input = new TextField("(a  + c + e + g + i )^3", 30);
    Label whatRing = new Label("-----------------------------------------------------");
    
    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");
*/
   
       
    
    clearInput.addActionListener(this);
    clearOutput.addActionListener(this);
    input.addActionListener(this);
    enter.addActionListener(this);
    shuffle.addActionListener(this);
    ringList.addActionListener(this);
    ringList.addItemListener(this);
    topPanel.add(clearOutput);
    topPanel.add(clearInput);
    
    topPanel.add(input);
    //topPanel.add(enter);
    
    try {
      Ring S = new PolynomialRing(Ring.Z, vars);
      whatRing.setText(S.toString());
      } catch ( Throwable t ) {
                output.append("Error: "+t.toString()+"\n");
                }
    secondPanel.add(whatRing);
    topPanel.add(shuffle);
   
    
/*
    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;
    //output.append(evt.toString());
        if( what == clearInput) {
              input.setText("");
              }
        else if( what == clearOutput ) {
              output.setText("");
              }
        else if( what == shuffle || what == input) {
              //output.append("Shuffling Variables\n");
              String str = "";
              try {
              ringFrom = new PolynomialRing(Ring.Z, vars);
              lastResult = ringFrom.map(input.getText().trim());
              output.append("Input: "+ lastResult.toString()+"\n");
              ringFrom = lastResult.getRing();
              for( int k = vars.length - 1; k >= 0; k-- ) {
                     int l = (int) (Math.random() * 100000) % (k+1);
                     String tmp = vars[k];
                     vars[k] = vars[l];
                     vars[l] = tmp;
                     }
              ringTo = new PolynomialRing(Ring.Z, vars);
              output.append(ringFrom.toString()+" -> ");
              output.append(ringTo.toString()+"\n");
              long j = System.currentTimeMillis(); 
              lastResult = ringTo.map(lastResult);
              long k = System.currentTimeMillis();
              output.append("Result: "+lastResult.toString()+"\n");
              output.append("Time in Milliseconds: "+(k-j)+"\n"); 
              whatRing.setText(ringTo.toString());
              } catch ( Throwable t ) {
                output.append("Error: "+t.toString()+"\n");
                }
             
              }
        else if( what == enter ) {
              String str = "";
              Ring S = null;
              try {
              S = new PolynomialRing(Ring.Z, vars);
              lastResult = S.map(input.getText().trim());
              str = lastResult.toString()+"\n"; 
              whatRing.setText(S.toString());
              } catch ( Throwable t ) {
                str = "Error, cannot map "+input.getText()+" into "+
                    S+"\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( "hmmm"              );
              } catch (Throwable t) {
                  output.append(t.toString());
                  }
             }
                      
    }


}

