samer@0
|
1 /*
|
samer@0
|
2 * Variable.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;
|
samer@0
|
13 import samer.core.Environment.Autocoder;
|
samer@0
|
14 import java.io.*;
|
samer@0
|
15
|
samer@0
|
16 /**
|
samer@0
|
17 A Variable is a Viewable with an associated mutable data
|
samer@0
|
18 model (that implements the Autocoder interface). The data
|
samer@0
|
19 model handles conversion of data to and from a small number of
|
samer@0
|
20 recognised interchange formats (String, character stream).
|
samer@0
|
21 This means that the data can be stored in a Binding.
|
samer@0
|
22
|
samer@0
|
23 The init() method handles registration of the Viewable
|
samer@0
|
24 and initialisation of the data value from a Shell property.
|
samer@0
|
25
|
samer@0
|
26 */
|
samer@0
|
27
|
samer@0
|
28 public class Variable extends Viewable // implement Environment.Autocoder
|
samer@0
|
29 {
|
samer@0
|
30 /** Means do not automatically load value from current
|
samer@0
|
31 environment on construction. */
|
samer@0
|
32 public final static int NOINIT = 1;
|
samer@0
|
33
|
samer@0
|
34 /** Means register this Viewable automatically on construction. */
|
samer@0
|
35 public final static int REGISTER = 2;
|
samer@0
|
36
|
samer@0
|
37 /** Not currently used. */
|
samer@0
|
38 public final static int NODEFAULT= 8;
|
samer@0
|
39
|
samer@0
|
40 public Variable(Node node) { super(node); }
|
samer@0
|
41 public Variable(String nm) { this(new Node(nm)); }
|
samer@0
|
42 public Variable(String nm, Autocoder m, int flags) { this(nm); init(m,flags); }
|
samer@0
|
43
|
samer@0
|
44 /** Store this variable's data in the given environment */
|
samer@0
|
45 public void save(Environment env) { env.store(node.fullName(), model); }
|
samer@0
|
46
|
samer@0
|
47 /** Restore this variable's data from the given environment */
|
samer@0
|
48 public void load(Environment env) throws Exception {
|
samer@0
|
49 env.datum(node.fullName()).get(model);
|
samer@0
|
50 changed();
|
samer@0
|
51 }
|
samer@0
|
52
|
samer@0
|
53 public String toString() { return super.toString()+"="+model.string(); }
|
samer@0
|
54
|
samer@0
|
55 public void init(Autocoder m, int flags) { model=m; init(flags); }
|
samer@0
|
56 public void init(int flags)
|
samer@0
|
57 {
|
samer@0
|
58 if ((flags&NOINIT)==0) try { load(Shell.env()); } catch (Exception ex) {} // ??
|
samer@0
|
59 if ((flags®ISTER)!=0) Shell.registerViewable(this);
|
samer@0
|
60 // what if no value found at this point?
|
samer@0
|
61 }
|
samer@0
|
62
|
samer@0
|
63 public Autocoder getModel() { return model; }
|
samer@0
|
64 private Autocoder model;
|
samer@0
|
65 }
|
samer@0
|
66
|