Search in sources :

Example 1 with JavaMembers

use of org.mozilla.javascript.JavaMembers in project servoy-client by Servoy.

the class FormController method getJSApplicationNames.

/**
 * @param src
 * @param function
 * @param useFormAsEventSourceEventually
 */
@Override
protected FormAndComponent getJSApplicationNames(Object source, Function function, boolean useFormAsEventSourceEventually) {
    Object src = source;
    if (src == null) {
        Object window = application.getRuntimeWindowManager().getCurrentWindowWrappedObject();
        if (window != null) {
            src = ((Window) window).getFocusOwner();
            while (src != null && !(src instanceof IComponent)) {
                src = ((Component) src).getParent();
            }
            // Test if this component really comes from the the controllers UI.
            if (src instanceof Component) {
                Container container = ((Component) src).getParent();
                while (container != null && !(container instanceof IFormUIInternal<?>)) {
                    container = container.getParent();
                }
                if (container != getFormUI()) {
                    // if not then this is not the trigger element for this form.
                    src = null;
                }
            }
        }
    }
    Scriptable thisObject = null;
    if (src instanceof IComponent && src instanceof IScriptableProvider) {
        // $NON-NLS-1$
        Object esObj = formScope.get("elements", formScope);
        if (esObj != Scriptable.NOT_FOUND) {
            ElementScope es = (ElementScope) esObj;
            String name = ((IComponent) src).getName();
            if (name != null && name.length() != 0) {
                Object o = es.get(name, es);
                if (o instanceof Scriptable) {
                    thisObject = (Scriptable) o;
                }
            }
            if (thisObject == null) {
                if (name == null || name.length() == 0) {
                    name = ComponentFactory.WEB_ID_PREFIX + System.currentTimeMillis();
                    // Check Web components always have a name! Because name can't be set
                    ((IComponent) src).setName(name);
                }
                IScriptable scriptObject = ((IScriptableProvider) src).getScriptObject();
                if (scriptObject != null) {
                    try {
                        Context.enter();
                        JavaMembers jm = ScriptObjectRegistry.getJavaMembers(scriptObject.getClass(), ScriptableObject.getTopLevelScope(formScope));
                        thisObject = new NativeJavaObject(formScope, scriptObject, jm);
                        es.setLocked(false);
                        es.put(name, es, thisObject);
                        es.setLocked(true);
                    } finally {
                        Context.exit();
                    }
                }
            }
        }
    }
    if (src == null && useFormAsEventSourceEventually)
        src = formScope;
    return new FormAndComponent(src, getName());
}
Also used : JavaMembers(org.mozilla.javascript.JavaMembers) IComponent(com.servoy.j2db.ui.IComponent) ElementScope(com.servoy.j2db.scripting.ElementScope) Scriptable(org.mozilla.javascript.Scriptable) IScriptable(com.servoy.j2db.scripting.IScriptable) IScriptable(com.servoy.j2db.scripting.IScriptable) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) Container(java.awt.Container) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IComponent(com.servoy.j2db.ui.IComponent) Component(java.awt.Component) JComponent(javax.swing.JComponent) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 2 with JavaMembers

use of org.mozilla.javascript.JavaMembers in project servoy-client by Servoy.

the class SwingForm method registerComponentsToScope.

private int registerComponentsToScope(Scriptable fs, ElementScope es, int counter, Object[] comps, Object[] compsRenderer, Component controller, Map<String, Object[]> hmChildrenJavaMembers) {
    if (comps != null) {
        for (int j = 0; j < comps.length; j++) {
            Object comp = comps[j];
            if (comp instanceof PortalComponent) {
                PortalComponent portal = (PortalComponent) comp;
                counter = registerComponentsToScope(fs, es, counter, portal.getEditorComponents(), portal.getRendererComponents(), portal, hmChildrenJavaMembers);
            }
            String name = null;
            if (comp instanceof IComponent) {
                name = ((IComponent) comp).getName();
            } else if (comp instanceof Component) {
                name = ((Component) comp).getName();
            }
            Object obj = comp;
            if (comp instanceof InvisibleBean) {
                obj = ((InvisibleBean) comp).getDelegate();
            } else if (comp instanceof VisibleBean) {
                obj = ((VisibleBean) comp).getDelegate();
            }
            String groupName = FormElementGroup.getName((String) formController.getComponentProperty(comp, ComponentFactory.GROUPID_COMPONENT_PROPERTY));
            if (obj instanceof IScriptableProvider)
                obj = ((IScriptableProvider) obj).getScriptObject();
            IRuntimeComponent baseMethodsObj = null;
            if (obj instanceof IRuntimeComponent) {
                baseMethodsObj = (IRuntimeComponent) obj;
            }
            JavaMembers jm = ScriptObjectRegistry.getJavaMembers(obj.getClass(), ScriptableObject.getTopLevelScope(fs));
            // $NON-NLS-1$
            boolean named = name != null && !name.equals("") && !name.startsWith(ComponentFactory.WEB_ID_PREFIX);
            if (groupName != null || named) {
                Object obj2 = null;
                if (compsRenderer != null) {
                    obj2 = compsRenderer[j];
                    if (obj2 instanceof InvisibleBean) {
                        obj2 = ((InvisibleBean) obj2).getDelegate();
                    }
                }
                try {
                    Scriptable s = null;
                    if (obj2 != null) {
                        if (obj2 instanceof IScriptableProvider)
                            obj2 = ((IScriptableProvider) obj2).getScriptObject();
                        NativeJavaObject s2 = new NativeJavaObject(fs, obj2, jm);
                        s = new TwoNativeJavaObject(fs, obj, s2, jm, controller);
                        // group properties have to be set to both
                        if (groupName != null && obj2 instanceof IRuntimeComponent) {
                            if (baseMethodsObj == null) {
                                baseMethodsObj = (IRuntimeComponent) obj2;
                            } else {
                                RuntimeGroup runtimeGroup = new RuntimeGroup(baseMethodsObj.getName());
                                runtimeGroup.addScriptBaseMethodsObj(baseMethodsObj);
                                runtimeGroup.addScriptBaseMethodsObj((IRuntimeComponent) obj2);
                                baseMethodsObj = runtimeGroup;
                            }
                        }
                    } else {
                        s = new NativeJavaObject(fs, obj, jm);
                    }
                    if (named) {
                        es.put(name, fs, s);
                        es.put(counter++, fs, s);
                        hmChildrenJavaMembers.put(name, new Object[] { jm, obj });
                    }
                    if (groupName != null) {
                        Object group = es.get(groupName, fs);
                        if (group == Scriptable.NOT_FOUND) {
                            group = new NativeJavaObject(fs, new RuntimeGroup(groupName), ScriptObjectRegistry.getJavaMembers(RuntimeGroup.class, ScriptableObject.getTopLevelScope(fs)));
                            es.put(groupName, fs, group);
                            es.put(counter++, fs, group);
                        }
                        if (baseMethodsObj != null && group instanceof NativeJavaObject && ((NativeJavaObject) group).unwrap() instanceof RuntimeGroup) {
                            ((RuntimeGroup) (((NativeJavaObject) group).unwrap())).addScriptBaseMethodsObj(baseMethodsObj);
                        }
                    }
                } catch (Throwable ex) {
                    // incase classdefnot founds are thrown for beans,applets/plugins
                    Debug.error(ex);
                }
            }
        }
    }
    return counter;
}
Also used : JavaMembers(org.mozilla.javascript.JavaMembers) IComponent(com.servoy.j2db.ui.IComponent) TwoNativeJavaObject(com.servoy.j2db.smart.scripting.TwoNativeJavaObject) PortalComponent(com.servoy.j2db.smart.dataui.PortalComponent) VisibleBean(com.servoy.j2db.smart.dataui.VisibleBean) InvisibleBean(com.servoy.j2db.smart.dataui.InvisibleBean) Scriptable(org.mozilla.javascript.Scriptable) IScriptable(com.servoy.j2db.scripting.IScriptable) Point(java.awt.Point) RuntimeGroup(com.servoy.j2db.scripting.RuntimeGroup) TwoNativeJavaObject(com.servoy.j2db.smart.scripting.TwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IComponent(com.servoy.j2db.ui.IComponent) Component(java.awt.Component) PortalComponent(com.servoy.j2db.smart.dataui.PortalComponent) JComponent(javax.swing.JComponent) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) TwoNativeJavaObject(com.servoy.j2db.smart.scripting.TwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 3 with JavaMembers

use of org.mozilla.javascript.JavaMembers in project servoy-client by Servoy.

the class WebForm method registerComponentsToScope.

private int registerComponentsToScope(Scriptable fs, ElementScope es, int counter, Object[] comps, Map<String, Object[]> hmChildrenJavaMembers, Object parent) {
    if (comps != null) {
        for (Object comp : comps) {
            if (comp instanceof WebCellBasedView) {
                WebCellBasedView portal = (WebCellBasedView) comp;
                counter = registerComponentsToScope(fs, es, counter, portal.getComponents(), hmChildrenJavaMembers, comp);
            }
            String name = null;
            if (comp instanceof WrapperContainer) {
                comp = ((WrapperContainer) comp).getDelegate();
            }
            if (comp instanceof IComponent) {
                name = ((IComponent) comp).getName();
            } else if (comp instanceof java.awt.Component) {
                name = ((java.awt.Component) comp).getName();
            }
            if (comp instanceof WebImageBeanHolder) {
                comp = ((WebImageBeanHolder) comp).getDelegate();
            } else if (comp instanceof WebBeanHolder) {
                comp = ((WebBeanHolder) comp).getDelegate();
            }
            String groupName = FormElementGroup.getName((String) formController.getComponentProperty(comp, ComponentFactory.GROUPID_COMPONENT_PROPERTY));
            Object scriptable = comp;
            if (comp instanceof IScriptableProvider)
                scriptable = ((IScriptableProvider) comp).getScriptObject();
            JavaMembers jm = ScriptObjectRegistry.getJavaMembers(scriptable.getClass(), ScriptableObject.getTopLevelScope(fs));
            // $NON-NLS-1$
            boolean named = name != null && !name.equals("") && !name.startsWith(ComponentFactory.WEB_ID_PREFIX);
            if (groupName != null || named) {
                try {
                    Scriptable s;
                    if (parent instanceof WebCellBasedView) {
                        s = new CellNativeJavaObject(fs, comp, jm, (WebCellBasedView) parent);
                    } else {
                        s = new NativeJavaObject(fs, scriptable, jm);
                    }
                    if (named) {
                        es.put(name, fs, s);
                        es.put(counter++, fs, s);
                        hmChildrenJavaMembers.put(name, new Object[] { jm, scriptable });
                    }
                    if (groupName != null) {
                        Object group = es.get(groupName, fs);
                        if (group == Scriptable.NOT_FOUND) {
                            group = new NativeJavaObject(fs, new RuntimeGroup(groupName), ScriptObjectRegistry.getJavaMembers(RuntimeGroup.class, ScriptableObject.getTopLevelScope(fs)));
                            es.put(groupName, fs, group);
                            es.put(counter++, fs, group);
                        }
                        if (scriptable instanceof IRuntimeComponent && group instanceof NativeJavaObject && ((NativeJavaObject) group).unwrap() instanceof RuntimeGroup) {
                            ((RuntimeGroup) (((NativeJavaObject) group).unwrap())).addScriptBaseMethodsObj((IRuntimeComponent) scriptable);
                        }
                    }
                } catch (Throwable ex) {
                    // incase classdefnot founds are thrown for beans,applets/plugins
                    Debug.error(ex);
                }
            }
        }
    }
    return counter;
}
Also used : WebImageBeanHolder(com.servoy.j2db.server.headlessclient.dataui.WebImageBeanHolder) JavaMembers(org.mozilla.javascript.JavaMembers) IComponent(com.servoy.j2db.ui.IComponent) WebCellBasedView(com.servoy.j2db.server.headlessclient.dataui.WebCellBasedView) Scriptable(org.mozilla.javascript.Scriptable) RuntimeGroup(com.servoy.j2db.scripting.RuntimeGroup) WebBeanHolder(com.servoy.j2db.server.headlessclient.dataui.WebBeanHolder) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IComponent(com.servoy.j2db.ui.IComponent) BaseComponent(com.servoy.j2db.persistence.BaseComponent) IFieldComponent(com.servoy.j2db.ui.IFieldComponent) Component(org.apache.wicket.Component) IRuntimeComponent(com.servoy.j2db.ui.runtime.IRuntimeComponent) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 4 with JavaMembers

use of org.mozilla.javascript.JavaMembers in project servoy-client by Servoy.

the class InstanceJavaMembers method registerClass.

static void registerClass(Scriptable scope, Class<?> cls, InstanceJavaMembers ijm) {
    ClassCache cache = ClassCache.get(scope);
    Map<Class<?>, JavaMembers> ct = cache.getClassCacheMap();
    ct.put(cls, ijm);
}
Also used : JavaMembers(org.mozilla.javascript.JavaMembers) ClassCache(org.mozilla.javascript.ClassCache)

Example 5 with JavaMembers

use of org.mozilla.javascript.JavaMembers in project servoy-client by Servoy.

the class ScriptObjectRegistry method getJavaMembers.

public static JavaMembers getJavaMembers(final Class<?> clss, Scriptable scope) {
    if (clss.isArray() || clss.isPrimitive() || clss == String.class || clss == Object.class || Number.class.isAssignableFrom(clss) || Date.class.isAssignableFrom(clss)) {
        return null;
    }
    Object key = scope;
    if (scope == null) {
        key = NULL_SCOPE;
    } else {
        key = ScriptableObject.getTopLevelScope(scope);
    }
    Map<Class<?>, JavaMembers> map = javaMembersCache.get(key);
    if (map == null) {
        map = new ConcurrentHashMap<Class<?>, JavaMembers>();
        javaMembersCache.put(key, map);
    }
    JavaMembers jm = map.get(clss);
    if (jm == null) {
        try {
            Context.enter();
            InstanceJavaMembers ijm;
            if (clss != DataException.class) {
                ijm = new InstanceJavaMembers(scope, clss);
            } else {
                // ugly workaround to hide the existence of an inherited deprecated method isServoyException (it was tested for existence in JS) and constants
                ijm = new InstanceJavaMembers(scope, clss) {

                    @Override
                    protected void reflectField(Scriptable scope, Field field) {
                        if (field.getDeclaringClass() == cl && IConstantsObject.class.isAssignableFrom(cl) && Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()) && Modifier.isPublic(field.getModifiers())) {
                            super.reflectField(scope, field);
                        }
                    }

                    /*
						 * (non-Javadoc)
						 *
						 * @see com.servoy.j2db.scripting.InstanceJavaMembers#isJsMethod(java.lang.String)
						 */
                    @Override
                    protected boolean isJsMethod(Method method, Class<?> originalClass) {
                        return super.isJsMethod(method, originalClass) && !"js_isServoyException".equals(method.getName());
                    }
                };
            }
            jm = ijm;
            if (ijm.getFieldIds(false).size() == 0 && ijm.getMethodIds(false).size() == 0 && ijm.getFieldIds(true).size() == 0 && !ReferenceOnlyInJS.class.isAssignableFrom(clss)) {
                jm = new JavaMembers(scope, clss);
            } else {
                if (scope != null) {
                    InstanceJavaMembers.registerClass(scope, clss, ijm);
                }
            }
            map.put(clss, jm);
        } catch (Throwable e) {
            // this is throwable to not let the smart client not start when a (plugin) class can;'t be found
            // $NON-NLS-1$
            Debug.error("Error creating java members returning null", e);
        } finally {
            Context.exit();
        }
    }
    return jm;
}
Also used : JavaMembers(org.mozilla.javascript.JavaMembers) Method(java.lang.reflect.Method) Scriptable(org.mozilla.javascript.Scriptable) Date(java.util.Date) Field(java.lang.reflect.Field) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Aggregations

JavaMembers (org.mozilla.javascript.JavaMembers)5 Scriptable (org.mozilla.javascript.Scriptable)4 ScriptableObject (org.mozilla.javascript.ScriptableObject)4 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)3 IComponent (com.servoy.j2db.ui.IComponent)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 IScriptable (com.servoy.j2db.scripting.IScriptable)2 RuntimeGroup (com.servoy.j2db.scripting.RuntimeGroup)2 IRuntimeComponent (com.servoy.j2db.ui.runtime.IRuntimeComponent)2 Component (java.awt.Component)2 JComponent (javax.swing.JComponent)2 BaseComponent (com.servoy.j2db.persistence.BaseComponent)1 ElementScope (com.servoy.j2db.scripting.ElementScope)1 FormAndComponent (com.servoy.j2db.scripting.JSApplication.FormAndComponent)1 WebBeanHolder (com.servoy.j2db.server.headlessclient.dataui.WebBeanHolder)1 WebCellBasedView (com.servoy.j2db.server.headlessclient.dataui.WebCellBasedView)1 WebImageBeanHolder (com.servoy.j2db.server.headlessclient.dataui.WebImageBeanHolder)1 InvisibleBean (com.servoy.j2db.smart.dataui.InvisibleBean)1 PortalComponent (com.servoy.j2db.smart.dataui.PortalComponent)1 VisibleBean (com.servoy.j2db.smart.dataui.VisibleBean)1