Mercurial > hg > jslab
view src/samer/core_/util/UserEnvironment.java @ 8:5e3cbbf173aa tip
Reorganise some more
author | samer |
---|---|
date | Fri, 05 Apr 2019 22:41:58 +0100 |
parents | bf79fb79ee13 |
children |
line wrap: on
line source
/* * UserEnvironment.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.core.util; import samer.core.types.*; import samer.core.*; import java.util.Hashtable; import java.awt.Color; /** * Environment which provides access to the user * using a GUI */ public class UserEnvironment extends Environment { boolean toggleBoolean=false; boolean state; public UserEnvironment(Environment parent) { super(parent,parent.node()); } public UserEnvironment(Environment parent, boolean bool) { super(parent,parent.node()); toggleBoolean=true; state=bool; } private static Hashtable reg=new Hashtable(); public interface Thingy { Viewable viewable(String name, Object obj); Object decode(Viewable vbl); } static { reg.put( Boolean.class, new Thingy() { public Object decode(Viewable vbl) { return new Boolean(((VBoolean)vbl).value); } public Viewable viewable(String name, Object obj) { return new VBoolean(name,((Boolean)obj).booleanValue(),Variable.NOINIT); } } ); reg.put( Integer.class, new Thingy() { public Object decode(Viewable vbl) { return new Integer(((VInteger)vbl).value); } public Viewable viewable(String name, Object obj) { return new VInteger(name,((Number)obj).intValue(),Variable.NOINIT); } } ); reg.put( Double.class, new Thingy() { public Object decode(Viewable vbl) { return new Double(((VDouble)vbl).value); } public Viewable viewable(String name, Object obj) { return new VDouble(name,((Number)obj).doubleValue(),Variable.NOINIT); } } ); reg.put( String.class, new Thingy() { public Object decode(Viewable vbl) { return ((VString)vbl).value; } public Viewable viewable(String name, Object obj) { return new VString(name,obj==null ? "" : (String)obj,Variable.NOINIT); } } ); reg.put( Color.class, new Thingy() { public Object decode(Viewable vbl) { return ((VColor)vbl).getColor(); } public Viewable viewable(String name, Object obj) { return obj!=null ? new VColor(name,(Color)obj,Variable.NOINIT) : new VColor(name,Variable.NOINIT); } } ); } public void add(Object o) { Shell.print(X.codec(o).string(o)); } public Binding add(String n, Object o) { Shell.print(n+"="+X.codec(o).string(o)); return super.add(n,o); } public Datum datum() { return new Gluon(".",Null); } // ?? public Datum datum(String name) { return new Gluon(rel(name),parent.datum(abs(name))); } private class Gluon implements Datum { String name; // name of this Binding Datum inherited; // parent's binding public Gluon(String nm, Datum inh) { name=nm; inherited=inh; } public String name() { return this.name; } public int score() { return 0; } // ie perfect match public Object get(Codec c, Object def) { if (toggleBoolean) { if (c.targetClass()==Boolean.class) return new Boolean(state); } if (def==null) def=inherited.get(c,def); else def=c.decode(def); // ?? Thingy thing=(Thingy)reg.get(c.targetClass()); if (thing==null) { Object strdef=(def==null?null:c.string(def)); Object result=get(X.StringCodec,strdef); return (result==strdef ? def : c.decode(result)); } // what if def is still null?? Shell.push(parent); try { // create, decode, and dispose of dialog with user Viewable vbl=thing.viewable(name,def); String rc=showdlg(vbl.getViewer().getComponent()); if (rc.equals("ok")) def=thing.decode(vbl); vbl.dispose(); check(rc); return def; } finally { Shell.pop(); } } public void get(Autocoder obj) { if (toggleBoolean) { if (obj instanceof VBoolean) obj.decode(new Boolean(state)); } else if (obj instanceof Viewable) { Viewable vbl=(Viewable)obj; Object old=obj.object(); Shell.push(parent); try { Viewer vwr=((Viewable)obj).getViewer(); String rc=showdlg(vwr.getComponent()); if (rc.equals("default")) { obj.decode(old); vbl.changed(); } vwr.detach(); check(rc); } finally { Shell.pop(); } } else { obj.decode(get(X.StringCodec,obj.string())); } } private void check(String rc) { if (rc.equals("cancel")) throw new Error("cancel"); } private String showdlg(java.awt.Component c) { Shell.Dialog dlg = Shell.getDialog(name.endsWith(".") ? "Parameter entry" : name); dlg.container().add(c); dlg.addAction("default"); dlg.addAction("cancel"); dlg.addAction("ok"); dlg.expose(); dlg.dispose(); return dlg.result(); } } }