FreeWRL/FreeX3D  3.0.0
Script.java
1 package vrml.node;
2 //JAS import java.util.Hashtable;
3 import vrml.*;
4 //
5 // This is the general Script class, to be subclassed by all scripts.
6 // Note that the provided methods allow the script author to explicitly
7 // throw tailored exceptions in case something goes wrong in the
8 // script.
9 //
10 public abstract class Script extends BaseNode
11 {
12  public Script() {
13  }
14 
15  // This method is called before any event is generated
16  public void initialize() { }
17 
18  // Get a Field by name.
19  // Throws an InvalidFieldException if fieldName isn't a valid
20  // field name for a node of this type.
21  protected final Field getField(String fieldName) {
22  String ftype = FWJavaScript.getFieldType(this, fieldName, "field");
23  if (ftype.equals("ILLEGAL"))
24  throw new InvalidFieldException(_get_nodeid()+"."+fieldName);
25 
26  /* split field type from field offset */
27  String sp[] = ftype.split (" ");
28  Field fval = FWCreateField.createField(sp[0]);
29  fval.setOffset(ftype);
30 
31 
32  /* read field only once, nobody except us may change it */
33  FWJavaScript.readField(this, fieldName, fval);
34  return fval;
35  }
36 
37  // Get an EventOut by name.
38  // Throws an InvalidEventOutException if eventOutName isn't a valid
39  // eventOut name for a node of this type.
40  // spec: protected
41  public final Field getEventOut(String eventOutName) {
42  String ftype = FWJavaScript
43  .getFieldType(this, eventOutName, "eventOut");
44  if (ftype.equals("ILLEGAL"))
45  throw new InvalidEventOutException(_get_nodeid()+"."+eventOutName);
46 
47  /* split field type from field offset */
48  String sp[] = ftype.split (" ");
49  Field fval = FWCreateField.createField(sp[0]);
50  fval.setOffset(ftype);
51 
52  fval.bind_to(new FWJavaScriptBinding(this, eventOutName, false));
53  return fval;
54  }
55 
56  // Get an EventIn by name.
57  // Throws an InvalidEventInException if eventInName isn't a valid
58  // eventIn name for a node of this type.
59  protected final Field getEventIn(String eventInName) {
60  String ftype = FWJavaScript
61  .getFieldType(this, eventInName, "eventIn");
62  if (ftype.equals("ILLEGAL"))
63  throw new InvalidEventOutException(_get_nodeid()+"."+eventInName);
64 
65  /* split field type from field offset */
66  String sp[] = ftype.split (" ");
67 
68  Field fval = FWCreateField.createField(sp[0]);
69  fval.setOffset(ftype);
70 
71  fval.bind_to(new FWJavaScriptBinding(this, eventInName, false));
72  return fval;
73  }
74 
75  // processEvents() is called automatically when the script receives
76  // some set of events. It shall not be called directly except by its subclass.
77  // count indicates the number of events delivered.
78  // Trevor John Thompson has submitted the following code.
79 
80  public void processEvents(final int count, final Event events[]) {
81  for (int i = 0; i < count && i < events.length; ++i) {
82  processEvent(events[i]);
83  }
84  }
85 
86  // processEvent() is called automatically when the script receives
87  // an event.
88  public void processEvent(Event event) { }
89 
90  // eventsProcessed() is called after every invocation of processEvents().
91  public void eventsProcessed() { }
92 
93  // shutdown() is called when this Script node is deleted.
94  public void shutdown() { }
95 }