/*
	ControlPanel.java
	by Richard Unger, March 1998

	This class implements a control panel for the Spheres application
	It should be adaptable to other applications with a minimum of fuss.
	It does not have specific interface elements relating to Spheres
	itself, rather it adds a numbe of specific sub-panels, accessible
	via labled tabs.

	At the bottom is a small list of the commands it understands and installs...
*/


import java.awt.*;
import java.util.Hashtable;
import java.util.StringTokenizer;

public class ControlPanel extends Panel implements CommandExecutor{
	private Panel buttonPanel,cardPanel;
	private CardLayout theCards;
	private Hashtable theControls;
	private CommandParser commander;



//constructor


	public ControlPanel(CommandParser commands){
		super();
		
		commander = commands;
		commander.AddCommand("control showpage <str>",this);
		
		theControls = new Hashtable(10);
		
		buttonPanel = new Panel();
		buttonPanel.setLayout(new GridLayout(0,1));	// any # rows, 1 collumn
		
		cardPanel = new Panel();
		theCards = new CardLayout();
		cardPanel.setLayout(theCards);
		
		setLayout(new BorderLayout());
		add("West",buttonPanel);
		add("Center",cardPanel);
		
		repaint();
		}



// handle commands


	public String performCommand(String command){
		StringTokenizer toke = new StringTokenizer(command);
		if (toke.countTokens()==3){
			toke.nextToken();
			toke.nextToken();
			ShowPanel(toke.nextToken());
			}
		return null;
		}



// methods dealing with the panels
	
	
	public void ShowPanel(String name){
		if (theControls.containsKey(name)){
			theCards.show(cardPanel,name);
			}
		else
			System.err.println("CommandParser.ShowPanel(): no such panel: "+name);
		}
		
	
	public void AddPanel(String name,Panel p){
		
		// for each panel, create a button
		// store button and panel in a data-structure by name
		// note, panel names should be alpha-numeric, one word...
		
		Button newButton = new Button(name);
		ControlSubPanel it = new ControlSubPanel(name,p,newButton);

		newButton.setActionCommand("control showpage "+name);
		newButton.addActionListener(commander);
		
		theControls.put(name,it);
		theCards.addLayoutComponent(p,name);
		cardPanel.add("Center",p);
		buttonPanel.add("South",newButton);
		}
	
	
	public void RemovePanel(String name){
		ControlSubPanel which;
		
		which = (ControlSubPanel)theControls.get(name);
		if (which!=null){
			buttonPanel.remove(which.theButton);
			theCards.removeLayoutComponent(which.thePanel);
			cardPanel.remove(which.thePanel);
			theControls.remove(name);
			}
		}
	
}


class ControlSubPanel{
	public Panel thePanel;
	public Button theButton;
	public String theName;
	
	public ControlSubPanel(String inName,Panel inPanel,Button inButton){
		thePanel = inPanel;
		theButton = inButton;
		theName = inName;
		}
	
		
}




/*
The control panel object makes use of the following commands:

control showpanel <name>

where name is any string - the format for specifying this is:
'control showpanel <str>'

*/