/** file: FlussoApp.java (applet) */

import java.awt.*;
import java.lang.Boolean;
import java.lang.Exception;

/** ================
      Mia Finestra
    ================ */
class myFrame extends Frame {

  public myFrame (String s) { super(s); }

  public boolean handleEvent (Event evt) {
    if (evt.id == Event.WINDOW_DESTROY) {
      dispose();
      return true;
    }
    return super.handleEvent(evt);
  }
} //myFrame

/** ====================
      APPLET PER PROVA
    ==================== */
/** FlussoApp e' l'applet che mostra come usare lo stream */
public class FlussoApp extends java.applet.Applet {
  /** Questo e' l'accumulatore che interroghero' */
  protected Accumulatore MioAcc;
  
  /** area per l'output (il risultato dell'elaborazione)  */
  protected TextArea  AreaOut;
  /** area per i messaggi di evoluzione del flusso di oggetti */
  protected TextArea  AreaMsg;
  /** Campi per il controllo */
  protected TextField CampoNumIni,
                      CampoNumFin;
  /** CheckBox per controllo della stampa dei messaggi */
  protected Checkbox  CBoxMess;
  /** Bottone "vai" */
  protected Button    BottoneVAI;
  protected static String NomeBottVAI = "VAI!";
  
  
  public FlussoApp() { super(); }

  /** Inizializzazione dell'applet */
  public void init() {
    setBackground (Color.blue);
    setForeground (Color.white);          // Aree di stampa
    setFont (new Font("TimesRoman", Font.PLAIN, 13));
    
    Panel PanInput = new Panel();
    GridBagLayout      GBL = new GridBagLayout();
    PanInput.setLayout(GBL);
    PanInput.setBackground (Color.green);
    PanInput.setForeground (Color.black);
    PanInput.setFont (new Font("TimesRoman", Font.PLAIN, 20));
    
    GridBagConstraints GBC = new GridBagConstraints();
    GBC.gridheight= 1;
    GBC.gridwidth = 2;
    
    CampoNumIni = new TextField ("2", 5);
    Label LabNI = new Label("Valore Iniziale");
    GBC.gridx = 1;
    GBC.gridy = 1;
    GBL.setConstraints (LabNI, GBC);
    PanInput.add (LabNI);
    GBC.gridx = 5;
    GBL.setConstraints (CampoNumIni, GBC);
    PanInput.add (CampoNumIni);
    
    CampoNumFin = new TextField ("17", 5);
    Label LabNF = new Label("Valore Finale");
    GBC.gridx = 1;
    GBC.gridy = 2;
    GBC.anchor= GridBagConstraints.WEST;
    GBL.setConstraints (LabNF, GBC);
    PanInput.add (LabNF);
    GBC.gridx = 5;
    GBL.setConstraints (CampoNumFin, GBC);
    PanInput.add (CampoNumFin);
    
    CBoxMess   = new Checkbox ("Messaggi");
    CBoxMess.setState(true);
    GBC.gridx = 8;
    GBC.gridy = 1;
    GBL.setConstraints (CBoxMess, GBC);
    PanInput.add (CBoxMess);

    BottoneVAI = new Button (NomeBottVAI);
    BottoneVAI.setBackground (Color.white);
    BottoneVAI.setForeground (Color.red);
    BottoneVAI.setFont (new Font("Helvetica", Font.BOLD, 20));
    GBC.gridx = 8;
    GBC.gridy = 2;
    GBC.anchor= GridBagConstraints.CENTER;
    GBL.setConstraints (BottoneVAI, GBC);
    PanInput.add (BottoneVAI);
    add (PanInput);

    
    Panel PanOut = new Panel();
    GridBagLayout      GBL2 = new GridBagLayout();
    GridBagConstraints GBC2 = new GridBagConstraints();
    GBC2.gridheight= 1;
    GBC2.gridwidth = 2;
    PanOut.setLayout(GBL2);
    
    AreaOut = new TextArea ("", 4,50);
                            //TextArea.SCROLLBARS_VERTICAL_ONLY);
    AreaOut.setBackground (Color.white);
    AreaOut.setForeground (Color.black);
    AreaOut.setFont (new Font("TimesRoman", Font.BOLD, 16));
    Label LabO1 = new Label ("Dati in USCITA");
    GBC2.gridx = 1;
    GBC2.gridy = 1;
    GBL2.setConstraints (LabO1, GBC2);
    PanOut.add (LabO1);
    GBC2.gridx = 1;
    GBC2.gridy = 2;
    GBL2.setConstraints (AreaOut, GBC2);
    PanOut.add (AreaOut);
    add (PanOut);
    
    Panel PanMsg = new Panel();
    PanMsg.setLayout(GBL2);
    AreaMsg = new TextArea ("", 10,82);
                            //TextArea.SCROLLBARS_VERTICAL_ONLY
    AreaMsg.setBackground (Color.black);
    AreaMsg.setForeground (Color.white);
    AreaMsg.setFont (new Font("Courier", Font.PLAIN, 12));
    Label LabO2 = new Label ("Area dei Messaggi");
    GBC2.gridx = 1;
    GBC2.gridy = 1;
    GBL2.setConstraints (LabO2, GBC2);
    PanMsg.add (LabO2);
    GBC2.gridx = 1;
    GBC2.gridy = 2;
    GBL2.setConstraints (AreaMsg, GBC2);
    PanMsg.add (AreaMsg);
    add (PanMsg);
  }//init
  
  /** Si comincia la generazione del flusso */
  public void AvviaGenerazione() {
    TextArea TA;
    AreaMsg.setText ("");    // cancella area messaggi
    AreaMsg.appendText ("INIZIO GENERAZIONE\n");
    AreaOut.setText ("");    // cancella area output
    try {
      // Prende i dati dai "campi di testo"
      int NumInizio=(new Integer(CampoNumIni.getText())).intValue();
      int NumFine  =(new Integer(CampoNumFin.getText())).intValue();
      AreaMsg.appendText ("DA "+NumInizio+" A "+NumFine);
      if (CBoxMess.getState()) TA = AreaMsg;
                          else TA = null;  // Non stampa messaggi;
      //  crea la struttura iniziale "Gen==>Acc":
      MioAcc = new Accumulatore (new Generatore(2,TA), TA);
      // nota: DEVO partire sempre da 2
      int Num = MioAcc.DammiSucc();   // Richiedo il PRIMO numero!
      int PosOut=1;                     // posizione di stampa
      while (Num < NumFine) {
        if (Num >= NumInizio) {       // Se e' nell'intervallo dato,
          AreaOut.appendText (Num+",  "); // Stampa in numero primo
          PosOut=PosOut+6;
          if (PosOut >= 50) {
            AreaOut.appendText ("\n"); // Conteggio per andare a capo
            PosOut=1;
          }
        }
        Num=MioAcc.DammiSucc();            // Chiede il numero successivo!
      }
      AreaMsg.appendText ("\nFINITO!");
    } catch (Exception ecc)
            {AreaMsg.appendText ("!!! ERRORE !!! "+ecc+"\n"); }
  }//AvviaGenerazione
  
  /** Rileva e gestisce la pressione del bottone */
  public boolean action (Event evt, Object arg) {
    if (evt.target instanceof Button) {
      String Nome = (String)arg;
      if (Nome.compareTo(NomeBottVAI) == 0) {
        AvviaGenerazione();
        return true;  // fine passaggio evento
      }
      return super.action (evt,arg);
    }//If Button
    return super.action (evt,arg);
  }//action

}//FlussoApp

//FINE FILE