samer@0
|
1 /*
|
samer@0
|
2 * UserEnvironment.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.core.util;
|
samer@0
|
13 import samer.core.types.*;
|
samer@0
|
14 import samer.core.*;
|
samer@0
|
15 import java.util.Hashtable;
|
samer@0
|
16 import java.awt.Color;
|
samer@0
|
17
|
samer@0
|
18 /**
|
samer@0
|
19 * Environment which provides access to the user
|
samer@0
|
20 * using a GUI
|
samer@0
|
21 */
|
samer@0
|
22
|
samer@0
|
23 public class UserEnvironment extends Environment
|
samer@0
|
24 {
|
samer@0
|
25 boolean toggleBoolean=false;
|
samer@0
|
26 boolean state;
|
samer@0
|
27
|
samer@0
|
28 public UserEnvironment(Environment parent) { super(parent,parent.node()); }
|
samer@0
|
29 public UserEnvironment(Environment parent, boolean bool) {
|
samer@0
|
30 super(parent,parent.node()); toggleBoolean=true; state=bool;
|
samer@0
|
31 }
|
samer@0
|
32
|
samer@0
|
33 private static Hashtable reg=new Hashtable();
|
samer@0
|
34
|
samer@0
|
35 public interface Thingy {
|
samer@0
|
36 Viewable viewable(String name, Object obj);
|
samer@0
|
37 Object decode(Viewable vbl);
|
samer@0
|
38 }
|
samer@0
|
39
|
samer@0
|
40 static {
|
samer@0
|
41 reg.put( Boolean.class, new Thingy() {
|
samer@0
|
42 public Object decode(Viewable vbl) { return new Boolean(((VBoolean)vbl).value); }
|
samer@0
|
43 public Viewable viewable(String name, Object obj) {
|
samer@0
|
44 return new VBoolean(name,((Boolean)obj).booleanValue(),Variable.NOINIT);
|
samer@0
|
45 }
|
samer@0
|
46 } );
|
samer@0
|
47
|
samer@0
|
48 reg.put( Integer.class, new Thingy() {
|
samer@0
|
49 public Object decode(Viewable vbl) { return new Integer(((VInteger)vbl).value); }
|
samer@0
|
50 public Viewable viewable(String name, Object obj) {
|
samer@0
|
51 return new VInteger(name,((Number)obj).intValue(),Variable.NOINIT);
|
samer@0
|
52 }
|
samer@0
|
53 } );
|
samer@0
|
54
|
samer@0
|
55 reg.put( Double.class, new Thingy() {
|
samer@0
|
56 public Object decode(Viewable vbl) { return new Double(((VDouble)vbl).value); }
|
samer@0
|
57 public Viewable viewable(String name, Object obj) {
|
samer@0
|
58 return new VDouble(name,((Number)obj).doubleValue(),Variable.NOINIT);
|
samer@0
|
59 }
|
samer@0
|
60 } );
|
samer@0
|
61
|
samer@0
|
62 reg.put( String.class, new Thingy() {
|
samer@0
|
63 public Object decode(Viewable vbl) { return ((VString)vbl).value; }
|
samer@0
|
64 public Viewable viewable(String name, Object obj) {
|
samer@0
|
65 return new VString(name,obj==null ? "" : (String)obj,Variable.NOINIT);
|
samer@0
|
66 }
|
samer@0
|
67 } );
|
samer@0
|
68
|
samer@0
|
69 reg.put( Color.class, new Thingy() {
|
samer@0
|
70 public Object decode(Viewable vbl) { return ((VColor)vbl).getColor(); }
|
samer@0
|
71 public Viewable viewable(String name, Object obj) {
|
samer@0
|
72 return obj!=null ?
|
samer@0
|
73 new VColor(name,(Color)obj,Variable.NOINIT)
|
samer@0
|
74 : new VColor(name,Variable.NOINIT);
|
samer@0
|
75 }
|
samer@0
|
76 } );
|
samer@0
|
77 }
|
samer@0
|
78
|
samer@0
|
79 public void add(Object o) { Shell.print(X.codec(o).string(o)); }
|
samer@0
|
80 public Binding add(String n, Object o) {
|
samer@0
|
81 Shell.print(n+"="+X.codec(o).string(o));
|
samer@0
|
82 return super.add(n,o);
|
samer@0
|
83 }
|
samer@0
|
84
|
samer@0
|
85 public Datum datum() { return new Gluon(".",Null); } // ??
|
samer@0
|
86 public Datum datum(String name) {
|
samer@0
|
87 return new Gluon(rel(name),parent.datum(abs(name)));
|
samer@0
|
88 }
|
samer@0
|
89
|
samer@0
|
90 private class Gluon implements Datum
|
samer@0
|
91 {
|
samer@0
|
92 String name; // name of this Binding
|
samer@0
|
93 Datum inherited; // parent's binding
|
samer@0
|
94
|
samer@0
|
95 public Gluon(String nm, Datum inh) { name=nm; inherited=inh; }
|
samer@0
|
96
|
samer@0
|
97 public String name() { return this.name; }
|
samer@0
|
98 public int score() { return 0; } // ie perfect match
|
samer@0
|
99
|
samer@0
|
100 public Object get(Codec c, Object def)
|
samer@0
|
101 {
|
samer@0
|
102 if (toggleBoolean) {
|
samer@0
|
103 if (c.targetClass()==Boolean.class) return new Boolean(state);
|
samer@0
|
104 }
|
samer@0
|
105
|
samer@0
|
106 if (def==null) def=inherited.get(c,def);
|
samer@0
|
107 else def=c.decode(def); // ??
|
samer@0
|
108
|
samer@0
|
109 Thingy thing=(Thingy)reg.get(c.targetClass());
|
samer@0
|
110
|
samer@0
|
111 if (thing==null) {
|
samer@0
|
112 Object strdef=(def==null?null:c.string(def));
|
samer@0
|
113 Object result=get(X.StringCodec,strdef);
|
samer@0
|
114 return (result==strdef ? def : c.decode(result));
|
samer@0
|
115 }
|
samer@0
|
116
|
samer@0
|
117 // what if def is still null??
|
samer@0
|
118
|
samer@0
|
119 Shell.push(parent);
|
samer@0
|
120 try {
|
samer@0
|
121 // create, decode, and dispose of dialog with user
|
samer@0
|
122 Viewable vbl=thing.viewable(name,def);
|
samer@0
|
123 String rc=showdlg(vbl.getViewer().getComponent());
|
samer@0
|
124 if (rc.equals("ok")) def=thing.decode(vbl);
|
samer@0
|
125 vbl.dispose();
|
samer@0
|
126 check(rc);
|
samer@0
|
127
|
samer@0
|
128 return def;
|
samer@0
|
129 } finally { Shell.pop(); }
|
samer@0
|
130 }
|
samer@0
|
131
|
samer@0
|
132 public void get(Autocoder obj)
|
samer@0
|
133 {
|
samer@0
|
134 if (toggleBoolean) {
|
samer@0
|
135 if (obj instanceof VBoolean) obj.decode(new Boolean(state));
|
samer@0
|
136 } else if (obj instanceof Viewable) {
|
samer@0
|
137 Viewable vbl=(Viewable)obj;
|
samer@0
|
138 Object old=obj.object();
|
samer@0
|
139
|
samer@0
|
140 Shell.push(parent);
|
samer@0
|
141 try {
|
samer@0
|
142 Viewer vwr=((Viewable)obj).getViewer();
|
samer@0
|
143 String rc=showdlg(vwr.getComponent());
|
samer@0
|
144 if (rc.equals("default")) { obj.decode(old); vbl.changed(); }
|
samer@0
|
145 vwr.detach();
|
samer@0
|
146 check(rc);
|
samer@0
|
147 }
|
samer@0
|
148 finally { Shell.pop(); }
|
samer@0
|
149 } else {
|
samer@0
|
150 obj.decode(get(X.StringCodec,obj.string()));
|
samer@0
|
151 }
|
samer@0
|
152 }
|
samer@0
|
153
|
samer@0
|
154 private void check(String rc) {
|
samer@0
|
155 if (rc.equals("cancel")) throw new Error("cancel");
|
samer@0
|
156 }
|
samer@0
|
157
|
samer@0
|
158 private String showdlg(java.awt.Component c) {
|
samer@0
|
159 Shell.Dialog dlg = Shell.getDialog(name.endsWith(".") ? "Parameter entry" : name);
|
samer@0
|
160
|
samer@0
|
161 dlg.container().add(c);
|
samer@0
|
162 dlg.addAction("default");
|
samer@0
|
163 dlg.addAction("cancel");
|
samer@0
|
164 dlg.addAction("ok");
|
samer@0
|
165 dlg.expose();
|
samer@0
|
166 dlg.dispose();
|
samer@0
|
167
|
samer@0
|
168 return dlg.result();
|
samer@0
|
169 }
|
samer@0
|
170 }
|
samer@0
|
171 }
|
samer@0
|
172
|