comparison src/samer/core_/util/UserEnvironment.java @ 0:bf79fb79ee13

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