/*
 * Aleph Toolkit
 *
 * Copyright 1999, Brown University, Providence, RI.
 * 
 *                         All Rights Reserved
 * 
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose other than its incorporation into a
 * commercial product is hereby granted without fee, provided that the
 * above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Brown University not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 * 
 * BROWN UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY
 * PARTICULAR PURPOSE.  IN NO EVENT SHALL BROWN UNIVERSITY BE LIABLE FOR
 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

package aleph.gui;

import aleph.Aleph;
import aleph.AlephDebug;
import aleph.Config;
import java.awt.Choice;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * User-controllable parameters.
 *
 * @author Maurice Herlihy
 * @date   January 1999
 **/

public class ControlPanel extends Panel {

  // my components
  private Choice outChoice;	// Window or aisle?
  private Choice compilerChoice; // exotic compiler?
  private Choice peChoice;	// number PEs
  private Choice verbChoice;	// verbosity

  private State state;
  private TextArea textArea;	// display output here

  /**
   * Sole Constructor.
   **/
  public ControlPanel (State state, TextArea textArea) {
    super();
    this.textArea = textArea;
    this.state    = state;

    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);

    GridBagConstraints c = new GridBagConstraints();

    // Chit-chat.
    Label verbLabel = new Label("Verbosity ", Label.RIGHT);
    add(verbLabel, c);
    verbChoice = new Choice();
    verbChoice.add("Laconic");
    verbChoice.add("Loquacious");
    verbChoice.add("Bloviating");
    verbChoice.select(Math.min(Aleph.getVerbosity(),
			       verbChoice.getItemCount()));
    verbChoice.addItemListener(new ItemListener() {
      public void itemStateChanged(ItemEvent e) {
        int verbosity = verbChoice.getSelectedIndex();
        // for local consumption
	Aleph.setVerbosity(verbosity);
        // for transmission to remote PEs
        Aleph.setProperty("aleph.verbosity", Integer.toString(verbosity));
      }
    });
    add(verbChoice, c);

    // Combien de PEs?
    Label peLabel = new Label("Number PEs ", Label.RIGHT);
    add(peLabel, c);
    peChoice = new Choice();
    // Calculate max PEs from Config and run-time switches
    int maxPEs = Aleph.getIntProperty("aleph.maxPEs", Config.maxPEs);
    for (int i = 1; i <= maxPEs; i++)
      peChoice.add(Integer.toString(i));

    // Calculate initial num PEs from Config and run-time switches
    peChoice.select(Math.max(0, state.numPEs - 1)); // Selection indexes from 0, not 1.
    // update state when user adjusts numPEs
    peChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	ControlPanel.this.state.numPEs = peChoice.getSelectedIndex() + 1;
      }
    });
    add(peChoice, c);

    // Where should output go?
    Label outLabel = new Label("Output ", Label.RIGHT);
    add(outLabel, c);
    outChoice = new Choice();
    outChoice.add("Console");
    outChoice.add("Paned Window");
    outChoice.add("Per-PE Windows");
    outChoice.add("Standard Output");
    outChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
        ControlPanel.this.state.outChoice = outChoice.getSelectedIndex();
      }});
    add(outChoice, c);

    // Which JIT compiler to use?
    Label compilerLabel = new Label("JIT Compiler ", Label.RIGHT);
    add(compilerLabel, c);
    compilerChoice = new Choice();
    compilerChoice.add("<default>");
    compilerChoice.add("<none>");
    if (Config.compilers != null && Config.compilers.length > 0)
      for (int i = 0; i < Config.compilers.length; i++)
	compilerChoice.add(Config.compilers[i]);
    compilerChoice.addItemListener(new ItemListener() {
      public void itemStateChanged (ItemEvent e) {
	int cChoice = compilerChoice.getSelectedIndex();
	switch(cChoice) {
 	case 0 :                // default
	  Aleph.removeProperty("aleph.compiler");
	  break;
	case 1 :                // no compiler
	  Aleph.setProperty("aleph.compiler", "none");
	  break;
	default :               // custom compiler
          try {
            Aleph.setProperty("aleph.compiler", Config.compilers[cChoice-2]);
          } catch (Exception x) {
            Aleph.panic("Unexpected compiler code: " + compilerChoice.getSelectedIndex());
          }
	  break;
	}
      }
    });
    add(compilerChoice, c);
  }

}
