/**
 * Creates a JConsole in a JInternalFrame and adds it to the desktop 
 *
 * @return this (the workspace scripted object for allowing access to the 
 *          frame, interpreter, etc.)
 *
 * @author Pat Niemeyer
 * @author Daniel Leuck (bug fixes)
 */

import javax.swing.*;
import bsh.Interpreter;
import bsh.BshClassManager;
import bsh.ClassPathException;
import bsh.util.JConsole;
import bsh.util.NameCompletionTable;

makeWorkspace( String name ) 
{
	if ( bsh.system.desktop == void ) {
		print("No desktop...");
		return;
	}

	this.console = new JConsole();
	this.name="Bsh Workspace: "+name;

	this.interpreter = new Interpreter( console );

	// provide name completion for console, name source is global namespace
	// move this into JConsole?

	// Access to read classpath is protected 
	try {
	this.nct = new NameCompletionTable();
	nct.add( interpreter.getNameSpace() );
	try {
		this.bcm = this.caller.namespace.getClassManager();
		if ( bcm != null ) {
			classNamesSource = bcm.getClassPath();
			nct.add( classNamesSource );
		}
	} catch ( ClassPathException e ) {
		error("classpath exception in name compl:"+e);
	}
	console.setNameCompletion( nct );
	// end setup name completion
	} catch ( SecurityException e ) { }

	// for convenience and backwards compatibility
	interpreter.set( "bsh.desktop",  bsh.system.desktop );

	this.frame = bsh.system.desktop.makeInternalFrame( name );
	frame.frameIcon=bsh.system.icons.workspace;
	
	/*
		Notes: Careful not to print anything before returning sys io...
		console is now gone.
	*/
	internalFrameClosing( e ) {
		if ( haveSysIO )
			returnSysIO();
	}
	internalFrameActivated(ife) {}
	internalFrameDeactivated(ife) {}
	internalFrameClosed(ife) {}
	internalFrameOpened(ife) {}
	internalFrameIconified(ife) {}
	internalFrameDeiconified(ife) {}	
	
	frame.addInternalFrameListener(this);

	actionPerformed( e ) 
	{
		this.com = e.getActionCommand();
		if ( com.equals("Workspace Editor") )
			workspaceEditor( interpreter, name );
		else if ( com.equals("Capture System in/out/err") )
			captureSysIO();
		else if	( com.equals("Close") )	{
			frame.setClosed(true);
		}
	}

	this.menubar = new JMenuBar();
	this.menu=new JMenu("File");
	this.mi=new JMenuItem("Workspace Editor");
	mi.addActionListener(this);
	menu.add(mi);
	mi=new JMenuItem("Capture System in/out/err");
	mi.addActionListener(this);
	menu.add(mi);
	mi=new JMenuItem("Close");
	mi.addActionListener(this);
	menu.add(mi);
	menubar.add(menu);

	menu = fontMenu(console);
	menubar.add(menu);

	frame.setMenuBar(menubar);

	frame.getContentPane().add("Center", console);
	//frame.pack();
	this.thread = new Thread( interpreter );
	thread.start();

	frame.setBounds(5,5,600,300);
	// cascade windows?
	//off=bsh.system.desktop.windowCount*10;
	//frame.setLocation( off, off );
	//frame.validate();
	bsh.system.desktop.addInternalFrame( frame	);
	frame.toFront();
	frame.setSelected(true);

	this.haveSysIO=false;
	this.sysIn = System.in;
	this.sysOut = System.out;
	this.sysErr = System.err;

	captureSysIO() {
		super.haveSysIO = true; // old scoping rules
		System.setIn( console.getInputStream() );
		System.setOut( console.getOut() );
		System.setErr( console.getErr() );
	}

	returnSysIO() {
		super.haveSysIO = false; // old scoping rules
		System.setIn( sysIn );
		System.setOut( sysOut );
		System.setErr( sysErr );
	}

	return this;
}

