samer@0
|
1 /*
|
samer@0
|
2 * VFunction.java
|
samer@0
|
3 *
|
samer@0
|
4 * Copyright (c) 2000, Samer Abdallah, King's College London.
|
samer@0
|
5 * All rights reserved.
|
samer@0
|
6 *
|
samer@0
|
7 * This software is provided AS iS and WITHOUT ANY WARRANTY;
|
samer@0
|
8 * without even the implied warranty of MERCHANTABILITY or
|
samer@0
|
9 * FITNESS FOR A PARTICULAR PURPOSE.
|
samer@0
|
10 */
|
samer@0
|
11
|
samer@0
|
12 package samer.maths;
|
samer@0
|
13 import samer.core.*;
|
samer@0
|
14 import samer.core.util.*;
|
samer@0
|
15 import samer.maths.*;
|
samer@0
|
16 import java.util.*;
|
samer@0
|
17
|
samer@0
|
18 import samer.core.util.Tools;
|
samer@0
|
19
|
samer@0
|
20 /**
|
samer@0
|
21 A Viewable for a Function - provides a UI for getting
|
samer@0
|
22 a plot of the function and for looking at its derivative
|
samer@0
|
23 (would like to be able to switch the Function too)
|
samer@0
|
24 */
|
samer@0
|
25
|
samer@0
|
26 public class VFunction extends Viewable implements Agent
|
samer@0
|
27 {
|
samer@0
|
28 Function fn;
|
samer@0
|
29
|
samer@0
|
30 public VFunction(String name) { this(name,null); }
|
samer@0
|
31 public VFunction(String name, Function f)
|
samer@0
|
32 {
|
samer@0
|
33 super(name); fn=f; setAgent(this);
|
samer@0
|
34 Shell.registerViewable(this);
|
samer@0
|
35 }
|
samer@0
|
36
|
samer@0
|
37 public void dispose() { Shell.deregisterViewable(this); super.dispose(); }
|
samer@0
|
38 public void setFunction(Function f) { fn=f; changed(); }
|
samer@0
|
39 public Function getFunction() { return fn; }
|
samer@0
|
40 public Viewer getViewer()
|
samer@0
|
41 {
|
samer@0
|
42 BaseViewer vwr = new BaseViewer(this) {
|
samer@0
|
43 { update(VFunction.this,null); }
|
samer@0
|
44 public void update(Observable o, Object a) {
|
samer@0
|
45 setText(
|
samer@0
|
46 getLabel() + ": "
|
samer@0
|
47 + (fn==null ? "null" : fn.format("t"))
|
samer@0
|
48 );
|
samer@0
|
49 super.update(o,a);
|
samer@0
|
50 }
|
samer@0
|
51 };
|
samer@0
|
52
|
samer@0
|
53 return vwr;
|
samer@0
|
54 }
|
samer@0
|
55
|
samer@0
|
56 public void getCommands(Agent.Registry r) {
|
samer@0
|
57 r.add("plotter").add("derivative").add("inverse");
|
samer@0
|
58 }
|
samer@0
|
59
|
samer@0
|
60 public void execute(String cmd, Environment env) {
|
samer@0
|
61 if (cmd.equals("plotter")) {
|
samer@0
|
62 Shell.push(new Node("plotter",getNode()));
|
samer@0
|
63 FunctionPlotter plotter = new FunctionPlotter(this);
|
samer@0
|
64 Shell.expose((Viewer)plotter,"window");
|
samer@0
|
65 Shell.pop();
|
samer@0
|
66 } else if (cmd.equals("derivative")) {
|
samer@0
|
67 Shell.push(node);
|
samer@0
|
68 final VFunction dfn = new VFunction("derivative",fn.derivative());
|
samer@0
|
69 Shell.pop();
|
samer@0
|
70 addObserver( new Observer() {
|
samer@0
|
71 // this chains any update messages along to derivatives
|
samer@0
|
72 public void update(Observable o,Object arg) { dfn.changed(); }
|
samer@0
|
73 } );
|
samer@0
|
74
|
samer@0
|
75 } else if (cmd.equals("inverse")) {
|
samer@0
|
76 Shell.push(node);
|
samer@0
|
77 final VFunction dfn = new VFunction("inverse",fn.inverse());
|
samer@0
|
78 Shell.pop();
|
samer@0
|
79 }
|
samer@0
|
80 }
|
samer@0
|
81 }
|