samer@0: /* samer@0: * VFunction.java samer@0: * samer@0: * Copyright (c) 2000, Samer Abdallah, King's College London. samer@0: * All rights reserved. samer@0: * samer@0: * This software is provided AS iS and WITHOUT ANY WARRANTY; samer@0: * without even the implied warranty of MERCHANTABILITY or samer@0: * FITNESS FOR A PARTICULAR PURPOSE. samer@0: */ samer@0: samer@0: package samer.maths; samer@0: import samer.core.*; samer@0: import samer.core.util.*; samer@0: import samer.maths.*; samer@0: import java.util.*; samer@0: samer@0: import samer.core.util.Tools; samer@0: samer@0: /** samer@0: A Viewable for a Function - provides a UI for getting samer@0: a plot of the function and for looking at its derivative samer@0: (would like to be able to switch the Function too) samer@0: */ samer@0: samer@0: public class VFunction extends Viewable implements Agent samer@0: { samer@0: Function fn; samer@0: samer@0: public VFunction(String name) { this(name,null); } samer@0: public VFunction(String name, Function f) samer@0: { samer@0: super(name); fn=f; setAgent(this); samer@0: Shell.registerViewable(this); samer@0: } samer@0: samer@0: public void dispose() { Shell.deregisterViewable(this); super.dispose(); } samer@0: public void setFunction(Function f) { fn=f; changed(); } samer@0: public Function getFunction() { return fn; } samer@0: public Viewer getViewer() samer@0: { samer@0: BaseViewer vwr = new BaseViewer(this) { samer@0: { update(VFunction.this,null); } samer@0: public void update(Observable o, Object a) { samer@0: setText( samer@0: getLabel() + ": " samer@0: + (fn==null ? "null" : fn.format("t")) samer@0: ); samer@0: super.update(o,a); samer@0: } samer@0: }; samer@0: samer@0: return vwr; samer@0: } samer@0: samer@0: public void getCommands(Agent.Registry r) { samer@0: r.add("plotter").add("derivative").add("inverse"); samer@0: } samer@0: samer@0: public void execute(String cmd, Environment env) { samer@0: if (cmd.equals("plotter")) { samer@0: Shell.push(new Node("plotter",getNode())); samer@0: FunctionPlotter plotter = new FunctionPlotter(this); samer@0: Shell.expose((Viewer)plotter,"window"); samer@0: Shell.pop(); samer@0: } else if (cmd.equals("derivative")) { samer@0: Shell.push(node); samer@0: final VFunction dfn = new VFunction("derivative",fn.derivative()); samer@0: Shell.pop(); samer@0: addObserver( new Observer() { samer@0: // this chains any update messages along to derivatives samer@0: public void update(Observable o,Object arg) { dfn.changed(); } samer@0: } ); samer@0: samer@0: } else if (cmd.equals("inverse")) { samer@0: Shell.push(node); samer@0: final VFunction dfn = new VFunction("inverse",fn.inverse()); samer@0: Shell.pop(); samer@0: } samer@0: } samer@0: }