Search in sources :

Example 1 with ISupportScriptProviders

use of com.servoy.j2db.persistence.ISupportScriptProviders in project servoy-client by Servoy.

the class FoundSet method getPrototype.

public Scriptable getPrototype() {
    if (prototypeScope == null) {
        LazyCompilationScope scope = new LazyCompilationScope(this, fsm.getApplication().getScriptEngine(), new ISupportScriptProviders() {

            public Iterator<? extends IScriptProvider> getScriptMethods(boolean sort) {
                List<ScriptMethod> methods = null;
                Iterator<TableNode> tableNodes = fsm.getApplication().getFlattenedSolution().getTableNodes(getTable());
                while (tableNodes.hasNext()) {
                    TableNode tn = tableNodes.next();
                    Iterator<ScriptMethod> fsMethods = tn.getFoundsetMethods(sort);
                    if (methods == null) {
                        if (!tableNodes.hasNext()) {
                            // just 1
                            return fsMethods;
                        }
                        methods = new ArrayList<ScriptMethod>();
                    }
                    while (fsMethods.hasNext()) {
                        methods.add(fsMethods.next());
                    }
                }
                return methods == null ? Collections.<ScriptMethod>emptyList().iterator() : methods.iterator();
            }

            public Iterator<ScriptVariable> getScriptVariables(boolean b) {
                return Collections.<ScriptVariable>emptyList().iterator();
            }

            public ScriptMethod getScriptMethod(int methodId) {
                // not called by LCS
                return null;
            }
        }) {

            @Override
            public String getClassName() {
                // $NON-NLS-1$
                return "FoundSetScope";
            }

            @Override
            public String getScopeName() {
                return getDataSource();
            }
        };
        // make sure functions like getSize cannot be overridden
        scope.setFunctionParentScriptable(this);
        prototypeScope = scope;
    }
    return prototypeScope;
}
Also used : ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) IScriptProvider(com.servoy.j2db.persistence.IScriptProvider) Iterator(java.util.Iterator) TableNode(com.servoy.j2db.persistence.TableNode) ArrayList(java.util.ArrayList) SafeArrayList(com.servoy.j2db.util.SafeArrayList) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) SafeArrayList(com.servoy.j2db.util.SafeArrayList) Collectors.toList(java.util.stream.Collectors.toList) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 2 with ISupportScriptProviders

use of com.servoy.j2db.persistence.ISupportScriptProviders in project servoy-client by Servoy.

the class ScriptEngine method getTableScope.

public Scriptable getTableScope(final ITable table) {
    if (tableScopes == null)
        tableScopes = new HashMap<ITable, Scriptable>();
    Scriptable tableScope = null;
    synchronized (table) {
        tableScope = tableScopes.get(table);
        if (tableScope == null) {
            Context.enter();
            try {
                tableScope = new TableScope(solutionScope, this, table, application.getFlattenedSolution(), new ISupportScriptProviders() {

                    public Iterator<? extends IScriptProvider> getScriptMethods(boolean sort) {
                        return application.getFlattenedSolution().getScriptCalculations(table, false);
                    }

                    public Iterator<ScriptVariable> getScriptVariables(boolean b) {
                        return Collections.<ScriptVariable>emptyList().iterator();
                    }

                    public ScriptMethod getScriptMethod(int methodId) {
                        // is not used for calculations
                        return null;
                    }
                });
                tableScopes.put(table, tableScope);
            } finally {
                Context.exit();
            }
        }
    }
    return tableScope;
}
Also used : ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) HashMap(java.util.HashMap) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) Scriptable(org.mozilla.javascript.Scriptable)

Example 3 with ISupportScriptProviders

use of com.servoy.j2db.persistence.ISupportScriptProviders in project servoy-client by Servoy.

the class BasicFormController method initForJSUsage.

/**
 * Initialize this FormController(or related classes/methods) to be used in javascript
 */
public synchronized JSForm initForJSUsage(CreationalPrototype creationalPrototype) {
    if (formScope == null) {
        try {
            // make scope for state delegation via prototype mechanism
            List<Form> forms = application.getFlattenedSolution().getFormHierarchy(form);
            List<ISupportScriptProviders> scriptProviders = new ArrayList<ISupportScriptProviders>();
            for (Form frm : forms) {
                scriptProviders.add(new RuntimeSupportScriptProviders(application, frm));
            }
            formScope = new FormScope(this, scriptProviders.toArray(new ISupportScriptProviders[scriptProviders.size()]));
            if (formModel != null) {
                ITable formTable = application.getFoundSetManager().getTable(form.getDataSource());
                formScope.setPrototype(new SelectedRecordScope(this, formTable == null ? null : application.getScriptEngine().getTableScope(formTable)));
                // $NON-NLS-1$
                formScope.putWithoutFireChange("foundset", formModel);
            }
            // create JS place holder for this object
            scriptableForm = new BasicFormController.JSForm(this);
            // set parent scope
            NativeJavaObject formObject = new NativeJavaObject(formScope, scriptableForm, ScriptObjectRegistry.getJavaMembers(FormController.JSForm.class, formScope));
            // $NON-NLS-1$
            formScope.putWithoutFireChange("controller", formObject);
            // register the place holder 'scriptableForm' in CreationalPrototype scope
            creationalPrototype.setLocked(false);
            // $NON-NLS-1$
            creationalPrototype.put(((Integer) creationalPrototype.get("length", creationalPrototype)).intValue(), creationalPrototype, formScope);
            creationalPrototype.put(getName(), creationalPrototype, formScope);
            creationalPrototype.setLocked(true);
            formScope.createVars();
        } catch (Exception ex) {
            // $NON-NLS-1$
            application.reportError(application.getI18NMessage("servoy.formPanel.error.initFormScope") + ": " + getName(), ex);
            return null;
        }
    }
    return scriptableForm;
}
Also used : Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) ServoyException(com.servoy.j2db.util.ServoyException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) FormScope(com.servoy.j2db.scripting.FormScope) ISupportScriptProviders(com.servoy.j2db.persistence.ISupportScriptProviders) RuntimeSupportScriptProviders(com.servoy.j2db.FormController.RuntimeSupportScriptProviders) SelectedRecordScope(com.servoy.j2db.scripting.SelectedRecordScope) ITable(com.servoy.j2db.persistence.ITable) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ITwoNativeJavaObject(com.servoy.j2db.scripting.ITwoNativeJavaObject)

Aggregations

ISupportScriptProviders (com.servoy.j2db.persistence.ISupportScriptProviders)3 ScriptVariable (com.servoy.j2db.persistence.ScriptVariable)2 ArrayList (java.util.ArrayList)2 RuntimeSupportScriptProviders (com.servoy.j2db.FormController.RuntimeSupportScriptProviders)1 Form (com.servoy.j2db.persistence.Form)1 IScriptProvider (com.servoy.j2db.persistence.IScriptProvider)1 ITable (com.servoy.j2db.persistence.ITable)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1 TableNode (com.servoy.j2db.persistence.TableNode)1 FormScope (com.servoy.j2db.scripting.FormScope)1 ITwoNativeJavaObject (com.servoy.j2db.scripting.ITwoNativeJavaObject)1 LazyCompilationScope (com.servoy.j2db.scripting.LazyCompilationScope)1 SelectedRecordScope (com.servoy.j2db.scripting.SelectedRecordScope)1 SafeArrayList (com.servoy.j2db.util.SafeArrayList)1 ServoyException (com.servoy.j2db.util.ServoyException)1 Arrays.asList (java.util.Arrays.asList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1