view src/samer/core_/Variable.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
/*
 *	Variable.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;
import	samer.core.Environment.Autocoder;
import	java.io.*;

/**
	A Variable is a Viewable with an associated mutable data 
	model (that implements the Autocoder interface). The data
	model handles conversion of data to and from a small number of
	recognised interchange formats (String, character stream).
	This means that the data can be stored in a Binding.

	The init() method handles registration of the Viewable
	and initialisation of the data value from a Shell property.

 */

public class Variable extends Viewable // implement Environment.Autocoder
{
	/** Means do not automatically load value from current
		environment on construction. */
	public final static int	NOINIT	= 1;

	/** Means register this Viewable automatically on construction. */
	public final static int	REGISTER	= 2;

	  /** Not currently used. */
	public final static int	NODEFAULT= 8;

	public Variable(Node node) { super(node); }
	public Variable(String nm) { this(new Node(nm)); }
	public Variable(String nm, Autocoder m, int flags) { this(nm); init(m,flags); }

	/** Store this variable's data in the given environment */
	public void save(Environment env) { env.store(node.fullName(), model); }

	/** Restore this variable's data from the given environment */
	public void load(Environment env) throws Exception {
		env.datum(node.fullName()).get(model);
		changed();
	}

	public String toString() { return super.toString()+"="+model.string(); }
	
	public void init(Autocoder m, int flags) { model=m; init(flags); }
	public void init(int flags)
	{
		if ((flags&NOINIT)==0)   try { load(Shell.env()); } catch (Exception ex) {} // ??
		if ((flags&REGISTER)!=0) Shell.registerViewable(this);
		// what if no value found at this point?
	}

	public  Autocoder	getModel()	{ return model; }
	private Autocoder	model;
}