Search in sources :

Example 1 with FormAndComponent

use of com.servoy.j2db.scripting.JSApplication.FormAndComponent 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 FormAndComponent

use of com.servoy.j2db.scripting.JSApplication.FormAndComponent in project servoy-client by Servoy.

the class BasicFormController method executeFunction.

@SuppressWarnings("nls")
protected Object executeFunction(Function f, Object[] args, Scriptable scope, Scriptable thisObject, boolean saveData, Object src, boolean testFindMode, boolean focusEvent, String methodKey, boolean executeWhenFieldValidationFailed, boolean useFormAsEventSourceEventually, boolean throwException) throws Exception {
    if (// only run certain methods in find
    !(testFindMode && isInFindMode())) {
        // this is a semi saveData , we do NOT want the record go out of edit(!) and is updated in db
        if (saveData) {
            application.getFoundSetManager().getEditRecordList().prepareForSave(false);
        }
        if (f != null) {
            if (!executeWhenFieldValidationFailed && Boolean.TRUE.equals(application.getRuntimeProperties().get(IServiceProvider.RT_LASTFIELDVALIDATIONFAILED_FLAG))) {
                if (Debug.tracing()) {
                    Debug.trace("Function not executed because a field is marked invalid");
                }
                return null;
            }
            FormAndComponent formAndComponent = getJSApplicationNames(src, f, useFormAsEventSourceEventually);
            try {
                currentFormExecutingFunctionCount.incrementAndGet();
                Object[] newArgs = args;
                if (formAndComponent != null) {
                    // for use of deprecated aplication.getMethodTriggerElementName() and aplication.getMethodTriggerFormName()
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().pushLastNames(formAndComponent);
                    }
                    if (methodKey != null) {
                        // add form event if needed
                        MethodTemplate methodTemplate = MethodTemplate.getTemplate(null, methodKey);
                        if (methodTemplate != null) {
                            MethodArgument[] methodArguments = methodTemplate.getArguments();
                            for (int i = 0; methodArguments != null && i < methodArguments.length; i++) {
                                if (methodArguments[i].getType() == ArgumentType.JSEvent) {
                                    // method template declares an event argument
                                    if (args == null || args.length <= i || args[i] == null) {
                                        // no event argument there yet, insert a form event
                                        JSEvent event = getJSEvent(formAndComponent.src, methodKey);
                                        if (args == null || args.length <= i) {
                                            newArgs = new Object[i + 1];
                                            if (args != null) {
                                                System.arraycopy(args, 0, newArgs, 0, args.length);
                                            }
                                        }
                                        newArgs[i] = event;
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
                if (newArgs != null && newArgs.length > 0) {
                    for (Object newArg : newArgs) {
                        if (newArg instanceof JSEvent) {
                            JSEvent event = (JSEvent) newArg;
                            if (formScope != null && event.getSource() instanceof IComponent && ((IComponent) event.getSource()).getName() != null) {
                                Object elementScope = formScope.get("elements");
                                if (elementScope instanceof Scriptable) {
                                    Object elementSrc = ((Scriptable) elementScope).get(((IComponent) event.getSource()).getName(), (Scriptable) elementScope);
                                    if (elementSrc != null) {
                                        if (elementSrc instanceof ITwoNativeJavaObject) {
                                            Object scriptable = event.getSource();
                                            if (scriptable instanceof IScriptableProvider) {
                                                scriptable = ((IScriptableProvider) scriptable).getScriptObject();
                                            }
                                            ((ITwoNativeJavaObject) elementSrc).setRealObject(scriptable);
                                        }
                                        event.setSource(elementSrc);
                                    }
                                }
                            }
                            break;
                        }
                    }
                }
                return application.getScriptEngine().executeFunction(f, scope, thisObject, newArgs, focusEvent, throwException);
            } finally {
                currentFormExecutingFunctionCount.decrementAndGet();
                if (formAndComponent != null) {
                    IExecutingEnviroment scriptEngine = application.getScriptEngine();
                    if (scriptEngine instanceof ScriptEngine) {
                        ((ScriptEngine) scriptEngine).getJSApplication().popLastStackNames(formAndComponent);
                    }
                }
                // after a script clear the unchanged records so that no records keep hanging around.
                if (!focusEvent && !"onRecordEditStopMethodID".equals(methodKey) && !"onRenderMethodID".equals(methodKey) && application.getFoundSetManager() != null) {
                    application.getFoundSetManager().getEditRecordList().removeUnChangedRecords(false, false);
                }
            }
        }
    }
    return null;
}
Also used : IExecutingEnviroment(com.servoy.j2db.scripting.IExecutingEnviroment) JSEvent(com.servoy.j2db.scripting.JSEvent) MethodArgument(com.servoy.j2db.persistence.MethodArgument) IComponent(com.servoy.j2db.ui.IComponent) MethodTemplate(com.servoy.j2db.persistence.MethodTemplate) Scriptable(org.mozilla.javascript.Scriptable) FormAndComponent(com.servoy.j2db.scripting.JSApplication.FormAndComponent) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject) IScriptableProvider(com.servoy.j2db.scripting.IScriptableProvider)

Aggregations

IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)2 FormAndComponent (com.servoy.j2db.scripting.JSApplication.FormAndComponent)2 IComponent (com.servoy.j2db.ui.IComponent)2 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)2 Scriptable (org.mozilla.javascript.Scriptable)2 MethodArgument (com.servoy.j2db.persistence.MethodArgument)1 MethodTemplate (com.servoy.j2db.persistence.MethodTemplate)1 ElementScope (com.servoy.j2db.scripting.ElementScope)1 IExecutingEnviroment (com.servoy.j2db.scripting.IExecutingEnviroment)1 IScriptable (com.servoy.j2db.scripting.IScriptable)1 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)1 JSEvent (com.servoy.j2db.scripting.JSEvent)1 ScriptEngine (com.servoy.j2db.scripting.ScriptEngine)1 Component (java.awt.Component)1 Container (java.awt.Container)1 JComponent (javax.swing.JComponent)1 JavaMembers (org.mozilla.javascript.JavaMembers)1 ScriptableObject (org.mozilla.javascript.ScriptableObject)1