/*
	CommandsPanel.java
	by Richard Unger, March 1998

	This class provides a simple panel with all the interface elements
	necessary to enter commands, display commands and run/record demo files.
*/

import java.awt.*;
import java.awt.event.*;


public class CommandsPanel extends Panel implements ActionListener, CommandTicker {

// local variables
	private GridBagLayout theLay;
	private TextField commEntry;
	private TextArea commDisplay;
	private Button demoButton;
	private TextField demoFile;
	private TextField logFile;
	private TalkerCheckbox logBox;
	private int commandsDisplayed = 0;
	public boolean canSee;
	private String displayText;
	
	private CommandParser commander;

// constructor - builds a new CommandsPanel, adding all the interface elements

	public CommandsPanel(boolean recordable,CommandParser commands){
		super();
		
		commander = commands;
		
		theLay = new GridBagLayout();
		setLayout(theLay);
			
		// create command entry field
		commEntry = new TextField(35);
		commEntry.setFont(new Font("Monaco",Font.PLAIN,10));
		add(commEntry,1,4,25,1,100,100,GridBagConstraints.NORTH,GridBagConstraints.HORIZONTAL);
		commEntry.addActionListener(this);
		
		// create scrolling command display
		commDisplay = new TextArea("",4,35,TextArea.SCROLLBARS_VERTICAL_ONLY);
		commDisplay.setFont(new Font("Monaco",Font.PLAIN,10));
		commDisplay.setEditable(false);
		displayText = new String("commands initialized");
		add(commDisplay,1,0,25,4,100,100,GridBagConstraints.SOUTH,GridBagConstraints.HORIZONTAL);
		addComponentListener(new CommandsPanelCAdapter());


		// create demo play button
		// create file-name entry
		demoButton = new Button("Play Demo File:"){
			protected void processActionEvent(ActionEvent e){
				setActionCommand("run "+demoFile.getText());
				super.processActionEvent(e);
				}
			};
		add(demoButton,26,0,5,1,100,100,GridBagConstraints.CENTER,GridBagConstraints.NONE);
		demoButton.addActionListener(commander);
		
		//add(new Label("File Name:"),26,1,1,1);
		
		demoFile = new TextField("spheresdemo.txt");
		add(demoFile,31,0,10,1,100,100,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL);
		
		// create demo record button
		// create file-name - entry
		if (recordable){
			logBox = new TalkerCheckbox("Log to File:",false){
				public String talkCommand(){
					if (getState())
						return "record "+logFile.getText();
					else
						return "stop record";
					}
				};
			add(logBox,26,2,5,1,100,100,GridBagConstraints.EAST,GridBagConstraints.NONE);
			logBox.addItemListener(commander);
		
			logFile = new TextField("commanddump.txt");
			add(logFile,31,2,10,1,100,100,GridBagConstraints.WEST,GridBagConstraints.HORIZONTAL);
			}
		
		// done creating the interface
		
		// handle some events...
		repaint();
		
		}

// actionlistener implementation

	public void actionPerformed(ActionEvent e){
		// we are only listening to actions from our text-field
		commander.SendCommand(commEntry.getText());
		commEntry.setText("");
		}
		

// componentlistener

	public class CommandsPanelCAdapter extends ComponentAdapter{
		
		 public void componentShown(ComponentEvent e){
			canSee = true;
			UpdateDisplay();
		 	}
		 	
		 public void componentHidden(ComponentEvent e){
			canSee = false;
		 	}
		
		}



// CommandTicker implementation


	private void UpdateDisplay(){
		commDisplay.setText(displayText);
		commDisplay.setCaretPosition(displayText.length());
		}
		

	public void printCommand(String commandInstance){
		if (commandsDisplayed>25){
			int index = displayText.indexOf('\n');
			displayText = displayText.substring(index+1);
			}
		displayText = displayText+'\n'+commandInstance;
		commandsDisplayed++;
		if (canSee)
			UpdateDisplay();
		}

	
// routines to handle the gridbag layout...

	public void add(Component what,int gridx,int gridy,int wid,int high,int wx,int wy,int anc,int fill){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx=gridx;
		c.gridy=gridy;
		c.gridwidth=wid;
		c.gridheight=high;
		c.weightx=wx;
		c.weighty=wy;
		c.anchor=anc;
		c.fill=fill;
		theLay.setConstraints(what,c);		//set constraints
		add(what);							//add it to panel
		}
	public void add(Component what,int gridx,int gridy,int wid,int high){
		add(what,gridx,gridy,wid,high,100,100,GridBagConstraints.WEST,GridBagConstraints.NONE);
		}


}