samer@0: /* samer@0: * Variable.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.core; samer@0: import samer.core.Environment.Autocoder; samer@0: import java.io.*; samer@0: samer@0: /** samer@0: A Variable is a Viewable with an associated mutable data samer@0: model (that implements the Autocoder interface). The data samer@0: model handles conversion of data to and from a small number of samer@0: recognised interchange formats (String, character stream). samer@0: This means that the data can be stored in a Binding. samer@0: samer@0: The init() method handles registration of the Viewable samer@0: and initialisation of the data value from a Shell property. samer@0: samer@0: */ samer@0: samer@0: public class Variable extends Viewable // implement Environment.Autocoder samer@0: { samer@0: /** Means do not automatically load value from current samer@0: environment on construction. */ samer@0: public final static int NOINIT = 1; samer@0: samer@0: /** Means register this Viewable automatically on construction. */ samer@0: public final static int REGISTER = 2; samer@0: samer@0: /** Not currently used. */ samer@0: public final static int NODEFAULT= 8; samer@0: samer@0: public Variable(Node node) { super(node); } samer@0: public Variable(String nm) { this(new Node(nm)); } samer@0: public Variable(String nm, Autocoder m, int flags) { this(nm); init(m,flags); } samer@0: samer@0: /** Store this variable's data in the given environment */ samer@0: public void save(Environment env) { env.store(node.fullName(), model); } samer@0: samer@0: /** Restore this variable's data from the given environment */ samer@0: public void load(Environment env) throws Exception { samer@0: env.datum(node.fullName()).get(model); samer@0: changed(); samer@0: } samer@0: samer@0: public String toString() { return super.toString()+"="+model.string(); } samer@0: samer@0: public void init(Autocoder m, int flags) { model=m; init(flags); } samer@0: public void init(int flags) samer@0: { samer@0: if ((flags&NOINIT)==0) try { load(Shell.env()); } catch (Exception ex) {} // ?? samer@0: if ((flags®ISTER)!=0) Shell.registerViewable(this); samer@0: // what if no value found at this point? samer@0: } samer@0: samer@0: public Autocoder getModel() { return model; } samer@0: private Autocoder model; samer@0: } samer@0: