view src/samer/core_/shells/AWTShell.java @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
line wrap: on
line source
/*
 *	AWTShell.java	
 *
 *	Copyright (c) 2000, Samer Abdallah, King's College London.
 *	All rights reserved.
 *
 *	This software is provided AS IS and WITHOUT ANY WARRANTY;
 *	without even the implied warranty of MERCHANTABILITY or 
 *	FITNESS FOR A PARTICULAR PURPOSE.
 */

package samer.core.shells;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

import samer.core.*;
import samer.core.util.*;
import samer.core.util.shell.*;
import samer.core.util.heavy.*;
import samer.core.util.heavy.Console;
import samer.core.util.heavy.Frame;
import samer.core.util.heavy.Dialog;
import samer.core.NumberViewer;
import samer.core.viewers.*;
import samer.core.types.*;

public class AWTShell extends AppShellBase
{
	public AWTShell() { this(getDefaultPropertiesFile()); }
	public AWTShell(String propsFile) 
	{
		super(propsFile);
		currentFrame = new java.awt.Frame("dummy"); // temporary dialog owner

		Shell.setShell(this);

		// going to cache font and menu font
		Font font=X.font(Shell.datum("font"),null);
		Font menufont=X.font(Shell.datum("menu.font"),null);
		if (font!=null) Shell.put("font", font);
		if (menufont!=null) Shell.put("menu.font", menufont);

		con   = new Console(); 
		confr = new Frame("console.frame");
		confr.setLayout( new BorderLayout(3,3));
		confr.add(con);
		confr.add( new CommandField(20), "South"); 
		confr.addWindowListener( Shell.exitListener());
		confr.expose();
		// currentFrame=confr;

		Shell.registerAgent(this);
		Shell.registerAgent(con);

		put(VDouble.class,DoubleViewer.class);
		put(VInteger.class,IntegerViewer.class);
		put(VBoolean.class,BooleanViewer.class);
		put(VString.class,StringViewer.class);
		put(VParameter.class,ParameterViewer.class);
		put(VFile.class,FileViewer.class);
		put(VColor.class,ColorButton.class);
		put(Variable.class,StringViewer.class);
		put(Viewable.class,DefaultViewer.class);

		try { execute("run",Shell.env()); }
		catch (Exception ex) { Shell.trace(ex.toString()); }
	}

	class VContainer extends WindowAdapter implements ViewableManager.ViewerContainer
	{
		Frame	frame;

		VContainer()
		{
			frame = new Frame("exposed");
			frame.setLayout( new StackLayout());
			frame.expose();
			frame.addWindowListener(this);
		}

		public void add(java.util.Iterator components)
		{
			Container c=frame.container();
			while (components.hasNext()) {
				c.add((Component)(components.next()));
			}
			c.validate();
			frame.pack();
		}

		public void removeAll() {
			frame.setVisible(false);
			frame.container().removeAll();
		}

		public void windowClosing(WindowEvent e) {
			vm.releaseViewerContainer();
			frame.dispose();
		}
	}

	public ViewableManager.ViewerContainer getViewerContainer() {
		return new VContainer();
	}

	public Shell.Dialog getDialog(String title) { return new Dialog(currentFrame,title,true); }
	public Shell.Window getWindow(String title) { return new BevelWindow(title); }
	public Viewer createViewerPanel(Viewer vwr) { return new VPanel(vwr); }
	public Container createButtonsFor(Agent agent)
	{
		ButtonBar bbar=new ButtonBar();
		bbar.setTarget(agent);
		agent.getCommands(bbar);
		return bbar.container();
	}

	public NumberViewer createNumberViewer(String label, int flags, NumberSink s) {
		return new TextualNumberViewer(label,flags,s);
	}

	public void exposeCommands( Agent agent)
	{
		if (buttonBar==null) {
			buttonBar = new ButtonBar();
			buttonBar.setBroadcaster(am.getBroadcaster());
			confr.add( buttonBar.container(), "North");
		}

		buttonBar.setTarget(agent);
		agent.getCommands(buttonBar);
		confr.validate();
	}

	public Component createLabel(String txt) { 
		Label l=new Label(txt);
		l.addMouseListener(MouseRetarget.listener); 
		return l;
	}

	// .......... Message printing .............. 

	public PrintWriter getPrintWriter() { 
		return new PrintWriter(con.getWriter(),true);
	}

	public void print(String msg) { con.write(msg); con.write("\n"); }
	public void status(String msg) { con.write(msg); con.write("\n"); }


	// ............... Private bits .............

	private java.awt.Frame	currentFrame=null;

	private Frame		confr=null;
	private Console	con=null;
	private ButtonBar	buttonBar=null;

	public static void main( String args[]) { new AWTShell(); }

}



class BevelWindow extends Frame implements Shell.Window
{
	JPanel panel; 

	public BevelWindow(String name) 
	{	
		super(name); 
		Shell.push(getNode());

		panel = new JPanel( Border.create(
			Border.NONE, Shell.getInt("borderWidth",2)
		));

		boolean bw = Shell.getBoolean("bevelChildren",false);
		if (bw) panel.setChildBorder(Border.create(Border.IN,0));

		Shell.pop();
		add(panel); 
	}

	public Container container() { return panel; }
}