Search in sources :

Example 76 with IPersist

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

the class DebugUtils method getScopesAndFormsToReload.

public static Set<IFormController>[] getScopesAndFormsToReload(final ClientState clientState, Collection<IPersist> changes) {
    Set<IFormController> scopesToReload = new HashSet<IFormController>();
    final Set<IFormController> formsToReload = new HashSet<IFormController>();
    final SpecProviderState specProviderState = WebComponentSpecProvider.getSpecProviderState();
    final Set<Form> formsUpdated = new HashSet<Form>();
    for (IPersist persist : changes) {
        clientState.getFlattenedSolution().updatePersistInSolutionCopy(persist);
        if (persist instanceof ScriptMethod) {
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    scopesToReload.add(formController);
                }
            } else if (persist.getParent() instanceof Solution) {
                LazyCompilationScope scope = clientState.getScriptEngine().getScopesScope().getGlobalScope(((ScriptMethod) persist).getScopeName());
                scope.remove((IScriptProvider) persist);
                scope.put((IScriptProvider) persist, (IScriptProvider) persist);
            } else if (persist.getParent() instanceof TableNode) {
                clientState.getFoundSetManager().reloadFoundsetMethod(((TableNode) persist.getParent()).getDataSource(), (IScriptProvider) persist);
            }
            if (clientState instanceof DebugJ2DBClient) {
                // ((DebugJ2DBClient)clientState).clearUserWindows();  no need for this as window API was refactored and it allows users to clean up dialogs
                ((DebugSwingFormMananger) ((DebugJ2DBClient) clientState).getFormManager()).fillScriptMenu();
            }
        } else if (persist instanceof ScriptVariable) {
            ScriptVariable sv = (ScriptVariable) persist;
            if (persist.getParent() instanceof Solution) {
                clientState.getScriptEngine().getScopesScope().getGlobalScope(sv.getScopeName()).put(sv);
            }
            if (persist.getParent() instanceof Form) {
                Form form = (Form) persist.getParent();
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    FormScope scope = formController.getFormScope();
                    scope.put(sv);
                }
            }
        } else if (persist.getAncestor(IRepository.FORMS) != null) {
            final Form form = (Form) persist.getAncestor(IRepository.FORMS);
            if (form != null && form.isFormComponent().booleanValue()) {
                // if the changed form is a reference form we need to check if that is referenced by a loaded form..
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
                for (IFormController fc : cachedFormControllers) {
                    fc.getForm().acceptVisitor(new IPersistVisitor() {

                        @Override
                        public Object visit(IPersist o) {
                            if (o instanceof WebComponent) {
                                WebComponent wc = (WebComponent) o;
                                WebObjectSpecification spec = FormTemplateGenerator.getWebObjectSpecification(wc);
                                Collection<PropertyDescription> properties = spec != null ? spec.getProperties(FormComponentPropertyType.INSTANCE) : null;
                                if (properties != null && properties.size() > 0) {
                                    Form persistForm = (Form) wc.getAncestor(IRepository.FORMS);
                                    for (PropertyDescription pd : properties) {
                                        Form frm = FormComponentPropertyType.INSTANCE.getForm(wc.getProperty(pd.getName()), clientState.getFlattenedSolution());
                                        if (frm != null && (form.equals(frm) || FlattenedForm.hasFormInHierarchy(frm, form) || isReferenceFormUsedInForm(clientState, form, frm)) && !formsUpdated.contains(persistForm)) {
                                            formsUpdated.add(persistForm);
                                            List<IFormController> cfc = clientState.getFormManager().getCachedFormControllers(persistForm);
                                            for (IFormController formController : cfc) {
                                                formsToReload.add(formController);
                                            }
                                        }
                                    }
                                }
                            }
                            return IPersistVisitor.CONTINUE_TRAVERSAL;
                        }
                    });
                }
            } else if (!formsUpdated.contains(form)) {
                formsUpdated.add(form);
                List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers(form);
                for (IFormController formController : cachedFormControllers) {
                    formsToReload.add(formController);
                }
            }
            if (persist instanceof Form && clientState.getFormManager() instanceof DebugUtils.DebugUpdateFormSupport) {
                ((DebugUtils.DebugUpdateFormSupport) clientState.getFormManager()).updateForm((Form) persist);
            }
        } else if (persist instanceof ScriptCalculation) {
            ScriptCalculation sc = (ScriptCalculation) persist;
            if (((RemoteDebugScriptEngine) clientState.getScriptEngine()).recompileScriptCalculation(sc)) {
                List<String> al = new ArrayList<String>();
                al.add(sc.getDataProviderID());
                try {
                    String dataSource = clientState.getFoundSetManager().getDataSource(sc.getTable());
                    ((FoundSetManager) clientState.getFoundSetManager()).getRowManager(dataSource).clearCalcs(null, al);
                    ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet(dataSource);
                } catch (Exception e) {
                    Debug.error(e);
                }
            }
        // if (clientState instanceof DebugJ2DBClient)
        // {
        // ((DebugJ2DBClient)clientState).clearUserWindows(); no need for this as window API was refactored and it allows users to clean up dialogs
        // }
        } else if (persist instanceof Relation) {
            ((FoundSetManager) clientState.getFoundSetManager()).flushSQLSheet((Relation) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            try {
                String primary = ((Relation) persist).getPrimaryDataSource();
                for (IFormController formController : cachedFormControllers) {
                    if (primary.equals(formController.getDataSource())) {
                        final IFormController finalController = formController;
                        final Relation finalRelation = (Relation) persist;
                        formController.getForm().acceptVisitor(new IPersistVisitor() {

                            @Override
                            public Object visit(IPersist o) {
                                if (o instanceof Tab && Utils.equalObjects(finalRelation.getName(), ((Tab) o).getRelationName())) {
                                    formsToReload.add(finalController);
                                    return o;
                                }
                                if (o instanceof Field && ((Field) o).getValuelistID() > 0) {
                                    ValueList vl = clientState.getFlattenedSolution().getValueList(((Field) o).getValuelistID());
                                    if (vl != null && Utils.equalObjects(finalRelation.getName(), vl.getRelationName())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                                if (o instanceof WebComponent) {
                                    WebComponent webComponent = (WebComponent) o;
                                    WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                                    if (spec != null) {
                                        Collection<PropertyDescription> properties = spec.getProperties(RelationPropertyType.INSTANCE);
                                        for (PropertyDescription pd : properties) {
                                            if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalRelation.getName())) {
                                                formsToReload.add(finalController);
                                                return o;
                                            }
                                        }
                                    }
                                }
                                return CONTINUE_TRAVERSAL;
                            }
                        });
                    }
                }
            } catch (Exception e) {
                Debug.error(e);
            }
        } else if (persist instanceof ValueList) {
            ComponentFactory.flushValueList(clientState, (ValueList) persist);
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            for (IFormController formController : cachedFormControllers) {
                final IFormController finalController = formController;
                final ValueList finalValuelist = (ValueList) persist;
                formController.getForm().acceptVisitor(new IPersistVisitor() {

                    @Override
                    public Object visit(IPersist o) {
                        if (o instanceof Field && ((Field) o).getValuelistID() > 0 && ((Field) o).getValuelistID() == finalValuelist.getID()) {
                            formsToReload.add(finalController);
                            return o;
                        }
                        if (o instanceof WebComponent) {
                            WebComponent webComponent = (WebComponent) o;
                            WebObjectSpecification spec = specProviderState == null ? null : specProviderState.getWebComponentSpecification(webComponent.getTypeName());
                            if (spec != null) {
                                Collection<PropertyDescription> properties = spec.getProperties(ValueListPropertyType.INSTANCE);
                                for (PropertyDescription pd : properties) {
                                    if (Utils.equalObjects(webComponent.getFlattenedJson().opt(pd.getName()), finalValuelist.getUUID().toString())) {
                                        formsToReload.add(finalController);
                                        return o;
                                    }
                                }
                            }
                        }
                        return CONTINUE_TRAVERSAL;
                    }
                });
            }
        } else if (persist instanceof Style) {
            ComponentFactory.flushStyle(null, ((Style) persist));
            List<IFormController> cachedFormControllers = clientState.getFormManager().getCachedFormControllers();
            String styleName = ((Style) persist).getName();
            for (IFormController formController : cachedFormControllers) {
                if (styleName.equals(formController.getForm().getStyleName())) {
                    formsToReload.add(formController);
                }
            }
        }
    }
    return new Set[] { scopesToReload, formsToReload };
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) Set(java.util.Set) HashSet(java.util.HashSet) Form(com.servoy.j2db.persistence.Form) FlattenedForm(com.servoy.j2db.persistence.FlattenedForm) ValueList(com.servoy.j2db.persistence.ValueList) LazyCompilationScope(com.servoy.j2db.scripting.LazyCompilationScope) FormScope(com.servoy.j2db.scripting.FormScope) WebComponent(com.servoy.j2db.persistence.WebComponent) Field(com.servoy.j2db.persistence.Field) Relation(com.servoy.j2db.persistence.Relation) Style(com.servoy.j2db.persistence.Style) ValueList(com.servoy.j2db.persistence.ValueList) List(java.util.List) ArrayList(java.util.ArrayList) Solution(com.servoy.j2db.persistence.Solution) HashSet(java.util.HashSet) FoundSetManager(com.servoy.j2db.dataprocessing.FoundSetManager) DebugSwingFormMananger(com.servoy.j2db.debug.DebugJ2DBClient.DebugSwingFormMananger) RhinoException(org.mozilla.javascript.RhinoException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ServoyException(com.servoy.j2db.util.ServoyException) PropertyDescription(org.sablo.specification.PropertyDescription) ScriptCalculation(com.servoy.j2db.persistence.ScriptCalculation) SpecProviderState(org.sablo.specification.SpecProviderState) Tab(com.servoy.j2db.persistence.Tab) IPersist(com.servoy.j2db.persistence.IPersist) IScriptProvider(com.servoy.j2db.persistence.IScriptProvider) TableNode(com.servoy.j2db.persistence.TableNode) ScriptVariable(com.servoy.j2db.persistence.ScriptVariable) IPersistVisitor(com.servoy.j2db.persistence.IPersistVisitor) Collection(java.util.Collection) IFormController(com.servoy.j2db.IFormController) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 77 with IPersist

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

the class EventExecutor method executeEvent.

public Object executeEvent(WebComponent component, String eventType, int eventId, Object[] eventArgs) {
    Scriptable scope = null;
    Function f = null;
    Object[] newargs = eventArgs != null ? Arrays.copyOf(eventArgs, eventArgs.length) : null;
    if (eventId > 0) {
        ScriptMethod scriptMethod = formController.getApplication().getFlattenedSolution().getScriptMethod(eventId);
        if (scriptMethod != null) {
            if (scriptMethod.getParent() instanceof Form) {
                FormScope formScope = formController.getFormScope();
                f = formScope.getFunctionByName(scriptMethod.getName());
                if (f != null && f != Scriptable.NOT_FOUND) {
                    scope = formScope;
                }
            } else // is it a global method
            if (scriptMethod.getParent() instanceof Solution) {
                scope = formController.getApplication().getScriptEngine().getScopesScope().getGlobalScope(scriptMethod.getScopeName());
                if (scope != null) {
                    f = ((GlobalScope) scope).getFunctionByName(scriptMethod.getName());
                }
            } else // very like a foundset/entity method
            {
                Scriptable foundsetScope = null;
                if (component instanceof WebFormComponent) {
                    IRecord rec = ((WebFormComponent) component).getDataAdapterList().getRecord();
                    if (rec != null) {
                        foundsetScope = (Scriptable) rec.getParentFoundSet();
                    }
                }
                if (foundsetScope == null)
                    foundsetScope = (Scriptable) formController.getFormModel();
                if (foundsetScope != null) {
                    // TODO ViewFoundSets should be come a scriptable if they have foundset methods..
                    scope = foundsetScope;
                    Object scopeMethod = scope.getPrototype().get(scriptMethod.getName(), scope);
                    if (scopeMethod instanceof Function)
                        f = (Function) scopeMethod;
                }
            }
            if (f == null) {
                Debug.error(// $NON-NLS-1$ //$NON-NLS-2$
                "No function found for " + scriptMethod + " when trying to execute the event " + eventType + '(' + eventId + ") of component: " + component, // $NON-NLS-1$
                new RuntimeException());
                return null;
            }
        } else {
            Debug.warn("Couldn't find the ScriptMethod for event: " + eventType + " with event id: " + eventId + " to execute for component " + component);
        }
    }
    // $NON-NLS-1$
    if (formController.isInFindMode() && !Utils.getAsBoolean(f.get("_AllowToRunInFind_", f)))
        return null;
    if (newargs != null) {
        for (int i = 0; i < newargs.length; i++) {
            if (newargs[i] instanceof JSONObject && "event".equals(((JSONObject) newargs[i]).optString("type"))) {
                JSONObject json = (JSONObject) newargs[i];
                JSEvent event = new JSEvent();
                JSEventType.fillJSEvent(event, json, component, formController);
                event.setType(getEventType(eventType));
                event.setName(RepositoryHelper.getDisplayName(eventType, BaseComponent.class));
                newargs[i] = event;
            } else {
                // try to convert the received arguments
                WebObjectFunctionDefinition propertyDesc = component.getSpecification().getHandler(eventType);
                List<PropertyDescription> parameters = propertyDesc.getParameters();
                if (i < parameters.size()) {
                    PropertyDescription parameterPropertyDescription = parameters.get(i);
                    ValueReference<Boolean> returnValueAdjustedIncommingValueForIndex = new ValueReference<Boolean>(Boolean.FALSE);
                    newargs[i] = NGConversions.INSTANCE.convertSabloComponentToRhinoValue(JSONUtils.fromJSON(null, newargs[i], parameterPropertyDescription, new BrowserConverterContext(component, PushToServerEnum.allow), returnValueAdjustedIncommingValueForIndex), parameterPropertyDescription, component, scope);
                }
            // TODO? if in propertyDesc.getAsPropertyDescription().getConfig() we have  "type":"${dataproviderType}" and parameterPropertyDescription.getType() is Object
            // then get the type from the dataprovider and try to convert the json to that type instead of simply object
            }
        }
    }
    if (component instanceof WebFormComponent) {
        IPersist persist = ((WebFormComponent) component).getFormElement().getPersistIfAvailable();
        if (persist instanceof AbstractBase) {
            List<Object> instanceMethodArguments = ((AbstractBase) persist).getFlattenedMethodArguments(eventType);
            if (instanceMethodArguments != null && instanceMethodArguments.size() > 0) {
                // create entries for the instanceMethodArguments if they are more then callback arguments
                if (instanceMethodArguments.size() > newargs.length) {
                    newargs = Utils.arrayJoin(newargs, new Object[instanceMethodArguments.size() - newargs.length]);
                }
                // use instanceMethodArguments if not null, else just use the callback argument
                for (int i = 0; i < instanceMethodArguments.size(); i++) {
                    Object value = instanceMethodArguments.get(i);
                    if (value != null && value != JSONObject.NULL) {
                        newargs[i] = Utils.parseJSExpression(value);
                    }
                }
            }
        }
    }
    try {
        formController.getApplication().updateLastAccessed();
        return formController.getApplication().getScriptEngine().executeFunction(f, scope, scope, newargs, false, false);
    } catch (Exception ex) {
        formController.getApplication().reportJSError(ex.getMessage(), ex);
        return null;
    }
}
Also used : GlobalScope(com.servoy.j2db.scripting.GlobalScope) BaseComponent(com.servoy.j2db.persistence.BaseComponent) Form(com.servoy.j2db.persistence.Form) WebFormComponent(com.servoy.j2db.server.ngclient.WebFormComponent) WebObjectFunctionDefinition(org.sablo.specification.WebObjectFunctionDefinition) FormScope(com.servoy.j2db.scripting.FormScope) Function(org.mozilla.javascript.Function) BrowserConverterContext(org.sablo.specification.property.BrowserConverterContext) Solution(com.servoy.j2db.persistence.Solution) ValueReference(org.sablo.util.ValueReference) JSEvent(com.servoy.j2db.scripting.JSEvent) IJSEvent(com.servoy.base.scripting.api.IJSEvent) IRecord(com.servoy.j2db.dataprocessing.IRecord) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Scriptable(org.mozilla.javascript.Scriptable) JSONException(org.json.JSONException) PropertyDescription(org.sablo.specification.PropertyDescription) JSONObject(org.json.JSONObject) IPersist(com.servoy.j2db.persistence.IPersist) JSONObject(org.json.JSONObject) ScriptMethod(com.servoy.j2db.persistence.ScriptMethod)

Example 78 with IPersist

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

the class PersistBasedFormElementImpl method convertFromTabPanelToNGProperties.

private void convertFromTabPanelToNGProperties(IFormElement persist, FlattenedSolution fs, Map<String, Object> map, Map<String, PropertyDescription> specProperties, PropertyPath propertyPath) {
    ArrayList<Map<String, Object>> tabList = new ArrayList<>();
    // add the tabs.
    Iterator<IPersist> tabs = ((TabPanel) persist).getTabs();
    putAndConvertProperty("tabIndex", 1, map, fs, specProperties.get("tabIndex"), propertyPath);
    PropertyDescription tabSpecProperties = specProperties.get("tabs").getProperty("[0]");
    boolean active = true;
    while (tabs.hasNext()) {
        Map<String, Object> tabMap = new HashMap<>();
        Tab tab = (Tab) tabs.next();
        putAndConvertProperty("text", tab.getText(), tabMap, fs, tabSpecProperties.getProperty("text"), propertyPath);
        putAndConvertProperty("relationName", tab.getRelationName(), tabMap, fs, tabSpecProperties.getProperty("relationName"), propertyPath);
        putAndConvertProperty("active", Boolean.valueOf(active), tabMap, fs, tabSpecProperties.getProperty("active"), propertyPath);
        tabMap.put("foreground", tab.getForeground());
        putAndConvertProperty("name", tab.getName(), tabMap, fs, tabSpecProperties.getProperty("name"), propertyPath);
        putAndConvertProperty("mnemonic", tab.getMnemonic(), tabMap, fs, tabSpecProperties.getProperty("mnemonic"), propertyPath);
        int containsFormID = tab.getContainsFormID();
        // TODO should this be resolved way later on?
        // if solution model then this form can change..
        Form form = fs.getForm(containsFormID);
        if (form != null) {
            putAndConvertProperty("containsFormId", form.getName(), tabMap, fs, tabSpecProperties.getProperty("containsFormId"), propertyPath);
        }
        putAndConvertProperty("disabled", false, tabMap, fs, tabSpecProperties.getProperty("disabled"), propertyPath);
        int orient = ((TabPanel) persist).getTabOrientation();
        if (orient != TabPanel.SPLIT_HORIZONTAL && orient != TabPanel.SPLIT_VERTICAL) {
            int tabMediaID = tab.getImageMediaID();
            if (tabMediaID > 0) {
                putAndConvertProperty("imageMediaID", Integer.valueOf(tabMediaID), tabMap, fs, tabSpecProperties.getProperty("imageMediaID"), propertyPath);
            }
        }
        tabList.add(tabMap);
        active = false;
    }
    map.put("tabs", tabList.toArray());
}
Also used : TabPanel(com.servoy.j2db.persistence.TabPanel) HashMap(java.util.HashMap) Form(com.servoy.j2db.persistence.Form) ArrayList(java.util.ArrayList) PropertyDescription(org.sablo.specification.PropertyDescription) Tab(com.servoy.j2db.persistence.Tab) IPersist(com.servoy.j2db.persistence.IPersist) JSONObject(org.json.JSONObject) ServoyJSONObject(com.servoy.j2db.util.ServoyJSONObject) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with IPersist

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

the class FormElementHelper method getRowHeight.

private int getRowHeight(Form form) {
    int rowHeight = 0;
    Part part = getBodyPart(form);
    int startPos = form.getPartStartYPos(part.getID());
    int endPos = part.getHeight();
    Iterator<IPersist> it = form.getAllObjects(PositionComparator.XY_PERSIST_COMPARATOR);
    while (it.hasNext()) {
        IPersist persist = it.next();
        if (persist instanceof GraphicalComponent && ((GraphicalComponent) persist).getLabelFor() != null)
            continue;
        if (persist instanceof BaseComponent) {
            BaseComponent bc = (BaseComponent) persist;
            Point location = bc.getLocation();
            if (startPos <= location.y && endPos > location.y) {
                if (rowHeight == 0) {
                    rowHeight = bc.getSize().height;
                    break;
                }
            }
        }
    }
    return rowHeight == 0 ? 20 : rowHeight;
}
Also used : BaseComponent(com.servoy.j2db.persistence.BaseComponent) IPersist(com.servoy.j2db.persistence.IPersist) Part(com.servoy.j2db.persistence.Part) GraphicalComponent(com.servoy.j2db.persistence.GraphicalComponent) Point(java.awt.Point) Point(java.awt.Point)

Example 80 with IPersist

use of com.servoy.j2db.persistence.IPersist 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

IPersist (com.servoy.j2db.persistence.IPersist)84 Point (java.awt.Point)26 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)23 Form (com.servoy.j2db.persistence.Form)22 ArrayList (java.util.ArrayList)22 IFormElement (com.servoy.j2db.persistence.IFormElement)20 BaseComponent (com.servoy.j2db.persistence.BaseComponent)19 HashMap (java.util.HashMap)16 IFieldComponent (com.servoy.j2db.ui.IFieldComponent)14 IComponent (com.servoy.j2db.ui.IComponent)13 IRuntimeComponent (com.servoy.j2db.ui.runtime.IRuntimeComponent)13 Component (org.apache.wicket.Component)13 AbstractBase (com.servoy.j2db.persistence.AbstractBase)12 JSONObject (org.json.JSONObject)10 PropertyDescription (org.sablo.specification.PropertyDescription)10 RepositoryException (com.servoy.j2db.persistence.RepositoryException)8 Tab (com.servoy.j2db.persistence.Tab)8 IPortalComponent (com.servoy.j2db.ui.IPortalComponent)8 FlattenedForm (com.servoy.j2db.persistence.FlattenedForm)7 ISupportAnchors (com.servoy.j2db.persistence.ISupportAnchors)7