Search in sources :

Example 1 with ComponentPropertyType

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

the class PersistBasedFormElementImpl method convertFromPortalToNGProperties.

private void convertFromPortalToNGProperties(Portal portal, FlattenedSolution fs, Map<String, Object> map, Map<String, PropertyDescription> specProperties, PropertyPath propertyPath) {
    try {
        map.remove("relationName");
        JSONObject relatedFoundset = new JSONObject();
        relatedFoundset.put("foundsetSelector", portal.getRelationName());
        // get property type 'foundset'
        PropertyDescription pd = specProperties.get("relatedFoundset");
        if (pd == null) {
            Debug.error(new RuntimeException("Cannot find foundset special type to use for portal."));
            return;
        } else {
            putAndConvertProperty("relatedFoundset", relatedFoundset, map, fs, pd, propertyPath);
        }
        map.remove("ngReadOnlyMode");
        if (portal.getNgReadOnlyMode() != null) {
            PropertyDescription readOnlyModePD = specProperties.get("readOnlyMode");
            if (pd == null) {
                Debug.error(new RuntimeException("Cannot find foundset special type to use for portal."));
                return;
            } else {
                putAndConvertProperty("readOnlyMode", portal.getNgReadOnlyMode(), map, fs, readOnlyModePD, propertyPath);
            }
        }
        // components: 'component[]',
        // component: {
        // definition: 'componentDef',
        // (...)
        // },
        // get property type 'component definition'
        pd = specProperties.get("childElements");
        if (pd != null)
            pd = ((CustomJSONArrayType<?, ?>) pd.getType()).getCustomJSONTypeDefinition();
        List<IPersist> components = ComponentFactory.sortElementsOnPositionAndGroup(portal.getAllObjectsAsList());
        if (pd == null) {
            Debug.error(new RuntimeException("Cannot find component definition special type to use for portal."));
            return;
        } else {
            // of ComponentTypeFormElementValue type (this array of objects corresponds to CustomJSONArrayType form element value
            Object[] componentFormElementValues = new Object[components.size()];
            int i = 0;
            ComponentPropertyType type = ((ComponentPropertyType) pd.getType());
            propertyPath.add("childElements");
            for (IPersist component : components) {
                if (component instanceof IFormElement) {
                    FormElement nfe = FormElementHelper.INSTANCE.getFormElement((IFormElement) component, fs, propertyPath, formElement.getDesignId() != null);
                    boolean somePropertyChanged = false;
                    propertyPath.add(i);
                    // remove the name of relation prefix from child dataproviders as it only stands in the way later on...
                    Collection<PropertyDescription> dataProviderProperties = nfe.getWebComponentSpec().getProperties(DataproviderPropertyType.INSTANCE);
                    String relationPrefix = portal.getRelationName() + '.';
                    Map<String, Object> elementProperties = new HashMap<>(nfe.getRawPropertyValues());
                    for (PropertyDescription dpProperty : dataProviderProperties) {
                        String dpPropertyName = dpProperty.getName();
                        // TODO adjust this when/if dataprovider properties change the form element value type in the future
                        String dp = (String) nfe.getPropertyValue(dpPropertyName);
                        if (dp != null && dp.startsWith(relationPrefix)) {
                            somePropertyChanged = true;
                            // portal always prefixes comp. dataproviders with related fs name
                            putAndConvertProperty(dpPropertyName, dp.substring(relationPrefix.length()), elementProperties, fs, nfe.getWebComponentSpec().getProperty(dpPropertyName), propertyPath);
                        }
                    }
                    // legacy portals need to set the same tabSeq. property for all children if they are to function properly
                    Collection<PropertyDescription> tabSequenceProperties = nfe.getWebComponentSpec().getProperties(NGTabSeqPropertyType.NG_INSTANCE);
                    for (PropertyDescription tabSeqProperty : tabSequenceProperties) {
                        String tabSeqPropertyName = tabSeqProperty.getName();
                        somePropertyChanged = true;
                        putAndConvertProperty(tabSeqPropertyName, Integer.valueOf(1), elementProperties, fs, nfe.getWebComponentSpec().getProperty(tabSeqPropertyName), // just put an 1 on all (default legacy portal doesn't allow non-default tab seq in it)
                        propertyPath);
                    }
                    if (somePropertyChanged)
                        nfe.updatePropertyValuesDontUse(elementProperties);
                    componentFormElementValues[i++] = type.getFormElementValue(null, pd, propertyPath, nfe, fs);
                    propertyPath.backOneLevel();
                }
            }
            propertyPath.backOneLevel();
            map.put("childElements", componentFormElementValues);
        }
    } catch (IllegalArgumentException e) {
        Debug.error(e);
        return;
    } catch (JSONException e) {
        Debug.error(e);
        return;
    }
}
Also used : ComponentPropertyType(com.servoy.j2db.server.ngclient.property.ComponentPropertyType) HashMap(java.util.HashMap) JSONException(org.json.JSONException) IFormElement(com.servoy.j2db.persistence.IFormElement) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IPersist(com.servoy.j2db.persistence.IPersist) CustomJSONArrayType(org.sablo.specification.property.CustomJSONArrayType) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject)

Example 2 with ComponentPropertyType

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

the class FormElementHelper method createBodyPortalFormElement.

private FormElement createBodyPortalFormElement(BodyPortal listViewPortal, FlattenedSolution fs, final boolean isInDesigner) {
    Form form = listViewPortal.getForm();
    Part bodyPart = null;
    for (Part prt : Utils.iterate(form.getParts())) {
        if (prt.getPartType() == Part.BODY) {
            bodyPart = prt;
            break;
        }
    }
    if (bodyPart != null) {
        try {
            String name = "svy_lvp_" + form.getName();
            int startPos = form.getPartStartYPos(bodyPart.getID());
            int endPos = bodyPart.getHeight();
            int bodyheight = endPos - startPos;
            JSONObject portal = new JSONObject();
            portal.put("name", name);
            portal.put("multiLine", !listViewPortal.isTableview());
            portal.put("rowHeight", !listViewPortal.isTableview() ? bodyheight : getRowHeight(form));
            int scrollbars = form.getScrollbars();
            if (!listViewPortal.isTableview()) {
                // handle horizontal scrollbar on form level for listview
                int verticalScrollBars = ISupportScrollbars.VERTICAL_SCROLLBAR_AS_NEEDED;
                if ((form.getScrollbars() & ISupportScrollbars.VERTICAL_SCROLLBAR_ALWAYS) != 0) {
                    verticalScrollBars = ISupportScrollbars.VERTICAL_SCROLLBAR_ALWAYS;
                } else if ((form.getScrollbars() & ISupportScrollbars.VERTICAL_SCROLLBAR_NEVER) != 0) {
                    verticalScrollBars = ISupportScrollbars.VERTICAL_SCROLLBAR_NEVER;
                }
                scrollbars = ISupportScrollbars.HORIZONTAL_SCROLLBAR_NEVER + verticalScrollBars;
            }
            portal.put("scrollbars", scrollbars);
            if (listViewPortal.isTableview()) {
                int headerHeight = 30;
                if (form.hasPart(Part.HEADER)) {
                    headerHeight = 0;
                }
                portal.put("headerHeight", headerHeight);
                portal.put("sortable", form.getOnSortCmdMethodID() != -1);
            }
            portal.put("readOnlyMode", form.getNgReadOnlyMode());
            JSONObject location = new JSONObject();
            location.put("x", 0);
            location.put("y", isInDesigner ? startPos : 0);
            portal.put("location", location);
            JSONObject size = new JSONObject();
            // size.put("width", (listViewPortal.isTableview() && !fillsWidth) ? getGridWidth(form) : form.getWidth());
            size.put("width", form.getWidth());
            size.put("height", bodyheight);
            portal.put("size", size);
            portal.put("visible", listViewPortal.getVisible());
            portal.put("enabled", listViewPortal.getEnabled());
            // empty contents; will be updated afterwards directly with form element values for components
            portal.put("childElements", new JSONArray());
            JSONObject relatedFoundset = new JSONObject();
            relatedFoundset.put("foundsetSelector", "");
            portal.put("relatedFoundset", relatedFoundset);
            PropertyPath propertyPath = new PropertyPath();
            propertyPath.setShouldAddElementName();
            FormElement portalFormElement = new FormElement("servoycore-portal", portal, form, name, fs, propertyPath, isInDesigner);
            PropertyDescription pd = portalFormElement.getWebComponentSpec().getProperties().get("childElements");
            if (pd != null)
                pd = ((CustomJSONArrayType<?, ?>) pd.getType()).getCustomJSONTypeDefinition();
            if (pd == null) {
                Debug.error(new RuntimeException("Cannot find component definition special type to use for portal."));
                return null;
            }
            ComponentPropertyType type = ((ComponentPropertyType) pd.getType());
            Map<String, Object> portalFormElementProperties = new HashMap<>(portalFormElement.getRawPropertyValues());
            portalFormElementProperties.put("anchors", IAnchorConstants.ALL);
            portalFormElementProperties.put("offsetY", startPos);
            portalFormElementProperties.put("partHeight", bodyPart.getHeight());
            portalFormElementProperties.put("formview", true);
            // now put real child component form element values in "childElements"
            Iterator<IPersist> it = form.getAllObjects(PositionComparator.XY_PERSIST_COMPARATOR);
            // contains actually ComponentTypeFormElementValue objects
            List<Object> children = new ArrayList<>();
            List<IPersist> labelFors = new ArrayList<>();
            propertyPath.add(portalFormElement.getName());
            propertyPath.add("childElements");
            // it's a generated table-view form portal (BodyPortal); we just
            // have to set the Portal's tabSeq to the first one of it's children for it to work properly
            int minBodyPortalTabSeq = -2;
            while (it.hasNext()) {
                IPersist persist = it.next();
                if (persist instanceof IFormElement) {
                    Point loc = CSSPositionUtils.getLocation((IFormElement) persist);
                    if (startPos <= loc.y && endPos > loc.y) {
                        if (listViewPortal.isTableview() && persist instanceof GraphicalComponent && ((GraphicalComponent) persist).getLabelFor() != null)
                            continue;
                        propertyPath.add(children.size());
                        FormElement fe = getFormElement((IFormElement) persist, fs, propertyPath, isInDesigner);
                        if (listViewPortal.isTableview()) {
                            String elementName = ((IFormElement) persist).getName();
                            Iterator<GraphicalComponent> graphicalComponents = form.getGraphicalComponents();
                            boolean hasLabelFor = false;
                            while (graphicalComponents.hasNext()) {
                                GraphicalComponent gc = graphicalComponents.next();
                                if (gc.getLabelFor() != null && Utils.equalObjects(elementName, gc.getLabelFor()) && startPos <= gc.getLocation().y && endPos > gc.getLocation().y) {
                                    labelFors.add(gc);
                                    hasLabelFor = true;
                                    break;
                                }
                            }
                            Map<String, Object> feRawProperties = new HashMap<>(fe.getRawPropertyValues());
                            feRawProperties.put("componentIndex", Integer.valueOf(children.size()));
                            if (hasLabelFor)
                                feRawProperties.put("headerIndex", Integer.valueOf(labelFors.size() - 1));
                            fe.updatePropertyValuesDontUse(feRawProperties);
                        }
                        children.add(type.getFormElementValue(null, pd, propertyPath, fe, fs));
                        propertyPath.backOneLevel();
                        Collection<PropertyDescription> tabSequenceProperties = fe.getWebComponentSpec().getProperties(NGTabSeqPropertyType.NG_INSTANCE);
                        for (PropertyDescription tabSeqProperty : tabSequenceProperties) {
                            String tabSeqPropertyName = tabSeqProperty.getName();
                            Integer tabSeqVal = (Integer) fe.getPropertyValue(tabSeqPropertyName);
                            // default is 0 == DEFAULT tab sequence
                            if (tabSeqVal == null)
                                tabSeqVal = Integer.valueOf(0);
                            if (minBodyPortalTabSeq < 0 || (minBodyPortalTabSeq > tabSeqVal.intValue() && tabSeqVal.intValue() >= 0))
                                minBodyPortalTabSeq = tabSeqVal.intValue();
                        }
                    }
                }
            }
            propertyPath.backOneLevel();
            propertyPath.backOneLevel();
            portalFormElementProperties.put("childElements", children.toArray());
            if (listViewPortal.isTableview()) {
                propertyPath.add("headers");
                List<Object> headers = new ArrayList<>();
                for (IPersist persist : labelFors) {
                    if (persist instanceof IFormElement) {
                        propertyPath.add(headers.size());
                        FormElement fe = getFormElement((IFormElement) persist, fs, propertyPath, isInDesigner);
                        headers.add(type.getFormElementValue(null, pd, propertyPath, fe, fs));
                        propertyPath.backOneLevel();
                    }
                }
                propertyPath.backOneLevel();
                propertyPath.backOneLevel();
                portalFormElementProperties.put("headers", headers.toArray());
            }
            // table view tab seq. is the minimum of it's children tabSeq'es
            portalFormElementProperties.put("tabSeq", Integer.valueOf(minBodyPortalTabSeq));
            portalFormElement.updatePropertyValuesDontUse(portalFormElementProperties);
            return portalFormElement;
        } catch (JSONException ex) {
            Debug.error("Cannot create list view portal component", ex);
        }
    }
    return null;
}
Also used : FormComponentPropertyType(com.servoy.j2db.server.ngclient.property.types.FormComponentPropertyType) ComponentPropertyType(com.servoy.j2db.server.ngclient.property.ComponentPropertyType) Form(com.servoy.j2db.persistence.Form) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) GraphicalComponent(com.servoy.j2db.persistence.GraphicalComponent) ArrayList(java.util.ArrayList) CustomJSONArrayType(org.sablo.specification.property.CustomJSONArrayType) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Point(java.awt.Point) Point(java.awt.Point) IFormElement(com.servoy.j2db.persistence.IFormElement) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IPersist(com.servoy.j2db.persistence.IPersist) Part(com.servoy.j2db.persistence.Part) PropertyPath(com.servoy.j2db.server.ngclient.property.types.PropertyPath) IBasicWebObject(com.servoy.j2db.persistence.IBasicWebObject) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) IChildWebObject(com.servoy.j2db.persistence.IChildWebObject)

Aggregations

IFormElement (com.servoy.j2db.persistence.IFormElement)2 IPersist (com.servoy.j2db.persistence.IPersist)2 ComponentPropertyType (com.servoy.j2db.server.ngclient.property.ComponentPropertyType)2 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)2 HashMap (java.util.HashMap)2 JSONException (org.json.JSONException)2 JSONObject (org.json.JSONObject)2 PropertyDescription (org.sablo.specification.PropertyDescription)2 CustomJSONArrayType (org.sablo.specification.property.CustomJSONArrayType)2 Form (com.servoy.j2db.persistence.Form)1 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)1 IBasicWebObject (com.servoy.j2db.persistence.IBasicWebObject)1 IChildWebObject (com.servoy.j2db.persistence.IChildWebObject)1 Part (com.servoy.j2db.persistence.Part)1 FormComponentPropertyType (com.servoy.j2db.server.ngclient.property.types.FormComponentPropertyType)1 PropertyPath (com.servoy.j2db.server.ngclient.property.types.PropertyPath)1 Point (java.awt.Point)1 ArrayList (java.util.ArrayList)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1