KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lobobrowser > js > JavaConstructorObject


1 /*
2     GNU LESSER GENERAL PUBLIC LICENSE
3     Copyright (C) 2006 The Lobo Project
4
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9
10     This library is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13     Lesser General Public License for more details.
14
15     You should have received a copy of the GNU Lesser General Public
16     License along with this library; if not, write to the Free Software
17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19     Contact info: xamjadmin@users.sourceforge.net
20 */

21 package org.lobobrowser.js;
22
23 import org.mozilla.javascript.*;
24
25 public class JavaConstructorObject extends ScriptableObject implements Function {
26     private final JavaClassWrapper classWrapper;
27     private final JavaInstantiator instantiator;
28     private final String JavaDoc name;
29     
30     public JavaConstructorObject(String JavaDoc name, JavaClassWrapper classWrapper) {
31         this.name = name;
32         this.classWrapper = classWrapper;
33         this.instantiator = new SimpleInstantiator(classWrapper);
34     }
35
36     public JavaConstructorObject(String JavaDoc name, JavaClassWrapper classWrapper, JavaInstantiator instantiator) {
37         this.name = name;
38         this.classWrapper = classWrapper;
39         this.instantiator = instantiator;
40     }
41
42     public String JavaDoc getClassName() {
43         return this.name;
44     }
45
46     public Object JavaDoc call(Context cx, Scriptable scope, Scriptable thisObj, Object JavaDoc[] args) {
47         throw new UnsupportedOperationException JavaDoc();
48     }
49
50     public Scriptable construct(Context cx, Scriptable scope, Object JavaDoc[] args) {
51         try {
52             Object JavaDoc javaObject = this.instantiator.newInstance();
53             Scriptable newObject = new JavaObjectWrapper(this.classWrapper, javaObject);
54             newObject.setParentScope(scope);
55             return newObject;
56         } catch(Exception JavaDoc err) {
57             throw new IllegalStateException JavaDoc(err.getMessage());
58         }
59     }
60     
61     public java.lang.Object JavaDoc getDefaultValue(java.lang.Class JavaDoc hint) {
62         if(String JavaDoc.class.equals(hint)) {
63             return "function " + this.name;
64         }
65         else {
66             return super.getDefaultValue(hint);
67         }
68     }
69     
70     public static class SimpleInstantiator implements JavaInstantiator {
71         private final JavaClassWrapper classWrapper;
72
73         public SimpleInstantiator(final JavaClassWrapper classWrapper) {
74             super();
75             this.classWrapper = classWrapper;
76         }
77
78         public Object JavaDoc newInstance() throws InstantiationException JavaDoc, IllegalAccessException JavaDoc {
79             return this.classWrapper.newInstance();
80         }
81     }
82 }
83
Popular Tags