Search in sources :

Example 1 with ElementScope

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

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

the class SwingForm method makeElementsScriptObject.

/**
 * @see com.servoy.j2db.IFormUIInternal#makeElementsScriptObject(org.mozilla.javascript.Scriptable)
 */
public ElementScope makeElementsScriptObject(Scriptable fs, Map<String, Object[]> hmChildrenJavaMembers, IDataRenderer[] dataRenderers, IView controller) {
    ElementScope es = new ElementScope(fs);
    int counter = 0;
    for (int i = FormController.FORM_RENDERER + 1; i < dataRenderers.length; i++) {
        IDataRenderer dr = dataRenderers[i];
        if (dr == null)
            continue;
        Object[] comps = null;
        Object[] compsRenderer = null;
        if (dr instanceof DataRenderer) {
            comps = ((DataRenderer) dr).getComponents();
            if (i == FormController.FORM_EDITOR && dataRenderers[FormController.FORM_RENDERER] != null) {
                compsRenderer = ((DataRenderer) dataRenderers[FormController.FORM_RENDERER]).getComponents();
            }
        } else if (dr instanceof TableView) {
            TableView tv = (TableView) dr;
            List<CellAdapter> nonViewableColumns = tv.getNonViewableColumns();
            int componentCount = tv.getColumnCount() + nonViewableColumns.size();
            comps = new Component[componentCount];
            compsRenderer = new Component[componentCount];
            CellAdapter[] tableCellAdapters = new CellAdapter[componentCount];
            for (int j = 0; j < componentCount; j++) {
                tableCellAdapters[j] = (j < tv.getColumnCount()) ? (CellAdapter) tv.getCellEditor(0, j) : nonViewableColumns.get(j - tv.getColumnCount());
                comps[j] = tableCellAdapters[j].getEditor();
                compsRenderer[j] = tableCellAdapters[j].getRenderer();
            }
            for (int j = 0; j < comps.length; j++) {
                if (compsRenderer[j] instanceof Component && !((Component) compsRenderer[j]).isVisible()) {
                    tv.getColumnModel().removeColumn(tableCellAdapters[j]);
                }
            }
        } else if (dr instanceof WebMarkupContainer) {
            comps = new Object[((WebMarkupContainer) dr).size()];
            Iterator<?> it = ((WebMarkupContainer) dr).iterator();
            int j = 0;
            while (it.hasNext()) {
                comps[j++] = it.next();
            }
        }
        counter = registerComponentsToScope(fs, es, counter, comps, compsRenderer, (Component) controller, hmChildrenJavaMembers);
    }
    es.setLocked(true);
    return es;
}
Also used : IDataRenderer(com.servoy.j2db.ui.IDataRenderer) DataRenderer(com.servoy.j2db.smart.dataui.DataRenderer) IDataRenderer(com.servoy.j2db.ui.IDataRenderer) ElementScope(com.servoy.j2db.scripting.ElementScope) TwoNativeJavaObject(com.servoy.j2db.smart.scripting.TwoNativeJavaObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) PageList(com.servoy.j2db.printing.PageList) ArrayList(java.util.ArrayList) List(java.util.List) JList(javax.swing.JList) 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) Point(java.awt.Point) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer)

Example 3 with ElementScope

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

the class WebForm method makeElementsScriptObject.

/**
 * @see com.servoy.j2db.IFormUIInternal#makeElementsScriptObject(org.mozilla.javascript.Scriptable, java.util.Map, com.servoy.j2db.ui.IDataRenderer[],
 *      com.servoy.j2db.IView)
 */
public ElementScope makeElementsScriptObject(Scriptable fs, Map<String, Object[]> hmChildrenJavaMembers, IDataRenderer[] dataRenderers, IView v) {
    ElementScope es = new ElementScope(fs);
    int counter = 0;
    for (int i = FormController.FORM_RENDERER + 1; i < dataRenderers.length; i++) {
        IDataRenderer dr = dataRenderers[i];
        if (dr == null)
            continue;
        Object[] comps = null;
        if (dr instanceof WebMarkupContainer) {
            comps = new Object[((WebMarkupContainer) dr).size()];
            Iterator it = ((WebMarkupContainer) dr).iterator();
            int j = 0;
            while (it.hasNext()) {
                comps[j++] = it.next();
            }
        }
        counter = registerComponentsToScope(fs, es, counter, comps, hmChildrenJavaMembers, dr);
    }
    if (v instanceof WebCellBasedView) {
        Object[] comps = ((WebCellBasedView) v).getComponents();
        counter = registerComponentsToScope(fs, es, counter, comps, hmChildrenJavaMembers, v);
    }
    es.setLocked(true);
    return es;
}
Also used : IDataRenderer(com.servoy.j2db.ui.IDataRenderer) ElementScope(com.servoy.j2db.scripting.ElementScope) Iterator(java.util.Iterator) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) WebCellBasedView(com.servoy.j2db.server.headlessclient.dataui.WebCellBasedView) Point(java.awt.Point) WebMarkupContainer(org.apache.wicket.markup.html.WebMarkupContainer)

Example 4 with ElementScope

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

the class JSEventType method fillJSEvent.

/**
 * @param event Event that needs to be filled
 * @param jsonObject The json data for that has the event data.
 * @param webObject The webObject element (WEbFormComponent or WebFormUI)
 * @param controller Optional the controller object if the caller knows this already
 * @return
 */
@SuppressWarnings("nls")
public static void fillJSEvent(JSBaseEvent event, JSONObject jsonObject, BaseWebObject webObject, IWebFormController controller) {
    event.setType(jsonObject.optString("eventType"));
    String formName = controller != null ? controller.getName() : "";
    String elementName = "";
    if (webObject instanceof WebFormComponent) {
        elementName = ((WebFormComponent) webObject).getFormElement().getRawName();
        if (elementName == null)
            elementName = "";
        if (formName.isEmpty()) {
            BaseWebObject parentWebObject = ((WebFormComponent) webObject).getParent();
            while (parentWebObject != null && !(parentWebObject instanceof WebFormUI)) {
                parentWebObject = ((WebFormComponent) parentWebObject).getParent();
            }
            if (parentWebObject instanceof WebFormUI) {
                formName = parentWebObject.getName();
            }
        }
    }
    if (formName.isEmpty()) {
        formName = jsonObject.optString("formName");
    }
    if (formName.isEmpty() && webObject instanceof WebFormUI) {
        // executeInlineScript with an event in params tells a DAL to execute it and it gives a formui as context to the fromJSON conversion
        // for JSEventType problem is that it can give any formName from the client (the component/service can give anything there as an arg); or, if the function
        // (sent to client before through "function" type) that will be used to execute the script is a global/scope function
        // then formName can be null and in that case the WebFormUI will be the main form or the window (not the most nested one)
        // so this is just a fallback and we do give priority to the form name determined on client through $window.createJSEvent(...) an put into
        // "jsonObject" (see if above) - to target the correct form - closest one to event
        formName = webObject.getName();
    }
    if (elementName.isEmpty()) {
        elementName = jsonObject.optString("elementName");
    }
    if (!formName.isEmpty())
        event.setFormName(formName);
    if (!elementName.isEmpty())
        event.setElementName(elementName);
    if (!formName.isEmpty()) {
        INGApplication application = ((IContextProvider) webObject).getDataConverterContext().getApplication();
        IWebFormController formController = controller != null ? controller : application.getFormManager().getForm(formName);
        if (formController != null) {
            FormScope formScope = formController.getFormScope();
            if (formScope != null) {
                ElementScope elementsScope = (ElementScope) formScope.get("elements", null);
                if (elementsScope != null) {
                    Object scriptableElement = !elementName.isEmpty() ? elementsScope.get(elementName, null) : null;
                    if (scriptableElement != null && scriptableElement != Scriptable.NOT_FOUND) {
                        event.setSource(scriptableElement);
                    } else if (webObject instanceof WebFormComponent) {
                        // quickly create a scriptable wrapper around the component so that the source can be set to a value that we expect.
                        FormElement fe = ((WebFormComponent) webObject).getFormElement();
                        RuntimeWebComponent runtimeComponent = new RuntimeWebComponent((WebFormComponent) webObject, webObject.getSpecification());
                        if (fe.isLegacy() || ((fe.getForm().getView() == IForm.LIST_VIEW || fe.getForm().getView() == FormController.LOCKED_LIST_VIEW || fe.getForm().getView() == FormController.TABLE_VIEW || fe.getForm().getView() == FormController.LOCKED_TABLE_VIEW) && fe.getTypeName().startsWith("svy-"))) {
                            // add legacy behavior
                            runtimeComponent.setPrototype(new RuntimeLegacyComponent((WebFormComponent) webObject));
                        }
                        event.setSource(runtimeComponent);
                    }
                }
            }
        }
    }
    try {
        if (jsonObject.has("x"))
            event.setLocation(new Point(jsonObject.optInt("x"), jsonObject.optInt("y")));
        if (jsonObject.has("modifiers"))
            event.setModifiers(jsonObject.optInt("modifiers"));
        if (jsonObject.has("data"))
            event.setData(jsonObject.opt("data"));
        if (jsonObject.has("timestamp"))
            event.setTimestamp(new Timestamp(jsonObject.getLong("timestamp")));
        else
            event.setTimestamp(new Date());
    } catch (Exception e) {
        Debug.error("error setting event properties from " + jsonObject + ", for component: " + elementName + " on form " + formName, e);
    }
}
Also used : WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) ElementScope(com.servoy.j2db.scripting.ElementScope) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Point(java.awt.Point) RuntimeWebComponent(com.servoy.j2db.server.ngclient.component.RuntimeWebComponent) Timestamp(java.sql.Timestamp) FormElement(com.servoy.j2db.server.ngclient.FormElement) Date(java.util.Date) JSONException(org.json.JSONException) BaseWebObject(org.sablo.BaseWebObject) FormScope(com.servoy.j2db.scripting.FormScope) WebFormUI(com.servoy.j2db.server.ngclient.WebFormUI) INGApplication(com.servoy.j2db.server.ngclient.INGApplication) JSONObject(org.json.JSONObject) BaseWebObject(org.sablo.BaseWebObject) RuntimeLegacyComponent(com.servoy.j2db.server.ngclient.component.RuntimeLegacyComponent)

Example 5 with ElementScope

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

the class WebFormUI method removeComponentFromElementsScope.

// this should be the opposite of formUI.contributeComponentToElementsScope(...)
public void removeComponentFromElementsScope(FormElement fe, WebObjectSpecification componentSpec, WebFormComponent component) {
    ElementScope elementsScope = getElementsScope();
    if (elementsScope != null) {
        if (!FormElement.ERROR_BEAN.equals(componentSpec.getName()) && (!fe.getName().startsWith("svy_") || ((fe.getPersistIfAvailable() instanceof IFormElement) && ((IFormElement) fe.getPersistIfAvailable()).getGroupID() != null))) {
            if (!fe.getName().startsWith("svy_")) {
                RuntimeWebComponent runtimeComponent = (RuntimeWebComponent) elementsScope.remove(fe.getRawName());
                elementsScope.removeIndexByValue(runtimeComponent);
            }
            String groupID = fe.getPersistIfAvailable() instanceof IFormElement ? ((IFormElement) fe.getPersistIfAvailable()).getGroupID() : null;
            if (groupID != null) {
                if (groups == null)
                    groups = new HashMap<String, RuntimeWebGroup>(4);
                RuntimeWebGroup group = groups.get(groupID);
                if (group != null) {
                    String groupName = FormElementGroup.getName(groupID);
                    group.remove(component);
                    if (group.getComponentCount() == 0)
                        elementsScope.remove(groupName);
                    groups.remove(groupID);
                }
            }
        }
    } else {
        Debug.error(new RuntimeException("Trying to remove component from a non-existent elements scope for form: " + getName()));
    }
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) RuntimeWebGroup(com.servoy.j2db.server.ngclient.component.RuntimeWebGroup) HashMap(java.util.HashMap) ElementScope(com.servoy.j2db.scripting.ElementScope) RuntimeWebComponent(com.servoy.j2db.server.ngclient.component.RuntimeWebComponent)

Aggregations

ElementScope (com.servoy.j2db.scripting.ElementScope)7 Point (java.awt.Point)3 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)3 ScriptableObject (org.mozilla.javascript.ScriptableObject)3 IFormElement (com.servoy.j2db.persistence.IFormElement)2 FormScope (com.servoy.j2db.scripting.FormScope)2 RuntimeWebComponent (com.servoy.j2db.server.ngclient.component.RuntimeWebComponent)2 IComponent (com.servoy.j2db.ui.IComponent)2 IDataRenderer (com.servoy.j2db.ui.IDataRenderer)2 Component (java.awt.Component)2 JComponent (javax.swing.JComponent)2 WebMarkupContainer (org.apache.wicket.markup.html.WebMarkupContainer)2 IRecordInternal (com.servoy.j2db.dataprocessing.IRecordInternal)1 PageList (com.servoy.j2db.printing.PageList)1 IScriptable (com.servoy.j2db.scripting.IScriptable)1 IScriptableProvider (com.servoy.j2db.scripting.IScriptableProvider)1 FormAndComponent (com.servoy.j2db.scripting.JSApplication.FormAndComponent)1 WebCellBasedView (com.servoy.j2db.server.headlessclient.dataui.WebCellBasedView)1 FormElement (com.servoy.j2db.server.ngclient.FormElement)1 INGApplication (com.servoy.j2db.server.ngclient.INGApplication)1