/**
 * Start the BeanShell GUI desktop in a JFrame.  A starter workspace is created
 * and added to the desktop.
 *
 * @method void desktop()
 *
 * @author Pat Niemeyer
 * @author Daniel Leuck
 */
 
import javax.swing.*;
import javax.swing.border.*;
import bsh.util.JConsole;
import bsh.util.Util;
import bsh.Interpreter;
import java.awt.Component;
import bsh.Capabilities;

desktop() 
{
		
	// need a way to set things to void again
	if ( bsh.system.desktop != void ) {
		print("There is	already	a desktop running...");
		return;
	} else
		bsh.system.desktop = this;   // race condition (hah!)

	bsh.system.icons=object();
	
	bsh.system.icons.bean=
		new ImageIcon(getResource("/bsh/util/lib/icon.gif"));
	bsh.system.icons.workspace=
		new ImageIcon(getResource("/bsh/util/lib/workspace.gif"));	
	bsh.system.icons.script=
		new ImageIcon(getResource("/bsh/util/lib/script.gif"));
	bsh.system.icons.eye=
		new ImageIcon(getResource("/bsh/util/lib/eye.jpg"));		

	// Disallowed by applet security 
	try {
		bsh.system.fonts = GraphicsEnvironment.getLocalGraphicsEnvironment().
			getAvailableFontFamilyNames();
	} catch ( SecurityException se ) { }

	JPanel stage=new JPanel(new BorderLayout());
	JPanel taskBar=new JPanel(new FlowLayout(FlowLayout.LEFT, 1, 1));

	// For some insane reason, access to the JPanel background color object 
	// is protected on the Mac
	borderColor = taskBar.background;
	try {
		borderColor = taskBar.background.darker();
	} catch ( SecurityException e ) { }
	taskBar.setBorder( new MatteBorder(1,0,0,0, borderColor) );

	ButtonGroup taskBarButtonGroup = new ButtonGroup();
	
	// Ignore unhandled method invocations from listeners.
	invoke( method, args ) { }

	makeInternalFrame( String name ) 
	{
		// Closable by default
		this.frame = new JInternalFrame( name, true, true, true, true );
		frame.frameIcon=bsh.system.icons.bean;
		frame.visible=true;
		return frame;
	}

	this.frameMap=new Hashtable();
	taskBarButtonListener() {
		actionPerformed(ae) {
			this.iframe=frameMap.get(ae.source);
			if(iframe.icon)
				iframe.icon=false;
			if(iframe!=null && !iframe.selected)
				iframe.selected=true;
		}
		
		return this;
	}
	this.taskBarButtonListener=taskBarButtonListener();
	
	addInternalFrame( frame ) 
	{
		iframeListener=new InternalFrameListener() {
			
			internalFrameClosing(ife) {
				for(e:new Hashtable(frameMap).entrySet()) {
					if(e.value.equals(ife.source)) {
						taskBar.remove(e.key);
						taskBarButtonGroup.remove(e.key);
						frameMap.remove(e.key);
						taskBar.validate();
						taskBar.repaint();
					}
				}
			}
			
			internalFrameActivated(ife) {
				for(e:frameMap.entrySet()) if(e.value.equals(ife.source)) {
					if(!e.key.selected) e.key.selected=true;
				}
			}
			
			internalFrameDeactivated(ife) {}
			internalFrameClosed(ife) {}
			internalFrameOpened(ife) {}
			internalFrameIconified(ife) {}
			internalFrameDeiconified(ife) {}
		};
		
		bsh.system.desktop.pane.add( frame	);
		frame.addInternalFrameListener(iframeListener);
		JToggleButton button = new JToggleButton(frame.title, frame.frameIcon);
		taskBarButtonGroup.add(button);

		// For some insane rason access to some of the color objects is 
		// protected on the Mac
		try {
		button.border=
			new CompoundBorder((Border)thinBorder(), new EmptyBorder(2,2,2,3));
		} catch ( SecurityException e ) { }

		button.addActionListener(taskBarButtonListener);
		taskBar.add(button);
		taskBar.validate();
		
		frameMap.put(button,frame);
	}

	this.windowCount=0;

	mousePressed( e	) {
		popup.show( pane, e.getX(), e.getY() );
	}

	shutdown() {
		/*
		ret = JOptionPane.showInternalConfirmDialog( pane,
			"This workspace	has not	been saved. Do you really want to exit?" );
		if ( ret == JOptionPane.YES_OPTION )
				exit();
		*/
		frame.dispose();
		exit();
	}

	actionPerformed( e ) 
	{
		this.com = e.getActionCommand();
		if ( com.equals("New Bsh Workspace") )
			makeWorkspace( ""+ super.windowCount++);
		if ( com.equals("New Class Browser") )
			classBrowser();
		else if	( com.equals("Save Workspace") )
			JOptionPane.showInternalMessageDialog( pane, "Unimplemented" );
		else if	( com.equals("Exit") )
			shutdown();
	}

	this.pane=new JDesktopPane();

	this.popup=new JPopupMenu("Root Menu");
	this.mi=new JMenuItem("New Bsh Workspace");
	mi.addActionListener(this);
	popup.add( mi );
	mi=new JMenuItem("New Class Browser");
	mi.addActionListener(this);
	popup.add( mi );
	mi=new JMenuItem("Save Workspace");
	mi.addActionListener(this);
	popup.add( mi );
	mi=new JMenuItem("Exit");
	mi.addActionListener(this);
	popup.add( mi );

	pane.addMouseListener( this );

	this.frame=new JFrame("BeanShell Desktop 1.1");
	
	stage.add(pane);
	stage.add(taskBar, BorderLayout.SOUTH);
	
	frame.getContentPane().add(stage);

	windowClosing( e ) {
		bsh.system.desktop = null;
		shutdown();
	}

	frame.iconImage=bsh.system.icons.bean.image;
	frame.addWindowListener( this );

	/*
		If available, add a listener for classpath mapping
		I'm planning to implement a GUI progress indicator here

	if ( Capabilities.canGenerateInterfaces() ) 
	{
		import bsh.classpath.BshClassPath;
		classFeedbackListener = new BshClassPath.MappingFeedback() 
		{
			startClassMapping() { }
			classMapping( msg ) { }
			errorWhileMapping( msg ) { }
			endClassMapping() { }
		};
		BshClassPath.addMappingFeedback( classFeedbackListener );
	}
	*/

	// start one terminal
	workSpace=makeWorkspace( ""+windowCount++	);

	frame.setSize(800,600);
	frame.show();
	
	Util.endSplashScreen();
	
	frame.toFront();
	workSpace.frame.selected=true;
}
