/*
 * 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.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.MenuBar;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintStream;

/**
 * Calls Aleph from the awt console.
 *
 * @author Maurice Herlihy
 * @date   January 1999
 **/

public class Console extends Frame {

  /**
   * Main program.
   **/
  public Console (String[] args) {
    super(Config.banner);
    try {
      State state = new State(this);

      addWindowListener(new WindowAdapter() {
	public void windowClosing(WindowEvent e) {
	  setVisible(false);
	  dispose();
	  System.exit(0);
	}
      });

      // Le Menu
      MenuBar mb = new MenuBar();
      setMenuBar(mb);
      mb.add(new FileMenu(state));
      mb.add(new HostMenu(state));
      mb.setHelpMenu(new HelpMenu(this));

      // If user didn't specify number of PEs, take one per selected host.
      if (state.numPEs == 0)
	state.numPEs = state.selectedHosts.size();

      // control format
      setLayout(new GridBagLayout());

      GridBagConstraints c = new GridBagConstraints();
      c.gridwidth = GridBagConstraints.REMAINDER;	// last in row
      c.weightx = 1.0;		// become wider
      c.weighty = 0.0;		// no higher
      c.fill = GridBagConstraints.BOTH;

      // create output display area but don't add it yet
      TextArea textArea = new TextArea(24, 80);
      textArea.setEditable(false);
      textArea.setBackground(Config.background);
      textArea.setForeground(Config.foreground);


      // start button, command line, and clear button
      add(new CommandPanel(state, args, textArea), c);

      // various run-time choices
      add(new ControlPanel(state, textArea), c);
      add(new RuntimePanel(state), c);

      // Output display area
      c.fill = GridBagConstraints.BOTH;
      c.weightx = 1.0;
      c.weighty = 1.0;
      c.anchor = GridBagConstraints.SOUTH;
      add(textArea, c);

      // Manager choices
      add(new ManagerPanel(), c);

      pack();
      setVisible(true);
    } catch (Exception e) {
      Aleph.panic("Consolem " + e);
    }
  }

}
