Search in sources :

Example 1 with ComponentContext

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

the class ComponentTypeSabloValue method createComponentIfNeededAndPossible.

protected void createComponentIfNeededAndPossible() {
    // so now we should be able to find a potentially linked foundset property value
    if (componentIsCreated || webObjectContext == null)
        return;
    final FoundsetTypeSabloValue foundsetPropValue = getFoundsetValue();
    if (foundsetPropValue != null)
        foundsetPropValue.addStateChangeListener(getFoundsetStateChangeListener());
    // foundset property value is not yet set or not yet attached to component
    if ((foundsetPropValue == null || foundsetPropValue.getDataAdapterList() == null) && forFoundsetTypedPropertyName != null)
        return;
    componentIsCreated = true;
    IWebFormUI formUI = getParentComponent().findParent(IWebFormUI.class);
    final IDataAdapterList dal = (foundsetPropValue != null ? foundsetPropValue.getDataAdapterList() : formUI.getDataAdapterList());
    if (foundsetPropValue != null) {
        // do this before creating the component so that any attach() methods of it's properties that register data links get caught
        ((FoundsetDataAdapterList) dal).addDataLinkedPropertyRegistrationListener(createDataLinkedPropertyRegistrationListener());
        foundsetLinkedPropOfComponentValueChangeHandler = new FoundsetLinkedValueChangeHandler(foundsetPropValue);
    }
    childComponent = ComponentFactory.createComponent(dal.getApplication(), dal, formElementValue.element, getParentComponent(), formUI.getController().getForm());
    if (foundsetPropValue != null) {
        dataLinkedPropertyRegistrationListener.componentIsNowAvailable();
    }
    childComponent.setDirtyPropertyListener(new IDirtyPropertyListener() {

        @Override
        public void propertyFlaggedAsDirty(String propertyName, boolean dirty, boolean granularUpdate) {
            if (dirty) {
                // this gets called whenever a property is flagged as dirty/changed/to be sent to browser
                if (forFoundsetTypedPropertyName != null && recordBasedProperties.contains(propertyName)) {
                    if (!((FoundsetDataAdapterList) dal).isQuietRecordChangeInProgress() && foundsetPropValue.getFoundset() != null && // if forFoundsetTypedPropertyName != null we are using a foundset DAL, so just cast
                    !foundsetPropValue.getFoundset().isInFindMode()) {
                        // for example valuelist properties can get filtered based on client sent filter in which case the property does change without
                        // any actual change in the record; in this case we need to mark it correctly in viewport as a change
                        foundsetLinkedPropOfComponentValueChangeHandler.valueChangedInFSLinkedUnderlyingValue(propertyName, viewPortChangeMonitor);
                    } else {
                        // else this change was probably determined by the fact that we reuse components, changing the record in the DAL to get data for a specific row;
                        // so we need to clear component changes for this property because we do not notify the parent here (we want to ignore the change) so
                        // we shouldn't keep the property marked as dirty - thus blocking future property changes to generate a valueChanged on parent's monitor
                        childComponent.clearChangedStatusForProperty(propertyName);
                    }
                } else {
                    // non-record related prop. changed...
                    monitor.valueChanged();
                }
            }
        }
    });
    for (String initialChangedProperty : childComponent.getProperties().content.keySet()) {
        if (forFoundsetTypedPropertyName == null || !recordBasedProperties.contains(initialChangedProperty)) {
            // non-record related prop. initially changed...
            monitor.valueChanged();
        }
    }
    childComponent.setComponentContext(new ComponentContext(formElementValue.propertyPath));
    if (componentPropertyDescription != null && Utils.getAsBoolean(componentPropertyDescription.getTag(TAG_ADD_TO_ELEMENTS_SCOPE))) {
        formUI.contributeComponentToElementsScope(formElementValue.element, formElementValue.element.getWebComponentSpec(), childComponent);
    }
    for (String handler : childComponent.getFormElement().getHandlers()) {
        Object value = childComponent.getFormElement().getPropertyValue(handler);
        if (value instanceof String) {
            IPersist function = formUI.getController().getApplication().getFlattenedSolution().searchPersist((String) value);
            Form form = formUI.getController().getForm();
            if (function == null) {
                Debug.warn("Script Method of value '" + value + "' not found trying just the form " + form);
                IPersist child = form.getChild(UUID.fromString((String) value));
                if (child != null) {
                    Debug.warn("Script Method " + child + " on the form " + form + " with uuid " + child.getUUID());
                    function = child;
                }
            }
            if (function != null) {
                childComponent.add(handler, function.getID());
            } else {
                Debug.warn("Event handler for " + handler + " with value '" + value + "' not found (form " + form + ", form element " + childComponent.getFormElement().getName() + ")");
            }
        } else if (value instanceof Number && ((Number) value).intValue() > 0) {
            childComponent.add(handler, ((Number) value).intValue());
        }
    }
    if (foundsetPropValue != null) {
        viewPortChangeMonitor = new ComponentTypeViewportDataChangeMonitor(monitor, new ComponentViewportRowDataProvider((FoundsetDataAdapterList) dal, childComponent, this), this);
        foundsetPropValue.addViewportDataChangeMonitor(viewPortChangeMonitor);
        setDataproviderNameToFoundset();
    }
    addPropagatingPropertyChangeListener(WebFormUI.READONLY, webObjectContext.getProperty(WebFormUI.READONLY));
    if (childComponent.hasChanges())
        monitor.valueChanged();
}
Also used : ComponentContext(com.servoy.j2db.server.ngclient.ComponentContext) Form(com.servoy.j2db.persistence.Form) IDirtyPropertyListener(org.sablo.IDirtyPropertyListener) IWebFormUI(com.servoy.j2db.server.ngclient.IWebFormUI) IPersist(com.servoy.j2db.persistence.IPersist) IDataAdapterList(com.servoy.j2db.server.ngclient.IDataAdapterList) JSONObject(org.json.JSONObject)

Aggregations

Form (com.servoy.j2db.persistence.Form)1 IPersist (com.servoy.j2db.persistence.IPersist)1 ComponentContext (com.servoy.j2db.server.ngclient.ComponentContext)1 IDataAdapterList (com.servoy.j2db.server.ngclient.IDataAdapterList)1 IWebFormUI (com.servoy.j2db.server.ngclient.IWebFormUI)1 JSONObject (org.json.JSONObject)1 IDirtyPropertyListener (org.sablo.IDirtyPropertyListener)1