Search in sources :

Example 21 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class TabPanelTest method testRelatedFormsInTabPanels.

@SuppressWarnings("nls")
@Test
public void testRelatedFormsInTabPanels() throws JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("f1");
    WebFormComponent webComponent = form.getFormUI().getWebComponent("tabpanel");
    Object property = webComponent.getProperty("tabIndex");
    Assert.assertEquals(((Number) property).longValue(), 1);
    StringWriter stringWriter = new StringWriter();
    JSONWriter jsonWriter = new JSONWriter(stringWriter);
    jsonWriter.object();
    form.getFormUI().writeAllComponentsProperties(jsonWriter, FullValueToJSONConverter.INSTANCE);
    jsonWriter.endObject();
    JSONAssert.assertEquals("{\"\":{\"enabled\":true,\"visible\":true,\"findmode\":false},\"tabpanel\":{\"enabled\":true,\"svyMarkupId\":\"s554517c05b68828168c38c67974bf993\",\"tabs\":{\"vEr\":3,\"w\":false,\"v\":[{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":3,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab1\",\"active\":true,\"containsFormId\":\"f2\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":3,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab2\",\"active\":false,\"containsFormId\":\"f3\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":3,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab3\",\"active\":false,\"containsFormId\":\"f4\",\"disabled\":false}}],\"svy_types\":{\"2\":\"JSON_obj\",\"1\":\"JSON_obj\",\"0\":\"JSON_obj\"}}},\"svy_types\":{\"tabpanel\":{\"tabs\":\"JSON_arr\"}}}", stringWriter.toString(), true);
    webComponent.setProperty("tabIndex", "tab2");
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    jsonWriter.object();
    form.getFormUI().writeAllComponentsProperties(jsonWriter, FullValueToJSONConverter.INSTANCE);
    jsonWriter.endObject();
    System.err.println(stringWriter.toString());
    JSONAssert.assertEquals("{\"\":{\"enabled\":true,\"visible\":true,\"findmode\":false},\"tabpanel\":{\"enabled\":true,\"svyMarkupId\":\"s554517c05b68828168c38c67974bf993\",\"tabIndex\":\"tab2\",\"tabs\":{\"vEr\":4,\"w\":false,\"v\":[{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":4,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab1\",\"active\":true,\"containsFormId\":\"f2\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":4,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab2\",\"active\":false,\"containsFormId\":\"f3\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":4,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab3\",\"active\":false,\"containsFormId\":\"f4\",\"disabled\":false}}],\"svy_types\":{\"2\":\"JSON_obj\",\"1\":\"JSON_obj\",\"0\":\"JSON_obj\"}}},\"svy_types\":{\"tabpanel\":{\"tabs\":\"JSON_arr\"}}}", stringWriter.toString(), true);
    webComponent.setProperty("tabIndex", Integer.valueOf(3));
    stringWriter = new StringWriter();
    jsonWriter = new JSONWriter(stringWriter);
    jsonWriter.object();
    form.getFormUI().writeAllComponentsProperties(jsonWriter, FullValueToJSONConverter.INSTANCE);
    jsonWriter.endObject();
    System.err.println(stringWriter.toString());
    JSONAssert.assertEquals("{\"\":{\"enabled\":true,\"visible\":true,\"findmode\":false},\"tabpanel\":{\"enabled\":true,\"svyMarkupId\":\"s554517c05b68828168c38c67974bf993\",\"tabIndex\":3,\"tabs\":{\"vEr\":5,\"w\":false,\"v\":[{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":5,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab1\",\"active\":true,\"containsFormId\":\"f2\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":5,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab2\",\"active\":false,\"containsFormId\":\"f3\",\"disabled\":false}},{\"rt\":\"servoydefault-tabpanel.tab\",\"vEr\":5,\"w\":false,\"v\":{\"relationName\":\"-1\",\"text\":\"tab3\",\"active\":false,\"containsFormId\":\"f4\",\"disabled\":false}}],\"svy_types\":{\"2\":\"JSON_obj\",\"1\":\"JSON_obj\",\"0\":\"JSON_obj\"}}},\"svy_types\":{\"tabpanel\":{\"tabs\":\"JSON_arr\"}}}", stringWriter.toString(), true);
}
Also used : JSONWriter(org.json.JSONWriter) StringWriter(java.io.StringWriter) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Example 22 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class ServoyApiObject method hideForm.

/**
 * Hide a form directly on the server for instance when a tab will change on the client, so it won't need to do a round trip
 * for hiding the form through the browser's component.
 *
 * @sample
 * servoyApi.hideForm(formToHideName)
 *
 * @param formName the form to hide
 * @return true if the form was hidden
 */
@JSFunction
public boolean hideForm(String nameOrUUID) {
    String formName = nameOrUUID;
    Form form = app.getFlattenedSolution().getForm(nameOrUUID);
    if (form == null) {
        form = (Form) app.getFlattenedSolution().searchPersist(nameOrUUID);
        if (form != null) {
            formName = form.getName();
        }
    }
    IWebFormController formController = app.getFormManager().getForm(formName);
    if (formController != null) {
        List<Runnable> invokeLaterRunnables = new ArrayList<Runnable>();
        boolean ret = formController.notifyVisible(false, invokeLaterRunnables);
        if (ret) {
            formController.setParentFormController(null);
        }
        Utils.invokeAndWait(app, invokeLaterRunnables);
        return ret;
    }
    return false;
}
Also used : Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 23 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController 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 24 with IWebFormController

use of com.servoy.j2db.server.ngclient.IWebFormController in project servoy-client by Servoy.

the class ValuelistPropTest method valueListAttachDetach.

@Test
public void valueListAttachDetach() throws JSONException {
    IWebFormController form = (IWebFormController) client.getFormManager().showFormInCurrentContainer("test");
    Assert.assertNotNull(form);
    // (SVY-12336)
    // assign a foundset and a column with foundset linked dp and valuelist linked to that dp; then set foundset to null, clear columns and set back all this; no exception should happen
    Context cx = Context.enter();
    try {
        cx.evaluateString(form.getFormScope(), "elements.myCustomComponent.myFoundset = foundset;" + "elements.myCustomComponent.columns = [{ myDataprovider: 'test1', myValuelist: 'myVl1' }];", "a", 1, null);
        String changes = NGUtils.formChangesToString(((Container) form.getFormUI()), FullValueToJSONConverter.INSTANCE);
        JSONObject changesJSON = new JSONObject(changes);
        JSONObject col0JSON = changesJSON.getJSONObject("changes").getJSONObject("myCustomComponent").getJSONObject("columns").getJSONArray("v").getJSONObject(0).getJSONObject("v");
        Assert.assertArrayEquals("data should get sent to client", new String[] { "value1", "value2", "value3", "value4", "value5", "value6", "value7", "value8", "value9", "value10", "value11", "value12", "value13", "value14", "value15", "value16", "value17", "value18" }, new JSONWrapperList(col0JSON.getJSONObject("myDataprovider").getJSONArray("vp")).toArray());
        Assert.assertEquals("valuelist should get sent to client", 4, col0JSON.getJSONObject("myValuelist").getJSONArray("values").length());
        // detach valuelist in column + change foundset prop. value - detach should have cleared registered property change listener for foundset and changing the foundset would result in a NPE if those listeners were still executed for disposed valuelist prop.
        cx.evaluateString(form.getFormScope(), "elements.myCustomComponent.columns = [];" + "elements.myCustomComponent.myFoundset = databaseManager.getFoundSet(foundset.getDataSource());" + "elements.myCustomComponent.myFoundset.foundset.loadAllRecords();" + "elements.myCustomComponent.columns = [{ myDataprovider: 'test2', myValuelist: 'myVl2' }];", "a", 1, null);
        changes = NGUtils.formChangesToString(((Container) form.getFormUI()), FullValueToJSONConverter.INSTANCE);
        changesJSON = new JSONObject(changes);
        col0JSON = changesJSON.getJSONObject("changes").getJSONObject("myCustomComponent").getJSONObject("columns").getJSONArray("v").getJSONObject(0).getJSONObject("v");
        Assert.assertArrayEquals("data should get sent to client", new String[] { "valueA", "valueB", "valueC", "valueD", "valueE", "valueF", "valueG", "valueH", "valueI", "valueJ", "valueK", "valueL", "valueM", "valueN", "valueO", "valueP", "valueQ", "valueR" }, new JSONWrapperList(col0JSON.getJSONObject("myDataprovider").getJSONArray("vp")).toArray());
    } finally {
        Context.exit();
    }
}
Also used : Context(org.mozilla.javascript.Context) Container(org.sablo.Container) JSONObject(org.json.JSONObject) JSONWrapperList(com.servoy.j2db.util.JSONWrapperList) IWebFormController(com.servoy.j2db.server.ngclient.IWebFormController) Test(org.junit.Test)

Aggregations

IWebFormController (com.servoy.j2db.server.ngclient.IWebFormController)24 Test (org.junit.Test)21 JSONObject (org.json.JSONObject)12 WebFormComponent (com.servoy.j2db.server.ngclient.WebFormComponent)10 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)9 StringWriter (java.io.StringWriter)9 JSONWriter (org.json.JSONWriter)9 BrowserConverterContext (org.sablo.specification.property.BrowserConverterContext)9 DataConversion (org.sablo.websocket.utils.DataConversion)8 JSONArray (org.json.JSONArray)4 Container (org.sablo.Container)4 FormScope (com.servoy.j2db.scripting.FormScope)3 Context (org.mozilla.javascript.Context)3 IFoundSetInternal (com.servoy.j2db.dataprocessing.IFoundSetInternal)2 Form (com.servoy.j2db.persistence.Form)2 RhinoMapOrArrayWrapper (com.servoy.j2db.server.ngclient.component.RhinoMapOrArrayWrapper)2 ServoyException (com.servoy.j2db.util.ServoyException)2 JSONException (org.json.JSONException)2 WebComponent (org.sablo.WebComponent)2 FoundSet (com.servoy.j2db.dataprocessing.FoundSet)1