view src/samer/maths/VFunction.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
/*
 *	VFunction.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.maths;
import  samer.core.*;
import  samer.core.util.*;
import  samer.maths.*;
import  java.util.*;

import  samer.core.util.Tools;

/**
	A Viewable for a Function - provides a UI for getting
	a plot of the function and for looking at its derivative
	(would like to be able to switch the Function too)
  */

public class VFunction extends Viewable implements Agent
{
	Function	fn;

	public VFunction(String name) { this(name,null); }
	public VFunction(String name, Function f) 
	{
		super(name); fn=f; setAgent(this);
		Shell.registerViewable(this);
	}

	public void dispose() { Shell.deregisterViewable(this); super.dispose(); }
	public void setFunction(Function f) { fn=f; changed(); }
	public Function getFunction() { return fn; }
	public Viewer getViewer()
	{
		BaseViewer vwr = new BaseViewer(this) {
			{ update(VFunction.this,null); }
			public void update(Observable o, Object a) {
				setText( 
					getLabel() + ": "
					+ (fn==null ? "null" : fn.format("t")) 
				);
				super.update(o,a);
			}
		};

		return vwr;
	}

	public void getCommands(Agent.Registry r) { 
		r.add("plotter").add("derivative").add("inverse"); 
	}

	public void execute(String cmd, Environment env) {
		if (cmd.equals("plotter")) {
			Shell.push(new Node("plotter",getNode()));
			FunctionPlotter plotter = new FunctionPlotter(this);
			Shell.expose((Viewer)plotter,"window");
			Shell.pop();
		} else if (cmd.equals("derivative")) {
			Shell.push(node);
			final VFunction dfn = new VFunction("derivative",fn.derivative());
			Shell.pop();
			addObserver( new Observer() {
				// this chains any update messages along to derivatives
				public void update(Observable o,Object arg) { dfn.changed(); }
			} );
			
		} else if (cmd.equals("inverse")) {
			Shell.push(node);
			final VFunction dfn = new VFunction("inverse",fn.inverse());
			Shell.pop();
		}
	}
}