FreeWRL/FreeX3D  3.0.0
FreeWRLScene.java
1 package sai;
2 import org.web3d.x3d.sai.*;
3 import java.util.*;
4 
5 
6 public class FreeWRLScene implements X3DScene {
7  ArrayList rNodes;
8  ArrayList routes;
9  FWComponentInfo[] components;
10  FWProfileInfo profile;
11  FreeWRLBrowser browser;
12  int encoding;
13  String worldURL;
14  String specVersion;
15  boolean disposed;
16  boolean scripted;
17  boolean current;
18  HashMap metaData;
19 
20  public FreeWRLScene(FreeWRLNode[] n, FreeWRLBrowser b) {
21  rNodes = new ArrayList(1);
22  routes = new ArrayList(1);
23  metaData = new HashMap();
24  disposed = false;
25 
26  browser = b;
27  components = null;
28  profile = FWProfInfo.getProfile(new String("Full"));
29  if (n != null) {
30  scripted = true;
31  current = false;
32  for (int i = 0; i < n.length; i++) {
33  rNodes.add(n[i]);
34  }
35  } else {
36  FreeWRLNode root;
37  X3DNode[] nodes;
38  int i;
39  int numRoutes;
40  FWRoute route;
41  FreeWRLNode tonode, fromnode;
42  String tofield, fromfield;
43  StringTokenizer tokens;
44  String retval;
45  int numnodes;
46  scripted = false;
47  current = true;
48  root = (FreeWRLNode) browser.getNode("_Sarah_this_is_the_FreeWRL_System_Root_Node");
49  //root = (FreeWRLNode) browser.getNode("ROOTNODE");
50  MFNode children = (MFNode) root.getField("children");
51  numnodes = children.size();
52  nodes = new X3DNode[numnodes];
53  children.getValue(nodes);
54  for (i = 0; i < numnodes; i++) {
55  rNodes.add(nodes[i]);
56  }
57  retval = browser.sendGlobalCommand("j");
58 
59  tokens = new StringTokenizer(retval);
60 
61  numRoutes = Integer.parseInt(tokens.nextToken());
62 
63  for (i = 0; i < numRoutes; i++) {
64  fromnode = new FreeWRLNode(browser);
65  fromnode.setPerlPtr(tokens.nextToken());
66  fromnode.setPointer(tokens.nextToken());
67  fromfield = tokens.nextToken();
68  tonode = new FreeWRLNode(browser);
69  tonode.setPerlPtr(tokens.nextToken());
70  tonode.setPointer(tokens.nextToken());
71  tofield = tokens.nextToken();
72  route = new FWRoute(fromnode, fromfield, tonode, tofield);
73  routes.add(route);
74  }
75  }
76  }
77 
78  public FreeWRLScene(FreeWRLBrowser b) {
79  disposed = false;
80  metaData = new HashMap();
81  rNodes = new ArrayList(1);
82  routes = new ArrayList(1);
83  browser = b;
84  components = null;
85  scripted = true;
86  current = false;
87  profile = FWProfInfo.getProfile(new String("Full"));
88  }
89 
91  disposed = false;
92  metaData = new HashMap();
93  scripted = true;
94  current = false;
95  browser = b;
96  components = c;
97  profile = p;
98  int i, j;
99  boolean flag;
100  ArrayList tempcomps;
101  rNodes = new ArrayList(1);
102  routes = new ArrayList(1);
103 
104  if ((profile != null) && (components != null)) {
105  ComponentInfo[] profcomps = profile.getComponents();
106  tempcomps = new ArrayList(1);
107 
108  for (i = 0; i < profcomps.length; i++) {
109  flag = false;
110  for (j = 0; j < components.length; j++) {
111  if ((profcomps[i].getName()).equals(components[j].getName())) {
112  if ((profcomps[i].getLevel()) > (components[j].getLevel())) {
113  tempcomps.add(profcomps[i]);
114  flag = true;
115  } else {
116  tempcomps.add(components[j]);
117  flag = true;
118  }
119  }
120  }
121  if (!flag) {
122  tempcomps.add(profcomps[i]);
123  }
124  }
125 
126  for (j = 0; j < components.length; j++) {
127  flag = false;
128  for (i = 0; i < profcomps.length; i++) {
129  if ((profcomps[i].getName()).equals(components[j].getName())) {
130  flag = true;
131  }
132  }
133  if (!flag) {
134  tempcomps.add(components[j]);
135  }
136  }
137 
138  Object[] returnval = tempcomps.toArray();
139 
140  components = new FWComponentInfo[returnval.length];
141 
142  for (i = 0; i < returnval.length; i++) {
143  components[i] = (FWComponentInfo) returnval[i];
144  }
145  }
146 
147  }
148 
149  public void setCurrent(boolean val) {
150  current = val;
151  }
152 
153  public String getMetaData(String key) throws InvalidExecutionContextException {
154  return (String) metaData.get(key);
155  }
156 
157  public void setMetaData(String key, String value) throws InvalidExecutionContextException {
158  metaData.put(key, value);
159  }
160 
161  public X3DNode getExportedNode(String nodeName) throws InvalidExecutionContextException, NodeUnavailableException, InvalidNameException {
162  checkValid();
163  return browser.getNode(nodeName);
164  }
165 
166  public void updateExportedNode(String nodeName, String newName) throws InvalidExecutionContextException, InvalidNameException {
167  String retval;
168 
169  X3DNode nodeRef;
170 
171  checkValid();
172 
173  nodeRef = browser.getNode(nodeName);
174 
175  retval = browser.sendGlobalCommand("c " + newName + " " + nodeRef);
176 
177  if (retval.equals("1")) {
178  throw new InvalidNameException("Unable to update node name: " + nodeName);
179  }
180  }
181 
182  public void removeExportedNode(String nodeName) throws InvalidExecutionContextException, InvalidNameException {
183  String retval;
184 
185  checkValid();
186 
187  retval = browser.sendGlobalCommand("d " + nodeName);
188 
189  if (retval.equals("1")) {
190  throw new InvalidNameException("Unable to remove node name: " + nodeName);
191  }
192  }
193 
195  FreeWRLNode root;
196  X3DNode[] nodes;
197 
198  checkValid();
199 
200  if (rootNode == null) {
201  return;
202  }
203 
204  nodes = new X3DNode[1];
205  nodes[0] = rootNode;
206  rNodes.add(rootNode);
207  if (current) {
208  root = (FreeWRLNode) browser.getNode("_Sarah_this_is_the_FreeWRL_System_Root_Node");
209  MFNode addChildren = (MFNode) root.getField("addChildren");
210  addChildren.setValue(1, nodes);
211  }
212  }
213 
214  public void removeRootNode(X3DNode rootNode) throws InvalidExecutionContextException {
215  FreeWRLNode root;
216  int index;
217  X3DNode[] nodes;
218 
219  checkValid();
220 
221  if (rootNode == null) {
222  return;
223  }
224 
225  nodes = new X3DNode[1];
226  nodes[0] = rootNode;
227  index = rNodes.indexOf(rootNode);
228  if (index == -1) {
229  System.out.println("Node not present");
230  return;
231  }
232  rNodes.remove(index);
233  if (current) {
234  root = (FreeWRLNode) browser.getNode("_Sarah_this_is_the_FreeWRL_System_Root_Node");
235  MFNode removeChildren = (MFNode) root.getField("removeChildren");
236  removeChildren.setValue(1, nodes);
237  }
238  }
239 
240  public String getSpecificationVersion() throws InvalidExecutionContextException {
241  String spec;
242  checkValid();
243  spec = browser.sendGlobalCommand("Y");
244  int version = Integer.parseInt(spec);
245  if (version == 3) {
246  spec = new String("2.0");
247  } else if (version == 4) {
248  spec = new String("1.0");
249  } else {
250  spec = new String("0.0");
251  }
252  return spec;
253  }
254 
255  public int getEncoding() throws InvalidExecutionContextException {
256  String spec;
257  checkValid();
258  spec = browser.sendGlobalCommand("Y");
259  return Integer.parseInt(spec);
260  }
261 
262  public ProfileInfo getProfile() throws InvalidExecutionContextException {
263  checkValid();
264  return profile;
265  }
266 
267  public ComponentInfo[] getComponents() throws InvalidExecutionContextException {
268  checkValid();
269  return components;
270  }
271 
272  public String getWorldURL() throws InvalidExecutionContextException {
273  String myurl = null;
274  checkValid();
275  if (!scripted)
276  myurl = browser.sendGlobalCommand("O");
277  return myurl;
278 
279  }
280 
281  public X3DNode getNamedNode(String nodeName) throws InvalidExecutionContextException, NodeUnavailableException, InvalidNameException {
282  checkValid();
283  return browser.getNode(nodeName);
284  }
285 
286  public X3DNode getImportedNode(String nodeName) throws InvalidExecutionContextException, NodeUnavailableException, InvalidNameException {
287  checkValid();
288  return browser.getNode(nodeName);
289  }
290 
291  public X3DNode createNode(String nodeName) throws InvalidExecutionContextException, InvalidNameException {
292  String retval;
293  StringTokenizer tokens;
294  FreeWRLNode newnode;
295 
296  checkValid();
297 
298  if (nodeName == null) {
299  throw new InvalidNameException("No node of type " + nodeName + " exists");
300  }
301 
302  retval = browser.sendGlobalCommand("a " + nodeName);
303 
304  if (retval.equals("")) {
305  throw new InvalidNameException("No node of type " + nodeName + " exists");
306  }
307  tokens = new StringTokenizer(retval);
308  newnode = new FreeWRLNode(browser);
309  newnode.setPerlPtr(tokens.nextToken());
310  newnode.setPointer(tokens.nextToken());
311  newnode.setType(FreeWRLFieldTypes.getIntType("h"));
312 
313  return newnode;
314  }
315 
316  public X3DProtoInstance createProto(String protoName) throws InvalidExecutionContextException, InvalidNameException {
317  String retval;
318  StringTokenizer tokens;
319  FWProtoInstance proto;
320 
321  checkValid();
322 
323  if (protoName == null) {
324  throw new InvalidNameException("No PROTO of type " + protoName + " exists");
325  }
326 
327  retval = browser.sendGlobalCommand("b " + protoName);
328  if (retval.equals("")) {
329  throw new InvalidNameException("No PROTO of type " + protoName + " exists");
330  }
331  tokens = new StringTokenizer(retval);
332  proto = new FWProtoInstance(browser);
333  proto.setPerlPtr(tokens.nextToken());
334  proto.setPointer(tokens.nextToken());
335  proto.setNodeName(protoName);
336  proto.setType(FreeWRLNodeTypes.X3DProtoInstance);
337 
338  return proto;
339 
340  }
341 
342  public void updateNamedNode(String nodeName, X3DNode nodeRef) throws InvalidExecutionContextException, InvalidNameException, ImportedNodeException {
343  String retval;
344 
345  checkValid();
346 
347  retval = browser.sendGlobalCommand("c " + nodeName + " " + nodeRef);
348 
349  if (retval.equals("1")) {
350  throw new InvalidNameException("Unable to update node name: " + nodeName);
351  }
352  }
353 
354  public void updateImportedNode(String nodeName, String importedName, X3DNode nodeRef) throws InvalidExecutionContextException, InvalidNameException, ImportedNodeException {
355  checkValid();
356  }
357 
358  public void removeNamedNode(String nodeName) throws InvalidExecutionContextException, InvalidNameException {
359  String retval;
360 
361  checkValid();
362 
363  retval = browser.sendGlobalCommand("d " + nodeName);
364 
365  if (retval.equals("1")) {
366  throw new InvalidNameException("Unable to remove node name: " + nodeName);
367  }
368  }
369 
370  public void removeImportedNode(String nodeName) throws InvalidExecutionContextException, InvalidNameException {
371  String retval;
372 
373  checkValid();
374 
375  retval = browser.sendGlobalCommand("d " + nodeName);
376 
377  if (retval.equals("1")) {
378  throw new InvalidNameException("Unable to remove imported node name: " + nodeName);
379  }
380  }
381 
382  public X3DProtoDeclaration getProtoDeclaration(String protoName) throws InvalidExecutionContextException, InvalidNameException {
383  String retval;
384  StringTokenizer tokens;
385  FWProtoDeclaration dec;
386  FreeWRLFieldDefinition[] defs;
387  int numFields;
388  String type;
389  String fieldType;
390  String fieldAccess;
391  String fieldName;
392  String temp;
393  String defaultValue;
394  int ft, fa;
395 
396  checkValid();
397  retval = browser.sendGlobalCommand("e " + protoName);
398 
399  if (retval.equals("1")) {
400  throw new InvalidNameException("The proto declaration " + protoName + " does not exist");
401  }
402 
403 
404  dec = new FWProtoDeclaration(browser);
405  tokens = new StringTokenizer(retval);
406 
407  dec.setProtoName(tokens.nextToken());
408  type = tokens.nextToken();
409  numFields = Integer.valueOf(tokens.nextToken()).intValue();
410 
411  defs = new FreeWRLFieldDefinition[numFields];
412 
413  for (int i = 0; i < numFields; i++) {
414  fieldAccess = tokens.nextToken();
415  fieldType = tokens.nextToken();
416  fieldName = tokens.nextToken();
417  ft = FreeWRLFieldTypes.getIntFromStringDesc(fieldType);
418  fa = FreeWRLFieldTypes.getAccessFromType(fieldAccess);
419  defs[i] = new FreeWRLFieldDefinition(fieldName, fa, ft);
420  defaultValue = tokens.nextToken();
421 
422  temp = "";
423 
424  char test = '\"';
425  String tests = "\"";
426  if (defaultValue.equals("[")) {
427  while(!temp.equals("]")) {
428  temp = tokens.nextToken();
429  defaultValue = defaultValue + " " + temp;
430  }
431  }
432 
433  if (defaultValue.charAt(0) == test) {
434  while(!temp.endsWith(tests)) {
435  temp = tokens.nextToken();
436  defaultValue = defaultValue + " " + temp;
437  }
438  }
439 
440  defs[i].setDefaultValue(defaultValue);
441  }
442 
443  dec.setFields(defs);
444 
445  return dec;
446  }
447 
448  public void updateProtoDeclaration(String protoName, X3DProtoDeclaration newDeclaration) throws InvalidExecutionContextException, InvalidNameException {
449  String retval;
450  checkValid();
451 
452  retval = browser.sendGlobalCommand("f " + protoName + " " + newDeclaration);
453  System.out.println("sent: f " + protoName + " " + newDeclaration);
454 
455  if (retval.equals("1")) {
456  throw new InvalidNameException("Proto name " + protoName + " is already in use");
457  }
458 
459  }
460 
461  public void removeProtoDeclaration(String protoName) throws InvalidExecutionContextException, InvalidNameException {
462  String retval;
463 
464  checkValid();
465  if (protoName == null) {
466  throw new InvalidNameException("The proto declaration " + protoName + " does not exist");
467  }
468  retval = browser.sendGlobalCommand("g " + protoName);
469 
470  if (retval.equals("1")) {
471  throw new InvalidNameException("Unable to remove proto name: " + protoName);
472  }
473  }
474 
475  public X3DExternProtoDeclaration getExternProtoDeclaration(String protoName) throws InvalidExecutionContextException, InvalidNameException, URLUnavailableException {
476  return (X3DExternProtoDeclaration) getProtoDeclaration(protoName);
477  }
478 
479  public void updateExternProtoDeclaration(String protoName, X3DExternProtoDeclaration newDeclaration) throws InvalidExecutionContextException {
480  updateProtoDeclaration(protoName, newDeclaration);
481  }
482 
483  public void removeExternProtoDeclaration(String protoName) throws InvalidExecutionContextException {
484  removeProtoDeclaration(protoName);
485  }
486 
487  public X3DNode[] getRootNodes() throws InvalidExecutionContextException {
488  checkValid();
489  Object[] returnval = rNodes.toArray();
490  FreeWRLNode[] temp = new FreeWRLNode[rNodes.size()];
491  for (int i = 0; i < rNodes.size(); i++)
492  temp[i] = (FreeWRLNode) returnval[i];
493  return temp;
494  }
495 
496  public X3DRoute[] getRoutes() throws InvalidExecutionContextException {
497  checkValid();
498  Object[] returnval = routes.toArray();
499  FWRoute[] temp = new FWRoute[routes.size()];
500  for (int i = 0; i < routes.size(); i++)
501  temp[i] = (FWRoute) returnval[i];
502  return temp;
503  }
504 
505  public X3DRoute addRoute(X3DNode startNode, String startName, X3DNode endNode, String endEvent) throws InvalidExecutionContextException, InvalidNodeException, InvalidFieldException {
506  checkValid();
507  FWRoute route;
508  String retval;
509  retval = browser.addRoute((FreeWRLNode) startNode, startName, (FreeWRLNode) endNode, endEvent);
510  System.out.println("retval: " + retval);
511  if (retval.equals("1")) {
512  throw new InvalidFieldException("Unable to create ROUTE");
513  }
514  route = new FWRoute((FreeWRLNode) startNode, startName, (FreeWRLNode) endNode, endEvent);
515  routes.add(route);
516  return route;
517  }
518 
520  checkValid();
521  int index;
522  if (route == null) {
523  throw new InvalidNodeException("No such route exists");
524  }
525 
526  index = routes.indexOf(route);
527  if (index == -1) {
528  System.out.println("Route not present");
529  return;
530  }
531 
532  routes.remove(index);
533 
534  browser.deleteRoute((FreeWRLNode)(route.getSourceNode()), route.getSourceField(), (FreeWRLNode)(route.getDestinationNode()), route.getDestinationField());
535  }
536 
537  public void checkValid() {
538  if (disposed) {
539  throw new InvalidExecutionContextException("This context has been disposed");
540  }
541  }
542 
543  public void dispose() {
544  disposed = true;
545  }
546 }
Definition: Viewer.h:174