Search in sources :

Example 1 with AbstractContainer

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

the class FormElementHelper method getControlledTabSeqReplacementFor.

/**
 * Generates a Servoy controlled tab-sequence-index. We try to avoid sending default (0 or null) tabSeq even
 * for forms that do use default tab sequence in order to avoid problems with nesting default and non-default tabSeq forms.
 *
 * @param designValue the value the persist holds for the tabSeq.
 * @param form the form containing the persist
 * @param persistIfAvailable the persist. For now, 'component' type properties might work with non-persist-linked FormElements so it could be null.
 * When those become persist based as well this will never be null.
 * @return the requested controlled tabSeq (should make tabSeq be identical to the one shown in developer).
 */
public Integer getControlledTabSeqReplacementFor(Integer designValue, PropertyDescription pd, Form flattenedForm, IPersist persistIfAvailable, // TODO more args will be needed here such as the tabSeq property name or description
FlattenedSolution flattenedSolution, // TODO more args will be needed here such as the tabSeq property name or description
boolean design) {
    // TODO this can be removed when we know we'll always have a persist here; currently don't handle this in any way as it's not supported
    if (persistIfAvailable == null || (nestedCall.get() != null && nestedCall.get().booleanValue()))
        return designValue;
    nestedCall.set(Boolean.TRUE);
    final boolean responsiveForm = flattenedForm.isResponsiveLayout();
    try {
        if (flattenedForm.isFormComponent() && persistIfAvailable instanceof AbstractBase) {
            String mainFormName = ((AbstractBase) persistIfAvailable).getRuntimeProperty(FORM_COMPONENT_FORM_NAME);
            if (mainFormName != null) {
                flattenedForm = flattenedSolution.getFlattenedForm(flattenedSolution.getForm(mainFormName));
                // just return the skip value if this is somehow invalid.
                if (flattenedForm == null)
                    return Integer.valueOf(-2);
            }
        }
        boolean formWasModifiedViaSolutionModel = flattenedSolution.hasCopy(flattenedForm);
        Map<TabSeqProperty, Integer> cachedTabSeq = null;
        if (formWasModifiedViaSolutionModel) {
            Pair<Long, Map<TabSeqProperty, Integer>> pair = flattenedForm.getRuntimeProperty(FORM_TAB_SEQUENCE);
            if (pair != null && flattenedForm.getLastModified() == pair.getLeft().longValue()) {
                cachedTabSeq = pair.getRight();
            }
        } else
            cachedTabSeq = formTabSequences.get(flattenedForm.getUUID());
        if (cachedTabSeq == null) {
            cachedTabSeq = new HashMap<TabSeqProperty, Integer>();
            SortedList<TabSeqProperty> selected = new SortedList<TabSeqProperty>(new Comparator<TabSeqProperty>() {

                public int compare(TabSeqProperty o1, TabSeqProperty o2) {
                    int seq1 = o1.getSeqValue();
                    int seq2 = o2.getSeqValue();
                    if (seq1 == ISupportTabSeq.DEFAULT && seq2 == ISupportTabSeq.DEFAULT) {
                        if (responsiveForm) {
                            if (o1.element.getParent() == o2.element.getParent()) {
                                int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
                                if (positionComparator != 0) {
                                    return positionComparator;
                                }
                            } else {
                                List<ISupportChilds> ancestors = new ArrayList<ISupportChilds>();
                                IPersist persist = o1.element;
                                while (persist.getParent() instanceof AbstractContainer) {
                                    ancestors.add(persist.getParent());
                                    persist = persist.getParent();
                                }
                                persist = o2.element;
                                while (persist.getParent() instanceof AbstractContainer) {
                                    if (ancestors.contains(persist.getParent())) {
                                        // we found the common ancestor
                                        int index = ancestors.indexOf(persist.getParent());
                                        IPersist comparablePersist = index == 0 ? o1.element : ancestors.get(index - 1);
                                        int positionComparator = PositionComparator.comparePoint(false, ((ISupportBounds) comparablePersist).getLocation(), ((ISupportBounds) persist).getLocation());
                                        if (positionComparator != 0) {
                                            return positionComparator;
                                        }
                                    }
                                    persist = persist.getParent();
                                }
                            }
                        } else {
                            int positionComparator = PositionComparator.comparePoint(false, o1.getLocation(), o2.getLocation());
                            if (positionComparator != 0) {
                                return positionComparator;
                            }
                        }
                    }
                    return compareTabSeq(seq1, o1.element, seq2, o2.element, flattenedSolution);
                }
            });
            Map<TabSeqProperty, List<TabSeqProperty>> listFormComponentMap = new HashMap<TabSeqProperty, List<TabSeqProperty>>();
            List<TabSeqProperty> listFormComponentElements = null;
            TabSeqProperty listFormComponentTabSeq = null;
            Iterator<IFormElement> iterator = flattenedForm.getFlattenedObjects(null).iterator();
            while (iterator.hasNext()) {
                IFormElement formElement = iterator.next();
                if (FormTemplateGenerator.isWebcomponentBean(formElement)) {
                    String componentType = FormTemplateGenerator.getComponentTypeName(formElement);
                    WebObjectSpecification specification = WebComponentSpecProvider.getSpecProviderState().getWebComponentSpecification(componentType);
                    if (specification != null) {
                        Collection<PropertyDescription> properties = specification.getProperties(NGTabSeqPropertyType.NG_INSTANCE);
                        Collection<PropertyDescription> formComponentProperties = specification.getProperties(FormComponentPropertyType.INSTANCE);
                        boolean isListFormComponent = isListFormComponent(formComponentProperties);
                        if (properties != null && properties.size() > 0) {
                            IBasicWebComponent webComponent = (IBasicWebComponent) formElement;
                            for (PropertyDescription tabSeqProperty : properties) {
                                int tabseq = Utils.getAsInteger(webComponent.getProperty(tabSeqProperty.getName()));
                                TabSeqProperty seqProperty = new TabSeqProperty(formElement, tabSeqProperty.getName());
                                if (listFormComponentTabSeq == null && isListFormComponent) {
                                    listFormComponentTabSeq = seqProperty;
                                    listFormComponentElements = new ArrayList<TabSeqProperty>();
                                    listFormComponentMap.put(listFormComponentTabSeq, listFormComponentElements);
                                }
                                if (tabseq >= 0) {
                                    selected.add(seqProperty);
                                } else {
                                    cachedTabSeq.put(seqProperty, Integer.valueOf(-2));
                                }
                            }
                        }
                        addFormComponentProperties(formComponentProperties, formElement, flattenedSolution, cachedTabSeq, selected, listFormComponentElements, design, new HashSet<String>());
                    }
                } else if (formElement instanceof ISupportTabSeq) {
                    if (((ISupportTabSeq) formElement).getTabSeq() >= 0) {
                        selected.add(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()));
                    } else {
                        cachedTabSeq.put(new TabSeqProperty(formElement, StaticContentSpecLoader.PROPERTY_TABSEQ.getPropertyName()), Integer.valueOf(-2));
                    }
                }
            }
            int i = 1;
            for (TabSeqProperty tabSeq : selected) {
                cachedTabSeq.put(tabSeq, Integer.valueOf(i++));
            }
            for (TabSeqProperty tabSeq : listFormComponentMap.keySet()) {
                List<TabSeqProperty> elements = listFormComponentMap.get(tabSeq);
                if (elements != null) {
                    Integer value = cachedTabSeq.get(tabSeq);
                    // all elements inside list form component have same tabindex as the list itself
                    for (TabSeqProperty tabSeqElement : elements) {
                        cachedTabSeq.put(tabSeqElement, value);
                    }
                }
            }
            if (!formWasModifiedViaSolutionModel) {
                formTabSequences.putIfAbsent(flattenedForm.getUUID(), cachedTabSeq);
            } else {
                flattenedForm.setRuntimeProperty(FORM_TAB_SEQUENCE, new Pair<>(Long.valueOf(flattenedForm.getLastModified()), cachedTabSeq));
            }
        }
        IPersist realPersist = flattenedForm.findChild(persistIfAvailable.getUUID());
        if (realPersist == null) {
            realPersist = persistIfAvailable;
        }
        Integer controlledTabSeq = cachedTabSeq.get(new TabSeqProperty(realPersist, pd.getName()));
        // if not in tabSeq, use "skip" value
        if (controlledTabSeq == null)
            controlledTabSeq = Integer.valueOf(-2);
        return controlledTabSeq;
    } finally {
        nestedCall.set(Boolean.FALSE);
    }
}
Also used : WebObjectSpecification(org.sablo.specification.WebObjectSpecification) AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) SortedList(com.servoy.j2db.util.SortedList) ISupportBounds(com.servoy.j2db.persistence.ISupportBounds) ISupportTabSeq(com.servoy.j2db.persistence.ISupportTabSeq) List(java.util.List) ArrayList(java.util.ArrayList) SortedList(com.servoy.j2db.util.SortedList) IBasicWebComponent(com.servoy.j2db.persistence.IBasicWebComponent) ISupportChilds(com.servoy.j2db.persistence.ISupportChilds) AbstractBase(com.servoy.j2db.persistence.AbstractBase) Point(java.awt.Point) PropertyDescription(org.sablo.specification.PropertyDescription) IFormElement(com.servoy.j2db.persistence.IFormElement) IPersist(com.servoy.j2db.persistence.IPersist) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) WeakHashMap(java.util.WeakHashMap)

Example 2 with AbstractContainer

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

the class JSForm method getButtons.

/**
 * Returns all JSButtons of this form, including the ones without a name.
 *
 * @sample
 * var buttons = myForm.getButtons();
 * for (var b in buttons)
 * {
 * 	if (buttons[b].name != null)
 * 		application.output(buttons[b].name);
 * 	else
 * 		application.output(buttons[b].text + " has no name ");
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from parent form
 * @return the list of all JSButtons on this forms
 */
@JSFunction
public JSButton[] getButtons(boolean returnInheritedElements) {
    List<JSButton> buttons = new ArrayList<JSButton>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<GraphicalComponent> graphicalComponents = form2use.getGraphicalComponents();
    while (graphicalComponents.hasNext()) {
        GraphicalComponent button = graphicalComponents.next();
        if (ComponentFactory.isButton(button)) {
            buttons.add(new JSButton(this, button, application, false));
        }
    }
    return buttons.toArray(new JSButton[buttons.size()]);
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) GraphicalComponent(com.servoy.j2db.persistence.GraphicalComponent) ArrayList(java.util.ArrayList) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 3 with AbstractContainer

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

the class JSForm method getLabels.

/**
 * Returns all JSLabels of this form (optionally including it super forms labels), including the ones without a name.
 *
 * @sample
 * var frm = solutionModel.getForm("myForm");
 * var labels = frm.getLabels();
 * for (var i in labels)
 * {
 * 	var lname = labels[i].name;
 * 	if (lname != null)
 * 		application.output(lname);
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from parent form
 * @return all JSLabels on this form
 */
@JSFunction
public JSLabel[] getLabels(boolean returnInheritedElements) {
    List<JSLabel> labels = new ArrayList<JSLabel>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<GraphicalComponent> graphicalComponents = form2use.getGraphicalComponents();
    while (graphicalComponents.hasNext()) {
        GraphicalComponent gc = graphicalComponents.next();
        if (!ComponentFactory.isButton(gc)) {
            labels.add(new JSLabel(this, gc, application, false));
        }
    }
    return labels.toArray(new JSLabel[labels.size()]);
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) GraphicalComponent(com.servoy.j2db.persistence.GraphicalComponent) ArrayList(java.util.ArrayList) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 4 with AbstractContainer

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

the class JSForm method getPortals.

/**
 * Returns all JSPortal objects of this form (optionally also the ones from the parent form), including the ones without a name.
 *
 * @sample
 * var frm = solutionModel.getForm("myForm");
 * var portals = frm.getPortals();
 * for (var i in portals)
 * {
 * 	var p = portals[i];
 * 	if (p.name != null)
 * 		application.output(p.name);
 * 	else
 * 		application.output("unnamed portal detected");
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from parent form
 * @return an array of all JSPortal objects on this form
 */
@JSFunction
public JSPortal[] getPortals(boolean returnInheritedElements) {
    List<JSPortal> portals = new ArrayList<JSPortal>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<Portal> iterator = form2use.getPortals();
    while (iterator.hasNext()) {
        portals.add(new JSPortal(this, iterator.next(), application, false));
    }
    return portals.toArray(new JSPortal[portals.size()]);
}
Also used : AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ArrayList(java.util.ArrayList) Portal(com.servoy.j2db.persistence.Portal) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Example 5 with AbstractContainer

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

the class JSForm method getFields.

/**
 * Returns all JSField objects of this form, including the ones without a name.
 *
 * @sample
 * var frm = solutionModel.getForm("myForm");
 * var fields = frm.getFields();
 * for (var f in fields)
 * {
 * 	var fname = fields[f].name;
 * 	if (fname != null)
 * 		application.output(fname);
 * }
 *
 * @param returnInheritedElements boolean true to also return the elements from the parent form
 * @return all JSField objects of this form
 */
@JSFunction
public JSField[] getFields(boolean returnInheritedElements) {
    List<JSField> fields = new ArrayList<JSField>();
    AbstractContainer form2use = returnInheritedElements ? getFlattenedContainer() : getContainer();
    Iterator<Field> iterator = form2use.getFields();
    while (iterator.hasNext()) {
        fields.add(JSField.createField(this, iterator.next(), application, false));
    }
    return fields.toArray(new JSField[fields.size()]);
}
Also used : Field(com.servoy.j2db.persistence.Field) AbstractContainer(com.servoy.j2db.persistence.AbstractContainer) ArrayList(java.util.ArrayList) JSFunction(org.mozilla.javascript.annotations.JSFunction)

Aggregations

AbstractContainer (com.servoy.j2db.persistence.AbstractContainer)13 ArrayList (java.util.ArrayList)10 JSFunction (org.mozilla.javascript.annotations.JSFunction)9 Form (com.servoy.j2db.persistence.Form)4 Point (java.awt.Point)4 ServoyClientSupport (com.servoy.base.scripting.annotations.ServoyClientSupport)3 IPersist (com.servoy.j2db.persistence.IPersist)3 LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)3 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)2 RepositoryException (com.servoy.j2db.persistence.RepositoryException)2 WebComponent (com.servoy.j2db.persistence.WebComponent)2 IBaseSMForm (com.servoy.base.solutionmodel.IBaseSMForm)1 AbstractBase (com.servoy.j2db.persistence.AbstractBase)1 BaseComponent (com.servoy.j2db.persistence.BaseComponent)1 Bean (com.servoy.j2db.persistence.Bean)1 Field (com.servoy.j2db.persistence.Field)1 IBasicWebComponent (com.servoy.j2db.persistence.IBasicWebComponent)1 IFormElement (com.servoy.j2db.persistence.IFormElement)1 ISupportBounds (com.servoy.j2db.persistence.ISupportBounds)1 ISupportChilds (com.servoy.j2db.persistence.ISupportChilds)1