JAL-1807 update
[jalviewjs.git] / site / js / j2sSwingJS.js
1 // j2sjmol.js 
2
3 // latest author: Bob Hanson, St. Olaf College, hansonr@stolaf.edu
4  
5 // Requires JSmolCore.js and (for now; probably) JSmol.js
6 // This version of j2slib requires jQuery and works in both Chrome and MSIE locally,
7 // though Chrome cannot read local data files, and MSIE cannot read local binary data files.
8
9 // Java programming notes by Bob Hanson:
10 //   
11 //   There are a few motifs to avoid when optimizing Java code to work smoothly
12 //   with the J2S compiler:
13 //   
14 //   arrays: 
15 //   
16 // 1. an array with null elements cannot be typed and must be avoided.
17 // 2. instances of Java "instance of" involving arrays must be found and convered to calls to Clazz.isA...
18 // 3. new int[n][] must not be used. Use instead JU.AU.newInt2(n);
19 // 4. new int[] { 1, 2, 3 } has problems because it creates simply [ ] and not IntArray32
20 //   
21 //   numbers:
22 //   
23 // 1. Remember that EVERY number in JavaScript is a double -- doesn't matter if it is in IntArray32 or not. 
24 // 2. You cannot reliably use Java long, because doubles consume bits for the exponent which cannot be tested.
25 // 3. Bit 31 of an integer is unreliable, since (int) -1 is now  , not just 0zFFFFFFFF, and 
26 //    FFFFFFFF + 1 = 100000000, not 0. In JavaScript, 0xFFFFFFFF is 4294967295, not -1.
27 //    This means that writeInt(b) will fail if b is negative. What you need is instead
28 //    writeInt((int)(b & 0xFFFFFFFFl) so that JavaScript knocks off the high bits explicitly. 
29 //
30 //   general:
31 //
32 // 1. j2sRequireImport xxxx is needed if xxxx is a method used in a static function
33 // 2. URL.getContent() is not supported. Use other means based on URL.toString()
34 // 3. It is critical for performance to avoid any significant amount of function overloading.
35 //    In particular, methods such as xxx(int a, int b) and xxx(float a, int b) MUST be renamed,
36 //    because JavaScript only has Number, and there is absolutely no way to tell these apart.
37 //    It's probably bad Java programming, anyway.
38 // 4. Calls to super(...) can almost always be avoided. These trigger the SAEM
39 //    (searchAndExecuteMethod) call, and it is very destructive to performance.
40 //    Just find another way to do it.   
41
42  // NOTES by Bob Hanson: 
43  
44  // J2S class changes:
45
46  // BH 7/24/2015 6:48:50 AM adding optional ?j2sdebug flag on page URL
47  //                      -- switches to using j2s/core/corexxx.js, not j2s/core/corexxx.z.js 
48  //                      -- adds ";//# sourceURL="+file  in eval(js)
49  //                      -- enables DebugJS.$(msg) call to debugger;
50  //  see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger
51  //  see https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Debug_eval_sources
52  // BH 7/23/2015 6:45:55 PM added sourceURL in each js class eval(), allowing full 
53  //                         breakpoint debugging and code checking in Firefox and Chrome
54  // BH 7/19/2015 6:18:17 PM added os.name, line.separator, etc. to System.getProperty()
55  // BH 7/19/2015 5:39:10 PM added java.lang.System = System
56  // BH 7/19/2015 10:33:10 AM fix for SAEM equating "null" with number or boolean
57  // BH 7/18/2015 6:08:05 PM for Jmol I was able to remove the $private/$fx business, but now
58  //    I see that in general that cannot be done. Thinking about a strategy...
59  // BH 7/18/2015 4:43:38 PM better handling of TypeError and InternalError for e.getMessage() and e.getStackTrace()
60  // BH 7/17/2015 11:51:15 AM adds class.getResource(name) and class.getResourceAsStream(name) 
61  // BH 7/16/2015 7:56:49 PM general instantiation using any constructor (in Java here):
62  // BH  x = class.forName("my.class.name").newInstance()
63  // BH or
64  // BH  x = class.forName("my.class.name").getConstructor(String.class,String.class).newInstance(new Object[] {"test", "now"})
65  // BH 7/15/2015 11:34:58 PM adding System.lineSeparator()
66  // BH 7/15/2015 7:32:41 AM adding class.getCanonicalName == getName
67  // BH 5/31/2015 5:38:14 PM  NPEExceptionPredicate fix
68  // BH 4/25/2015 9:16:12 AM SAEM misrepresnting Number as Object in parameters and Integer as Number 
69  // BH 4/24/2015 7:32:54 AM Object.hashCode() and System.getIdentityHashCode() fail. changed to:     return this._$hashcode || (this._$hashcode = ++Clazz._hashCode)
70  // BH 4/23/2015 9:08:59 AM Clazz.instanceOf(a, b) needs to check for a == b.   
71  // BH 4/23/2015 9:08:59 AM xx.getContentType() is nonfunctional. Array.newInstance now defines a wrapper for .getClass().getComponentType() that works  
72  // BH 4/12/2015 11:48:03 AM added Clazz.getStackTrace(-n) -- reports actual parameter values for n levels
73  // BH 4/10/2015 8:23:05 AM adding Int32Array.prototype.clone and Float64.prototype.clone
74  // BH 4/5/2015 8:12:57 AM refactoring j2slib (this file) to make private functions really private using var
75  // BH 4/3/2015 6:14:34 AM adding anonymous local "ClazzLoader" (Clazz._Loader) --> "_Loader"
76  // BH 4/3/2015 6:14:34 AM adding Clazz._Loader._classPending, Clazz._Loader._classCount
77  // BH 4/3/2015 6:14:34 AM adding Clazz._Loader._checkLoad 
78  //  -- forces asynchronous class loading
79  //  -- builds Clazz._Loader._classPending and Clazz._classCount
80  //  -- allows reporting 
81  
82  // BH 3/24/2015 4:11:26 AM better file load failure message in _Loader.evaluate 
83  // BH 2/28/2015 7:30:25 AM corrects newIntArray32() and newArray() for pre-defined arrays 
84  //             int[] a =  new int[] {1,2,3,343};
85  //             int[][] b = new int[][] {new int[]{4,5},new int[]{5,6}}; 
86
87  // BH 9/29/2014 11:34:19 PM removing support for getClass().isArray() 
88  // BH 8/29/2014 9:15:57 AM total reworking of Java2Script in preparation for all-asynchronous loading
89  //                         (currently sync loading is only for 
90  //                                                                                                LOAD command and load() function without ASYNC
91  //                            getInterface() 
92  //                         see JSmol.js and Jmol._isAsync flag
93  // BH 5/11/2015 5:58:42 AM adding __signatures for debugging SAEM issues 
94  // BH 3/29/2015 8:12:44 PM System.getProperty(x, "") does not return ""
95  // BH 8/23/2014 10:04:19 AM cleaning up a few general methods; Clazz.removeArrayItem
96  // BH 6/1/2014 10:58:46 AM fix for Clazz.isAP() not working
97  // BH 5/26/2014 5:19:29 PM removing superConstructor call in creating Enum constants
98  // BH 4/1/2014 7:55:54 PM removing all $fz references and instances where sub/super classes have same private function names
99  // BH 4/1/2014 4:47:30 PM all $_X removed; this is taken care of by Google Closure Compiler
100  // BH 4/1/2014 6:40:08 AM removing ClassLoader -- equals Clazz._Loader
101  // BH 4/1/2014 6:40:08 AM removing ClassLoaderProgressMonitor -- equals _LoaderProgressMonitor
102  // BH 4/1/2014 6:17:21 AM removing Class  -- only used for "Class.forName" in Jmol, which ANT will now change to "Clazz._4Name"
103  // BH 3/7/2014 9:05:06 AM Array.prototype.toString should not be aliased. -- http://sourceforge.net/p/jmol/bugs/560/ with Google Visualization
104
105  // BH 1/30/2014 12:54:22 PM gave all field variables prefix underscore. This allows Google Closure Compiler to skip them.  
106  // BH 12/3/2013 3:39:57 PM window["j2s.lib"].base implemented
107  // BH 12/1/2013 5:34:21 AM removed _LoaderProgressMonitor.initialize and all Clazz.event business; handled by Jmol.clearVars()
108  // BH 11/30/2013 12:43:58 PM adding Clazz.arrayIs() -- avoids Number.constructor.toString() infinite recursion
109  // BH 11/29/2013 6:33:51 AM adding Clazz._profiler -- reports use of SAEM
110  // BH 11/10/2013 9:02:20 AM fixing fading in MSIE  
111  // BH 11/3/2013 7:21:39 AM additional wrapping functions for better compressibility
112  // BH 10/30/2013 8:10:58 AM added getClass().getResource() -- returning a relative string, not a URL
113  // BH 10/30/2013 6:43:00 AM removed second System def and added System.$props and default System.property "line.separator" 
114  // BH 6/15/2013 8:02:07 AM corrections to Class.isAS to return true if first element is null
115  // BH 6/14/2013 4:41:09 PM corrections to Clazz.isAI and related methods to include check for null object
116  // BH 3/17/2013 11:54:28 AM adds stackTrace for ERROR 
117
118  // BH 3/13/2013 6:58:26 PM adds Clazz.clone(me) for BS clone 
119  // BH 3/12/2013 6:30:53 AM fixes Clazz.exceptionOf for ERROR condition trapping
120  // BH 3/2/2013 9:09:53 AM delete globals c$ and $fz
121  // BH 3/2/2013 9:10:45 AM optimizing defineMethod using "look no further" "@" parameter designation (see "\\@" below -- removed 3/23/13)
122  // BH 2/27/2013 optimizing Clazz.getParamsType for common cases () and (Number)
123  // BH 2/27/2013 optimizing SAEM delegation for hashCode and equals -- disallows overloading of equals(Object)
124  
125  // BH 2/23/2013 found String.replaceAll does not work -- solution was to never call it.
126  // BH 2/9/2013 9:18:03 PM Int32Array/Float64Array fixed for MSIE9
127  // BH 1/25/2013 1:55:31 AM moved package.js from j2s/java to j2s/core 
128  // BH 1/17/2013 4:37:17 PM String.compareTo() added
129  // BH 1/17/2013 4:52:22 PM Int32Array and Float64Array may not have .prototype.sort method
130  // BH 1/16/2013 6:20:34 PM Float64Array not available in Safari 5.1
131  // BH 1/14/2013 11:28:58 PM  Going to all doubles in JavaScript (Float64Array, not Float32Array)
132  //   so that (new float[] {13.48f})[0] == 13.48f, effectively
133
134  // BH 1/14/2013 12:53:41 AM  Fix for Opera 10 not loading any files
135  // BH 1/13/2013 11:50:11 PM  Fix for MSIE not loading (nonbinary) files locally
136  
137  // BH 12/1/2012 9:52:26 AM Compiler note: Thread.start() cannot be executed within the constructor;
138  
139  // BH 11/24/2012 11:08:39 AM removed unneeded sections
140  // BH 11/24/2012 10:23:22 AM  all XHR uses sync loading (_Loader.setLoadingMode)
141  // BH 11/21/2012 7:30:06 PM    if (base)       map["@" + pkg] = base;  critical for multiple applets
142
143  // BH 10/8/2012 3:27:41 PM         if (clazzName.indexOf("Array") >= 0) return "Array"; in Clazz.getClassName for function
144  // BH removed Clazz.ie$plit = "\\2".split (/\\/).length == 1; unnecessary; using RegEx slows process significantly in all browsers
145  // BH 10/6/12 added Int32Array, Float32Array, newArrayBH, upgraded java.lang and java.io
146  // BH added Integer.bitCount in core.z.js
147  // BH changed alert to Clazz.alert in java.lang.Class.js *.ClassLoader.js, java.lang.thread.js
148  // BH removed toString from innerFunctionNames due to infinite recursion
149  // BH note: Logger.error(null, e) does not work -- get no constructor for (String) (TypeError)
150  // BH added j2s.lib.console
151  // BH allowed for alias="."
152  // BH removed alert def --> Clazz.alert
153  // BH added wrapper at line 2856 
154  // BH newArray fix at line 2205
155  // BH System.getProperty fix at line 6693
156  // BH added Enum .value() method at line 2183
157  // BH added System.getSecurityManager() at end
158  // BH added String.contains() at end
159  // BH added System.gc() at end
160  // BH added Clazz.exceptionOf = updated
161  // BH added String.getBytes() at end
162  
163
164 LoadClazz = function() {
165
166 // BH This is the ONLY global used in J2S now. I do not think it is necessary,
167 // but it is created by the compiler, and I have not found a work-around.
168 // it is used as a local variable in class definitions to point to the 
169 // current method. See Clazz.p0p and Clazz.pu$h
170
171 c$ = null;
172
173 if (!window["j2s.clazzloaded"])
174         window["j2s.clazzloaded"] = false;
175
176 if (window["j2s.clazzloaded"])return;
177
178 window["j2s.clazzloaded"] = true;
179
180 window["j2s.object.native"] = true;
181
182  // Clazz changes:
183
184  /* http://j2s.sf.net/ *//******************************************************************************
185  * Copyright (c) 2007 java2script.org and others.
186  * All rights reserved. This program and the accompanying materials
187  * are made available under the terms of the Eclipse Public License v1.0
188  * which accompanies this distribution, and is available at
189  * http://www.eclipse.org/legal/epl-v10.html
190  *
191  * Contributors:
192  *     Zhou Renjian - initial API and implementation
193  *****************************************************************************/
194 /*******
195  * @author zhou renjian
196  * @create Nov 5, 2005
197  *******/
198  
199
200 /**
201  * Class Clazz. All the methods are static in this class.
202  */
203 /* static */
204 /*Class = */ Clazz = {
205   _isQuiet: false,
206   _debugging: false
207 };
208
209 ;(function(Clazz, Jmol) {
210
211
212 try {
213 Clazz._debugging = (document.location.href.indexOf("j2sdebug") >= 0);
214 } catch (e) {
215 }
216 var __debuggingBH = false;
217 var _globals = ["j2s.clazzloaded", "j2s.object.native"];
218 Clazz.setGlobal = function(a, v) {
219         _globals.push(a);
220         window[a] = v;
221 }
222
223 Clazz.getGlobals = function() {
224         return _globals.sort().join("\n");
225 }
226
227 Clazz.setConsoleDiv = function(d) {
228         window["j2s.lib"] && (window["j2s.lib"].console = d);
229 };
230
231 // BH Clazz.getProfile monitors exactly what is being delegated with SAEM,
232 // which could be a bottle-neck for function calling.
233 // This is critical for performance optimization.
234
235 // Jmol.getProfile()
236
237 var _profile = (window["j2s.doProfile"]  && self.JSON ? {} : null);
238
239 NullObject = function () {};
240
241 /* protected */
242 Clazz._supportsNativeObject = window["j2s.object.native"];
243
244 if (Clazz._supportsNativeObject) {
245         Clazz._O = function () {};
246         Clazz._O.__CLASS_NAME__ = "Object";
247         Clazz._O["getClass"] = function () { return Clazz._O; }; 
248 } else {
249         Clazz._O = Object;
250 }
251
252 Clazz.Console = {};
253 Clazz.dateToString = Date.prototype.toString;
254 Clazz._hashCode = 0;
255
256 var addProto = function(proto, name, func) {
257         return proto[name] = func;
258 };
259
260 ;(function(proto) {
261         addProto(proto, "equals", function (obj) {
262                 return this == obj;
263         });
264
265         addProto(proto, "hashCode", function () {
266   
267     return this._$hashcode || (this._$hashcode = ++Clazz._hashCode)
268
269   
270                 try {
271                         return this.toString ().hashCode ();
272                 } catch (e) {
273                         var str = ":";
274                         for (var s in this) {
275                                 str += s + ":"
276                         }
277                         return str.hashCode ();
278                 }
279         });
280
281         addProto(proto, "getClass", function () { return Clazz.getClass (this); });
282
283         addProto(proto, "clone", function () { return Clazz.clone(this); });
284
285         Clazz.clone = function(me) {
286                 // BH allows @j2sNative access without super constructor
287                 var o = new me.constructor();
288                 for (var i in me) {
289                         o[i] = me[i];
290       }
291                 return o;
292         }
293 /*
294  * Methods for thread in Object
295  */
296         addProto(proto, "finalize", function () {});
297         addProto(proto, "notify", function () {});
298         addProto(proto, "notifyAll", function () {});
299         addProto(proto, "wait", function () {});
300         addProto(proto, "to$tring", Object.prototype.toString);
301         addProto(proto, "toString", function () { return (this.__CLASS_NAME__ ? "[" + this.__CLASS_NAME__ + " object]" : this.to$tring.apply(this, arguments)); });
302         Clazz._extendedObjectMethods = [ "equals", "hashCode", "getClass", "clone", "finalize", "notify", "notifyAll", "wait", "to$tring", "toString" ];
303
304 })(Clazz._O.prototype);
305
306 Clazz.extendJO = function(c, name) {  
307         if (name)
308                 c.__CLASS_NAME__ = c.prototype.__CLASS_NAME__ = name;
309         if (Clazz._supportsNativeObject) {
310                 for (var i = 0; i < Clazz._extendedObjectMethods.length; i++) {
311                         var p = Clazz._extendedObjectMethods[i];
312                         addProto(c.prototype, p, Clazz._O.prototype[p]);
313                 }
314         }
315 };
316
317 /**
318  * Try to fix bug on Safari
319  */
320 //InternalFunction = Object;
321
322 Clazz.extractClassName = function(clazzStr) {
323         // [object Int32Array]
324         var clazzName = clazzStr.substring (1, clazzStr.length - 1);
325         return (clazzName.indexOf("Array") >= 0 ? "Array" // BH -- for Float64Array and Int32Array
326                 : clazzName.indexOf ("object ") >= 0 ? clazzName.substring (7) // IE
327                 : clazzName);
328 }
329 /**
330  * Return the class name of the given class or object.
331  *
332  * @param clazzHost given class or object
333  * @return class name
334  */
335 /* public */
336 Clazz.getClassName = function (obj) {
337         if (obj == null)
338                 return "NullObject";
339         if (obj instanceof Clazz.CastedNull)
340                 return obj.clazzName;
341         switch(typeof obj) {
342         case "number":
343                 return "n";
344         case "boolean":
345                 return "b";
346         case "string":
347                 // Always treat the constant string as String object.
348                 // This will be compatiable with Java String instance.
349                 return "String";
350         case "function":
351                 if (obj.__CLASS_NAME__)
352                         return (arguments[1] ? obj.__CLASS_NAME__ : "Class"); /* user defined class name */
353                 var s = obj.toString();
354                 var idx0 = s.indexOf("function");
355                 if (idx0 < 0)
356                         return (s.charAt(0) == '[' ? Clazz.extractClassName(s) : s.replace(/[^a-zA-Z0-9]/g, ''));
357                 var idx1 = idx0 + 8;
358                 var idx2 = s.indexOf ("(", idx1);
359                 if (idx2 < 0)
360                         return "Object";
361                 s = s.substring (idx1, idx2);
362                 if (s.indexOf("Array") >= 0)
363                         return "Array"; 
364                 s = s.replace (/^\s+/, "").replace (/\s+$/, "");
365                 return (s == "anonymous" || s == "" ? "Function" : s);
366         case "object":
367                 if (obj.__CLASS_NAME__) // user defined class name
368                         return obj.__CLASS_NAME__;
369                 if (!obj.constructor)
370                         return "Object"; // For HTML Element in IE
371                 if (!obj.constructor.__CLASS_NAME__) {
372                         if (obj instanceof Number)
373                                 return "Number";
374                         if (obj instanceof Boolean)
375                                 return "Boolean";
376                         if (obj instanceof Array)
377                                 return "Array";
378                         var s = obj.toString();
379       // "[object Int32Array]"
380                         if (s.charAt(0) == '[')
381                                 return Clazz.extractClassName(s);
382                 }
383         return Clazz.getClassName (obj.constructor, true);
384         }
385   // some new, unidentified class
386   return "Object";
387 };
388 /**
389  * Return the class of the given class or object.
390  *
391  * @param clazzHost given class or object
392  * @return class name
393  */
394 /* public */
395 Clazz.getClass = function (clazzHost) {
396         if (!clazzHost)
397                 return Clazz._O;        // null/undefined is always treated as Object
398         if (typeof clazzHost == "function")
399                 return clazzHost;
400         var clazzName;
401         if (clazzHost instanceof Clazz.CastedNull) {
402                 clazzName = clazzHost.clazzName;
403         } else {
404                 switch (typeof clazzHost) {
405                 case "string":
406                         return String;
407           case "object":
408                         if (!clazzHost.__CLASS_NAME__)
409                                 return (clazzHost.constructor || Clazz._O);
410                         clazzName = clazzHost.__CLASS_NAME__;
411                 break;
412                 default:
413                         return clazzHost.constructor;
414                 }
415         }
416         return Clazz.evalType(clazzName, true);
417 };
418
419
420 /* private */
421 var checkInnerFunction = function (hostSuper, funName) {
422         for (var k = 0; k < Clazz.innerFunctionNames.length; k++)
423                 if (funName == Clazz.innerFunctionNames[k] && 
424                                 Clazz._innerFunctions[funName] === hostSuper[funName])
425                         return true;
426         return false;
427 };
428
429 var args4InheritClass = function () {};
430
431 Clazz.inheritArgs = new args4InheritClass ();
432
433 /**
434  * Inherit class with "extends" keyword and also copy those static members. 
435  * Example, as in Java, if NAME is a static member of ClassA, and ClassB 
436  * extends ClassA then ClassB.NAME can be accessed in some ways.
437  *
438  * @param clazzThis child class to be extended
439  * @param clazzSuper super class which is inherited from
440  * @param objSuper super class instance
441  */
442 /* protected */
443 Clazz.inheritClass = function (clazzThis, clazzSuper, objSuper) {
444         //var thisClassName = Clazz.getClassName (clazzThis);
445         for (var o in clazzSuper) {
446                 if (o != "b$" && o != "prototype" && o != "superClazz"
447                                 && o != "__CLASS_NAME__" && o != "implementz"
448                                 && !checkInnerFunction (clazzSuper, o)) {
449                         clazzThis[o] = clazzSuper[o];
450                 }
451         }
452         if (Clazz.unloadedClasses[Clazz.getClassName(clazzThis, true)]) {
453                 // Don't change clazzThis.protoype! Keep it!
454         } else if (objSuper) {
455                 // ! Unsafe reference prototype to an instance!
456                 // Feb 19, 2006 --josson
457                 // OK for this reference to an instance, as this is anonymous instance,
458                 // which is not referenced elsewhere.
459                 // March 13, 2006
460                 clazzThis.prototype = objSuper; 
461         } else if (clazzSuper !== Number) {
462                 clazzThis.prototype = new clazzSuper (Clazz.inheritArgs);
463         } else { // Number
464                 clazzThis.prototype = new Number ();
465         }
466         clazzThis.superClazz = clazzSuper;
467         /*
468          * Is it necessary to reassign the class name?
469          * Mar 10, 2006 --josson
470          */
471         //clazzThis.__CLASS_NAME__ = thisClassName;
472         clazzThis.prototype.__CLASS_NAME__ = clazzThis.__CLASS_NAME__;
473 };
474
475 /**
476  * Implementation of Java's keyword "implements".
477  * As in JavaScript there are on "implements" keyword implemented, a property
478  * of "implementz" is added to the class to record the interfaces the class
479  * is implemented.
480  * 
481  * @param clazzThis the class to implement
482  * @param interfacez Array of interfaces
483  */
484 /* public */
485 Clazz.implementOf = function (clazzThis, interfacez) {
486         if (arguments.length >= 2) {
487                 if (!clazzThis.implementz)
488                         clazzThis.implementz = [];
489                 var impls = clazzThis.implementz;
490                 if (arguments.length == 2) {
491                         if (typeof interfacez == "function") {
492                                 impls.push(interfacez);
493                                 copyProperties(clazzThis, interfacez);
494                         } else if (interfacez instanceof Array) {
495                                 for (var i = 0; i < interfacez.length; i++) {
496                                         impls.push(interfacez[i]);
497                                         copyProperties(clazzThis, interfacez[i]);
498                                 }
499                         }
500                 } else {
501                         for (var i = 1; i < arguments.length; i++) {
502                                 impls.push(arguments[i]);
503                                 copyProperties(clazzThis, arguments[i]);
504                         }
505                 }
506         }
507 };
508
509 /*
510  * Copy members of interface
511  */
512 /* private */
513 var copyProperties = function(clazzThis, clazzSuper) {
514         for (var o in clazzSuper)
515                 if (o != "b$" 
516                                 && o != "prototype" && o != "superClazz"
517                                 && o != "__CLASS_NAME__" && o != "implementz"
518                                 && (typeof clazzSuper[o] != "function" || !checkInnerFunction(clazzSuper, o)))
519                         clazzThis[o] = clazzThis.prototype[o] = clazzSuper[o];
520 };
521
522 /**
523  * TODO: More should be done for interface's inheritance
524  */
525 /* public */
526 Clazz.extendInterface = Clazz.implementOf;
527
528 /* protected */
529 Clazz.equalsOrExtendsLevel = function (clazzThis, clazzAncestor) {
530         if (clazzThis === clazzAncestor)
531                 return 0;
532         if (clazzThis.implementz) {
533                 var impls = clazzThis.implementz;
534                 for (var i = 0; i < impls.length; i++) {
535                         var level = Clazz.equalsOrExtendsLevel (impls[i], clazzAncestor);
536                         if (level >= 0)
537                                 return level + 1;
538                 }
539         }
540         return -1;
541 };
542
543 /* protected */
544 Clazz.getInheritedLevel = function (clazzTarget, clazzBase) {
545         if (clazzTarget === clazzBase)
546                 return 0;
547         var isTgtStr = (typeof clazzTarget == "string");
548         if (isTgtStr && ("void" == clazzTarget || "unknown" == clazzTarget))
549                 return -1;
550         var isBaseStr = (typeof clazzBase == "string");
551         if (isBaseStr && ("void" == clazzBase || "unknown" == clazzBase))
552                 return -1;
553         if (clazzTarget === (isTgtStr ? "NullObject" : NullObject)) {
554                 switch (clazzBase) {
555     case "n":
556     case "b":
557       return -1;
558                 case Number:
559                 case Boolean:
560                 case NullObject:
561                         break;
562                 default:
563                         return 0;
564                 }
565         }
566         if (isTgtStr)
567                 clazzTarget = Clazz.evalType(clazzTarget);
568         if (isBaseStr)
569                 clazzBase = Clazz.evalType(clazzBase);
570         if (!clazzBase || !clazzTarget)
571                 return -1;
572         var level = 0;
573         var zzalc = clazzTarget; // zzalc <--> clazz
574         while (zzalc !== clazzBase && level < 10) {
575                 /* maybe clazzBase is interface */
576                 if (zzalc.implementz) {
577                         var impls = zzalc.implementz;
578                         for (var i = 0; i < impls.length; i++) {
579                                 var implsLevel = Clazz.equalsOrExtendsLevel (impls[i], clazzBase);
580                                 if (implsLevel >= 0)
581                                         return level + implsLevel + 1;
582                         }
583                 }
584                 zzalc = zzalc.superClazz;
585                 if (!zzalc)
586                         return (clazzBase === Object || clazzBase === Clazz._O ? 
587                                 // getInheritedLevel(String, CharSequence) == 1
588                                 // getInheritedLevel(String, Object) == 1.5
589                                 // So if both #test(CharSequence) and #test(Object) existed,
590                                 // #test("hello") will correctly call #test(CharSequence)
591                                 // instead of #test(Object).
592                                 level + 1.5 // 1.5! Special!
593                         : -1);
594                 level++;
595         }
596         return level;
597 };
598
599
600 /**
601  * Implements Java's keyword "instanceof" in JavaScript's way.
602  * As in JavaScript part of the object inheritance is implemented in only-
603  * JavaScript way.
604  *
605  * @param obj the object to be tested
606  * @param clazz the class to be checked
607  * @return whether the object is an instance of the class
608  */
609 /* public */
610 Clazz.instanceOf = function (obj, clazz) {
611   // allows obj to be a class already, from arrayX.getClass().isInstance(y)
612         return (obj != null && clazz && (obj == clazz || obj instanceof clazz || Clazz.getInheritedLevel(Clazz.getClassName(obj), clazz) >= 0));
613 };
614
615 /**
616  * Call super method of the class. 
617  * The same effect as Java's expression:
618  * <code> super.* () </code>
619  * 
620  * @param objThis host object
621  * @param clazzThis class of declaring method scope. It's hard to determine 
622  * which super class is right class for "super.*()" call when it's in runtime
623  * environment. For example,
624  * 1. ClasssA has method #run()
625  * 2. ClassB extends ClassA overriding method #run() with "super.run()" call
626  * 3. ClassC extends ClassB
627  * 4. objC is an instance of ClassC
628  * Now we have to decide which super #run() method is to be invoked. Without
629  * explicit clazzThis parameter, we only know that objC.getClass() is ClassC 
630  * and current method scope is #run(). We do not known we are in scope 
631  * ClassA#run() or scope of ClassB#run(). if ClassB is given, Clazz can search
632  * all super methods that are before ClassB and get the correct super method.
633  * This is the reason why there must be an extra clazzThis parameter.
634  * @param funName method name to be called
635  * @param funParams Array of method parameters
636  */
637 /* public */
638 Clazz.superCall = function (objThis, clazzThis, funName, funParams) {
639         var fx = null;
640         var i = -1;
641         var clazzFun = objThis[funName];
642         if (clazzFun) {
643                 if (clazzFun.claxxOwner) { 
644                         // claxxOwner is a mark for methods that is single.
645                         if (clazzFun.claxxOwner !== clazzThis) {
646                                 // This is a single method, call directly!
647                                 fx = clazzFun;
648         
649                         }
650                 } else if (!clazzFun.stacks && !(clazzFun.lastClaxxRef
651                                         && clazzFun.lastClaxxRef.prototype[funName]
652                                         && clazzFun.lastClaxxRef.prototype[funName].stacks)) { // super.toString
653                         fx = clazzFun;
654                 } else { // normal wrapped method
655                         var stacks = clazzFun.stacks;
656                         if (!stacks)
657                                 stacks = clazzFun.lastClaxxRef.prototype[funName].stacks;
658                         for (i = stacks.length; --i >= 0;) {
659                                 /*
660                                  * Once super call is computed precisely, there are no need 
661                                  * to calculate the inherited level but just an equals
662                                  * comparision
663                                  */
664                                 //var level = Clazz.getInheritedLevel (clazzThis, stacks[i]);
665                                 if (clazzThis === stacks[i]) { // level == 0
666                                         if (i > 0) {
667                                                 fx = stacks[--i].prototype[funName];
668                                         } else {
669                                                 /*
670                                                  * Will this case be reachable?
671                                                  * March 4, 2006
672                                                  * Should never reach here if all things are converted
673                                                  * by Java2Script
674                                                  */
675                                                 fx = stacks[0].prototype[funName]["\\unknown"];
676                                         }
677                                         break;
678                                 } else if (Clazz.getInheritedLevel (clazzThis, stacks[i]) > 0) {
679                                         fx = stacks[i].prototype[funName];
680                                         break;
681                                 }
682                         } // end of for loop
683                 } // end of normal wrapped method
684         } // end of clazzFun
685         if (!fx) {
686                 if (funName != "construct") {
687                         Clazz.alert (["j2slib","no class found",(funParams).typeString])
688                         newMethodNotFoundException(objThis, clazzThis, funName, 
689                                         Clazz.getParamsType(funParams).typeString);     
690                 }
691                 /* there are members which are initialized out of the constructor */
692                 /* No super constructor! */
693                 return;
694         }
695         /* there are members which are initialized out of the constructor */
696         if (i == 0 && funName == "construct") {
697                 var ss = clazzFun.stacks;
698                 if (ss && !ss[0].superClazz && ss[0].con$truct)
699                         ss[0].con$truct.apply (objThis, []);
700         }
701         /*# {$no.debug.support} >>x #*/
702         /* not used in Jmol
703         if (Clazz.tracingCalling) {
704                 var caller = arguments.callee.caller;
705                 if (caller === Clazz.superConstructor) {
706                         caller = caller.arguments.callee.caller;
707                 }
708                 Clazz._callingStackTraces.push(new Clazz.callingStack (caller, clazzThis));
709                 var ret = fx.apply (objThis, (funParams == null) ? [] : funParams);
710                 Clazz._callingStackTraces.pop();
711                 return ret;
712         }
713         */
714         /*# x<< #*/
715         return fx.apply (objThis, funParams || []);
716 };
717
718 /**
719  * Call super constructor of the class. 
720  * The same effect as Java's expression: 
721  * <code> super () </code>
722  */
723 /* public */
724 Clazz.superConstructor = function (objThis, clazzThis, funParams) {
725         Clazz.superCall (objThis, clazzThis, "construct", funParams);
726         /* If there are members which are initialized out of the constructor */
727         if (clazzThis.con$truct) {
728                 clazzThis.con$truct.apply (objThis, []);
729         }
730 };
731
732 /**
733  * Class for null with a given class as to be casted.
734  * This class will be used as an implementation of Java's casting way.
735  * For example,
736  * <code> this.call ((String) null); </code>
737  */
738 /* public */
739 Clazz.CastedNull = function (asClazz) {
740         if (asClazz) {
741                 if (asClazz instanceof String) {
742                         this.clazzName = asClazz;
743                 } else if (asClazz instanceof Function) {
744                         this.clazzName = Clazz.getClassName (asClazz, true);
745                 } else {
746                         this.clazzName = "" + asClazz;
747                 }
748         } else {
749                 this.clazzName = "Object";
750         }
751         this.toString = function () {
752                 return null;
753         };
754         this.valueOf = function () {
755                 return null;
756         };
757 };
758
759 /**
760  * API for Java's casting null.
761  * @see Clazz.CastedNull
762  *
763  * @param asClazz given class
764  * @return an instance of class Clazz.CastedNull
765  */
766 /* public */
767 Clazz.castNullAs = function (asClazz) {
768         return new Clazz.CastedNull (asClazz);
769 };
770
771 /////////////////////////// Exception handling ////////////////////////////
772
773 /*
774  * Use to mark that the Throwable instance is created or not.
775  * 
776  * Called from java.lang.Throwable, as defined in JSmolJavaExt.js
777  * 
778  * The underscore is important - it tells the JSmol ANT task to NOT 
779  * turn this into Clazz_initializingException, because coreBottom2.js does 
780  * not include that call, and so Google Closure Compiler does not minify it.
781  *        
782  */
783 /* public */
784 Clazz._initializingException = false;
785
786 /**
787  * BH: used in Throwable
788  *  
789  */  
790 /* public */
791 Clazz._callingStackTraces = [];
792
793 /** 
794  * MethodException will be used as a signal to notify that the method is
795  * not found in the current clazz hierarchy.
796  */
797 /* private */
798 var MethodException = function () {
799         this.toString = function () {
800                 return "J2S MethodException";
801         };
802 };
803 /* private */
804 //var MethodNotFoundException = function () {
805 //      this.toString = function () {
806 //              return "J2S MethodNotFoundException";
807 //      };
808 //};
809
810   var _isNPEExceptionPredicate;
811
812 /* super private */
813 ;(function() { 
814   /* sgurin: native exception detection mechanism. Only NullPointerException detected and wrapped to java excepions */
815   /** private utility method for creating a general regexp that can be used later  
816    * for detecting a certain kind of native exceptions. use with error messages like "blabla IDENTIFIER blabla"
817    * @param msg String - the error message
818    * @param spliterName String, must be contained once in msg
819    * spliterRegex String, a string with the regexp literal for identifying the spitter in exception further error messages.
820    */
821   // reproduce NullPointerException for knowing how to detect them, and create detector function Clazz._isNPEExceptionPredicate
822   var $$o$$ = null;
823   
824   try {
825         $$o$$.hello();
826   } catch (e) {
827     var _ex_reg = function(msg, spliterName, spliterRegex) {
828         if(!spliterRegex) 
829                 spliterRegex="[^\\s]+"; 
830         var idx = msg.indexOf (spliterName), 
831                 str = msg.substring (0, idx) + spliterRegex + msg.substring(idx + spliterName.length), 
832                 regexp = new RegExp("^"+str+"$");
833         return regexp;
834     };
835         if(/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {// opera throws an exception with fixed messages like "Statement on line 23: Cannot convert undefined or null to Object Backtrace: Line....long text... " 
836                 var idx1 = e.message.indexOf(":"), idx2 = e.message.indexOf(":", idx1+2);
837                 var _NPEMsgFragment = e.message.substr(idx1+1, idx2-idx1-20);
838                 _isNPEExceptionPredicate = function(e) { return e.message.indexOf(_NPEMsgFragment)!=-1; };
839         }       else if(navigator.userAgent.toLowerCase().indexOf("webkit")!=-1) { //webkit, google chrome prints the property name accessed. 
840                 var _exceptionNPERegExp = _ex_reg(e.message, "hello");
841                 _isNPEExceptionPredicate = function(e) { return _exceptionNPERegExp.test(e.message); };
842         }       else {// ie, firefox and others print the name of the object accessed: 
843                 var _exceptionNPERegExp = _ex_reg(e.message, "$$o$$");
844                 _isNPEExceptionPredicate = function(e) { return _exceptionNPERegExp.test(e.message); };
845         }               
846   };
847 })();
848
849 /**sgurin
850  * Implements Java's keyword "instanceof" in JavaScript's way **for exception objects**.
851  * 
852  * calls Clazz.instanceOf if e is a Java exception. If not, try to detect known native 
853  * exceptions, like native NullPointerExceptions and wrap it into a Java exception and 
854  * call Clazz.instanceOf again. if the native exception can't be wrapped, false is returned.
855  * 
856  * @param obj the object to be tested
857  * @param clazz the class to be checked
858  * @return whether the object is an instance of the class
859  * @author: sgurin
860  */
861 Clazz.exceptionOf = function(e, clazz) {
862         if(e.__CLASS_NAME__)
863                 return Clazz.instanceOf(e, clazz);
864   if (!e.getMessage) {
865     e.getMessage = function() {return "" + this};
866   }
867   if (!e.printStackTrace) {
868     e.printStackTrace = function(){};
869     alert(e + " try/catch path:" + Clazz.getStackTrace(-10));
870   }
871         if(clazz == Error) {
872                 if (("" + e).indexOf("Error") < 0)
873       return false;
874                 System.out.println (Clazz.getStackTrace());
875     return true;
876                 // everything here is a Java Exception, not a Java Error
877         }
878         return (clazz == Exception || clazz == Throwable
879                 || clazz == NullPointerException && _isNPEExceptionPredicate(e));
880 };
881
882 /**
883  * BH need to limit this, as JavaScript call stack may be recursive
884  */ 
885 Clazz.getStackTrace = function(n) {
886         n || (n = 25);
887   // updateNode and updateParents cause infinite loop here
888         var s = "\n";
889         var c = arguments.callee;
890   var showParams = (n < 0);
891   if (showParams)
892     n = -n;
893         for (var i = 0; i < n; i++) {
894                 if (!(c = c.caller))
895       break;
896     var sig = (c.toString ? c.toString().substring(0, c.toString().indexOf("{")) : "<native method>");
897                 s += i + " " + (c.exName ? (c.claxxOwner ? c.claxxOwner.__CLASS_NAME__ + "."  : "") + c.exName  + sig.replace(/function /,""): sig) + "\n";
898                 if (c == c.caller) {
899       s += "<recursing>\n";
900       break;
901     }
902     if (showParams) {
903       var args = c.arguments;
904       for (var j = 0; j < args.length; j++) {
905         var sa = "" + args[j];
906         if (sa.length > 60)
907           sa = sa.substring(0, 60) + "...";
908         s += " args[" + j + "]=" + sa.replace(/\s+/g," ") + "\n";
909       }
910     }
911         }
912         return s;
913 }
914
915 ///////////////////// method creation ////////////////////////////////
916
917 /**
918  * Make constructor for the class with the given function body and parameters
919  * signature.
920  * 
921  * @param clazzThis host class
922  * @param funBody constructor body
923  * @param funParams constructor parameters signature
924  */
925 /* public */
926 Clazz.makeConstructor = function (clazzThis, funBody, funParams) {
927         Clazz.defineMethod (clazzThis, "construct", funBody, funParams);
928         if (clazzThis.con$truct) {
929                 clazzThis.con$truct.index = clazzThis.con$truct.stacks.length;
930         }
931         //clazzThis.con$truct = clazzThis.prototype.con$truct = null;
932 };
933
934 /**
935  * Override constructor for the class with the given function body and
936  * parameters signature.
937  * 
938  * @param clazzThis host class
939  * @param funBody constructor body
940  * @param funParams constructor parameters signature
941  */
942 /* public */
943 Clazz.overrideConstructor = function (clazzThis, funBody, funParams) {
944         Clazz.overrideMethod (clazzThis, "construct", funBody, funParams);
945         if (clazzThis.con$truct) {
946                 clazzThis.con$truct.index = clazzThis.con$truct.stacks.length;
947         }
948         //clazzThis.con$truct = clazzThis.prototype.con$truct = null;
949 };
950
951
952 /*
953  * Define method for the class with the given method name and method
954  * body and method parameter signature.
955  *
956  * @param clazzThis host class in which the method to be defined
957  * @param funName method name
958  * @param funBody function object, e.g function () { ... }
959  * @param funParams paramether signature, e.g ["string", "number"]
960  * @return method of the given name. The method may be funBody or a wrapper
961  * of the given funBody.
962  */
963 /* public */
964 Clazz.defineMethod = function (clazzThis, funName, funBody, funParams) {
965         if (Clazz.assureInnerClass) 
966     Clazz.assureInnerClass(clazzThis, funBody);
967         funBody.exName = funName;
968         var fpName = formatParameters(funParams);
969         var proto = clazzThis.prototype;
970         var f$ = proto[funName];
971   if (Clazz._Loader._checkLoad)
972     checkDuplicate(clazzThis, funName, fpName);
973         if (!f$ || (f$.claxxOwner === clazzThis && f$.funParams == fpName)) {
974                 // property "funParams" will be used as a mark of only-one method
975                 funBody.funParams = fpName; 
976                 funBody.claxxOwner = clazzThis;
977                 funBody.exClazz = clazzThis; // make it traceable
978                 return addProto(proto, funName, funBody);
979         }
980   // we have found a duplicate
981         var oldFun = null;
982         var oldStacks = f$.stacks;
983                 if (!oldStacks) {
984                         /* method is not defined by Clazz.defineMethod () */
985       oldStacks = [];
986                         oldFun = f$;
987                         if (f$.claxxOwner) {
988                                 oldStacks[0] = oldFun.claxxOwner;
989                         }
990                 }
991                 /*
992          * Method that is already defined in super class will be overridden
993          * with a new proxy method with class hierarchy stored in a stack.
994          * That is to say, the super methods are lost in this class' proxy
995          * method. 
996          * When method are being called, methods defined in the new proxy 
997          * method will be searched through first. And if no method fitted,
998          * it will then try to search method in the super class stacks.
999          */
1000         if (!f$.stacks || f$.claxxReference !== clazzThis) {
1001                 //Generate a new delegating method for the class                
1002     var id = ++SAEMid;
1003         var delegate = function () {
1004                 return searchAndExecuteMethod(id, this, arguments.callee.claxxReference, arguments.callee.methodName, arguments);
1005         };
1006         delegate.methodName = funName;
1007         delegate.claxxReference = clazzThis;
1008                 f$ = addProto(proto, funName, delegate);                                
1009                 // Keep the class inheritance stacks
1010                 var arr = [];
1011                 for (var i = 0; i < oldStacks.length; i++)
1012                         arr[i] = oldStacks[i];
1013                 f$.stacks = arr;
1014         }
1015         var ss = f$.stacks;
1016         if (findArrayItem(ss, clazzThis) < 0) ss.push(clazzThis);
1017
1018         if (oldFun) {
1019                 if (oldFun.claxxOwner === clazzThis) {
1020                         f$[oldFun.funParams] = oldFun;
1021                         oldFun.claxxOwner = null;
1022                         // property "funParams" will be used as a mark of only-one method
1023                         oldFun.funParams = null; // null ? safe ? // safe for != null
1024                 } else if (!oldFun.claxxOwner) {
1025                         /*
1026                          * The function is not defined Clazz.defineMethod ().
1027                          * Try to fixup the method ...
1028                          * As a matter of lost method information, I just suppose
1029                          * the method to be fixed is with void parameter!
1030                          */
1031                         f$["\\unknown"] = oldFun;
1032                 }
1033         }
1034         funBody.exClazz = clazzThis; // make it traceable
1035         f$[fpName] = funBody;
1036         return f$;
1037 };                                                
1038
1039 duplicatedMethods = {};
1040
1041 var checkDuplicate = function(clazzThis, funName, fpName) {
1042         var proto = clazzThis.prototype;
1043         var f$ = proto[funName];
1044   if (f$ && (f$.claxxOwner || f$.claxxReference) === clazzThis) {
1045     key = clazzThis.__CLASS_NAME__ + "." + funName + fpName;
1046     var m = duplicatedMethods[key];
1047     if (m) {
1048       var s = "Warning! Duplicate method found for " + key;
1049       System.out.println(s);
1050       Clazz.alert(s);
1051       duplicatedMethods[key] = m + 1; 
1052     } else {
1053       duplicatedMethods[key] = 1;
1054     }
1055   }
1056 }
1057
1058 Clazz.showDuplicates = function(quiet) {
1059   var s = "";
1060   var a = duplicatedMethods;
1061   var n = 0;
1062   for (var key in a)
1063     if (a[key] > 1) {
1064       s += a[key] + "\t" + key + "\n";
1065       n++;
1066     }
1067   s = "Duplicates: " + n + "\n\n" + s;
1068   System.out.println(s);
1069   if (!quiet)
1070     alert(s);
1071 }
1072
1073 var findArrayItem = function(arr, item) {
1074         if (arr && item)
1075                 for (var i = arr.length; --i >= 0;)
1076                         if (arr[i] === item)
1077                                 return i;
1078         return -1;
1079 }
1080
1081 var removeArrayItem = function(arr, item) {
1082         var i = findArrayItem(arr, item);
1083         if (i >= 0) {
1084                 var n = arr.length - 1;
1085                 for (; i < n; i++)
1086                         arr[i] = arr[i + 1];
1087                 arr.length--;
1088                 return true;
1089         }
1090 }
1091
1092 /*
1093  * Other developers may need to extend this formatParameters method
1094  * to deal complicated situation.
1095  */
1096 /* protected */
1097 var formatParameters = function (funParams) {
1098         return (funParams ? funParams.replace (/~([NABSO])/g, 
1099       function ($0, $1) {
1100         switch ($1) {
1101         case 'N':
1102                 return "n";
1103         case 'B':
1104                 return "b";
1105         case 'S':
1106                 return "String";
1107         case 'O':
1108                 return "Object";
1109         case 'A':
1110                 return "Array";
1111         }
1112         return "Unknown";
1113       }).replace (/\s+/g, "").replace (/^|,/g, "\\").replace (/\$/g, "org.eclipse.s") : "\\void");
1114 };
1115
1116 /*
1117  * Override the existed methods which are in the same name.
1118  * Overriding methods is provided for the purpose that the JavaScript
1119  * does not need to search the whole hierarchied methods to find the
1120  * correct method to execute.
1121  * Be cautious about this method. Incorrectly using this method may
1122  * break the inheritance system.
1123  *
1124  * @param clazzThis host class in which the method to be defined
1125  * @param funName method name
1126  * @param funBody function object, e.g function () { ... }
1127  * @param funParams paramether signature, e.g ["string", "number"]
1128  */
1129 /* public */
1130 Clazz.overrideMethod = function(clazzThis, funName, funBody, funParams) {
1131         if (Clazz.assureInnerClass) Clazz.assureInnerClass (clazzThis, funBody);
1132         funBody.exName = funName;
1133         var fpName = formatParameters(funParams);
1134   if (Clazz._Loader._checkLoad)
1135     checkDuplicate(clazzThis, funName, fpName);
1136         /*
1137          * Replace old methods with new method. No super methods are kept.
1138          */
1139         funBody.funParams = fpName; 
1140         funBody.claxxOwner = clazzThis;
1141         return addProto(clazzThis.prototype, funName, funBody);
1142 };
1143
1144 //////////////  Overridden and Overloaded Java Method Handling //////////////////
1145 //                       SAEM (SearchAndExecuteMethod)
1146 // adapted by BH
1147 //
1148
1149 /*
1150  * BH Clazz.getProfile monitors exactly what is being delegated with SAEM,
1151  * which could be a bottle-neck for function calling.
1152  * This is critical for performance optimization.
1153  */ 
1154
1155   var __signatures = ""; 
1156
1157 Clazz.getProfile = function() {
1158         var s = "";
1159         if (_profile) {
1160                 var l = [];
1161                 for (var i in _profile) {
1162                         var n = "" + _profile[i];
1163                         l.push("        ".substring(n.length) + n + "\t" + i);
1164                 }
1165                 s = l.sort().reverse().join("\r\n");
1166                 _profile = {};
1167         }
1168         return s + __signatures;
1169 }
1170
1171 var addProfile = function(c, f, p, id) {
1172         var s = id + " " + c.__CLASS_NAME__ + " " + f + " ";// + JSON.stringify(p);
1173   if (__signatures.indexOf(s) < 0)
1174     __signatures += s + "\n";    
1175         _profile[s] || (_profile[s] = 0);
1176         _profile[s]++;
1177 }
1178
1179 /**
1180  * Called also by Throwable
1181  *  
1182 /* public */
1183 Clazz.getParamsType = function (funParams) {
1184         // bh: optimization here for very common cases
1185         var n = funParams.length;
1186         switch (n) {
1187         case 0:
1188                 var params = ["void"];
1189                 params.typeString = "\\void";
1190                 return params;
1191         case 1:
1192           // BH just so common
1193     switch (typeof obj) {
1194     case "number":
1195                         var params = ["n"];
1196                         params.typeString = "\\n";
1197                         return params;
1198     case "boolean":
1199                         var params = ["b"];
1200                         params.typeString = "\\b";
1201                         return params;
1202                 }
1203         }
1204
1205         var params = [];
1206         params.hasCastedNull = false;
1207         if (funParams) {
1208                 for (var i = 0; i < n; i++) {
1209                         params[i] = Clazz.getClassName (funParams[i]);
1210                         if (funParams[i] instanceof Clazz.CastedNull) {
1211                                 params.hasCastedNull = true;
1212                         }
1213                 }
1214         }
1215         params.typeString = "\\" + params.join ('\\');
1216         return params;
1217 };
1218
1219 var SAEMid = 0;
1220 xxxSAEMlist = "";
1221
1222 //var SAEMarray = [];
1223 /**
1224  * BH: OK, this was an idea that doesn't work. The idea was to tag SAEM calls
1225  * and then refer back to an array. But the id number cannot be put in the right place.
1226  * 
1227  * Say we have this:
1228  * 
1229  * StringBuffer sb = new StringBuffer(); 
1230  * sb.append("").append(1);
1231  * 
1232  * Here we have two different append methods to call. They are saved under two
1233  * names:  StringBuffer.prototype.append["\\String"] 
1234  *     and StringBuffer.prototype.append["\\Number"]
1235  * 
1236  * The job of generateDelegatingMethod is to discriminate between those two. We can do
1237  * that, but the real issue is that we have to do that EVERY time a call is made.
1238  * This is a problem that must be handled at compile time. There is no way to 
1239  * make .append("") to go one way the first time and another way the second time. 
1240  * What we need at run time is something like this:
1241  * 
1242  * Clazz.delegate(sb.append,1,[""]) and Clazz.delegate(sb.append,2,[1])
1243  * The we would be able to log those numbers at run time and refer to them.
1244  *                     
1245  * The only real way to avoid SAEM is: 
1246  * 
1247  * 1) to never call super() -- always call a differently named function in a superclass.
1248  * 2) don't overload functions 
1249  *  
1250  */   
1251
1252
1253 /**
1254  * Search the given class prototype, find the method with the same
1255  * method name and the same parameter signatures by the given 
1256  * parameters, and then run the method with the given parameters.
1257  *
1258  * @param objThis the current host object
1259  * @param claxxRef the current host object's class
1260  * @param fxName the method name
1261  * @param funParams the given arguments
1262  * @return the result of the specified method of the host object,
1263  * the return maybe void.
1264  * @throws MethodNotFoundException if no matched method is found
1265  */
1266 /* protected */
1267 var searchAndExecuteMethod = function (id, objThis, claxxRef, fxName, args, _saem) {
1268
1269 //  var fx = SAEMarray[id];
1270 //  if (fx) {
1271 //    return fx.apply(objThis, args);
1272 //  }
1273
1274
1275         fx = objThis[fxName];
1276         var params = Clazz.getParamsType(args);
1277
1278
1279 var s = "SAEM " + claxxRef.__CLASS_NAME__ + "." + fxName + "(" + params+ ")\n";
1280 if (xxxSAEMlist.length > 300)xxxSAEMlist = "";
1281 xxxSAEMlist += s;
1282  
1283
1284   if (!fx)    
1285     try {System.out.println(Clazz.getStackTrace(5))} catch (e){}
1286         _profile && addProfile(claxxRef, fxName, params, id);
1287         // Cache last matched method
1288         if (fx.lastParams == params.typeString && fx.lastClaxxRef === claxxRef) {
1289                 var methodParams;
1290                 if (params.hasCastedNull) {
1291                         methodParams = [];
1292                         // For Clazz.CastedNull instances, the type name is
1293                         // already used to indentified the method in searchMethod.
1294                         for (var k = 0; k < args.length; k++)
1295                                 methodParams[k] = (args[k] instanceof Clazz.CastedNull ? null : args[k]);
1296                 } else {
1297 //      if (fx.lastMethod) SAEMarray[id] = fx.lastMethod;
1298                         methodParams = args;
1299                 }
1300                 return (fx.lastMethod ? fx.lastMethod.apply(objThis, methodParams) : null);
1301         }
1302         fx.lastParams = params.typeString;
1303         fx.lastClaxxRef = claxxRef;
1304
1305         var stacks = fx.stacks;
1306         if (!stacks)
1307                 stacks = claxxRef.prototype[fxName].stacks;
1308         var length = stacks.length;
1309
1310         /*
1311          * Search the inheritance stacks to get the given class' function
1312          */
1313         var began = false; // began to search its super classes
1314         for (var i = length; --i >= 0;) {
1315                 if (began || stacks[i] === claxxRef) {
1316                         /*
1317                          * First try to search method within the same class scope
1318                          * with stacks[i] === claxxRef
1319                          */
1320                         var clazzFun = stacks[i].prototype[fxName];
1321                         var ret = tryToSearchAndExecute(id, fxName, objThis, clazzFun, params,
1322                                         args, fx);
1323                         if (!(ret instanceof MethodException)) {
1324                                 return ret;
1325                         }
1326                         /*
1327                          * As there are no such methods in current class, Clazz will try 
1328                          * to search its super class stacks. Here variable began indicates
1329                          * that super searchi is began, and there is no need checking
1330                          * <code>stacks[i] === claxxRef</code>
1331                          */
1332                         began = true; 
1333                 } // end of if
1334         } // end of for
1335         if ("construct" == fxName) {
1336                 /*
1337                  * For non existed constructors, just return without throwing
1338                  * exceptions. In Java codes, extending Object can call super
1339                  * default Object#constructor, which is not defined in JS.
1340                  */
1341                 return;
1342         }
1343         newMethodNotFoundException(objThis, claxxRef, 
1344                         fxName, params.typeString);
1345 };
1346
1347
1348 /* private */
1349 var tryToSearchAndExecute = function(id, fxName, objThis, clazzFun, params, args, fx, _ttsaem) {
1350         var method = [];
1351         var generic = true;
1352         for (var fn in clazzFun) {
1353                 if (fn.charCodeAt(0) == 92) { // 92 == '\\'.charCodeAt (0)
1354                         var ps = fn.substring(1).split("\\");
1355                         (ps.length == params.length) && method.push(ps);
1356                 generic = false;
1357                         continue;
1358                 }
1359                 /*
1360                  * When there is only one method in the class, use the args
1361                  * to identify the parameter type.
1362                  *
1363                  * AbstractCollection.remove (Object)
1364                  * AbstractList.remove (int)
1365                  * ArrayList.remove (int)
1366                  *
1367                  * Then calling #remove (Object) method on ArrayList instance will 
1368                  * need to search up to the AbstractCollection.remove (Object),
1369                  * which contains only one method.
1370                  */
1371                 /*
1372                  * See Clazz#defineMethod --Mar 10, 2006, josson
1373                  */
1374                 if (generic && fn == "funParams" && clazzFun.funParams) {
1375                         fn = clazzFun.funParams;
1376                         var ps = fn.substring(1).split ("\\");
1377                         (ps.length == params.length) && (method[0] = ps);
1378                         break;
1379                 }
1380         }
1381   var debug = false;//(method.length > 1 && method.join().indexOf("Listen")< 0 && params.join().indexOf("Null") >= 0)
1382   if (debug)alert(fxName + " -- " + method.join("|") + " -- searching for method with " + params)
1383   if (method.length == 0 || !(method = searchMethod(method, params, debug)))
1384           return new MethodException();
1385   if (debug) alert("OK: \\" + method)
1386         var f = (generic ? clazzFun : clazzFun["\\" + method]);
1387         //if (generic) 
1388   //{ /* Use the generic method */
1389                 /*
1390                  * Will this case be reachable?
1391                  * March 4, 2006 josson
1392                  * 
1393                  * Reachable for calling #remove (Object) method on 
1394                  * ArrayList instance
1395                  * May 5, 2006 josson
1396                  */
1397         var methodParams = null;
1398         if (params.hasCastedNull) {
1399                 methodParams = [];
1400                 for (var k = 0; k < args.length; k++) {
1401                         if (args[k] instanceof Clazz.CastedNull) {
1402                                 /*
1403                                  * For Clazz.CastedNull instances, the type name is
1404                                  * already used to indentify the method in searchMethod.
1405                                  */
1406                                 methodParams[k] = null;
1407                         } else {
1408                                 methodParams[k] = args[k];
1409                         }
1410                 }
1411         } else {
1412                 methodParams = args;
1413         }
1414         fx.lastMethod = f;
1415   //if (!params.hasCastedNull) SAEMarray[id] = f;
1416         return f.apply(objThis, methodParams);
1417 };
1418
1419 /**
1420  * Search the existed polymorphic methods to get the matched method with
1421  * the given parameter types.
1422  *
1423  * @param existedMethods Array of string which contains method parameters
1424  * @param paramTypes Array of string that is parameter type.
1425  * @return string of method parameters seperated by "\\"
1426  */
1427 /* private */
1428 var searchMethod = function(roundOne, paramTypes, debug) {
1429
1430 // Filter out all the fitted methods for the given parameters
1431         var roundTwo = [];
1432         var len = roundOne.length;
1433         for (var i = 0; i < len; i++) {
1434                 var fittedLevel = [];
1435                 var isFitted = true;
1436                 var len2 = roundOne[i].length;
1437                 for (var j = 0; j < len2; j++) {
1438     
1439                         fittedLevel[j] = Clazz.getInheritedLevel (paramTypes[j], 
1440                                         roundOne[i][j]);
1441       //if (debug)alert([paramTypes[j],fittedLevel[j],roundOne[i][j]])    
1442                         if (fittedLevel[j] < 0) {
1443                                 isFitted = false;
1444                                 break;
1445                         }
1446                 }
1447                 if (isFitted) {
1448                         fittedLevel[paramTypes.length] = i; // Keep index for later use
1449                         roundTwo.push(fittedLevel);
1450                 }
1451         }
1452         if (roundTwo.length == 0)
1453                 return null;
1454         // Find out the best method according to the inheritance.
1455         var resultTwo = roundTwo;
1456         var min = resultTwo[0];
1457         for (var i = 1; i < resultTwo.length; i++) {
1458                 var isVectorLesser = true;
1459                 for (var j = 0; j < paramTypes.length; j++) {
1460                         if (min[j] < resultTwo[i][j]) {
1461                                 isVectorLesser = false;;
1462                                 break;
1463                         }
1464                 }
1465                 if (isVectorLesser)
1466                         min = resultTwo[i];
1467         }
1468         var index = min[paramTypes.length]; // Get the previously stored index
1469         /*
1470          * Return the method parameters' type string as indentifier of the
1471          * choosen method.
1472          */
1473         return roundOne[index].join ('\\');
1474 };
1475
1476 ////////////////////////////////// package loading ///////////////////////
1477
1478 /*
1479  * all root packages. e.g. java.*, org.*, com.*
1480  */
1481 /* protected */
1482 Clazz.allPackage = {};
1483
1484 /**
1485  * Will be used to keep value of whether the class is defined or not.
1486  */
1487 /* protected */
1488 Clazz.allClasses = {};
1489
1490 Clazz.lastPackageName = null;
1491 Clazz.lastPackage = null;
1492
1493 /* protected */
1494 Clazz.unloadedClasses = [];
1495
1496 /* public */
1497 Clazz.declarePackage = function (pkgName) {
1498         if (Clazz.lastPackageName == pkgName)
1499                 return Clazz.lastPackage;
1500         if (pkgName && pkgName.length) {
1501                 var pkgFrags = pkgName.split (/\./);
1502                 var pkg = Clazz.allPackage;
1503                 for (var i = 0; i < pkgFrags.length; i++) {
1504                         if (!pkg[pkgFrags[i]]) {
1505                                 pkg[pkgFrags[i]] = { 
1506                                         __PKG_NAME__ : (pkg.__PKG_NAME__ ? 
1507                                                 pkg.__PKG_NAME__ + "." + pkgFrags[i] : pkgFrags[i])
1508                                 }; 
1509                                 // pkg[pkgFrags[i]] = {};
1510                                 if (i == 0) {
1511                                         // eval ...
1512                                         Clazz.setGlobal(pkgFrags[i], pkg[pkgFrags[i]]);
1513                                 }
1514                         }
1515                         pkg = pkg[pkgFrags[i]]
1516                 }
1517                 Clazz.lastPackageName = pkgName;
1518                 Clazz.lastPackage = pkg;
1519                 return pkg;
1520         }
1521 };
1522
1523 /* protected */
1524 Clazz.evalType = function (typeStr, isQualified) {
1525         var idx = typeStr.lastIndexOf(".");
1526         if (idx != -1) {
1527                 var pkgName = typeStr.substring (0, idx);
1528                 var pkg = Clazz.declarePackage (pkgName);
1529                 var clazzName = typeStr.substring (idx + 1);
1530                 return pkg[clazzName];
1531         } 
1532         if (isQualified)
1533                 return window[typeStr];
1534         switch (typeStr) {
1535         case "string":
1536                 return String;
1537         case "number":
1538                 return Number;
1539   case "object":
1540                 return Clazz._O;
1541         case "boolean":
1542                 return Boolean;
1543         case "function":
1544                 return Function;
1545   case "void":
1546   case "undefined":
1547   case "unknown":
1548                 return typeStr;
1549         case "NullObject":
1550                 return NullObject;
1551         default:
1552                 return window[typeStr];
1553         }
1554 };
1555
1556 /**
1557  * Define a class or interface.
1558  *
1559  * @param qClazzName String presents the qualified name of the class
1560  * @param clazzFun Function of the body
1561  * @param clazzParent Clazz to inherit from, may be null
1562  * @param interfacez Clazz may implement one or many interfaces
1563  *   interfacez can be Clazz object or Array of Clazz objects.
1564  * @return Ruturn the modified Clazz object
1565  */
1566 /* public */
1567 Clazz.defineType = function (qClazzName, clazzFun, clazzParent, interfacez) {
1568         var cf = Clazz.unloadedClasses[qClazzName];
1569         if (cf) {
1570                 clazzFun = cf;
1571         }
1572         var idx = qClazzName.lastIndexOf (".");
1573         if (idx != -1) {
1574                 var pkgName = qClazzName.substring (0, idx);
1575                 var pkg = Clazz.declarePackage (pkgName);
1576                 var clazzName = qClazzName.substring (idx + 1);
1577                 if (pkg[clazzName]) {
1578                         // already defined! Should throw exception!
1579                         return pkg[clazzName];
1580                 }
1581                 pkg[clazzName] = clazzFun;
1582         } else {
1583                 if (window[qClazzName]) {
1584                         // already defined! Should throw exception!
1585                         return window[qClazzName];
1586                 }
1587                 Clazz.setGlobal(qClazzName, clazzFun);
1588         }
1589         Clazz.decorateAsType(clazzFun, qClazzName, clazzParent, interfacez);
1590         /*# {$no.javascript.support} >>x #*/
1591         var iFun = Clazz._innerFunctions;
1592         clazzFun.defineMethod = iFun.defineMethod;
1593         clazzFun.defineStaticMethod = iFun.defineStaticMethod;
1594         clazzFun.makeConstructor = iFun.makeConstructor;
1595         /*# x<< #*/
1596         return clazzFun;
1597 };
1598
1599 var isSafari = (navigator.userAgent.indexOf ("Safari") != -1);
1600 var isSafari4Plus = false;
1601 if (isSafari) {
1602         var ua = navigator.userAgent;
1603         var verIdx = ua.indexOf("Version/");
1604         if (verIdx  != -1) {
1605                 var verStr = ua.substring(verIdx + 8);
1606                 var verNumber = parseFloat(verStr);
1607                 isSafari4Plus = verNumber >= 4.0;
1608         }
1609 }
1610
1611 /* public */
1612 Clazz.instantialize = function (objThis, args) {
1613
1614
1615         if (args && args.length == 1 && args[0] 
1616                         && args[0] instanceof args4InheritClass) {
1617                 return;
1618         }
1619         if (objThis instanceof Number) {
1620                 objThis.valueOf = function () {
1621                         return this;
1622                 };
1623         }
1624         if (isSafari4Plus) { // Fix bug of Safari 4.0+'s over-optimization
1625                 var argsClone = [];
1626                 for (var k = 0; k < args.length; k++) {
1627                         argsClone[k] = args[k];
1628                 }
1629                 args = argsClone;
1630         }
1631
1632         var c = objThis.construct;
1633         if (c) {
1634                 if (!objThis.con$truct) { // no need to init fields
1635                         c.apply (objThis, args);
1636                 } else if (!objThis.getClass ().superClazz) { // the base class
1637                         objThis.con$truct.apply (objThis, []);
1638                         c.apply (objThis, args);
1639                 } else if ((c.claxxOwner 
1640                                 && c.claxxOwner === objThis.getClass ())
1641                                 || (c.stacks 
1642                                 && c.stacks[c.stacks.length - 1] == objThis.getClass ())) {
1643                         /*
1644                          * This #construct is defined by this class itself.
1645                          * #construct will call Clazz.superConstructor, which will
1646                          * call #con$truct back
1647                          */
1648                         c.apply (objThis, args);
1649                 } else { // constructor is a super constructor
1650                         if (c.claxxOwner && !c.claxxOwner.superClazz 
1651                                                 && c.claxxOwner.con$truct) {
1652                                 c.claxxOwner.con$truct.apply (objThis, []);
1653                         } else if (c.stacks && c.stacks.length == 1
1654                                         && !c.stacks[0].superClazz) {
1655                                 c.stacks[0].con$truct.apply (objThis, []);
1656                         }
1657                         c.apply (objThis, args);
1658                         objThis.con$truct.apply (objThis, []);
1659                 }
1660         } else if (objThis.con$truct) {
1661                 objThis.con$truct.apply (objThis, []);
1662         }
1663 };
1664
1665 /**
1666  * Once there are other methods registered to the Function.prototype, 
1667  * those method names should be add to the following Array.
1668  */
1669 /*
1670  * static final member of interface may be a class, which may
1671  * be function.
1672  */
1673 /* protected */
1674 Clazz.innerFunctionNames = [
1675         "isInstance", "equals", "hashCode", /*"toString",*/ "getName", "getCanonicalName", "getClassLoader", "getResource", "getResourceAsStream" /*# {$no.javascript.support} >>x #*/, "defineMethod", "defineStaticMethod",
1676         "makeConstructor" /*# x<< #*/
1677 ];
1678
1679 /*
1680  * Static methods
1681  */
1682 Clazz._innerFunctions = {
1683         /*
1684          * Similar to Object#equals
1685          */
1686    
1687   isInstance: function(c) {
1688     return Clazz.instanceOf(c, this);
1689   },
1690   
1691         equals : function (aFun) {
1692                 return this === aFun;
1693         },
1694
1695         hashCode : function () {
1696                 return this.getName ().hashCode ();
1697         },
1698
1699         toString : function () {
1700                 return "class " + this.getName ();
1701         },
1702
1703         /*
1704          * Similar to Class#getName
1705          */
1706         getName : function () {
1707                 return Clazz.getClassName (this, true);
1708         },
1709         getCanonicalName : function () {
1710                 return this.__CLASS_NAME__;
1711         },
1712         getClassLoader : function () {
1713                 var clazzName = this.__CLASS_NAME__;
1714                 var baseFolder = Clazz._Loader.getClasspathFor(clazzName);
1715                 var x = baseFolder.lastIndexOf (clazzName.replace (/\./g, "/"));
1716                 if (x != -1) {
1717                         baseFolder = baseFolder.substring (0, x);
1718                 } else {
1719                         baseFolder = Clazz._Loader.getClasspathFor(clazzName, true);
1720                 }
1721                 var loader = Clazz._Loader.requireLoaderByBase(baseFolder);
1722                 loader.getResourceAsStream = Clazz._innerFunctions.getResourceAsStream;
1723                 loader.getResource = Clazz._innerFunctions.getResource; // BH
1724                 return loader;
1725         },
1726
1727         getResource : function(name) {
1728                 var stream = this.getResourceAsStream(name);
1729     return (stream ? stream.url : null);
1730         },
1731
1732         getResourceAsStream : function (name) {
1733                 if (!name)
1734                         return null;
1735                 name = name.replace (/\\/g, '/');
1736                 var baseFolder = null;
1737     var fname = name;
1738                 var clazzName = this.__CLASS_NAME__;
1739                 if (arguments.length == 2 && name.indexOf ('/') != 0) { // additional argument
1740                         name = "/" + name;
1741                 }
1742                 if (name.indexOf ('/') == 0) {
1743                         //is.url = name.substring (1);
1744                         if (arguments.length == 2) { // additional argument
1745                                 baseFolder = arguments[1];
1746                                 if (!baseFolder)
1747                                         baseFolder = Clazz.binaryFolders[0];
1748                         } else if (Clazz._Loader) {
1749                                 baseFolder = Clazz._Loader.getClasspathFor(clazzName, true);
1750                         }
1751                         if (!baseFolder) {
1752                                 fname = name.substring (1);
1753                         } else {
1754                                 baseFolder = baseFolder.replace (/\\/g, '/');
1755                                 var length = baseFolder.length;
1756                                 var lastChar = baseFolder.charAt (length - 1);
1757                                 if (lastChar != '/') {
1758                                         baseFolder += "/";
1759                                 }
1760                                 fname = baseFolder + name.substring (1);
1761                         }
1762                 } else {
1763                         if (this.base) {
1764                                 baseFolder = this.base;
1765                         } else if (Clazz._Loader) {
1766                                 baseFolder = Clazz._Loader.getClasspathFor(clazzName);
1767                                 var x = baseFolder.lastIndexOf (clazzName.replace (/\./g, "/"));
1768                                 if (x != -1) {
1769                                         baseFolder = baseFolder.substring (0, x);
1770                                 } else {
1771                                         //baseFolder = null;
1772                                         var y = -1;
1773                                         if (baseFolder.indexOf (".z.js") == baseFolder.length - 5
1774                                                         && (y = baseFolder.lastIndexOf ("/")) != -1) {
1775                                                 baseFolder = baseFolder.substring (0, y + 1);
1776                                                 var pkgs = clazzName.split (/\./);
1777                                                 for (var k = 1; k < pkgs.length; k++) {
1778                                                         var pkgURL = "/";
1779                                                         for (var j = 0; j < k; j++) {
1780                                                                 pkgURL += pkgs[j] + "/";
1781                                                         }
1782                                                         if (pkgURL.length > baseFolder.length) {
1783                                                                 break;
1784                                                         }
1785                                                         if (baseFolder.indexOf (pkgURL) == baseFolder.length - pkgURL.length) {
1786                                                                 baseFolder = baseFolder.substring (0, baseFolder.length - pkgURL.length + 1);
1787                                                                 break;
1788                                                         }
1789                                                 }
1790                                         } else {
1791                                                 baseFolder = Clazz._Loader.getClasspathFor(clazzName, true);
1792                                         }
1793                                 }
1794                         } else {
1795                                 var bins = Clazz.binaryFolders;
1796                                 if (bins && bins.length) {
1797                                         baseFolder = bins[0];
1798                                 }
1799                         }
1800                         if (!baseFolder)
1801                                 baseFolder = "j2s/";
1802                         baseFolder = baseFolder.replace (/\\/g, '/');
1803                         var length = baseFolder.length;
1804                         var lastChar = baseFolder.charAt (length - 1);
1805                         if (lastChar != '/') {
1806                                 baseFolder += "/";
1807                         }
1808                         if (this.base) {
1809                                 fname = baseFolder + name;
1810                         } else {
1811                                 var idx = clazzName.lastIndexOf ('.');
1812                                 if (idx == -1 || this.base) {
1813                                         fname = baseFolder + name;
1814                                 } else {
1815                                         fname = baseFolder + clazzName.substring (0, idx)
1816                                                         .replace (/\./g, '/') +  "/" + name;
1817                                 }
1818                         }            
1819                 }
1820     var url = null;
1821     try {
1822       if (fname.indexOf(":/") < 0) {
1823         var d = document.location.href.split("?")[0].split("/");
1824         d[d.length - 1] = fname;
1825         fname = d.join("/");
1826       }
1827       url = new java.net.URL(fname);
1828     } catch (e) {
1829     }
1830                 var data = (url == null ? null : Jmol._getFileData(fname.toString()));
1831     if (!data || data == "error" || data.indexOf("[Exception") == 0)
1832       return null;
1833     var bytes = new java.lang.String(data).getBytes();      
1834     var is = new java.io.BufferedInputStream ( new java.io.ByteArrayInputStream (bytes)); 
1835     is.url = url;
1836                 return is;
1837         }/*# {$no.javascript.support} >>x #*/,
1838
1839         /*
1840          * For JavaScript programmers
1841          */
1842         defineMethod : function (methodName, funBody, paramTypes) {
1843                 Clazz.defineMethod (this, methodName, funBody, paramTypes);
1844         },
1845
1846         /*
1847          * For JavaScript programmers
1848          */
1849         defineStaticMethod : function (methodName, funBody, paramTypes) {
1850                 Clazz.defineMethod (this, methodName, funBody, paramTypes);
1851                 this[methodName] = this.prototype[methodName];
1852         },
1853
1854         /*
1855          * For JavaScript programmers
1856          */
1857         makeConstructor : function (funBody, paramTypes) {
1858                 Clazz.makeConstructor (this, funBody, paramTypes);
1859         }
1860         /*# x<< #*/
1861 };
1862
1863
1864 var cStack = [];
1865
1866 /**
1867  * BH: I would like to be able to remove "self.c$" here, but that is tricky.
1868  */
1869   
1870 Clazz.pu$h = function (c) {
1871   c || (c = self.c$); // old style
1872         c && cStack.push(c);
1873 };
1874
1875 Clazz.p0p = function () {
1876         return cStack.pop();
1877 };
1878
1879 /* protected */
1880 Clazz.decorateAsClass = function (clazzFun, prefix, name, clazzParent, 
1881                 interfacez, parentClazzInstance, _decorateAsClass) {
1882     
1883         var prefixName = null;
1884         if (prefix) {
1885                 prefixName = prefix.__PKG_NAME__;
1886                 if (!prefixName)
1887                         prefixName = prefix.__CLASS_NAME__;      
1888         }
1889         var qName = (prefixName ? prefixName + "." : "") + name;
1890   
1891     if (Clazz._Loader._classPending[qName]) {
1892       delete Clazz._Loader._classPending[qName];
1893       Clazz._Loader._classCountOK++;
1894       Clazz._Loader._classCountPending--;
1895     }
1896   if (Clazz._Loader && Clazz._Loader._checkLoad) {
1897     System.out.println("decorating class " + prefixName + "." + name);
1898   }
1899         var cf = Clazz.unloadedClasses[qName];
1900         if (cf) {
1901                 clazzFun = cf;
1902         }
1903         var qName = null;
1904         decorateFunction(clazzFun, prefix, name);
1905         if (parentClazzInstance) {
1906                 Clazz.inheritClass (clazzFun, clazzParent, parentClazzInstance);
1907         } else if (clazzParent) {
1908                 Clazz.inheritClass (clazzFun, clazzParent);
1909         }
1910         if (interfacez) {
1911                 Clazz.implementOf (clazzFun, interfacez);
1912         }
1913         return clazzFun;
1914 };
1915
1916 /* private */
1917 var decorateFunction = function (clazzFun, prefix, name, _decorateFunction) {
1918         var qName;
1919         if (!prefix) {
1920                 // e.g. Clazz.declareInterface (null, "ICorePlugin", org.eclipse.ui.IPlugin);
1921                 qName = name;
1922                 Clazz.setGlobal(name, clazzFun);
1923         } else if (prefix.__PKG_NAME__) {
1924                 // e.g. Clazz.declareInterface (org.eclipse.ui, "ICorePlugin", org.eclipse.ui.IPlugin);
1925                 qName = prefix.__PKG_NAME__ + "." + name;
1926                 prefix[name] = clazzFun;
1927                 if (prefix === java.lang)
1928                         Clazz.setGlobal(name, clazzFun);
1929         } else {
1930                 // e.g. Clazz.declareInterface (org.eclipse.ui.Plugin, "ICorePlugin", org.eclipse.ui.IPlugin);
1931                 qName = prefix.__CLASS_NAME__ + "." + name;
1932                 prefix[name] = clazzFun;
1933         }
1934         Clazz.extendJO(clazzFun, qName);
1935         var inF = Clazz.innerFunctionNames;
1936         for (var i = 0; i < inF.length; i++) {
1937                 clazzFun[inF[i]] = Clazz._innerFunctions[inF[i]];
1938         }
1939
1940         if (Clazz._Loader) 
1941     Clazz._Loader.updateNodeForFunctionDecoration(qName);
1942 };
1943
1944 /* protected */
1945 Clazz.declareInterface = function (prefix, name, interfacez, _declareInterface) {
1946         var clazzFun = function () {};
1947         decorateFunction(clazzFun, prefix, name);
1948         if (interfacez) {
1949                 Clazz.implementOf (clazzFun, interfacez);
1950         }
1951         return clazzFun;
1952 };
1953
1954 /* public */
1955 Clazz.declareType = function (prefix, name, clazzParent, interfacez, 
1956                 parentClazzInstance, _declareType) {
1957         var f = function () {
1958                 Clazz.instantialize (this, arguments);
1959         };
1960         return Clazz.decorateAsClass (f, prefix, name, clazzParent, interfacez, 
1961                         parentClazzInstance);
1962 };
1963
1964 /* public */
1965 Clazz.declareAnonymous = function (prefix, name, clazzParent, interfacez, 
1966                 parentClazzInstance, _declareAnonymous) {
1967         var f = function () {
1968                 Clazz.prepareCallback(this, arguments);
1969                 Clazz.instantialize (this, arguments);
1970         };
1971         return Clazz.decorateAsClass (f, prefix, name, clazzParent, interfacez, 
1972                         parentClazzInstance);
1973 };
1974
1975 /* public */
1976 Clazz.decorateAsType = function (clazzFun, qClazzName, clazzParent, 
1977                 interfacez, parentClazzInstance, inheritClazzFuns, _decorateAsType) {
1978         Clazz.extendJO(clazzFun, qClazzName);
1979         clazzFun.equals = Clazz._innerFunctions.equals;
1980         clazzFun.getName = Clazz._innerFunctions.getName;
1981         if (inheritClazzFuns) {
1982                 for (var i = 0; i < Clazz.innerFunctionNames.length; i++) {
1983                         var methodName = Clazz.innerFunctionNames[i];
1984                         clazzFun[methodName] = Clazz._innerFunctions[methodName];
1985                 }
1986         }
1987         if (parentClazzInstance) {
1988                 Clazz.inheritClass (clazzFun, clazzParent, parentClazzInstance);
1989         } else if (clazzParent) {
1990                 Clazz.inheritClass (clazzFun, clazzParent);
1991         }
1992         if (interfacez) {
1993                 Clazz.implementOf (clazzFun, interfacez);
1994         }
1995         return clazzFun;
1996 };
1997
1998
1999 ////////////////////////// default package declarations ////////////////////////
2000
2001 /* sgurin: preserve Number.prototype.toString */
2002 Number.prototype._numberToString=Number.prototype.toString;
2003
2004
2005 Clazz.declarePackage ("java.io");
2006 //Clazz.declarePackage ("java.lang");
2007 Clazz.declarePackage ("java.lang.annotation"); // java.lang
2008 Clazz.declarePackage ("java.lang.instrument"); // java.lang
2009 Clazz.declarePackage ("java.lang.management"); // java.lang
2010 Clazz.declarePackage ("java.lang.reflect"); // java.lang
2011 Clazz.declarePackage ("java.lang.ref");  // java.lang.ref
2012 java.lang.ref.reflect = java.lang.reflect;
2013 Clazz.declarePackage ("java.util");
2014 //var reflect = Clazz.declarePackage ("java.lang.reflect");
2015 Clazz.declarePackage ("java.security");
2016
2017
2018 /*
2019  * Consider these interfaces are basic!
2020  */
2021 Clazz.declareInterface (java.io,"Closeable");
2022 Clazz.declareInterface (java.io,"DataInput");
2023 Clazz.declareInterface (java.io,"DataOutput");
2024 Clazz.declareInterface (java.io,"Externalizable");
2025 Clazz.declareInterface (java.io,"Flushable");
2026 Clazz.declareInterface (java.io,"Serializable");
2027 Clazz.declareInterface (java.lang,"Iterable");
2028 Clazz.declareInterface (java.lang,"CharSequence");
2029 Clazz.declareInterface (java.lang,"Cloneable");
2030 Clazz.declareInterface (java.lang,"Appendable");
2031 Clazz.declareInterface (java.lang,"Comparable");
2032 Clazz.declareInterface (java.lang,"Runnable");
2033 Clazz.declareInterface (java.util,"Comparator");
2034
2035 java.lang.ClassLoader = {
2036         __CLASS_NAME__ : "ClassLoader"
2037 };
2038
2039 /******************************************************************************
2040  * Copyright (c) 2007 java2script.org and others.
2041  * All rights reserved. This program and the accompanying materials
2042  * are made available under the terms of the Eclipse Public License v1.0
2043  * which accompanies this distribution, and is available at
2044  * http://www.eclipse.org/legal/epl-v10.html
2045  *
2046  * Contributors:
2047  *     Zhou Renjian - initial API and implementation
2048  *****************************************************************************/
2049 /*******
2050  * @author zhou renjian
2051  * @create March 10, 2006
2052  *******/
2053
2054 /**
2055  * Once ClassExt.js is part of Class.js.
2056  * In order to make the Class.js as small as possible, part of its content
2057  * is moved into this ClassExt.js.
2058  *
2059  * See also http://j2s.sourceforge.net/j2sclazz/
2060  */
2061  
2062 /**
2063  * Clazz.MethodNotFoundException is used to notify the developer about calling
2064  * methods with incorrect parameters.
2065  */
2066 /* protected */
2067 // Override the Clazz.MethodNotFoundException in Class.js to give details
2068 var newMethodNotFoundException = function (obj, clazz, method, params) {
2069         var paramStr = "";
2070         if (params) {
2071                 paramStr = params.substring (1).replace (/\\/g, ",");
2072         }
2073         var leadingStr = "";
2074         if (method && method != "construct") {
2075                 leadingStr = "Method";
2076         } else {
2077                 leadingStr = "Constructor";
2078         }
2079         var message = leadingStr + " " + Clazz.getClassName (clazz, true) + "." 
2080                                         + method + "(" + paramStr + ") is not found!";
2081   throw new java.lang.NoSuchMethodException(message);        
2082 };
2083
2084 /**
2085  * Prepare "callback" for instance of anonymous Class.
2086  * For example for the callback:
2087  *     this.callbacks.MyEditor.sayHello();
2088  *     
2089  * This is specifically for inner classes that are referring to 
2090  * outer class methods and fields.   
2091  *
2092  * @param objThis the host object for callback
2093  * @param args arguments object. args[0] will be classThisObj -- the "this"
2094  * object to be hooked
2095  * 
2096  * Attention: parameters should not be null!
2097  */
2098 /* protected */
2099 Clazz.prepareCallback = function (innerObj, args) {
2100         var outerObj = args[0];
2101         var cbName = "b$"; // "callbacks";
2102         if (innerObj && outerObj && outerObj !== window) {
2103                 var className = Clazz.getClassName(outerObj, true);             
2104                 var obs = {};
2105                 if (innerObj[cbName]) // must make a copy!
2106                         for (var s in innerObj[cbName])
2107                                 obs[s] = innerObj[cbName][s];
2108                 innerObj[cbName] = obs;
2109                 
2110                 /*
2111                  * TODO: the following line is SWT-specific! Try to move it out!
2112                  */
2113                 //                      obs[className.replace (/org\.eclipse\.swt\./, "$wt.")] = outerObj;
2114
2115         // all references to outer class and its superclass objects must be here as well
2116                 obs[className] = outerObj;
2117                 var clazz = Clazz.getClass(outerObj);
2118                 while (clazz.superClazz) {
2119                         clazz = clazz.superClazz;
2120                         /*
2121                          * TODO: the following line is SWT-specific! Try to move it out!
2122                          */
2123                         //                              obs[Clazz.getClassName (clazz, true)
2124                         //                                              .replace (/org\.eclipse\.swt\./, "$wt.")] = outerObj;
2125                         obs[Clazz.getClassName(clazz, true)] = outerObj;
2126                 }
2127                 var cbs = outerObj[cbName];
2128                 if (cbs)
2129                         for (var s in cbs)
2130                                 obs[s] = cbs[s];
2131         }
2132         // remove "this" argument
2133         // note that args is an instance of arguments -- NOT an array; does not have the .shift() method!
2134         for (var i = 0; i < args.length - 1; i++)
2135                 args[i] = args[i + 1];
2136         args.length--;
2137 };
2138
2139 /**
2140  * Construct instance of the given inner class.
2141  *
2142  * @param classInner given inner class, alway with name like "*$*"
2143  * @param innerObj this instance which can be used to call back.
2144  * @param finalVars final variables which the inner class may use
2145  * @return the constructed object
2146  *
2147  * @see Clazz#cloneFinals
2148  */
2149 /* public */
2150 Clazz.innerTypeInstance = function (clazzInner, innerObj, finalVars) {
2151         if (!clazzInner)
2152                 clazzInner = arguments.callee.caller;
2153         var obj;
2154         if (finalVars || innerObj.$finals) {
2155                         obj = new clazzInner(innerObj, Clazz.inheritArgs);
2156                 // f$ is short for the once choosen "$finals"
2157                 if (finalVars) {
2158                         if (innerObj.f$) {
2159                                 var o = {};
2160                                 for (var attr in innerObj.f$)
2161                                         o[attr] = innerObj.f$[attr];
2162                                 for (var attr in finalVars)
2163                                         o[attr] = finalVars[attr];
2164                                 obj.f$ = o;
2165                         } else {
2166                                 obj.f$ = finalVars;
2167                         }
2168                 } else if (innerObj.f$) {
2169                         obj.f$ = innerObj.f$;
2170                 }
2171         } else {
2172                 switch (arguments.length) {
2173                 case 3:
2174                         return new clazzInner(innerObj);
2175                 case 4:
2176                         return (innerObj.__CLASS_NAME__ == clazzInner.__CLASS_NAME__
2177                                         && arguments[3] === Clazz.inheritArgs ? innerObj : new clazzInner(innerObj, arguments[3]));
2178                 case 5:
2179                         return new clazzInner(innerObj, arguments[3], arguments[4]);
2180                 case 6:
2181                         return new clazzInner(innerObj, arguments[3], arguments[4], 
2182                                         arguments[5]);
2183                 case 7:
2184                         return new clazzInner(innerObj, arguments[3], arguments[4], 
2185                                         arguments[5], arguments[6]);
2186                 case 8:
2187                         return new clazzInner(innerObj, arguments[3], arguments[4], 
2188                                         arguments[5], arguments[6], arguments[7]);
2189                 case 9:
2190                         return new clazzInner(innerObj, arguments[3], arguments[4], 
2191                                         arguments[5], arguments[6], arguments[7], arguments[8]);
2192                 case 10:
2193                         return new clazzInner(innerObj, arguments[3], arguments[4], 
2194                                         arguments[5], arguments[6], arguments[7], arguments[8],
2195                                         arguments[9]);
2196                 default:
2197                         //Should construct instance manually.
2198                         obj = new clazzInner(innerObj, Clazz.inheritArgs);
2199                         break;
2200                 }
2201         }
2202         var n = arguments.length - 3;
2203         var args = new Array(n);
2204         for (var i = n; --i >= 0;)
2205                 args[i] = arguments[i + 3];
2206         Clazz.instantialize(obj, args);
2207         return obj;
2208 };
2209
2210 /**
2211  * Clone variables whose modifier is "final".
2212  * Usage: var o = Clazz.cloneFinals ("name", name, "age", age);
2213  *
2214  * @return Object with all final variables
2215  */
2216 /* public */
2217 Clazz.cloneFinals = function () {
2218         var o = {};
2219         var len = arguments.length / 2;
2220         for (var i = len; --i >= 0;)
2221                 o[arguments[i + i]] = arguments[i + i + 1];
2222         return o;
2223 };
2224
2225 /* public */
2226 Clazz.isClassDefined = Clazz.isDefinedClass = function(clazzName) {
2227         if (!clazzName) 
2228                 return false;           /* consider null or empty name as non-defined class */
2229         if (Clazz.allClasses[clazzName])
2230                 return true;
2231         var pkgFrags = clazzName.split (/\./);
2232         var pkg = null;
2233         for (var i = 0; i < pkgFrags.length; i++)
2234                 if (!(pkg = (pkg ? pkg[pkgFrags[i]] : Clazz.allPackage[pkgFrags[0]]))) {
2235                         return false;
2236     }
2237   return (pkg && (Clazz.allClasses[clazzName] = true));
2238 };
2239 /**
2240  * Define the enum constant.
2241  * @param classEnum enum type
2242  * @param enumName enum constant
2243  * @param enumOrdinal enum ordinal
2244  * @param initialParams enum constant constructor parameters
2245  * @return return defined enum constant
2246  */
2247 /* public */
2248 Clazz.defineEnumConstant = function (clazzEnum, enumName, enumOrdinal, initialParams, clazzEnumExt) {
2249         var o = (clazzEnumExt ? new clazzEnumExt() : new clazzEnum());
2250         // BH avoids unnecessary calls to SAEM
2251         o.$name = enumName;
2252         o.$ordinal = enumOrdinal;
2253         //Clazz.superConstructor (o, clazzEnum, [enumName, enumOrdinal]);
2254         if (initialParams && initialParams.length)
2255                 o.construct.apply (o, initialParams);
2256         clazzEnum[enumName] = o;
2257         clazzEnum.prototype[enumName] = o;
2258         if (!clazzEnum["$ values"]) {         // BH added
2259                 clazzEnum["$ values"] = []          // BH added
2260                 clazzEnum.values = function() {     // BH added
2261                         return this["$ values"];          // BH added
2262                 };                                  // BH added
2263         }
2264         clazzEnum["$ values"].push(o);
2265         return o;
2266 };
2267
2268 //////// (int) conversions //////////
2269
2270 Clazz.floatToInt = function (x) {
2271         return x < 0 ? Math.ceil(x) : Math.floor(x);
2272 };
2273
2274 Clazz.floatToByte = Clazz.floatToShort = Clazz.floatToLong = Clazz.floatToInt;
2275 Clazz.doubleToByte = Clazz.doubleToShort = Clazz.doubleToLong = Clazz.doubleToInt = Clazz.floatToInt;
2276
2277 Clazz.floatToChar = function (x) {
2278         return String.fromCharCode (x < 0 ? Math.ceil(x) : Math.floor(x));
2279 };
2280
2281 Clazz.doubleToChar = Clazz.floatToChar;
2282
2283
2284
2285 ///////////////////////////////// Array additions //////////////////////////////
2286 //
2287 // BH: these are necessary for integer processing, especially
2288 //
2289 //
2290
2291 var getArrayClone = function(nbits) {
2292   return function() {
2293     var me = this;
2294     var n = me.length;
2295     var a = (nbits == 32 ? new Int32Array(n) : new Float64Array(n));
2296     for (var i = n; --i >= 0;)
2297       a[i] = me[i];
2298     return a; 
2299   }
2300 }
2301
2302 if (self.Int32Array && self.Int32Array != Array) {
2303         Clazz.haveInt32 = true;
2304         if (!Int32Array.prototype.sort)
2305                 Int32Array.prototype.sort = Array.prototype.sort
2306         if (!Int32Array.prototype.clone)
2307                 Int32Array.prototype.clone = getArrayClone(32);
2308 } else {
2309         Int32Array = function(n) {
2310                 if (!n) n = 0;
2311                 var b = new Array(n);
2312                 b.toString = function(){return "[object Int32Array]"}
2313                 for (var i = 0; i < n; i++)b[i] = 0
2314                 return b;
2315         }
2316         Clazz.haveInt32 = false;
2317         Int32Array.prototype.sort = Array.prototype.sort
2318         Int32Array.prototype.clone = getArrayClone(32);
2319         Int32Array.prototype.int32Fake = function(){};
2320 }
2321
2322 if (self.Float64Array && self.Float64Array != Array) {
2323         Clazz.haveFloat64 = true;
2324         if (!Float64Array.prototype.sort)
2325                 Float64Array.prototype.sort = Array.prototype.sort
2326         if (!Float64Array.prototype.clone)
2327                 Float64Array.prototype.clone = getArrayClone(64);
2328 } else {
2329         Clazz.haveFloat64 = false;
2330         Float64Array = function(n) {
2331                 if (!n) n = 0;
2332                 var b = new Array(n);
2333                 for (var i = 0; i < n; i++)b[i] = 0.0
2334                 return b;
2335         };
2336         Float64Array.prototype.sort = Array.prototype.sort
2337         Float64Array.prototype.clone = getArrayClone(64);
2338         Float64Array.prototype.float64Fake = function() {}; // "present"
2339         Float64Array.prototype.toString = function() {return "[object Float64Array]"};
2340 // Darn! Mozilla makes this a double, not a float. It's 64-bit.
2341 // and Safari 5.1 doesn't have Float64Array 
2342 }
2343
2344 /**
2345  * Make arrays.
2346  *
2347  * @return the created Array object
2348  */
2349 /* public */
2350 Clazz.newArray  = function () {
2351         if (arguments[0] instanceof Array) {
2352                 // recursive, from newArray(n,m,value)
2353                 // as newArray([m, value], newInt32Array)
2354                 var args = arguments[0];
2355                 var f = arguments[1];
2356         } else {
2357                 var args = arguments;
2358                 var f = Array;
2359         }
2360         var dim = args[0];
2361         if (typeof dim == "string") {
2362                 dim = dim.charCodeAt (0); // char
2363         }
2364         var len = args.length - 1;
2365         var val = args[len];
2366   switch (args.length) {
2367   case 0: // never
2368   case 1:
2369                 return []; // maybe never?
2370   case 2:
2371                 if (val == null)
2372                 return new Array(dim);
2373           if (f === true && Clazz.haveInt32) return new Int32Array(dim);
2374           if (f === false && Clazz.haveFloat64) return new Float64Array(dim);
2375                 var arr = (f === true ? new Int32Array() : f === false ? new Float64Array() : dim < 0 ? val : new Array(dim));
2376                 for (var i = dim; --i >= 0;)
2377                 arr[i] = val;
2378           return arr;
2379   default:
2380         var xargs = new Array (len);
2381         for (var i = 0; i < len; i++) {
2382                 xargs[i] = args[i + 1];
2383         }
2384         var arr = new Array (dim);
2385         if (val == null || val >= 0 || len > 2)
2386                 for (var i = 0; i < dim; i++) {
2387                 // Call recursively!
2388                         arr[i] = Clazz.newArray (xargs, f);
2389                 }
2390         return arr;
2391         }
2392 };
2393
2394 Clazz.newArray32 = function(args, isInt32) {
2395         var dim = args[0];
2396         if (typeof dim == "string")
2397                 dim = dim.charCodeAt (0); // char
2398         var len = args.length - 1;
2399         var val = args[len];
2400         switch (args.length) {
2401         case 0:
2402         case 1:  
2403                 alert ("ERROR IN newArray32 -- args.length < 2");
2404                 return new Array(0);
2405         case 2:
2406     var isDefined = (dim < 0);
2407     if (isDefined)
2408       dim = val.length;
2409     var a = (val < 0 ? new Array(dim) : isInt32 ? new Int32Array(dim) : new Float64Array(dim));
2410     if (isDefined)
2411       for (var i = dim; --i >= 0;)
2412         a[i] = val[i];
2413     return a;
2414         }
2415         var xargs = new Array(len);
2416         for (var i = len; --i >= 0;) {
2417                 xargs[i] = args[i + 1];
2418         }
2419         var arr = new Array (dim);
2420         for (var i = 0; i < dim; i++) {
2421                 // Call newArray referencing this array type
2422                 // only for the final iteration, and only if val === 0
2423                 arr[i] = Clazz.newArray (xargs, isInt32);
2424         }
2425         return arr;
2426 };
2427
2428
2429 /**
2430  * Make arrays.
2431  *
2432  * @return the created Array object
2433  */
2434 /* public */
2435 Clazz.newInt32Array  = function () {
2436         return Clazz.newArray32(arguments, true);
2437 }
2438
2439 /**
2440  * Make arrays.
2441  *
2442  * @return the created Array object
2443  */
2444 /* public */
2445 Clazz.newFloat64Array  = function () {
2446         return Clazz.newArray32(arguments, false);
2447 }
2448
2449 Clazz.newFloatArray = Clazz.newDoubleArray = Clazz.newFloat64Array;
2450 Clazz.newIntArray = Clazz.newLongArray = Clazz.newShortArray = Clazz.newByteArray = Clazz.newInt32Array;
2451 Clazz.newCharArray = Clazz.newBooleanArray = Clazz.newArray;
2452
2453 //$_AI=Clazz.newIntArray;
2454 //$_AF=Clazz.newFloatArray;
2455 //$_AD=Clazz.newDoubleArray;
2456 //$_AL=Clazz.newLongArray;
2457 //$_AS=Clazz.newShortArray;
2458 //$_AB=Clazz.newByteArray;
2459 //$_AC=Clazz.newCharArray;
2460 //$_Ab=Clazz.newBooleanArray;
2461
2462
2463 var arrayIs = function(a, what) {
2464         // for some reason, Number.constructor.toString() now gives "too much recursion"
2465         return a.constructor && a.constructor != Number && a.constructor.toString().indexOf(what) >= 0
2466 }
2467
2468 Clazz.isAS = function(a) { // just checking first parameter
2469         return (a && typeof a == "object" && arrayIs(a, " Array") && (typeof a[0] == "string" || typeof a[0] == "undefined"));
2470 }
2471
2472 Clazz.isASS = function(a) {
2473         return (a && typeof a == "object" && Clazz.isAS(a[0]));
2474 }
2475
2476 Clazz.isAP = function(a) {
2477         return (a && Clazz.getClassName(a[0]) == "JU.P3");
2478 }
2479
2480 Clazz.isAI = function(a) {
2481         return (a && typeof a == "object" && (Clazz.haveInt32 ? arrayIs(a, "Int32Array") : a.int32Fake ? true : false));
2482 }
2483
2484 Clazz.isAII = function(a) { // assumes non-null a[0]
2485         return (a && typeof a == "object" && Clazz.isAI(a[0]));
2486 }
2487
2488 Clazz.isAF = function(a) {
2489         return (a && typeof a == "object" && (Clazz.haveFloat64 ? arrayIs(a, "Float64Array") : a.float64Fake ? true : false));
2490 }
2491
2492 Clazz.isAFF = function(a) { // assumes non-null a[0]
2493         return (a && typeof a == "object" && Clazz.isAF(a[0]));
2494 }
2495
2496 Clazz.isAFFF = function(a) { // assumes non-null a[0]
2497         return (a && typeof a == "object" && Clazz.isAFF(a[0]));
2498 }
2499
2500 Clazz.isAFloat = function(a) { // just checking first parameter
2501         return (a && typeof a == "object" && arrayIs(a, " Array") && Clazz.instanceOf(a[0], Float));
2502 }
2503
2504
2505 /**
2506  * Make the RunnableCompatiability instance as a JavaScript function.
2507  *
2508  * @param jsr Instance of RunnableCompatiability
2509  * @return JavaScript function instance represents the method run of jsr.
2510  */
2511 /* public */
2512 /*
2513 Clazz.makeFunction = function (jsr) {
2514 // never used in Jmol -- called by Enum, but not accessible to it -- part of SWT
2515         return function(e) {
2516                 if (!e)
2517                         e = window.event;
2518                 if (jsr.setEvent)
2519                         jsr.setEvent(e);
2520                 jsr.run();
2521                 switch (jsr.returnSet) {
2522                 case 1: 
2523                         return jsr.returnNumber;
2524                 case 2:
2525                         return jsr.returnBoolean;
2526                 case 3:
2527                         return jsr.returnObject;
2528                 }
2529         };
2530 };
2531 */
2532
2533 /* protected */
2534 Clazz.defineStatics = function (clazz) {
2535         for (var j = arguments.length, i = (j - 1) / 2; --i >= 0;) {
2536                 var val = arguments[--j]
2537                 var name = arguments[--j];
2538                 clazz[name] = clazz.prototype[name] = val;
2539         }
2540 };
2541
2542 /* public */
2543 Clazz.prepareFields = function (clazz, fieldsFun) {
2544         var stacks = [];
2545         if (clazz.con$truct) {
2546                 var ss = clazz.con$truct.stacks;
2547                 var idx = 0;//clazz.con$truct.index;
2548                 for (var i = idx; i < ss.length; i++) {
2549                         stacks[i] = ss[i];
2550                 }
2551         }
2552         addProto(clazz.prototype, "con$truct", clazz.con$truct = function () {
2553                 var stacks = arguments.callee.stacks;
2554                 if (stacks) {
2555                         for (var i = 0; i < stacks.length; i++) {
2556                                 stacks[i].apply (this, []);
2557                         }
2558                 }
2559         });
2560         stacks.push(fieldsFun);
2561         clazz.con$truct.stacks = stacks;
2562         clazz.con$truct.index = 0;
2563 };
2564
2565 /*
2566  * Serialize those public or protected fields in class 
2567  * net.sf.j2s.ajax.SimpleSerializable.
2568  */
2569 /* protected */
2570 /*
2571 Clazz.registerSerializableFields = function (clazz) {
2572         var args = arguments;
2573         var length = args.length;
2574         var newArr = [];
2575         if (clazz.declared$Fields) {
2576                 for (var i = 0; i < clazz.declared$Fields.length; i++) {
2577                         newArr[i] = clazz.declared$Fields[i];
2578                 }
2579         }
2580         clazz.declared$Fields = newArr;
2581
2582         if (length > 0 && length % 2 == 1) {
2583                 var fs = clazz.declared$Fields;
2584                 var n = (length - 1) / 2;
2585                 for (var i = 1; i <= n; i++) {
2586                         var o = { name : args[i + i - 1], type : args[i + i] };
2587                         var existed = false;
2588                         for (var j = 0; j < fs.length; j++) {
2589                                 if (fs[j].name == o.name) { // reloaded classes
2590                                         fs[j].type = o.type; // update type
2591                                         existed = true;
2592                                         break;
2593                                 }
2594                         }
2595                         if (!existed)
2596                                 fs.push(o);
2597                 }
2598         }
2599 };
2600 */
2601 /*
2602  * Get the caller method for those methods that are wrapped by 
2603  * Clazz.searchAndExecuteMethod.
2604  *
2605  * @param args caller method's arguments
2606  * @return caller method, null if there is not wrapped by 
2607  * Clazz.searchAndExecuteMethod or is called directly.
2608  */
2609 /* protected */
2610 /*
2611 Clazz.getMixedCallerMethod = function (args) {
2612         var o = {};
2613         var argc = args.callee.caller; // tryToSearchAndExecute
2614         if (argc && argc !== tryToSearchAndExecute) // inherited method's apply
2615                 argc = argc.arguments.callee.caller;
2616         if (argc !== tryToSearchAndExecute
2617                 || (argc = argc.arguments.callee.caller) !== Clazz.searchAndExecuteMethod)
2618                 return null;
2619         o.claxxRef = argc.arguments[1];
2620         o.fxName = argc.arguments[2];
2621         o.paramTypes = Clazz.getParamsType(argc.arguments[3]);  
2622         argc = argc.arguments.callee.caller // Clazz.generateDelegatingMethod 
2623                                         && argc.arguments.callee.caller; // the private method's caller
2624         if (!argc)
2625                 return null;
2626         o.caller = argc;
2627         return o;
2628 };
2629 */
2630 /* BH -- The issue here is a subclass calling its private method FOO when
2631  *       there is also a private method of the same name in its super class.
2632  *       This can ALWAYS be avoided and, one could argue, is bad 
2633  *       program design anyway. In Jmol, the presence of this possibility
2634  *       creates over 8000 references to the global $fx, which was only
2635  *       checked in a few rare cases. We can then also remove $fz references.
2636  *         
2637  */
2638
2639 /*
2640  * Check and return super private method.
2641  * In order make private methods be executed correctly, some extra javascript
2642  * must be inserted into the beggining of the method body of the non-private 
2643  * methods that with the same method signature as following:
2644  * <code>
2645  *                      var $private = Clazz.checkPrivateMethod (arguments);
2646  *                      if ($private) {
2647  *                              return $private.apply (this, arguments);
2648  *                      }
2649  * </code>
2650  * Be cautious about this. The above codes should be insert by Java2Script
2651  * compiler or with double checks to make sure things work correctly.
2652  *
2653  * @param args caller method's arguments
2654  * @return private method if there are private method fitted for the current 
2655  * calling environment
2656  */
2657 /* public */
2658
2659 Clazz.checkPrivateMethod = function () {
2660   // get both this one and the one calling it
2661   me = arguments.callee.caller;
2662   caller = arguments.callee.caller.caller;
2663   var stack = me.stacks;
2664   // if their classes are the same, no issue
2665   var mySig = "\\" + Clazz.getParamsType(arguments[0]).join("\\")
2666   if (!me.privateNote) {
2667     me.privateNote = "You are seeing this note because the method " 
2668     + me.exName + mySig + " in class " 
2669     + me.exClazz.__CLASS_NAME__
2670     + " has a superclass method by the same name (possibly with the same parameters) that is private and "
2671     + " therefore might be called improperly from this class. If your "
2672     + " code does not run properly, or you want to make it run faster, change the name of this method to something else."
2673     System.out.println(me.privateNote);
2674     alert(me.privateNote);
2675   }
2676   /*
2677   alert([me.exClazz.__CLASS_NAME__, me.exName,
2678     caller.exClazz.__CLASS_NAME__, caller.exName,stack,mySig])
2679   if (stack == null || caller.exClazz == me.exClazz)
2680     return null;
2681   // I am being called by a different class...
2682   
2683   for (var i = stack.length; --i >= 0;) {
2684     if (stacks[i] != caller.claxxRef)
2685       continue;
2686     // and it is on MY class stack
2687 //    if (
2688      
2689   }
2690   */
2691   
2692 /*      var m = Clazz.getMixedCallerMethod (args);
2693         if (m == null) return null;
2694         var callerFx = m.claxxRef.prototype[m.caller.exName];
2695         if (callerFx == null) return null; // may not be in the class hierarchies
2696         var ppFun = null;
2697         if (callerFx.claxxOwner ) {
2698                 ppFun = callerFx.claxxOwner.prototype[m.fxName];
2699         } else {
2700                 var stacks = callerFx.stacks;
2701                 for (var i = stacks.length - 1; i >= 0; i--) {
2702                         var fx = stacks[i].prototype[m.caller.exName];
2703                         if (fx === m.caller) {
2704                                 ppFun = stacks[i].prototype[m.fxName];
2705                         } else if (fx ) {
2706                                 for (var fn in fx) {
2707                                         if (fn.indexOf ('\\') == 0 && fx[fn] === m.caller) {
2708                                                 ppFun = stacks[i].prototype[m.fxName];
2709                                                 break;
2710                                         }
2711                                 }
2712                         }
2713                         if (ppFun) {
2714                                 break;
2715                         }
2716                 }
2717         }
2718         if (ppFun && ppFun.claxxOwner == null) {
2719                 ppFun = ppFun["\\" + m.paramTypes];
2720         }
2721         if (ppFun && ppFun.isPrivate && ppFun !== args.callee) {
2722                 return ppFun;
2723         }
2724 */  
2725         return null;
2726 };
2727
2728
2729 //$fz = null; // for private method declaration
2730
2731
2732 // /*# {$no.debug.support} >>x #*/
2733 // /*
2734 //  * Option to switch on/off of stack traces.
2735 //  */
2736 // /* protect */
2737 //Clazz.tracingCalling = false;
2738
2739 // /* private */
2740 // Clazz.callingStack = function (caller, owner) {
2741 //      this.caller = caller;
2742 //      this.owner = owner;
2743 // };
2744
2745 /*# x<< #*/
2746
2747 /**
2748  * The first folder is considered as the primary folder.
2749  * And try to be compatiable with _Loader system.
2750  */
2751 /* private */
2752
2753
2754 /*** not used in Jmol
2755  * *
2756 if (window["_Loader"] && _Loader.binaryFolders) {
2757         Clazz.binaryFolders = _Loader.binaryFolders;
2758 } else {
2759         Clazz.binaryFolders = ["j2s/", "", "j2slib/"];
2760 }
2761
2762 Clazz.addBinaryFolder = function (bin) {
2763         if (bin) {
2764                 var bins = Clazz.binaryFolders;
2765                 for (var i = 0; i < bins.length; i++) {
2766                         if (bins[i] == bin) {
2767                                 return ;
2768                         }
2769                 }
2770                 bins[bins.length] = bin;
2771         }
2772 };
2773 Clazz.removeBinaryFolder = function (bin) {
2774         if (bin) {
2775                 var bins = Clazz.binaryFolders;
2776                 for (var i = 0; i < bins.length; i++) {
2777                         if (bins[i] == bin) {
2778                                 for (var j = i; j < bins.length - 1; j++) {
2779                                         bins[j] = bins[j + 1];
2780                                 }
2781                                 bins.length--;
2782                                 return bin;
2783                         }
2784                 }
2785         }
2786         return null;
2787 };
2788 Clazz.setPrimaryFolder = function (bin) {
2789         if (bin) {
2790                 Clazz.removeBinaryFolder (bin);
2791                 var bins = Clazz.binaryFolders;
2792                 for (var i = bins.length - 1; i >= 0; i--) {
2793                         bins[i + 1] = bins[i];
2794                 }
2795                 bins[0] = bin;
2796         }
2797 };
2798
2799 ***/
2800
2801
2802 ///////////////// special definitions of standard Java class methods ///////////
2803
2804 /**
2805  * This is a simple implementation for Clazz#load. It just ignore dependencies
2806  * of the class. This will be fine for jar *.z.js file.
2807  * It will be overriden by _Loader#load.
2808  * For more details, see _Loader.js
2809  */
2810 /* protected */
2811 /*
2812 Clazz.load = function (musts, clazz, optionals, declaration) {
2813         // not used in Jmol
2814         if (declaration)
2815                 declaration ();
2816 };
2817 */
2818
2819 /*
2820  * Invade the Object prototype!
2821  * TODO: make sure that invading Object prototype does not affect other
2822  * existed library, such as Dojo, YUI, Prototype, ...
2823  */
2824 java.lang.Object = Clazz._O;
2825
2826 Clazz._O.getName = Clazz._innerFunctions.getName;
2827
2828
2829 java.lang.System = System = {
2830         props : null, //new java.util.Properties (),
2831         $props : {},
2832         arraycopy : function (src, srcPos, dest, destPos, length) {
2833                 if (src !== dest) {
2834                         for (var i = 0; i < length; i++) {
2835                                 dest[destPos + i] = src[srcPos + i];
2836                         }
2837                 } else {
2838                         var swap = [];
2839                         for (var i = 0; i < length; i++) {
2840                                 swap[i] = src[srcPos + i];
2841                         }
2842                         for (var i = 0; i < length; i++) {
2843                                 dest[destPos + i] = swap[i];
2844                         }
2845                 }
2846         },
2847         currentTimeMillis : function () {
2848                 return new Date ().getTime ();
2849         },
2850         gc : function() {}, // bh
2851         getProperties : function () {
2852                 return System.props;
2853         },
2854         getProperty : function (key, def) {
2855                 if (System.props)
2856                         return System.props.getProperty (key, def);
2857                 var v = System.$props[key];
2858     if (typeof v != "undefined")
2859       return v;
2860     if (key.indexOf(".") > 0) {
2861       v = null;    
2862       switch (key) {
2863       case "java.version":
2864         v = "1.6";
2865       case "file.separator":
2866       case "path.separator":
2867         v = "/";
2868         break;        
2869       case "line.separator":
2870         v = (navigator.userAgent.indexOf("Windows") >= 0 ? "\r\n" : "\n");
2871         break;
2872       case "os.name":
2873       case "os.version":
2874         v = navigator.userAgent;
2875         break;
2876       }
2877       if (v)
2878         return System.$props[key] = v;
2879     }
2880     return (arguments.length == 1 ? null : def == null ? key : def); // BH
2881         },
2882         getSecurityManager : function() { return null },  // bh
2883         setProperties : function (props) {
2884                 System.props = props;
2885         },
2886   lineSeparator : function() { return '\n' }, // bh
2887         setProperty : function (key, val) {
2888                 if (!System.props)
2889                         return System.$props[key] = val; // BH
2890                 System.props.setProperty (key, val);
2891         }
2892 };
2893
2894 System.identityHashCode=function(obj){
2895   if(obj==null)
2896     return 0;
2897     
2898         return obj._$hashcode || (obj._$hashcode = ++Clazz._hashCode)
2899
2900 /*    
2901   try{
2902     return obj.toString().hashCode();
2903   }catch(e){
2904     var str=":";
2905     for(var s in obj){
2906      str+=s+":"
2907     }
2908     return str.hashCode();
2909   }
2910 */  
2911 }
2912
2913 System.out = new Clazz._O ();
2914 System.out.__CLASS_NAME__ = "java.io.PrintStream";
2915 System.out.print = function () {};
2916 System.out.printf = function () {};
2917 System.out.println = function () {};
2918 System.out.write = function () {};
2919
2920 System.err = new Clazz._O ();
2921 System.err.__CLASS_NAME__ = "java.io.PrintStream";
2922 System.err.print = function () {};
2923 System.err.printf = function () {};
2924 System.err.println = function () {};
2925 System.err.write = function () {};
2926
2927 Clazz.popup = Clazz.assert = Clazz.log = Clazz.error = window.alert;
2928
2929 Thread = function () {};
2930 Thread.J2S_THREAD = Thread.prototype.J2S_THREAD = new Thread ();
2931 Thread.currentThread = Thread.prototype.currentThread = function () {
2932         return this.J2S_THREAD;
2933 };
2934
2935 /* not used in Jmol
2936 Clazz.intCast = function (n) { // 32bit
2937         var b1 = (n & 0xff000000) >> 24;
2938         var b2 = (n & 0xff0000) >> 16;
2939         var b3 = (n & 0xff00) >> 8;
2940         var b4 = n & 0xff;
2941         if ((b1 & 0x80) != 0) {
2942                 return -(((b1 & 0x7f) << 24) + (b2 << 16) + (b3 << 8) + b4 + 1);
2943         } else {
2944                 return (b1 << 24) + (b2 << 16) + (b3 << 8) + b4;
2945         }
2946 };
2947 Clazz.shortCast = function (s) { // 16bit
2948         var b1 = (n & 0xff00) >> 8;
2949         var b2 = n & 0xff;
2950         if ((b1 & 0x80) != 0) {
2951                 return -(((b1 & 0x7f) << 8) + b2 + 1);
2952         } else {
2953                 return (b1 << 8) + b4;
2954         }
2955 };
2956
2957 Clazz.byteCast = function (b) { // 8bit
2958         if ((b & 0x80) != 0) {
2959                 return -((b & 0x7f) + 1);
2960         } else {
2961                 return b & 0xff;
2962         }
2963 };
2964
2965 Clazz.charCast = function (c) { // 8bit
2966         return String.fromCharCode (c & 0xff).charAt (0);
2967 };
2968
2969 Clazz.floatCast = function (f) { // 32bit
2970         return f;
2971 };
2972
2973 */
2974
2975
2976 /*
2977  * Try to fix JavaScript's shift operator defects on long type numbers.
2978  */
2979
2980 /* not used in Jmol
2981
2982 Clazz.longMasks = [];
2983
2984 Clazz.longReverseMasks = [];
2985
2986 Clazz.longBits = [];
2987
2988 ;(function () {
2989         var arr = [1];
2990         for (var i = 1; i < 53; i++) {
2991                 arr[i] = arr[i - 1] + arr[i - 1]; // * 2 or << 1
2992         }
2993         Clazz.longBits = arr;
2994         Clazz.longMasks[52] = arr[52];
2995         for (var i = 51; i >= 0; i--) {
2996                 Clazz.longMasks[i] = Clazz.longMasks[i + 1] + arr[i];
2997         }
2998         Clazz.longReverseMasks[0] = arr[0];
2999         for (var i = 1; i < 52; i++) {
3000                 Clazz.longReverseMasks[i] = Clazz.longReverseMasks[i - 1] + arr[i];
3001         }
3002 }) ();
3003
3004
3005 Clazz.longLeftShift = function (l, o) { // 64bit
3006         if (o == 0) return l;
3007         if (o >= 64) return 0;
3008         if (o > 52) {
3009                 error ("[Java2Script] Error : JavaScript does not support long shift!");
3010                 return l;
3011         }
3012         if ((l & Clazz.longMasks[o - 1]) != 0) {
3013                 error ("[Java2Script] Error : Such shift operator results in wrong calculation!");
3014                 return l;
3015         }
3016         var high = l & Clazz.longMasks[52 - 32 + o];
3017         if (high != 0) {
3018                 return high * Clazz.longBits[o] + (l & Clazz.longReverseMasks[32 - o]) << 0;
3019         } else {
3020                 return l << o;
3021         }
3022 };
3023
3024 Clazz.intLeftShift = function (n, o) { // 32bit
3025         return (n << o) & 0xffffffff;
3026 };
3027
3028 Clazz.longRightShift = function (l, o) { // 64bit
3029         if ((l & Clazz.longMasks[52 - 32]) != 0) {
3030                 return Math.round((l & Clazz.longMasks[52 - 32]) / Clazz.longBits[32 - o]) + (l & Clazz.longReverseMasks[o]) >> o;
3031         } else {
3032                 return l >> o;
3033         }
3034 };
3035
3036 Clazz.intRightShift = function (n, o) { // 32bit
3037         return n >> o; // no needs for this shifting wrapper
3038 };
3039
3040 Clazz.long0RightShift = function (l, o) { // 64bit
3041         return l >>> o;
3042 };
3043
3044 Clazz.int0RightShift = function (n, o) { // 64bit
3045         return n >>> o; // no needs for this shifting wrapper
3046 };
3047
3048 */
3049 // Compress the common public API method in shorter name
3050 //$_L=Clazz.load;
3051 //$_W=Clazz.declareAnonymous;$_T=Clazz.declareType;
3052 //$_J=Clazz.declarePackage;$_C=Clazz.decorateAsClass;
3053 //$_Z=Clazz.instantialize;$_I=Clazz.declareInterface;$_D=Clazz.isClassDefined;
3054 //$_H=Clazz.pu$h;$_P=Clazz.p0p;$_B=Clazz.prepareCallback;
3055 //$_N=Clazz.innerTypeInstance;$_K=Clazz.makeConstructor;$_U=Clazz.superCall;$_R=Clazz.superConstructor;
3056 //$_M=Clazz.defineMethod;$_V=Clazz.overrideMethod;$_S=Clazz.defineStatics;
3057 //$_E=Clazz.defineEnumConstant;
3058 //$_F=Clazz.cloneFinals;
3059 //$_Y=Clazz.prepareFields;$_A=Clazz.newArray;$_O=Clazz.instanceOf;
3060 //$_G=Clazz.inheritArgs;$_X=Clazz.checkPrivateMethod;$_Q=Clazz.makeFunction;
3061 //$_s=Clazz.registerSerializableFields;
3062 //$_k=Clazz.overrideConstructor;
3063
3064
3065 /////////////////////// inner function support /////////////////////////////////
3066
3067 /* public */
3068 Clazz.innerFunctionNames = Clazz.innerFunctionNames.concat ([
3069     "getSuperclass", "isAssignableFrom", 
3070     "getConstructor", 
3071     "getDeclaredMethod", "getDeclaredMethods",
3072     "getMethod", "getMethods",   
3073                 "getModifiers", /*"isArray",*/ "newInstance"]);
3074
3075 /* public */
3076 Clazz._innerFunctions.getSuperclass = function () {
3077         return this.superClazz; 
3078 };
3079
3080 /* public */
3081 Clazz._innerFunctions.isAssignableFrom = function (clazz) {
3082         return Clazz.getInheritedLevel (clazz, this) >= 0;      
3083 };
3084
3085 /* public */
3086 Clazz._innerFunctions.getConstructor = function () {
3087         return new java.lang.reflect.Constructor (this, [], [], 
3088                         java.lang.reflect.Modifier.PUBLIC);
3089 };
3090 /**
3091  * TODO: fix bug for polymorphic methods!
3092  */
3093 /* public */
3094 Clazz._innerFunctions.getDeclaredMethods = Clazz._innerFunctions.getMethods = function () {
3095         var ms = [];
3096         var p = this.prototype;
3097         for (var attr in p) {
3098                 if (typeof p[attr] == "function" && !p[attr].__CLASS_NAME__) {
3099                         /* there are polynormical methods. */
3100                         ms.push(new java.lang.reflect.Method (this, attr,
3101                                         [], java.lang.Void, [], java.lang.reflect.Modifier.PUBLIC));
3102                 }
3103         }
3104         p = this;
3105         for (var attr in p) {
3106                 if (typeof p[attr] == "function" && !p[attr].__CLASS_NAME__) {
3107                         ms.push(new java.lang.reflect.Method (this, attr,
3108                                         [], java.lang.Void, [], java.lang.reflect.Modifier.PUBLIC
3109                                         | java.lang.reflect.Modifier.STATIC));
3110                 }
3111         }
3112         return ms;
3113 };
3114 /* public */
3115 Clazz._innerFunctions.getDeclaredMethod = Clazz._innerFunctions.getMethod = function (name, clazzes) {
3116         var p = this.prototype;
3117         for (var attr in p) {
3118                 if (name == attr && typeof p[attr] == "function" 
3119                                 && !p[attr].__CLASS_NAME__) {
3120                         /* there are polynormical methods. */
3121                         return new java.lang.reflect.Method (this, attr,
3122                                         [], java.lang.Void, [], java.lang.reflect.Modifier.PUBLIC);
3123                 }
3124         }
3125         p = this;
3126         for (var attr in p) {
3127                 if (name == attr && typeof p[attr] == "function" 
3128                                 && !p[attr].__CLASS_NAME__) {
3129                         return new java.lang.reflect.Method (this, attr,
3130                                         [], java.lang.Void, [], java.lang.reflect.Modifier.PUBLIC
3131                                         | java.lang.reflect.Modifier.STATIC);
3132                 }
3133         }
3134         return null;
3135 };
3136 /* public */
3137 Clazz._innerFunctions.getModifiers = function () {
3138         return java.lang.reflect.Modifier.PUBLIC;
3139 };
3140
3141 Clazz._innerFunctions.newInstance = function (a) {
3142         var clz = this;
3143   switch(a == null ? 0 : a.length) {
3144   case 0:
3145     return new clz();
3146   case 1:
3147         return new clz(a[0]);
3148   case 2:
3149         return new clz(a[0], a[1]);
3150   case 3:
3151         return new clz(a[0], a[1], a[2]);
3152   case 4:
3153         return new clz(a[0], a[1], a[2], a[3]);
3154   default:
3155     var x = "new " + clz.__CLASS_NAME__ + "(";
3156     for (var i = 0; i < a.length; i++)
3157      x += (i == 0 ? "" : ",") + "a[" + i + "]";
3158     x += ")";
3159     return eval(x);
3160   }
3161 };
3162
3163 //Object.newInstance = Clazz._innerFunctions.newInstance;
3164 ;(function(){  // BH added wrapper here
3165         var inF = Clazz.innerFunctionNames;
3166         for (var i = 0; i < inF.length; i++) {
3167                 Clazz._O[inF[i]] = Clazz._innerFunctions[inF[i]];
3168                 Array[inF[i]] = Clazz._innerFunctions[inF[i]];
3169         }
3170         //Array["isArray"] = function () {
3171         //      return true;
3172         //};
3173 })();
3174
3175 //////////////////////////// hotspot and unloading /////////////////////////////
3176 /* For hotspot and unloading */
3177
3178 if (window["Clazz"] && !window["Clazz"].unloadClass) {
3179
3180 /* public */
3181 Clazz.unloadClass = function (qClazzName) {
3182         var cc = Clazz.evalType (qClazzName);
3183         if (cc) {
3184                 Clazz.unloadedClasses[qClazzName] = cc;
3185                 var clazzName = qClazzName;
3186                 var pkgFrags = clazzName.split (/\./);
3187                 var pkg = null;
3188                 for (var i = 0; i < pkgFrags.length - 1; i++)
3189                         pkg = (pkg ? pkg[pkgFrags[i]] : Clazz.allPackage[pkgFrags[0]]);
3190                 if (!pkg) {
3191                         Clazz.allPackage[pkgFrags[0]] = null;
3192                         window[pkgFrags[0]] = null;
3193                         // also try to unload inner or anonymous classes
3194                         for (var c in window) {
3195                                 if (c.indexOf (qClazzName + "$") == 0) {
3196                                         Clazz.unloadClass (c);
3197                                         window[c] = null;
3198                                 }
3199                         }
3200                 } else {
3201                         pkg[pkgFrags[pkgFrags.length - 1]] = null;
3202                         // also try to unload inner or anonymous classes
3203                         for (var c in pkg) {
3204                                 if (c.indexOf (pkgFrags[pkgFrags.length - 1] + "$") == 0) {
3205                                         Clazz.unloadClass (pkg.__PKG_NAME__ + "." + c);
3206                                         pkg[c] = null;
3207                                 }
3208                         }
3209                 }
3210
3211                 if (Clazz.allClasses[qClazzName]) {
3212                         Clazz.allClasses[qClazzName] = false;
3213                         // also try to unload inner or anonymous classes
3214                         for (var c in Clazz.allClasses) {
3215                                 if (c.indexOf (qClazzName + "$") == 0) {
3216                                         Clazz.allClasses[c] = false;
3217                                 }
3218                         }
3219                 }
3220
3221                 for (var m in cc) {
3222                         cleanDelegateMethod (cc[m]);
3223                 }
3224                 for (var m in cc.prototype) {
3225                         cleanDelegateMethod (cc.prototype[m]);
3226                 }
3227
3228                 if (Clazz._Loader) {
3229                         Clazz._Loader.unloadClassExt(qClazzName);
3230                 }
3231
3232                 return true;
3233         }
3234         return false;
3235 };
3236
3237 /* private */
3238 var cleanDelegateMethod = function (m) {
3239         if (!m) 
3240                 return;
3241         if (typeof m == "function" && m.lastMethod
3242                         && m.lastParams && m.lastClaxxRef) {
3243                 m.lastMethod = null;
3244                 m.lastParams = null;
3245                 m.lastClaxxRef = null;
3246         }
3247 };
3248
3249 } // if (window["Clazz"] && !window["Clazz"].unloadClass)
3250
3251 /******************************************************************************
3252  * Copyright (c) 2007 java2script.org and others.
3253  * All rights reserved. This program and the accompanying materials
3254  * are made available under the terms of the Eclipse Public License v1.0
3255  * which accompanies this distribution, and is available at
3256  * http://www.eclipse.org/legal/epl-v10.html
3257  *
3258  * Contributors:
3259  *     Zhou Renjian - initial API and implementation
3260  *****************************************************************************/
3261 /*******
3262  * @author zhou renjian
3263  * @create July 10, 2006
3264  *******/
3265
3266 //if (window["ClazzNode"] == null) {
3267 /**
3268  * TODO:
3269  * Make optimization over class dependency tree.
3270  */
3271
3272 /*
3273  * ClassLoader Summary
3274  * 
3275  * ClassLoader creates SCRIPT elements and setup class path and onload 
3276  * callback to continue class loading.
3277  *
3278  * In the onload callbacks, _Loader will try to calculate the next-to-be-
3279  * load *.js and load it. In *.js, it will contains some codes like
3280  * Clazz.load (..., "$wt.widgets.Control", ...);
3281  * to provide information to build up the class dependency tree.
3282  *
3283  * Some known problems of different browsers:
3284  * 1. In IE, loading *.js through SCRIPT will first triggers onreadstatechange 
3285  * event, and then executes inner *.js source.
3286  * 2. In Firefox, loading *.js will first executes *.js source and then 
3287  * triggers onload event.
3288  * 3. In Opera, similar to IE, but trigger onload event. (TODO: More details 
3289  * should be studied. Currently, Opera supports no multiple-thread-loading)
3290  * 
3291  * For class dependency tree, actually, it is not a tree. It is a reference
3292  * net with nodes have n parents and n children. There is a root, which 
3293  * ClassLoader knows where to start searching and loading classes, for such
3294  * a net. Each node is a class. Each class may require a set of must-classes, 
3295  * which must be loaded before itself getting initialized, and also need a set
3296  * of optional classes, which also be loaded before being called.
3297  *
3298  * The class loading status will be in 6 stages.
3299  * 1. Unknown, the class is newly introduced by other class.
3300  * 2. Known, the class is already mentioned by other class.
3301  * 3. Loaded, *.js source is in memory, but may not be initialized yet. It 
3302  * requires all its must-classes be intiailized, which is in the next stage.
3303  * 4. Musts loaded, all must classes is already loaded and declared.
3304  * 5. Delcared, the class is already declared (_Loader#isClassDefined).
3305  * 6. Optionals loaded, all optional classes is loaded and declared.
3306  *
3307  * The ClassLoader tries to load all necessary classes in order, and intialize
3308  * them in order. For such job, it will traverse the dependency tree, and try 
3309  * to next class to-be-loaded. Sometime, the class dependencies may be in one
3310  * or more cycles, which must be broken down so classes is loaded in correct
3311  * order.
3312  *
3313  * Loading order and intializing order is very important for the ClassLoader.
3314  * The following technical options are considered:
3315  * 1. SCRIPT is loading asynchronously, which means controling order must use
3316  * callback methods to continue.
3317  * 2. Multiple loading threads are later introduced, which requires the 
3318  * ClassLoader should use variables to record the class status.
3319  * 3. Different browsers have different loading orders, which means extra tests
3320  * should be tested to make sure loading order won't be broken.
3321  * 4. Java2Script simulator itself have some loading orders that must be 
3322  * honored, which means it should be integrated seamlessly to Clazz system.
3323  * 5. Packed *.z.js is introduced to avoid lots of small *.js which requires 
3324  * lots of HTTP connections, which means that packed *.z.js should be treated
3325  * specially (There will be mappings for such packed classes).
3326  * 6. *.js or *.css loading may fail according to network status, which means
3327  * another loading try should be performed, so _Loader is more robust.
3328  * 7. SWT lazy loading is later introduced, which means that class loading
3329  * process may be paused and should be resumed later.
3330  *
3331  * Some known bugs:
3332  * <code>$_L(["$wt.graphics.Drawable","$wt.widgets.Widget"],
3333  *  "$wt.widgets.Control", ...</code>
3334  * has errors while must classes in different order such as
3335  * <code>$_L(["$wt.widgets.Widget", "$wt.graphics.Drawable"],
3336  *  "$wt.widgets.Control", ...</code>
3337  * has no error.
3338  * 
3339  * Other maybe bug scenarios:
3340  * 1. In <code>_Loader.maxLoadingThreads = 1;</code> single loading thread 
3341  * mode, there are no errors, but in default multiple thread loading mode, 
3342  * there are errors.
3343  * 2. No errors in one browser, but has errors on other browsers (Browser 
3344  * script loading order differences).
3345  * 3. First time loading has errors, but reloading it gets no errors (Maybe 
3346  * HTTP connections timeout, but should not accur in local file system, or it
3347  * is a loading bug by using JavaScript timeout thread).
3348  */
3349
3350 /*
3351  * The following comments with "#" are special configurations for a much
3352  * smaller *.js file size.
3353  *
3354  * @see net.sf.j2s.lib/src/net/sf/j2s/lib/build/SmartJSCompressor.java
3355  */
3356 /**
3357  * Static class loader class
3358  */
3359 Clazz._Loader = Clazz.ClazzLoader = function () {};
3360
3361 /**
3362  * Class dependency tree node
3363  */
3364 /* private */
3365 var Node = function () {
3366         this.parents = [];
3367         this.musts = [];
3368         this.optionals = [];
3369         this.declaration = null;
3370         this.name = null; // id
3371         this.path = null;
3372 //      this.requires = null;
3373 //      this.requiresMap = null;
3374         this.onLoaded = null;
3375         this.status = 0;
3376         this.random = 0.13412;
3377 };
3378
3379
3380 ;(function(Clazz, _Loader) {
3381
3382 _Loader._checkLoad = Jmol._checkLoad;
3383  
3384 _Loader.updateNodeForFunctionDecoration = function(qName) {
3385         var node = findNode(qName);
3386         if (node && node.status == Node.STATUS_KNOWN) {
3387                 window.setTimeout((function(nnn) {
3388                         return function() {
3389                                 updateNode(nnn);
3390                         };
3391                 })(node), 1);
3392         }
3393 }
3394
3395 Node.prototype.toString = function() {
3396         return this.name || this.path || "ClazzNode";
3397 }
3398
3399 Node.STATUS_UNKNOWN = 0;
3400 Node.STATUS_KNOWN = 1;
3401 Node.STATUS_CONTENT_LOADED = 2;
3402 Node.STATUS_MUSTS_LOADED = 3;
3403 Node.STATUS_DECLARED = 4;
3404 Node.STATUS_LOAD_COMPLETE = 5;
3405
3406                                                  
3407 var loaders = [];
3408
3409 /* public */
3410 _Loader.requireLoaderByBase = function (base) {
3411         for (var i = 0; i < loaders.length; i++) {
3412                 if (loaders[i].base == base) {
3413                         return loaders[i];
3414                 }
3415         }
3416         var loader = new _Loader ();
3417         loader.base = base; 
3418         loaders.push(loader);
3419         return loader;
3420 };
3421
3422 /**
3423  * Class dependency tree
3424  */
3425 var clazzTreeRoot = new Node();
3426
3427 /**
3428  * Used to keep the status whether a given *.js path is loaded or not.
3429  */
3430 /* private */
3431 var loadedScripts = {};
3432
3433 /**
3434  * Multiple threads are used to speed up *.js loading.
3435  */
3436 /* private */
3437 var inLoadingThreads = 0;
3438
3439 /**
3440  * Maximum of loading threads
3441  */
3442 /* private */
3443 var maxLoadingThreads = 6;
3444
3445 var userAgent = navigator.userAgent.toLowerCase ();
3446 var isOpera = (userAgent.indexOf ("opera") != -1);
3447 var isIE = (userAgent.indexOf ("msie") != -1) && !isOpera;
3448 var isGecko = (userAgent.indexOf ("gecko") != -1);
3449
3450 /*
3451  * Opera has different loading order which will result in performance degrade!
3452  * So just return to single thread loading in Opera!
3453  *
3454  * FIXME: This different loading order also causes bugs in single thread!
3455  */
3456 if (isOpera) {
3457         maxLoadingThreads = 1;
3458         var index = userAgent.indexOf ("opera/");
3459         if (index != -1) {
3460                 var verNumber = 9.0;
3461                 try {
3462                         verNumber = parseFloat(userAgent.subString (index + 6));
3463                 } catch (e) {}
3464                 if (verNumber >= 9.6) {
3465                         maxLoadingThreads = 6;
3466                 }
3467         } 
3468 }
3469
3470 /**
3471  * Try to be compatiable with Clazz system.
3472  * In original design _Loader and Clazz are independent!
3473  *  -- zhourenjian @ December 23, 2006
3474  */
3475 var isClassdefined;
3476 var definedClasses;
3477
3478 if (self.Clazz && Clazz.isClassDefined) {
3479         isClassDefined = Clazz.isClassDefined;
3480 } else {
3481         definedClasses = {};
3482         isClassDefined = function (clazzName) {
3483                 return definedClasses[clazzName] == true;
3484         };
3485 }
3486
3487 /**
3488  * Expand the shortened list of class names.
3489  * For example:
3490  * JU.Log, $.Display, $.Decorations
3491  * will be expanded to 
3492  * JU.Log, JU.Display, JU.Decorations
3493  * where "$." stands for the previous class name's package.
3494  *
3495  * This method will be used to unwrap the required/optional classes list and 
3496  * the ignored classes list.
3497  */
3498 /* private */
3499 var unwrapArray = function (arr) {
3500         if (!arr || arr.length == 0)
3501                 return [];
3502         var last = null;
3503         for (var i = 0; i < arr.length; i++) {
3504                 if (!arr[i])
3505                         continue;
3506                 if (arr[i].charAt (0) == '$') {
3507                         if (arr[i].charAt (1) == '.') {
3508                                 if (!last)
3509                                         continue;
3510                                 var idx = last.lastIndexOf (".");
3511                                 if (idx != -1) {
3512                                         var prefix = last.substring (0, idx);
3513                                         arr[i] = prefix + arr[i].substring (1);
3514                                 }
3515                         } else {
3516                                 arr[i] = "org.eclipse.s" + arr[i].substring (1);
3517                         }
3518                 }
3519                 last = arr[i];
3520         }
3521         return arr;
3522 };
3523
3524 /**
3525  * Used to keep to-be-loaded classes.
3526  */
3527 /* private */
3528 var classQueue = [];
3529
3530 /* private */
3531 var classpathMap = {};
3532
3533 /* private */
3534 var pkgRefCount = 0;
3535
3536 /* public */
3537 _Loader.loadPackageClasspath = function (pkg, base, isIndex, fSuccess, mode, pt) {
3538         var map = classpathMap;
3539         mode || (mode = 0);
3540         fSuccess || (fSuccess = null);
3541         pt || (pt = 0);
3542
3543         /*
3544          * In some situation, maybe,
3545          * _Loader.packageClasspath ("java", ..., true);
3546          * is called after other _Loader#packageClasspath, e.g.
3547          * <code>
3548          * _Loader.packageClasspath ("org.eclipse.swt", "...", true);
3549          * _Loader.packageClasspath ("java", "...", true);
3550          * </code>
3551          * which is not recommended. But _Loader should try to adjust orders
3552          * which requires "java" to be declared before normal _Loader
3553          * #packageClasspath call before that line! And later that line
3554          * should never initialize "java/package.js" again!
3555          */
3556         var isPkgDeclared = (isIndex && map["@" + pkg]);
3557         if (mode == 0 && isIndex && !map["@java"] && pkg.indexOf ("java") != 0 && needPackage("java")) {
3558                 _Loader.loadPackage("java", fSuccess ? function(_package){_Loader.loadPackageClasspath(pkg, base, isIndex, fSuccess, 1)} : null);
3559                 if (fSuccess)
3560                         return;
3561         }
3562         if (pkg instanceof Array) {
3563                 unwrapArray(pkg);
3564                 if (fSuccess) {
3565                         if (pt < pkg.length)
3566                                 _Loader.loadPackageClasspath(pkg[pt], base, isIndex, function(_loadPackageClassPath){_Loader.loadPackageClasspath(pkg, base, isIndex, fSuccess, 1, pt + 1)}, 1);
3567                         else
3568                                 fSuccess();
3569                 } else {
3570                         for (var i = 0; i < pkg.length; i++)
3571                                 _Loader.loadPackageClasspath(pkg[i], base, isIndex, null);
3572                 }
3573                 return;
3574         }
3575         switch (pkg) {
3576         case "java.*":
3577                 pkg = "java";
3578                 // fall through
3579         case "java":
3580                 if (base) {
3581                         // support ajax for default
3582                         var key = "@net.sf.j2s.ajax";
3583                         if (!map[key])
3584                                 map[key] = base;
3585                         key = "@net.sf.j2s";
3586                         if (!map[key])
3587                                 map[key] = base;
3588                 }               
3589                 break;
3590         case "swt":
3591                 pkg = "org.eclipse.swt";
3592                 break;
3593         case "ajax":
3594                 pkg = "net.sf.j2s.ajax";
3595                 break;
3596         case "j2s":
3597                 pkg = "net.sf.j2s";
3598                 break;
3599         default:
3600                 if (pkg.lastIndexOf(".*") == pkg.length - 2)
3601                         pkg = pkg.substring(0, pkg.length - 2);
3602                 break;
3603         }
3604         if (base) // critical for multiple applets
3605                 map["@" + pkg] = base;
3606         if (isIndex && !isPkgDeclared && !window[pkg + ".registered"]) {
3607                 pkgRefCount++;
3608                 if (pkg == "java")
3609                         pkg = "core" // JSmol -- moves java/package.js to core/package.js
3610                 _Loader.loadClass(pkg + ".package", function () {
3611                                         if (--pkgRefCount == 0)
3612                                                 runtimeLoaded();
3613                                         //fSuccess && fSuccess();
3614                                 }, true, true, 1);
3615                 return;
3616         }
3617         fSuccess && fSuccess();
3618 };
3619
3620 /**
3621  * BH: allows user/developer to load classes even though wrapping and Google
3622  * Closure Compiler has not been run on the class.
3623  *   
3624  */
3625 Clazz.loadClass = function (name, onLoaded, async) {
3626   if (!self.Class) {
3627     Class = Clazz;
3628     Class.forName = Clazz._4Name;
3629     JavaObject = Clazz._O;
3630     // maybe more here
3631   }
3632   return (name && _Loader.loadClass(name, onLoaded, true, async, 1));
3633 }
3634
3635 /**
3636  * Load the given class ant its related classes.
3637  */
3638 /* public */
3639 _Loader.loadClass = function (name, onLoaded, forced, async, mode) {
3640
3641   mode || (mode = 0); // BH: not implemented
3642   (async == null) && (async = false);
3643   
3644         if (typeof onLoaded == "boolean")
3645                 return Clazz.evalType(name);
3646
3647   System.out.println("loadClass " + name)
3648
3649         // Make sure that packageClasspath ("java", base, true); 
3650         // is called before any _Loader#loadClass is called.
3651
3652         if (needPackage("java"))
3653                 _Loader.loadPackage("java");
3654         if (needPackage("core"))
3655                 _Loader.loadPackage("core");    
3656
3657 //      var swtPkg = "org.eclipse.swt";
3658 //      if (name.indexOf (swtPkg) == 0 || name.indexOf ("$wt") == 0) {
3659 //              _Loader.assurePackageClasspath (swtPkg);
3660 //      }
3661 //      if (name.indexOf ("junit") == 0) {
3662 //              _Loader.assurePackageClasspath ("junit");
3663 //      }
3664
3665         // Any _Loader#loadClass calls will be queued until java.* core classes are loaded.
3666
3667         _Loader.keepOnLoading = true;
3668         
3669         if (!forced && (pkgRefCount && name.lastIndexOf(".package") != name.length - 8
3670                         || name.indexOf("java.") != 0 && !isClassDefined(runtimeKeyClass)
3671                  )) {   
3672                 queueBe4KeyClazz.push([name, onLoaded]);
3673     
3674     
3675   System.out.println("loadclass-queuing" + name+ runtimeKeyClass + " "+ isClassDefined(runtimeKeyClass))
3676
3677                 return;    
3678         }
3679         var b;
3680         if ((b = isClassDefined(name)) || isClassExcluded(name)) {
3681                 if (b && onLoaded) {
3682                         var nn = findNode(name);
3683                         if (!nn || nn.status >= Node.STATUS_LOAD_COMPLETE) {
3684                                 if (async) {
3685                                         window.setTimeout(onLoaded, 25);
3686                                 } else {
3687                                         onLoaded();
3688                                 }
3689                         }
3690                 }
3691                 return;
3692         }
3693         var path = _Loader.getClasspathFor(name);
3694   var existed = loadedScripts[path];
3695         var qq = classQueue;
3696         if (!existed)
3697                 for (var i = qq.length; --i >= 0;)
3698                         if (qq[i].path == path || qq[i].name == name) {
3699                                 existed = true;
3700                                 break;
3701                         }
3702         if (existed) {
3703                 if (onLoaded) {
3704                         var n = findNode(name);
3705                         if (n) {
3706                                 if (!n.onLoaded) {
3707                                         n.onLoaded = onLoaded;
3708                                 } else if (onLoaded != n.onLoaded) {
3709                                         n.onLoaded = (function (nF, oF) { return function () { nF(); oF() };    }) (n.onLoaded, onLoaded);
3710                                 }
3711                         }
3712                 }
3713                 return;
3714         }
3715
3716         var n = (Clazz.unloadedClasses[name] && findNode(name) || new Node());
3717         n.name = name;
3718         n.path = path;
3719         n.isPackage = (path.lastIndexOf("package.js") == path.length - 10);
3720         mappingPathNameNode(path, name, n);
3721         n.onLoaded = onLoaded;
3722         n.status = Node.STATUS_KNOWN;
3723         var needBeingQueued = false;
3724         for (var i = qq.length; --i >= 0;) {
3725                 if (qq[i].status != Node.STATUS_LOAD_COMPLETE) {
3726                         needBeingQueued = true;
3727                         break;
3728                 }
3729         }
3730         
3731         if (n.isPackage) {//forced
3732                 // push class to queue
3733                 var pt = qq.length;
3734                 for (; --pt >= 0;) {
3735                         if (qq[pt].isPackage) 
3736                                 break;
3737                         qq[pt + 1] = qq[pt];
3738                 }
3739                 qq[++pt] = n;
3740         } else if (needBeingQueued) {
3741                 qq.push(n);
3742         }
3743         if (!needBeingQueued) { // can be loaded directly
3744                 var bSave = false;
3745                 if (onLoaded) { 
3746                         bSave = isLoadingEntryClass;
3747                         isLoadingEntryClass = true;
3748                 }
3749     if (forced)onLoaded = null;
3750                 addChildClassNode(clazzTreeRoot, n, true);
3751                 loadScript(n, n.path, n.requiredBy, false, onLoaded ? function(_loadClass){ isLoadingEntryClass = bSave; onLoaded()}: null);
3752         }
3753 };
3754
3755 /*
3756  * Check whether given package's classpath is setup or not.
3757  * Only "java" and "org.eclipse.swt" are accepted in argument.
3758  */
3759 /* private */
3760 var needPackage = function(pkg) {
3761   // note that false != null and true != null
3762         return (window[pkg + ".registered"] != null && !classpathMap["@" + pkg]);
3763 }
3764
3765 /* private */
3766 _Loader.loadPackage = function(pkg, fSuccess) {
3767         fSuccess || (fSuccess = null);
3768         window[pkg + ".registered"] = false;
3769         _Loader.loadPackageClasspath(pkg, 
3770                 (_Loader.J2SLibBase || (_Loader.J2SLibBase = (_Loader.getJ2SLibBase() || "j2s/"))), 
3771                 true, fSuccess);
3772 };
3773
3774 /**
3775  * Register classes to a given *.z.js path, so only a single *.z.js is loaded
3776  * for all those classes.
3777  */
3778 /* public */
3779 _Loader.jarClasspath = function (jar, clazzes) {
3780         if (!(clazzes instanceof Array))
3781                 clazzes = [classes];
3782         unwrapArray(clazzes);
3783         for (var i = clazzes.length; --i >= 0;)
3784                 classpathMap["#" + clazzes[i]] = jar;
3785         classpathMap["$" + jar] = clazzes;
3786 };
3787
3788 /**
3789  * Usually be used in .../package.js. All given packages will be registered
3790  * to the same classpath of given prefix package.
3791  */
3792 /* public */
3793 _Loader.registerPackages = function (prefix, pkgs) {
3794         //_Loader.checkInteractive ();
3795         var base = _Loader.getClasspathFor (prefix + ".*", true);
3796         for (var i = 0; i < pkgs.length; i++) {
3797                 if (window["Clazz"]) {
3798                         Clazz.declarePackage (prefix + "." + pkgs[i]);
3799                 }
3800                 _Loader.loadPackageClasspath (prefix + "." + pkgs[i], base);
3801         }
3802 };
3803
3804 /**
3805  * Using multiple sites to load *.js in multiple threads. Using multiple
3806  * sites may avoid 2 HTTP 1.1 connections recommendation limit.
3807  * Here is a default implementation for http://archive.java2script.org.
3808  * In site archive.java2script.org, there are 6 sites:
3809  * 1. http://archive.java2script.org or http://a.java2script.org
3810  * 2. http://erchive.java2script.org or http://e.java2script.org
3811  * 3. http://irchive.java2script.org or http://i.java2script.org
3812  * 4. http://orchive.java2script.org or http://o.java2script.org
3813  * 5. http://urchive.java2script.org or http://u.java2script.org
3814  * 6. http://yrchive.java2script.org or http://y.java2script.org
3815  */
3816 /* protected */
3817         /*
3818 _Loader.multipleSites = function (path) {
3819         var deltas = window["j2s.update.delta"];
3820         if (deltas && deltas instanceof Array && deltas.length >= 3) {
3821                 var lastOldVersion = null;
3822                 var lastNewVersion = null;
3823                 for (var i = 0; i < deltas.length / 3; i++) {
3824                         var oldVersion = deltas[i + i + i];
3825                         if (oldVersion != "$") {
3826                                 lastOldVersion = oldVersion;
3827                         }
3828                         var newVersion = deltas[i + i + i + 1];
3829                         if (newVersion != "$") {
3830                                 lastNewVersion = newVersion;
3831                         }
3832                         var relativePath = deltas[i + i + i + 2];
3833                         var key = lastOldVersion + "/" + relativePath;
3834                         var idx = path.indexOf (key);
3835                         if (idx != -1 && idx == path.length - key.length) {
3836                                 path = path.substring (0, idx) + lastNewVersion + "/" + relativePath;
3837                                 break;
3838                         }
3839                 }
3840         }
3841         var length = path.length;
3842         if (maxLoadingThreads > 1 
3843                         && ((length > 15 && path.substring (0, 15) == "http://archive.")
3844                         || (length > 9 && path.substring (0, 9) == "http://a."))) {
3845                 var index = path.lastIndexOf("/");
3846                 if (index < length - 3) {
3847                         var arr = ['a', 'e', 'i', 'o', 'u', 'y'];
3848                         var c1 = path.charCodeAt (index + 1);
3849                         var c2 = path.charCodeAt (index + 2);
3850                         var idx = (length - index) * 3 + c1 * 5 + c2 * 7; // Hash
3851                         return path.substring (0, 7) + arr[idx % 6] + path.substring (8);
3852                 }
3853         }
3854         return path;
3855 };
3856         */
3857
3858 /**
3859  * Return the *.js path of the given class. Maybe the class is contained
3860  * in a *.z.js jar file.
3861  * @param clazz Given class that the path is to be calculated for. May
3862  * be java.package, or java.lang.String
3863  * @param forRoot Optional argument, if true, the return path will be root
3864  * of the given classs' package root path.
3865  * @param ext Optional argument, if given, it will replace the default ".js"
3866  * extension.
3867  */
3868 /* public */
3869 _Loader.getClasspathFor = function (clazz, forRoot, ext) {
3870         var path = classpathMap["#" + clazz];
3871         if (!path || forRoot || ext) {
3872                 var base;
3873                 var idx;
3874                 if (path) {
3875                         clazz = clazz.replace(/\./g, "/");      
3876                         if ((idx = path.lastIndexOf(clazz)) >= 0 
3877                                 || (idx = clazz.lastIndexOf("/")) >= 0 
3878                                         && (idx = path.lastIndexOf(clazz.substring(0, idx))) >= 0)
3879                                 base = path.substring(0, idx);
3880                 } else {
3881                         idx = clazz.length + 2;
3882                         while ((idx = clazz.lastIndexOf(".", idx - 2)) >= 0)
3883                                 if ((base = classpathMap["@" + clazz.substring(0, idx)]))
3884                                         break;
3885                         if (!forRoot)
3886                                 clazz = clazz.replace (/\./g, "/");     
3887                 }
3888                 if (base == null) {
3889                         var bins = "binaryFolders";
3890                         base = (window["Clazz"] && Clazz[bins] && Clazz[bins].length ? Clazz[bins][0] 
3891                                 : _Loader[bins] && _Loader[bins].length ? _Loader[bins][0]
3892                                 : "j2s");
3893                 }
3894                 path = (base.lastIndexOf("/") == base.length - 1 ? base : base + "/") + (forRoot ? ""
3895                         : clazz.lastIndexOf("/*") == clazz.length - 2 ? clazz.substring(0, idx + 1)
3896                         : clazz + (!ext ? ".js" : ext.charAt(0) != '.' ? "." + ext : ext));
3897         }               
3898         return path;//_Loader.multipleSites(path);
3899 };
3900
3901 /**
3902  * To ignore some classes.
3903  */
3904 /* public */
3905 _Loader.ignore = function () {
3906         var clazzes = (arguments.length == 1 && arguments[0] instanceof Array ?
3907                         clazzes = arguments[0] : null);
3908         var n = (clazzes ? clazzes.length : arguments.length);
3909         if (!clazzes) {
3910                 clazzes = new Array(n);
3911                 for (var i = 0; i < n; i++)
3912                         clazzes[i] = arguments[i];
3913         }
3914         unwrapArray(clazzes);
3915         for (var i = 0; i < n; i++)
3916                 excludeClassMap["@" + clazzes[i]] = 1;
3917 };
3918
3919 /**
3920  * The following *.script* can be overriden to indicate the 
3921  * status of classes loading.
3922  *
3923  * TODO: There should be a Java interface with name like INativeLoaderStatus
3924  */
3925 /* public */
3926 _Loader.onScriptLoading = function (file){};
3927
3928 /* public */
3929 _Loader.onScriptLoaded = function (file, isError){};
3930
3931 /* public */
3932 _Loader.onScriptInitialized = function (file){};
3933
3934 /* public */
3935 _Loader.onScriptCompleted = function (file){};
3936
3937 /* public */
3938 _Loader.onClassUnloaded = function (clazz){};
3939
3940 /**
3941  * After all the classes are loaded, this method will be called.
3942  * Should be overriden to run *.main([]).
3943  */
3944 /* public */
3945 _Loader.onGlobalLoaded = function () {};
3946
3947 /* public */
3948 _Loader.keepOnLoading = true; // never set false in this code
3949
3950
3951 /* private */
3952 var mapPath2ClassNode = {};
3953
3954 /* private */
3955 var isClassExcluded = function (clazz) {
3956         return excludeClassMap["@" + clazz];
3957 };
3958
3959 /* Used to keep ignored classes */
3960 /* private */
3961 var excludeClassMap = {};
3962
3963 /* private */
3964 var evaluate = function(file, file0, js) {
3965                 try {
3966                         eval(js + ";//# sourceURL="+file);
3967                 } catch (e) {      
3968       if (Clazz._isQuiet) 
3969         return;
3970                         var s = "[Java2Script] The required class file \n\n" + file + (js.indexOf("[Exception") == 0 && js.indexOf("data: no") ? 
3971          "\nwas not found.\n"
3972         : "\ncould not be loaded. Script error: " + e.message + " \n\ndata:\n\n" + js) + "\n\n" + Clazz.getStackTrace();
3973                 alert(s)
3974                         Clazz.alert(s);
3975                         throw e;
3976                 }
3977                 _Loader.onScriptLoaded(file, false);
3978                 tryToLoadNext(file0);
3979 }
3980
3981 /* private */
3982 var failedHandles = {};
3983
3984 /* private */
3985 var generateRemovingFunction = function (node) {
3986         return function () {
3987                 if (node.readyState != "interactive") {
3988                         try {
3989                                 if (node.parentNode)
3990                                         node.parentNode.removeChild (node);
3991                         } catch (e) { }
3992                         node = null;
3993                 }
3994         };
3995 };
3996
3997 /* private */
3998 var removeScriptNode = function (n) {
3999         if (window["j2s.script.debugging"]) {
4000                 return;
4001         }
4002         // lazily remove script nodes.
4003         window.setTimeout (generateRemovingFunction (n), 1);
4004 };
4005
4006 /* public */
4007 Clazz._4Name = function(clazzName, applet, state) {
4008         if (Clazz.isClassDefined(clazzName))
4009                 return Clazz.evalType(clazzName);
4010         var f = (Jmol._isAsync && applet ? applet._restoreState(clazzName, state) : null);
4011         if (f == 1)
4012                 return null; // must be already being created
4013         if (_Loader.setLoadingMode(f ? _Loader.MODE_SCRIPT : "xhr.sync")) {
4014                 _Loader.loadClass(clazzName, f, false, true, 1);
4015                 return null; // this will surely throw an error, but that is OK
4016         }
4017         //alert ("Using Java reflection: " + clazzName + " for " + applet._id + " \n"+ Clazz.getStackTrace());
4018         _Loader.loadClass(clazzName);
4019         return Clazz.evalType(clazzName);
4020 };
4021
4022 /**
4023  * BH: possibly useful for debugging
4024  */ 
4025 Clazz.currentPath= "";
4026
4027 /**
4028  * Load *.js by adding script elements into head. Hook the onload event to
4029  * load the next class in dependency tree.
4030  */
4031 /* private */
4032 var loadScript = function (node, file, why, ignoreOnload, fSuccess, _loadScript) {
4033
4034         Clazz.currentPath = file;
4035         if (ignoreOnload)alert("WHY>>")
4036 //BH removed    // maybe some scripts are to be loaded without needs to know onload event.
4037 //      if (!ignoreOnload && loadedScripts[file]) {
4038 //              _Loader.tryToLoadNext(file);
4039 //              return;
4040 //      }
4041         loadedScripts[file] = true;
4042         // also remove from queue
4043         removeArrayItem(classQueue, file);
4044
4045     // forces not-found message
4046     isUsingXMLHttpRequest = true;
4047     isAsynchronousLoading = false;
4048   if (_Loader._checkLoad) {
4049     System.out.println("\t" + file + (why ? "\n -- required by " + why : "") + "  ajax=" + isUsingXMLHttpRequest + " async=" + isAsynchronousLoading)
4050   }
4051
4052   var file0 = file;
4053   if (Clazz._debugging) {
4054     file = file.replace(/\.z\.js/,".js");
4055   }
4056
4057         _Loader.onScriptLoading(file);
4058         if (isUsingXMLHttpRequest && !isAsynchronousLoading) {
4059                 // alert("\t" + file + (why ? "\n -- required by " + why : "") + "  ajax=" + isUsingXMLHttpRequest + " async=" + isAsynchronousLoading + " " + Clazz.getStackTrace())
4060                 // synchronous loading
4061                 // works in MSIE locally unless a binary file :)
4062                 // from Jmol.api.Interface only
4063                 var data = Jmol._getFileData(file);
4064     try{
4065                   evaluate(file, file0, data);
4066     }catch(e) {
4067       alert(e + " loading file " + file + " " + node.name + " " + Clazz.getStackTrace());
4068     }
4069     if (fSuccess) {
4070 //      System.out.println("firing in loadScript " + file + " " + (fSuccess && fSuccess.toString()))
4071       fSuccess(); 
4072     }
4073                 return;
4074         }
4075   
4076   
4077 System.out.println("for file " + file +" fSuccess = " + (fSuccess ? fSuccess.toString() : ""))
4078         var info = {
4079                 dataType:"script",
4080                 async:true, 
4081                 type:"GET", 
4082                 url:file,
4083                 success:W3CScriptOnCallback(file, false, fSuccess),
4084                 error:W3CScriptOnCallback(file, true, fSuccess)
4085         };
4086         inLoadingThreads++;
4087         Jmol.$ajax(info);
4088 };
4089
4090 /* private */
4091 var W3CScriptOnCallback = function (path, forError, fSuccess) {
4092   var s = Clazz.getStackTrace();
4093   // if (!fSuccess)alert("why no fSuccess?" + s)
4094         return function () {
4095   //System.out.println("returning " + (fSuccess ? fSuccess.toString() : "no function ") + s) 
4096                 if (forError && __debuggingBH)Clazz.alert ("############ forError=" + forError + " path=" + path + " ####" + (forError ? "NOT" : "") + "LOADED###");
4097                 if (isGecko && this.timeoutHandle)
4098                         window.clearTimeout(this.timeoutHandle), this.timeoutHandle = null;
4099                 if (inLoadingThreads > 0)
4100                         inLoadingThreads--;
4101                 //System.out.println("w3ccalback for " + path + " " + inLoadingThreads + " threads")
4102                 this.onload = null;
4103                 this.onerror = null;
4104                 if (forError) 
4105                         alert ("There was a problem loading " + path);
4106                 _Loader.onScriptLoaded(path, true);
4107                 var node = this;                        
4108                 var f;
4109     if (fSuccess)
4110       f = function(_W3scriptFS){removeScriptNode(node);tryToLoadNext(path, fSuccess); };
4111     else
4112       f = function(_W3script){removeScriptNode(node);tryToLoadNext(path)};
4113                 if (loadingTimeLag >= 0)
4114                         window.setTimeout(function() { tryToLoadNext(path, f); }, loadingTimeLag);
4115                 else
4116                         tryToLoadNext(path, f);
4117         };
4118 };
4119
4120 /* private */
4121 var isLoadingEntryClass = true;
4122
4123 /* private */
4124 var besidesJavaPackage = false;
4125
4126 /**
4127  * After class is loaded, this method will be executed to check whether there
4128  * are classes in the dependency tree that need to be loaded.
4129  */
4130 /* private */
4131 var tryToLoadNext = function (file, fSuccess) {
4132         var node = mapPath2ClassNode["@" + file];
4133         if (!node) // maybe class tree root
4134                 return;
4135         var n;
4136   // check for content loaded
4137         var clazzes = classpathMap["$" + file];
4138         if (clazzes) {
4139                 for (var i = 0; i < clazzes.length; i++) {
4140                         var name = clazzes[i];
4141                         if (name != node.name && (n = findNode(name))) {
4142                                 if (n.status < Node.STATUS_CONTENT_LOADED) {
4143                                         n.status = Node.STATUS_CONTENT_LOADED;
4144                                         updateNode(n);
4145                                 }
4146                         } else {
4147                                 n = new Node();
4148                                 n.name = name;
4149                                 var pp = classpathMap["#" + name];
4150                                 if (!pp) {
4151                                         alert (name + " J2S error in tryToLoadNext");
4152                                         error("Java2Script implementation error! Please report this bug!");
4153                                 }
4154                                 n.path = pp;
4155                                 mappingPathNameNode (n.path, name, n);
4156                                 n.status = Node.STATUS_CONTENT_LOADED;
4157                                 addChildClassNode(clazzTreeRoot, n, false);
4158                                 updateNode(n);
4159                         }
4160                 }
4161         }
4162         if (node instanceof Array) {
4163                 for (var i = 0; i < node.length; i++) {
4164                         if (node[i].status < Node.STATUS_CONTENT_LOADED) {
4165                                 node[i].status = Node.STATUS_CONTENT_LOADED;
4166                                 updateNode(node[i]);
4167                         }
4168                 }
4169         } else if (node.status < Node.STATUS_CONTENT_LOADED) {
4170                 var stillLoading = false;
4171                 var ss = document.getElementsByTagName ("SCRIPT");
4172                 for (var i = 0; i < ss.length; i++) {
4173                         if (isIE) {
4174                                 if (ss[i].onreadystatechange && ss[i].onreadystatechange.path == node.path
4175                                                 && ss[i].readyState == "interactive") {
4176                                         stillLoading = true;
4177                                         break;
4178                                 }
4179                         } else if (ss[i].onload && ss[i].onload.path == node.path) {
4180                                 stillLoading = true;
4181                                 break;
4182                         }
4183                 }
4184                 if (!stillLoading) {
4185                         node.status = Node.STATUS_CONTENT_LOADED;
4186                         updateNode(node);
4187                 }
4188         }
4189         /*
4190          * Maybe in #optinalLoaded inside above _Loader#updateNode calls, 
4191          * _Loader.keepOnLoading is set false (Already loaded the wanted
4192          * classes), so here check to stop.
4193          */
4194          
4195         if (!_Loader.keepOnLoading) // set externally
4196                 return;
4197
4198  // check for a "must" class that has content and load it
4199         var cq;
4200         var working = true;
4201         if ((n = findNextMustClass(Node.STATUS_KNOWN))) {
4202                 loadClassNode(n);
4203                 while (inLoadingThreads < maxLoadingThreads) {
4204                         if (!(n = findNextMustClass(Node.STATUS_KNOWN)))
4205                                 break;
4206                         loadClassNode(n); // will increase inLoadingThreads!
4207                 }
4208         } else if ((cq = classQueue).length != 0) { 
4209                 /* queue must be loaded in order! */
4210                 n = cq.shift();
4211                 if (!loadedScripts[n.path] 
4212                                 || cq.length != 0 
4213                                 || !isLoadingEntryClass
4214                                 || n.musts.length
4215                                 || n.optionals.length) {
4216                         addChildClassNode(clazzTreeRoot, n, true);
4217                         loadScript(n, n.path, n.requiredBy, false);
4218                 } else if (isLoadingEntryClass) {
4219                         /*
4220                          * The first time reaching here is the time when ClassLoader
4221                          * is trying to load entry class. Class with #main method and
4222                          * is to be executed is called Entry Class.
4223                          *
4224                          * Here when loading entry class, ClassLoader should not call
4225                          * the next following loading script. This is because, those
4226                          * scripts will try to mark the class as loaded directly and
4227                          * then continue to call #onLoaded callback method,
4228                          * which results in an script error!
4229                          */
4230                         isLoadingEntryClass = false;
4231                 }
4232         } else if ((n = findNextRequiredClass(Node.STATUS_KNOWN))) {
4233                 loadClassNode(n);
4234                 while (inLoadingThreads < maxLoadingThreads) {
4235                         if (!(n = findNextRequiredClass(Node.STATUS_KNOWN)))
4236                                 break;
4237                         loadClassNode(n); // will increase inLoadingThreads!
4238                 }
4239         } else {
4240                 working = false;
4241         }
4242         if (working || inLoadingThreads > 0)
4243                 return;
4244   // 
4245   // now check all classes that MUST be loaded prior to initialization 
4246   // of some other class (static calls, extends, implements)
4247   // and all classes REQUIRED somewhere in that class, possibly by the constructor
4248   // (that is, "new xxxx()" called somewhere in code) and update them
4249   // that have content but are not declared already 
4250         var f = [findNextMustClass,findNextRequiredClass];
4251         var lastNode = null;
4252         for (var i = 0; i < 2; i++)
4253                 while ((n = f[i](Node.STATUS_CONTENT_LOADED))) {
4254                         if (i == 1 && lastNode === n) // Already existed cycle ?
4255                                 n.status = Node.STATUS_LOAD_COMPLETE;
4256                         updateNode(n);
4257                         lastNode = n;
4258                 }
4259     
4260   // check for load cycles
4261   
4262         while (true) {
4263                 tracks = [];
4264                 if (!checkCycle(clazzTreeRoot, file))
4265                         break;
4266         }
4267   
4268   // and update all MUST and REQUIRED classes that are declared already 
4269   
4270         for (var i = 0; i < 2; i++) {
4271                 lastNode = null;
4272                 while ((n = f[i](Node.STATUS_DECLARED))) {
4273                         if (lastNode === n) 
4274                                 break;
4275                         updateNode(lastNode = n);
4276                 }
4277         }
4278         var done = [];
4279         for (var i = 0; i < 2; i++) 
4280                 while ((n = f[i](Node.STATUS_DECLARED)))
4281                         done.push(n), n.status = Node.STATUS_LOAD_COMPLETE;
4282         if (done.length) {
4283                 for (var i = 0; i < done.length; i++)
4284                         destroyClassNode(done[i]);
4285                 for (var i = 0; i < done.length; i++)
4286                         if ((f = done[i].onLoaded))
4287                                 done[i].onLoaded = null, f();
4288         }
4289   
4290   
4291   
4292   
4293   
4294   
4295   
4296         //System.out.println(node.name + " loaded completely" + _Loader.onGlobalLoaded + "\n\n")
4297   if (fSuccess) {
4298     //System.out.println("tryToLoadNext firing " + _Loader._classCountOK + "/" + _Loader._classCountPending + " "   + fSuccess.toString() + " " + Clazz.getStackTrace())
4299           fSuccess();
4300   } else if (_Loader._classCountPending) {
4301     for (var name in _Loader._classPending) {
4302       var n = findNode(name);
4303       System.out.println("class left pending " + name + " " + n);
4304       if (n) {
4305         updateNode(n);
4306         break;
4307       }
4308     }
4309   } else {
4310     
4311  // System.out.println("I think I'm done " 
4312   // + _Loader._classCountOK + "/" + _Loader._classCountPending + " " 
4313    //+ _Loader.onGlobalLoaded.toString() + " " + Clazz.getStackTrace()
4314  //  )
4315     if (_Loader._checkLoad) {
4316       System.out.println("I think I'm done: SAEM call count: " + SAEMid);
4317       Clazz.showDuplicates(true);
4318     }
4319   }
4320         _Loader.onGlobalLoaded();
4321 };
4322
4323
4324 var tracks = [];
4325
4326 /*
4327  * There are classes reference cycles. Try to detect and break those cycles.
4328  */
4329 /* private */
4330 var checkCycle = function (node, file) {
4331         var ts = tracks;
4332         var len = ts.length;
4333   // add this node to tracks
4334         ts.push(node);
4335         var i = len;
4336         for (; --i >= 0;)
4337                 if (ts[i] === node && ts[i].status >= Node.STATUS_DECLARED) 
4338                         break;
4339         if (i >= 0) {
4340     // this node is already in tracks, and it has been declared already
4341     // for each node in tracks, set its status to "LOAD_COMPLETE"
4342     // update all parents, remove all parents, and fire its onLoaded function
4343     // then clear tracks and return true (keep checking)  
4344     if (_Loader._checkLoad) {
4345       var msg = "cycle found loading " + file + " for " + node;
4346       System.out.println(msg)
4347     } 
4348                 for (; i < len; i++) {
4349       var n = ts[i];
4350                         n.status = Node.STATUS_LOAD_COMPLETE;
4351                         destroyClassNode(n); // Same as above
4352                         for (var k = 0; k < n.parents.length; k++)
4353                                 updateNode(n.parents[k]);
4354                         n.parents = [];
4355       var f = n.onLoaded;
4356       if (_Loader._checkLoad) {
4357         var msg = "cycle setting status to LOAD_COMPLETE for " + n.name + (f ? " firing " + f.toString() : "");
4358         System.out.println(msg)
4359       } 
4360                         if (f)
4361                                 n.onLoaded = null, f();
4362                 }
4363                 ts.length = 0;
4364                 return true;
4365         }
4366         var a = [node.musts, node.optionals];
4367         for (var j = 0; j < 2; j++)
4368                 for (var r = a[j], i = r.length; --i >= 0;)
4369                         if (r[i].status == Node.STATUS_DECLARED && checkCycle(r[i], file)) 
4370                                 return true;
4371   // reset _tracks to its original length      
4372         ts.length = len;
4373         return false; // done 
4374 };
4375
4376
4377 _Loader._classCountPending = 0;
4378 _Loader._classCountOK = 0;
4379 _Loader._classPending = {};
4380
4381 _Loader.showPending = function() {
4382   var a = [];
4383   for (var name in _Loader._classPending) {
4384     var n = findNode(name);
4385     if (!n) {
4386       alert("No node for " + name);
4387       continue;
4388     }
4389     a.push(n);
4390     System.out.println(showNode("", "", n, "", 0));     
4391   }  
4392   return a;
4393 }
4394
4395 var showNode = function(s, names, node, inset, level) {
4396   names += "--" + node.name;
4397   s += names + "\n";
4398   if (level > 5) {
4399     s += inset + " ...\n";
4400     return s;
4401   }
4402   inset += "\t";
4403   s += inset + "status: " + node.status + "\n";
4404   if (node.parents && node.parents.length && node.parents[0] && node.parents[0].name) {
4405     s += inset + "parents: " + node.parents.length + "\n";
4406     for (var i = 0; i < node.parents.length; i++) {
4407       s = showNode(s, names, node.parents[i], inset + "\t", level+1);
4408     }
4409     s += "\n";
4410   }
4411 //  if (node.requiredBy) {
4412 //    s += inset + "requiredBy:\n";
4413 //    s = showNode(s, names, node.requiredBy, inset + "\t", level+1);
4414 //    s += "\n";
4415 //  }
4416   return s;    
4417 }     
4418
4419 /**
4420  * Update the dependency tree nodes recursively.
4421  */
4422 /* private */
4423 updateNode = function(node, _updateNode) {
4424         if (!node.name || node.status >= Node.STATUS_LOAD_COMPLETE) {
4425                 destroyClassNode(node);
4426                 return;
4427         }
4428         var ready = true;
4429   // check for declared and also having MUSTS
4430         if (node.musts.length && node.declaration) {
4431                 for (var mustLength = node.musts.length, i = mustLength; --i >= 0;) {
4432                         var n = node.musts[i];
4433                         n.requiredBy = node;
4434                         if (n.status < Node.STATUS_DECLARED && isClassDefined (n.name)) {
4435                                 var nns = []; // a stack for onLoaded events
4436                                 n.status = Node.STATUS_LOAD_COMPLETE;
4437                                 destroyClassNode(n); // Same as above
4438                                 if (n.declaration       && n.declaration.clazzList) {
4439                                         // For those classes within one *.js file, update them synchronously.
4440                                         for (var j = 0, list = n.declaration.clazzList, l = list.length; j < l; j++) {
4441                                                 var nn = findNode (list[j]);
4442                                                 if (nn && nn.status != Node.STATUS_LOAD_COMPLETE
4443                                                                 && nn !== n) {
4444                                                         nn.status = n.status;
4445                                                         nn.declaration = null;
4446                                                         destroyClassNode(nn);
4447                                                         nn.onLoaded && nns.push(nn);
4448                                                 }
4449                                         }
4450                                         n.declaration = null;
4451                                 }
4452         // fire all onLoaded events
4453                                 if (n.onLoaded)
4454                                         nns.push(n);
4455                                 for (var j = 0; j < nns.length; j++) {
4456                                         var onLoaded = nns[j].onLoaded;
4457                                         if (onLoaded) {
4458                                                 nns[j].onLoaded = null;
4459                                                 onLoaded();
4460                                         }
4461                                 }
4462                         } else {
4463                                 (n.status == Node.STATUS_CONTENT_LOADED) && updateNode(n); // musts may be changed
4464                                 if (n.status < Node.STATUS_DECLARED)
4465                                         ready = false;
4466                         }
4467                         if (node.musts.length != mustLength) {
4468                                 // length changed -- restart!
4469                                 i = mustLength = node.musts.length;
4470                                 ready = true;
4471                         }
4472                 }
4473         }
4474         if (!ready)
4475                 return;
4476         if (node.status < Node.STATUS_DECLARED) {
4477                 var decl = node.declaration;
4478                 if (decl)
4479                         decl(), decl.executed = true;
4480     if(_Loader._checkLoad) {
4481             if (_Loader._classPending[node.name]) {
4482               delete _Loader._classPending[node.name];
4483               _Loader._classCountOK;
4484               _Loader._classCountPending--;
4485 //              System.out.println("OK " + (_Loader._classCountOK) + " FOR " + node.name)
4486             }
4487     }
4488                 node.status = Node.STATUS_DECLARED;
4489                 if (definedClasses)
4490                         definedClasses[node.name] = true;
4491                 _Loader.onScriptInitialized(node.path);
4492                 if (node.declaration && node.declaration.clazzList) {
4493                         // For those classes within one *.js file, update them synchronously.
4494                         for (var j = 0, list = node.declaration.clazzList, l = list.length; j < l; j++) {
4495                                 var nn = findNode(list[j]);
4496                                 if (nn && nn.status != Node.STATUS_DECLARED
4497                                                 && nn !== node) {
4498                                         nn.status = Node.STATUS_DECLARED;
4499                                         if (definedClasses)
4500                                                 definedClasses[nn.name] = true;
4501                                         _Loader.onScriptInitialized(nn.path);
4502                                 }
4503                         }
4504                 }
4505         }
4506         var level = Node.STATUS_DECLARED;
4507         if (node.optionals.length == 0 && node.musts.length == 0
4508                         || node.status > Node.STATUS_KNOWN && !node.declaration
4509                         || checkStatusIs(node.musts, Node.STATUS_LOAD_COMPLETE)
4510                                         && checkStatusIs(node.optionals, Node.STATUS_LOAD_COMPLETE)) { 
4511                 level = Node.STATUS_LOAD_COMPLETE;
4512                 if (!doneLoading(node, level))
4513                         return false;
4514                         // For those classes within one *.js file, update them synchronously.
4515                 if (node.declaration && node.declaration.clazzList) {
4516                         for (var j = 0, list = node.declaration.clazzList, l = list.length; j < l; j++) {
4517                                 var nn = findNode(list[j]);
4518                                 if (nn && nn.status != level && nn !== node) {
4519                                         nn.declaration = null;
4520                                         if (!doneLoading(nn, level))
4521                                                 return false;
4522                                 }
4523                         }
4524                 }
4525         }
4526   // _Loader.updateParents = function (node, level, _updateParents)
4527         if (node.parents && node.parents.length) {
4528         for (var i = 0; i < node.parents.length; i++) {
4529                 var p = node.parents[i];
4530                 if (p.status < level) 
4531                         updateNode(p, p.name);
4532         }
4533         if (level == Node.STATUS_LOAD_COMPLETE)
4534                 node.parents = [];
4535   }
4536 };
4537
4538 /* private */
4539 var checkStatusIs = function(arr, status){
4540         for (var i = arr.length; --i >= 0;)
4541                 if (arr[i].status < status)
4542                         return false;
4543         return true;
4544 }
4545 /* private */
4546 var doneLoading = function(node, level, _doneLoading) {
4547         node.status = level;
4548         _Loader.onScriptCompleted(node.path);
4549   
4550         var onLoaded = node.onLoaded;
4551         if (onLoaded) {
4552                 node.onLoaded = null;
4553                 onLoaded();
4554                 if (!_Loader.keepOnLoading)
4555                         return false;
4556         }
4557   
4558         destroyClassNode(node);
4559         return true;
4560 }
4561
4562 /*
4563  * Be used to record already used random numbers. And next new random
4564  * number should not be in the property set.
4565  */
4566 /* private */
4567 var usedRandoms = {
4568   "r0.13412" : 1
4569 };
4570
4571 /* private */
4572 var getRnd = function() {
4573         while (true) { // get a unique random number
4574                 var rnd = Math.random();
4575                 var s = "r" + rnd;
4576                 if (!usedRandoms[s])
4577                         return (usedRandoms[s] = 1, clazzTreeRoot.random = rnd);
4578         }
4579 }
4580
4581 /* protected */
4582 var findNode = function(clazzName) {
4583         getRnd();
4584         return findNodeUnderNode(clazzName, clazzTreeRoot);
4585 };
4586
4587 /* private */
4588 var findNextRequiredClass = function(status) {
4589         getRnd();
4590         return findNextRequiredNode(clazzTreeRoot, status);
4591 };
4592
4593 /* private */
4594 var findNextMustClass = function(status) {
4595         return findNextMustNode(clazzTreeRoot, status);
4596 };
4597
4598 /* private */
4599 var findNodeUnderNode = function(clazzName, node) {
4600         var n;
4601         // node, then musts then optionals
4602         return (node.name == clazzName ? node 
4603                 : (n = findNodeWithin(clazzName, node.musts))
4604                 || (n = findNodeWithin(clazzName, node.optionals)) 
4605                 ? n : null);
4606 };
4607
4608 /* private */
4609 var findNodeWithin = function(name, arr) {
4610         var rnd = clazzTreeRoot.random;
4611         for (var i = arr.length; --i >= 0;) {
4612                 var n = arr[i];
4613                 if (n.name == name)
4614                         return n;
4615                 if (n.random != rnd) {
4616                         n.random = rnd;
4617                         if ((n = findNodeUnderNode(name, n)))
4618                                 return n;
4619                 }
4620         }
4621         return null;
4622 }
4623
4624 /* private */
4625 var checkStatus = function(n, status) {
4626         return (n.status == status 
4627                         && (status != Node.STATUS_KNOWN || !loadedScripts[n.path])
4628                         && (status == Node.STATUS_DECLARED      || !isClassDefined (n.name)));
4629 }
4630
4631 /* private */
4632 var findNextMustNode = function(node, status) {
4633         for (var i = node.musts.length; --i >= 0;) {
4634                 var n = node.musts[i];
4635                 if (checkStatus(n, status) || (n = findNextMustNode(n, status)))
4636                         return n;       
4637         }
4638         return (checkStatus(node, status) ? node : null); 
4639 };
4640
4641 /* private */
4642 var findNextRequiredNode = function (node, status) {
4643         // search musts first
4644         // search optionals second
4645         // search itself last
4646         var n;
4647         return ((n = searchClassArray(node.musts, status))
4648                 || (n = searchClassArray(node.optionals, status))
4649                 || checkStatus(n = node, status) ? n : null);
4650 };
4651
4652 /* private */
4653 var searchClassArray = function (arr, status) {
4654         if (arr) {
4655                 var rnd = clazzTreeRoot.random;
4656                 for (var i = 0; i < arr.length; i++) {
4657                         var n = arr[i];
4658                         if (checkStatus(n, status))
4659                                 return n;
4660                         if (n.random != rnd) {
4661                                 n.random = rnd; // mark as visited!
4662                                 if ((n = findNextRequiredNode(n, status)))
4663                                         return n;
4664                         }
4665                 }
4666         }
4667         return null;
4668 };
4669
4670 /**
4671  * This map variable is used to mark that *.js is correctly loaded.
4672  * In IE, _Loader has defects to detect whether a *.js is correctly
4673  * loaded or not, so inner loading mark is used for detecting.
4674  */
4675 /* private */
4676 var innerLoadedScripts = {};
4677
4678 /**
4679  * This method will be called in almost every *.js generated by Java2Script
4680  * compiler.
4681  */
4682 /* public */
4683 var load = function (musts, name, optionals, declaration) {
4684   // called as name.load in Jmol
4685         if (name instanceof Array) {
4686                 unwrapArray(name);
4687                 for (var i = 0; i < name.length; i++)
4688                         load(musts, name[i], optionals, declaration, name);
4689                 return;
4690         }       
4691
4692   if (_Loader._checkLoad) {
4693     if (_Loader._classPending[name]) {
4694       //alert("duplicate load for " + name)
4695     } else {
4696       _Loader._classPending[name] = 1;
4697       if (_Loader._classCountPending++ == 0)
4698         _Loader._classCountOK = 0;
4699       System.out.println("Loading class " + name);
4700     }
4701   }
4702
4703 //      if (clazz.charAt (0) == '$')
4704 //              clazz = "org.eclipse.s" + clazz.substring (1);
4705         var node = mapPath2ClassNode["#" + name];
4706         if (!node) { // load called inside *.z.js?
4707                 var n = findNode(name);
4708                 node = (n ? n : new Node());
4709                 node.name = name;
4710                 node.path = classpathMap["#" + name] || "unknown";
4711                 mappingPathNameNode(node.path, name, node);
4712                 node.status = Node.STATUS_KNOWN;
4713                 addChildClassNode(clazzTreeRoot, node, false);
4714         }
4715         processRequired(node, musts, true);
4716         if (arguments.length == 5 && declaration) {
4717                 declaration.status = node.status;
4718                 declaration.clazzList = arguments[4];
4719         }
4720         node.declaration = declaration;
4721         if (declaration) 
4722                 node.status = Node.STATUS_CONTENT_LOADED;
4723         processRequired(node, optionals, false);
4724 };
4725
4726 /* private */
4727 var processRequired = function(node, arr, isMust) {
4728         if (arr && arr.length) {
4729                 unwrapArray(arr);
4730                 for (var i = 0; i < arr.length; i++) {
4731                         var name = arr[i];
4732                         if (!name)
4733                                 continue;
4734                         if (isClassDefined(name)
4735                                         || isClassExcluded(name))
4736                                 continue;
4737                         var n = findNode(name);
4738                         if (!n) {
4739                                 n = new Node();
4740                                 n.name = name;
4741                                 n.status = Node.STATUS_KNOWN;
4742                         }
4743                         n.requiredBy = node;
4744                         addChildClassNode(node, n, isMust);
4745                 }
4746         }
4747 }
4748
4749 /*
4750  * Try to be compatiable of Clazz
4751  */
4752 if (window["Clazz"]) {
4753         Clazz.load = load;
4754 } else {
4755   _Loader.load = load;
4756 }  
4757 /**
4758  * Map different class to the same path! Many classes may be packed into
4759  * a *.z.js already.
4760  *
4761  * @path *.js path
4762  * @name class name
4763  * @node Node object
4764  */
4765 /* private */
4766 var mappingPathNameNode = function (path, name, node) {
4767         var map = mapPath2ClassNode;
4768         var keyPath = "@" + path;
4769         var v = map[keyPath];
4770         if (v) {
4771                 if (v instanceof Array) {
4772                         var existed = false;
4773                         for (var i = 0; i < v.length; i++) {
4774                                 if (v[i].name == name) {
4775                                         existed = true;
4776                                         break;
4777                                 }
4778                         }
4779                         if (!existed)
4780                                 v.push(node);
4781                 } else {
4782                         map[keyPath] = [v, node];
4783                 }
4784         } else {
4785                 map[keyPath] = node;
4786         }
4787         map["#" + name] = node;
4788 };
4789
4790 /* protected */
4791 var loadClassNode = function (node) {
4792         var name = node.name;
4793         if (!isClassDefined (name) 
4794                         && !isClassExcluded (name)) {
4795                 var path = _Loader.getClasspathFor (name/*, true*/);
4796                 node.path = path;
4797                 mappingPathNameNode (path, name, node);
4798                 if (!loadedScripts[path]) {
4799                         loadScript(node, path, node.requiredBy, false);
4800                         return true;
4801                 }
4802         }
4803         return false;
4804 };
4805
4806
4807 /**
4808  * Used in package
4809 /* public */
4810 var runtimeKeyClass = _Loader.runtimeKeyClass = "java.lang.String";
4811
4812 /**
4813  * Queue used to store classes before key class is loaded.
4814  */
4815 /* private */
4816 var queueBe4KeyClazz = [];
4817
4818 /* private */
4819 var J2sLibBase;
4820
4821 /**
4822  * Return J2SLib base path from existed SCRIPT src attribute.
4823  */
4824 /* public */
4825 _Loader.getJ2SLibBase = function () {
4826         var o = window["j2s.lib"];
4827         return (o ? o.base + (o.alias == "." ? "" : (o.alias ? o.alias : (o.version ? o.version : "1.0.0")) + "/") : null);
4828 };
4829
4830 /**
4831  * Indicate whether _Loader is loading script synchronously or 
4832  * asynchronously.
4833  */
4834 /* private */
4835 var isAsynchronousLoading = true;
4836
4837 /* private */
4838 var isUsingXMLHttpRequest = false;
4839
4840 /* private */
4841 var loadingTimeLag = -1;
4842
4843 _Loader.MODE_SCRIPT = 4;
4844 _Loader.MODE_XHR = 2;
4845 _Loader.MODE_SYNC = 1;
4846
4847 /**
4848  * String mode:
4849  * asynchronous modes:
4850  * async(...).script, async(...).xhr, async(...).xmlhttprequest,
4851  * script.async(...), xhr.async(...), xmlhttprequest.async(...),
4852  * script
4853  * 
4854  * synchronous modes:
4855  * sync(...).xhr, sync(...).xmlhttprequest,
4856  * xhr.sync(...), xmlhttprequest.sync(...),
4857  * xmlhttprequest, xhr
4858  *                                                    
4859  * Integer mode:
4860  * Script 4; XHR 2; SYNC bit 1; 
4861  */
4862 /* public */
4863 _Loader.setLoadingMode = function (mode, timeLag) {
4864         var async = true;
4865         var ajax = true;
4866         if (typeof mode == "string") {
4867                 mode = mode.toLowerCase();
4868                 if (mode.indexOf("script") >= 0)
4869                         ajax = false;
4870                 else
4871                         async = (mode.indexOf("async") >=0);
4872                 async = false; // BH
4873         } else {
4874                 if (mode & _Loader.MODE_SCRIPT)
4875                         ajax = false;
4876                 else
4877                         async = !(mode & _Loader.MODE_SYNC);
4878         }
4879         isUsingXMLHttpRequest = ajax;
4880         isAsynchronousLoading = async;
4881         loadingTimeLag = (async && timeLag >= 0 ? timeLag: -1);
4882         return async;
4883 };
4884
4885 /* private */
4886 var runtimeLoaded = function () {
4887         if (pkgRefCount || !isClassDefined(runtimeKeyClass))
4888                 return;
4889         var qbs = queueBe4KeyClazz;
4890         for (var i = 0; i < qbs.length; i++)
4891                 _Loader.loadClass(qbs[i][0], qbs[i][1]);
4892         queueBe4KeyClazz = [];
4893 };
4894
4895 /*
4896  * Load those key *.z.js. This *.z.js will be surely loaded before other 
4897  * queued *.js.
4898  */
4899 /* public */
4900 _Loader.loadZJar = function (zjarPath, keyClass) {
4901 // used only by package.js for core.z.js
4902         var f = null;
4903         var isArr = (keyClass instanceof Array);
4904         if (isArr)
4905                 keyClass = keyClass[keyClass.length - 1];
4906         else
4907                 f = (keyClass == runtimeKeyClass ? runtimeLoaded : null);                       
4908         _Loader.jarClasspath(zjarPath, isArr ? keyClass : [keyClass]);
4909         // BH note: runtimeKeyClass is java.lang.String 
4910         _Loader.loadClass(keyClass, f, true);
4911 };
4912
4913 var NodeMap = {};
4914 var _allNodes = [];
4915
4916 /**
4917  * The method help constructing the multiple-binary class dependency tree.
4918  */
4919 /* private */
4920 var addChildClassNode = function (parent, child, isMust) {
4921         var existed = false;
4922         var arr;
4923         if (isMust) {
4924                 arr = parent.musts;
4925                 if (!child.requiredBy)
4926                         child.requiredBy = parent;
4927 //              if (!parent.requiresMap){
4928 //                      parent.requires = [];
4929 //                      parent.requiresMap = {};
4930 //              }
4931 //              if (!parent.requiresMap[child.name]) {
4932 //                      parent.requiresMap[child.name] = 1;
4933 //                      parent.requires.push[child];
4934 //              }
4935         } else {
4936                 arr = parent.optionals;
4937         }
4938         if (!NodeMap[child.name]) {
4939                 _allNodes.push(child)
4940                 NodeMap[child.name]=child
4941         }
4942         for (var i = 0; i < arr.length; i++) {
4943                 if (arr[i].name == child.name) {
4944                         existed = true;
4945                         break;
4946                 }
4947         }
4948         if (!existed) {
4949                 arr.push(child);
4950                 if (isLoadingEntryClass 
4951                                 && child.name.indexOf("java") != 0 
4952                                 && child.name.indexOf("net.sf.j2s.ajax") != 0) {
4953                         if (besidesJavaPackage)
4954                                 isLoadingEntryClass = false;
4955                         besidesJavaPackage = true;
4956 //              } else if (child.name.indexOf("org.eclipse.swt") == 0 
4957 //                              || child.name.indexOf("$wt") == 0) {
4958 //                      window["swt.lazy.loading.callback"] = swtLazyLoading;
4959 //                      if (needPackage("org.eclipse.swt"))
4960 //                              return _Loader.loadPackage("org.eclipse.swt", function() {addParentClassNode(child, parent)});
4961                 }
4962         }
4963         addParentClassNode(child, parent);
4964 };
4965
4966 /* private */
4967 var addParentClassNode = function(child, parent) {
4968         if (parent.name && parent != clazzTreeRoot && parent != child)
4969                 for (var i = 0; i < child.parents.length; i++)
4970                         if (child.parents[i].name == parent.name)
4971                                 return;
4972         child.parents.push(parent);
4973 }
4974
4975 /* private */
4976 var destroyClassNode = function (node) {
4977         var parents = node.parents;
4978         if (parents)
4979                 for (var k = parents.length; --k >= 0;)
4980                         removeArrayItem(parents[k].musts, node) || removeArrayItem(parents[k].optionals, node);
4981 };
4982
4983 /* public */
4984 _Loader.unloadClassExt = function (qClazzName) {
4985         if (definedClasses)
4986                 definedClasses[qClazzName] = false;
4987         if (classpathMap["#" + qClazzName]) {
4988                 var pp = classpathMap["#" + qClazzName];
4989                 classpathMap["#" + qClazzName] = null;
4990                 var arr = classpathMap["$" + pp];
4991                 removeArrayItem(arr, qClazzName) && (classpathMap["$" + pp] = arr);
4992         }
4993         var n = findNode(qClazzName);
4994         if (n) {
4995                 n.status = Node.STATUS_KNOWN;
4996                 loadedScripts[n.path] = false;
4997         }
4998         var path = _Loader.getClasspathFor (qClazzName);
4999         loadedScripts[path] = false;
5000         innerLoadedScripts[path] && (innerLoadedScripts[path] = false);
5001         _Loader.onClassUnloaded(qClazzName);
5002 };
5003
5004 /* private */
5005 var assureInnerClass = function (clzz, fun) {
5006         clzz = clzz.__CLASS_NAME__;
5007         if (Clazz.unloadedClasses[clzz]) {
5008                 if (clzz.indexOf("$") >= 0)
5009                         return;
5010                 var list = [];
5011                 var key = clzz + "$";
5012                 for (var s in Clazz.unloadedClasses)
5013                         if (Clazz.unloadedClasses[s] && s.indexOf(key) == 0)
5014                                 list.push(s);
5015                 if (!list.length) 
5016                         return;
5017                 fun = "" + fun;
5018                 var idx1, idx2;
5019                 if ((idx1 = fun.indexOf(key)) < 0 || (idx2 = fun.indexOf("\"", idx1 + key.length)) < 0) 
5020                         return;
5021                 clzz = fun.substring(idx1, idx2);
5022                 if (!Clazz.unloadedClasses[clzz] || (idx1 = fun.indexOf("{", idx2) + 1) == 0)
5023                         return;
5024                 if ((idx2 = fun.indexOf("(" + clzz + ",", idx1 + 3)) < 0
5025                         || (idx2 = fun.lastIndexOf("}", idx2 - 1)) < 0)
5026                                 return;
5027                 eval(fun.substring(idx1, idx2));
5028                 Clazz.unloadedClasses[clzz] = null;
5029         }
5030 };
5031
5032 Clazz.binaryFolders =  _Loader.binaryFolders = [ _Loader.getJ2SLibBase() ];
5033
5034 })(Clazz, Clazz._Loader);
5035
5036 //}
5037 /******************************************************************************
5038  * Copyright (c) 2007 java2script.org and others.
5039  * All rights reserved. This program and the accompanying materials
5040  * are made available under the terms of the Eclipse Public License v1.0
5041  * which accompanies this distribution, and is available at
5042  * http://www.eclipse.org/legal/epl-v10.html
5043  *
5044  * Contributors:
5045  *     Zhou Renjian - initial API and implementation
5046  *****************************************************************************/
5047 /*******
5048  * @author zhou renjian
5049  * @create Jan 11, 2007
5050  *******/
5051
5052 Clazz._LoaderProgressMonitor = {};
5053
5054 ;(function(CLPM, Jmol) {
5055
5056 var fadeOutTimer = null;
5057 var fadeAlpha = 0;
5058 var monitorEl = null;
5059 var lastScrollTop = 0;
5060 var bindingParent = null;
5061
5062 CLPM.DEFAULT_OPACITY = (Jmol && Jmol._j2sLoadMonitorOpacity ? Jmol._j2sLoadMonitorOpacity : 55);
5063
5064 /* public */
5065 /*CLPM.initialize = function (parent) {
5066         bindingParent = parent;
5067         if (parent && !attached) {
5068                 attached = true;
5069                 //Clazz.addEvent (window, "unload", cleanup);
5070                 // window.attachEvent ("onunload", cleanup);
5071         }
5072 };
5073 */
5074
5075 /* public */
5076 CLPM.hideMonitor = function () {
5077         monitorEl.style.display = "none";
5078 }
5079
5080 /* public */
5081 CLPM.showStatus = function (msg, fading) {
5082         if (!monitorEl) {
5083                 createHandle ();
5084                 if (!attached) {
5085                         attached = true;
5086                         //Clazz.addEvent (window, "unload", cleanup);
5087                         // window.attachEvent ("onunload", cleanup);
5088                 }
5089         }
5090         clearChildren(monitorEl);
5091   if (msg == null) {
5092     if (fading) {
5093       fadeOut();
5094     } else {
5095         CLPM.hideMonitor();
5096     }
5097     return;
5098   }
5099   
5100         monitorEl.appendChild(document.createTextNode ("" + msg));
5101         if (monitorEl.style.display == "none") {
5102                 monitorEl.style.display = "";
5103         }
5104         setAlpha(CLPM.DEFAULT_OPACITY);
5105         var offTop = getFixedOffsetTop();
5106         if (lastScrollTop != offTop) {
5107                 lastScrollTop = offTop;
5108                 monitorEl.style.bottom = (lastScrollTop + 4) + "px";
5109         }
5110         if (fading) {
5111                 fadeOut();
5112         }
5113 };
5114
5115 /* private static */ 
5116 var clearChildren = function (el) {
5117         if (!el)
5118                 return;
5119         for (var i = el.childNodes.length; --i >= 0;) {
5120                 var child = el.childNodes[i];
5121                 if (!child)
5122                         continue;
5123                 if (child.childNodes && child.childNodes.length)
5124                         clearChildren (child);
5125                 try {
5126                         el.removeChild (child);
5127                 } catch (e) {};
5128         }
5129 };
5130 /* private */ 
5131 var setAlpha = function (alpha) {
5132         if (fadeOutTimer && alpha == CLPM.DEFAULT_OPACITY) {
5133                 window.clearTimeout (fadeOutTimer);
5134                 fadeOutTimer = null;
5135         }
5136         fadeAlpha = alpha;
5137         var ua = navigator.userAgent.toLowerCase();
5138         monitorEl.style.filter = "Alpha(Opacity=" + alpha + ")";
5139         monitorEl.style.opacity = alpha / 100.0;
5140 };
5141 /* private */ 
5142 var hidingOnMouseOver = function () {
5143   CLPM.hideMonitor();
5144 };
5145
5146 /* private */ 
5147 var attached = false;
5148 /* private */ 
5149 var cleanup = function () {
5150         //if (monitorEl) {
5151         //      monitorEl.onmouseover = null;
5152         //}
5153         monitorEl = null;
5154         bindingParent = null;
5155         //Clazz.removeEvent (window, "unload", cleanup);
5156         //window.detachEvent ("onunload", cleanup);
5157         attached = false;
5158 };
5159 /* private */ 
5160 var createHandle = function () {
5161         var div = document.createElement ("DIV");
5162         div.id = "_Loader-status";
5163         div.style.cssText = "position:absolute;bottom:4px;left:4px;padding:2px 8px;"
5164                         + "z-index:" + (window["j2s.lib"].monitorZIndex || 10000) + ";background-color:#8e0000;color:yellow;" 
5165                         + "font-family:Arial, sans-serif;font-size:10pt;white-space:nowrap;";
5166         div.onmouseover = hidingOnMouseOver;
5167         monitorEl = div;
5168         if (bindingParent) {
5169                 bindingParent.appendChild(div);
5170         } else {
5171                 document.body.appendChild(div);
5172         }
5173         return div;
5174 };
5175 /* private */ 
5176
5177 var fadeOut = function () {
5178         if (monitorEl.style.display == "none") return;
5179         if (fadeAlpha == CLPM.DEFAULT_OPACITY) {
5180                 fadeOutTimer = window.setTimeout(function () {
5181                                         fadeOut();
5182                                 }, 750);
5183                 fadeAlpha -= 5;
5184         } else if (fadeAlpha - 10 >= 0) {
5185                 setAlpha(fadeAlpha - 10);
5186                 fadeOutTimer = window.setTimeout(function () {
5187                                         fadeOut();
5188                                 }, 40);
5189         } else {
5190                 monitorEl.style.display = "none";
5191         }
5192 };
5193 /* private */
5194 var getFixedOffsetTop = function (){
5195         if (bindingParent) {
5196                 var b = bindingParent;
5197                 return b.scrollTop;
5198         }
5199         var dua = navigator.userAgent;
5200         var b = document.body;
5201         var p = b.parentNode;
5202         var pcHeight = p.clientHeight;
5203         var bcScrollTop = b.scrollTop + b.offsetTop;
5204         var pcScrollTop = p.scrollTop + p.offsetTop;
5205         return (dua.indexOf("Opera") < 0 && document.all ? (pcHeight == 0 ? bcScrollTop : pcScrollTop)
5206                 : dua.indexOf("Gecko") < 0 ? (pcHeight == p.offsetHeight 
5207                                 && pcHeight == p.scrollHeight ? bcScrollTop : pcScrollTop) : bcScrollTop);
5208 };
5209
5210 /* not used in Jmol
5211 if (window["ClazzLoader"]) {
5212         _Loader.onScriptLoading = function(file) {
5213                 CLPM.showStatus("Loading " + file + "...");
5214         };
5215         _Loader.onScriptLoaded = function(file, isError) {
5216                 CLPM.showStatus(file + (isError ? " loading failed." : " loaded."), true);
5217         };
5218         _Loader.onGlobalLoaded = function(file) {
5219                 CLPM.showStatus("Application loaded.", true);
5220         };
5221         _Loader.onClassUnloaded = function(clazz) {
5222                 CLPM.showStatus("Class " + clazz + " is unloaded.", true);
5223   };
5224 }
5225 */
5226
5227 })(Clazz._LoaderProgressMonitor, Jmol);
5228
5229 //}
5230 /******************************************************************************
5231  * Copyright (c) 2007 java2script.org and others.
5232  * All rights reserved. This program and the accompanying materials
5233  * are made available under the terms of the Eclipse Public License v1.0
5234  * which accompanies this distribution, and is available at
5235  * http://www.eclipse.org/legal/epl-v10.html
5236  *
5237  * Contributors:
5238  *     Zhou Renjian - initial API and implementation
5239  *****************************************************************************/
5240 /*******
5241  * @author zhou renjian
5242  * @create Nov 5, 2005
5243  *******/
5244
5245 ;(function(Con, Sys) {
5246 /**
5247  * Setting maxTotalLines to -1 will not limit the console result
5248  */
5249 /* protected */
5250 Con.maxTotalLines =     10000;
5251
5252 /* protected */
5253 Con.setMaxTotalLines = function (lines) {
5254         Con.maxTotalLines = (lines > 0 ? lines : 999999);
5255 }
5256
5257 /* protected */
5258 Con.maxLatency = 40;
5259
5260 /* protected */
5261 Con.setMaxLatency = function (latency) {
5262         Con.maxLatency = (latency > 0 ? latency : 40);
5263 };
5264
5265 /* protected */
5266 Con.pinning  = false;
5267
5268 /* protected */
5269 Con.enablePinning = function (enabled) {
5270         Con.pinning = enabled;
5271 };
5272
5273 /* private */
5274 Con.linesCount = 0;
5275
5276 /* private */
5277 Con.metLineBreak = false;
5278
5279
5280 /*
5281  * Give an extension point so external script can create and bind the console
5282  * themself.
5283  *
5284  * TODO: provide more template of binding console window to browser.
5285  */
5286 /* protected */
5287 Con.createConsoleWindow = function (parentEl) {
5288         var console = document.createElement ("DIV");
5289         console.style.cssText = "font-family:monospace, Arial, sans-serif;";
5290         document.body.appendChild (console);
5291         return console;
5292 };
5293
5294 var c160 = String.fromCharCode(160); //nbsp;
5295 c160 += c160+c160+c160;
5296
5297 /* protected */
5298 Con.consoleOutput = function (s, color) {
5299         var o = window["j2s.lib"];
5300         var console = (o && o.console);
5301         if (console && typeof console == "string")
5302                 console = document.getElementById(console)
5303         if (!console)
5304                 return false; // BH this just means we have turned off all console action
5305         if (Con.linesCount > Con.maxTotalLines) {
5306                 for (var i = 0; i < Con.linesCount - Con.maxTotalLines; i++) {
5307                         if (console && console.childNodes.length > 0) {
5308                                 console.removeChild (console.childNodes[0]);
5309                         }
5310                 }
5311                 Con.linesCount = Con.maxTotalLines;
5312         }
5313
5314         var willMeetLineBreak = false;
5315         s = (typeof s == "undefined" ? "" : s == null ? "null" : "" + s);
5316         s = s.replace (/\t/g, c160);
5317         if (s.length > 0)
5318                 switch (s.charAt(s.length - 1)) {
5319                 case '\n':
5320                 case '\r':
5321                         s = (s.length > 1 ? s.substring (0, s.length - (s.charAt (s.length - 2) == '\r' ? 2 : 1)) : "");
5322                         willMeetLineBreak = true;
5323                         break;
5324                 }
5325
5326         var lines = null;
5327         s = s.replace (/\t/g, c160);
5328         lines = s.split(/\r\n|\r|\n/g);
5329         for (var i = 0, last = lines.length - 1; i <= last; i++) {
5330                 var lastLineEl = null;
5331                 if (Con.metLineBreak || Con.linesCount == 0 
5332                                 || console.childNodes.length < 1) {
5333                         lastLineEl = document.createElement ("DIV");
5334                         console.appendChild (lastLineEl);
5335                         lastLineEl.style.whiteSpace = "nowrap";
5336                         Con.linesCount++;
5337                 } else {
5338                         try {
5339                                 lastLineEl = console.childNodes[console.childNodes.length - 1];
5340                         } catch (e) {
5341                                 lastLineEl = document.createElement ("DIV");
5342                                 console.appendChild (lastLineEl);
5343                                 lastLineEl.style.whiteSpace = "nowrap";
5344                                 Con.linesCount++;
5345                         }
5346                 }
5347                 var el = document.createElement ("SPAN");
5348                 lastLineEl.appendChild (el);
5349                 el.style.whiteSpace = "nowrap";
5350                 if (color)
5351                         el.style.color = color;
5352                 var l = lines[i]
5353                 if (l.length == 0)
5354                         l = c160;
5355                 el.appendChild(document.createTextNode(l));
5356                 if (!Con.pinning)
5357                         console.scrollTop += 100;
5358                 Con.metLineBreak = (i != last || willMeetLineBreak);
5359         }
5360
5361         var cssClazzName = console.parentNode.className;
5362         if (!Con.pinning && cssClazzName
5363                         && cssClazzName.indexOf ("composite") != -1) {
5364                 console.parentNode.scrollTop = console.parentNode.scrollHeight;
5365         }
5366         Con.lastOutputTime = new Date ().getTime ();
5367 };
5368
5369 /*
5370  * Clear all contents inside the console.
5371  */
5372 /* public */
5373 Con.clear = function () {
5374         try {
5375                 Con.metLineBreak = true;
5376                 var o = window["j2s.lib"];
5377                 var console = o && o.console;
5378                 if (!console || !(console = document.getElementById (console)))
5379                         return;
5380                 var childNodes = console.childNodes;
5381                 for (var i = childNodes.length; --i >= 0;)
5382                         console.removeChild (childNodes[i]);
5383                 Con.linesCount = 0;
5384         } catch(e){};
5385 };
5386
5387 /* public */
5388 Clazz.alert = function (s) {
5389         Con.consoleOutput (s + "\r\n");
5390 };
5391
5392
5393 /* public */
5394 Sys.out.print = function (s) { 
5395         Con.consoleOutput (s);
5396 };
5397 /* public */
5398 Sys.out.println = function(s) { 
5399         Con.consoleOutput(typeof s == "undefined" ? "\r\n" : s == null ?  s = "null\r\n" : s + "\r\n");
5400 };
5401
5402 Sys.out.write = function (buf, offset, len) {
5403         Sys.out.print(String.instantialize(buf).substring(offset, offset+len));
5404 };
5405
5406 /* public */
5407 Sys.err.__CLASS_NAME__ = "java.io.PrintStream";
5408
5409 /* public */
5410 Sys.err.print = function (s) { 
5411         Con.consoleOutput (s, "red");
5412 };
5413
5414 /* public */
5415 Sys.err.println = function (s) {
5416         Con.consoleOutput (typeof s == "undefined" ? "\r\n" : s == null ?  s = "null\r\n" : s + "\r\n", "red");
5417 };
5418
5419 Sys.err.write = function (buf, offset, len) {
5420         Sys.err.print(String.instantialize(buf).substring(offset, offset+len));
5421 };
5422
5423 })(Clazz.Console, System);
5424
5425 })(Clazz, Jmol); // requires JSmolCore.js
5426
5427 }; // called by external application