Search in sources :

Example 6 with IFormElement

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

the class AngularFormGenerator method writePosition.

/**
 * @param writer
 * @param o
 */
@SuppressWarnings("nls")
public static void writePosition(JSONWriter writer, IPersist o, Form form, WebFormComponent webComponent, boolean isDesigner) {
    if (o instanceof BaseComponent && ((BaseComponent) o).getCssPosition() != null) {
        CSSPosition position = ((BaseComponent) o).getCssPosition();
        if (webComponent != null) {
            Object runtimeValue = webComponent.getProperty(IContentSpecConstants.PROPERTY_CSS_POSITION);
            if (runtimeValue instanceof CSSPosition) {
                position = (CSSPosition) runtimeValue;
            }
        }
        writer.key("position");
        writer.object();
        if (CSSPositionUtils.isSet(position.left)) {
            writer.key("left").value(CSSPositionUtils.getCSSValue(position.left));
        }
        if (CSSPositionUtils.isSet(position.top)) {
            String top = position.top;
            if (!isDesigner) {
                Point location = CSSPositionUtils.getLocation((BaseComponent) o);
                Part prt = form.getPartAt(location.y);
                if (prt != null) {
                    int topStart = form.getPartStartYPos(prt.getID());
                    if (topStart > 0) {
                        if (top.endsWith("px")) {
                            top = top.substring(0, top.length() - 2);
                        }
                        int topInteger = Utils.getAsInteger(top, -1);
                        if (topInteger != -1) {
                            top = String.valueOf(topInteger - topStart);
                        } else {
                            top = "calc(" + top + "-" + topStart + "px)";
                        }
                    }
                }
            }
            writer.key("top").value(CSSPositionUtils.getCSSValue(top));
        }
        if (CSSPositionUtils.isSet(position.bottom)) {
            writer.key("bottom").value(CSSPositionUtils.getCSSValue(position.bottom));
        }
        if (CSSPositionUtils.isSet(position.right)) {
            writer.key("right").value(CSSPositionUtils.getCSSValue(position.right));
        }
        if (CSSPositionUtils.isSet(position.width)) {
            if (CSSPositionUtils.isSet(position.left) && CSSPositionUtils.isSet(position.right)) {
                writer.key("min-width").value(CSSPositionUtils.getCSSValue(position.width));
            } else {
                writer.key("width").value(CSSPositionUtils.getCSSValue(position.width));
            }
        }
        if (CSSPositionUtils.isSet(position.height)) {
            if (CSSPositionUtils.isSet(position.top) && CSSPositionUtils.isSet(position.bottom)) {
                writer.key("min-height").value(CSSPositionUtils.getCSSValue(position.height));
            } else {
                writer.key("height").value(CSSPositionUtils.getCSSValue(position.height));
            }
        }
        writer.endObject();
    } else {
        Point location = ((IFormElement) o).getLocation();
        Dimension size = ((IFormElement) o).getSize();
        if (webComponent != null) {
            Object runtimeValue = webComponent.getProperty(IContentSpecConstantsBase.PROPERTY_LOCATION);
            if (runtimeValue instanceof Point) {
                location = (Point) runtimeValue;
            }
            runtimeValue = webComponent.getProperty(IContentSpecConstantsBase.PROPERTY_SIZE);
            if (runtimeValue instanceof Dimension) {
                size = (Dimension) runtimeValue;
            }
        }
        if (location != null && size != null) {
            int anchorFlags = ((BaseComponent) o).getAnchors();
            boolean anchoredTop = (anchorFlags & IAnchorConstants.NORTH) != 0;
            boolean anchoredRight = (anchorFlags & IAnchorConstants.EAST) != 0;
            boolean anchoredBottom = (anchorFlags & IAnchorConstants.SOUTH) != 0;
            boolean anchoredLeft = (anchorFlags & IAnchorConstants.WEST) != 0;
            if (!anchoredLeft && !anchoredRight)
                anchoredLeft = true;
            if (!anchoredTop && !anchoredBottom)
                anchoredTop = true;
            writer.key("position");
            writer.object();
            if (anchoredTop) {
                writer.key("top");
                writer.value(location.y + "px");
            }
            if (anchoredBottom) {
                writer.key("bottom");
                int partHeight = form.getSize().height;
                if (!isDesigner) {
                    // search for element's part using its design time location
                    Part prt = form.getPartAt(((IFormElement) o).getLocation().y);
                    if (prt != null) {
                        int prtEnd = form.getPartEndYPos(prt.getID());
                        if (prtEnd > form.getSize().height)
                            prtEnd = form.getSize().height;
                        partHeight = prtEnd - form.getPartStartYPos(prt.getID());
                    }
                }
                writer.value(partHeight - location.y - size.height + "px");
            }
            if (!anchoredTop || !anchoredBottom) {
                writer.key("height");
                writer.value(size.height + "px");
            }
            if (anchoredLeft) {
                writer.key("left");
                writer.value(location.x + "px");
            }
            if (anchoredRight) {
                writer.key("right");
                writer.value((form.getWidth() - location.x - size.width) + "px");
            }
            if (!anchoredLeft || !anchoredRight) {
                writer.key("width");
                writer.value(size.width + "px");
            }
            if (anchoredTop && anchoredBottom) {
                writer.key("min-height");
                writer.value(size.height + "px");
            }
            if (anchoredLeft && anchoredRight) {
                writer.key("min-width");
                writer.value(size.width + "px");
            }
            writer.endObject();
        }
    }
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) BaseComponent(com.servoy.j2db.persistence.BaseComponent) Part(com.servoy.j2db.persistence.Part) CSSPosition(com.servoy.j2db.persistence.CSSPosition) Point(java.awt.Point) Dimension(java.awt.Dimension) Point(java.awt.Point)

Example 7 with IFormElement

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

the class FormLayoutGenerator method generateFormComponent.

public static String generateFormComponent(Form form, FlattenedSolution fs, IFormElementCache cache) {
    StringWriter out = new StringWriter();
    PrintWriter writer = new PrintWriter(out);
    Iterator<? extends IPersist> componentsIterator = null;
    if (!form.isResponsiveLayout()) {
        List<IPersist> components = fs.getFlattenedForm(form).getAllObjectsAsList();
        List<IFormElement> formElements = new ArrayList<>();
        for (IPersist persist : components) {
            if (persist instanceof IFormElement) {
                formElements.add((IFormElement) persist);
            }
        }
        Collections.sort(formElements, new Comparator<IFormElement>() {

            public int compare(IFormElement o1, IFormElement o2) {
                int result = FlattenedForm.FORM_INDEX_WITH_HIERARCHY_COMPARATOR.compare(o1, o2);
                if (result == 0) {
                    result = TabSeqComparator.compareTabSeq(getTabSeq(o1), o1, getTabSeq(o2), o2);
                }
                return result;
            }
        });
        componentsIterator = formElements.iterator();
    } else {
        componentsIterator = fs.getFlattenedForm(form).getAllObjects(PositionComparator.XY_PERSIST_COMPARATOR);
    }
    while (componentsIterator.hasNext()) {
        IPersist component = componentsIterator.next();
        // if (PartWrapper.isSecurityVisible(component, fs, form))
        if (component instanceof LayoutContainer) {
            FormLayoutStructureGenerator.generateLayoutContainer((LayoutContainer) component, form, fs, writer, null, cache);
        } else if (component instanceof IFormElement) {
            FormElement fe = cache.getFormElement((IFormElement) component, fs, null, false);
            if (form != null && !form.isResponsiveLayout()) {
                FormLayoutGenerator.generateFormElementWrapper(writer, fe, form, form.isResponsiveLayout());
            }
            FormLayoutGenerator.generateFormElement(writer, fe, form);
            if (form != null && !form.isResponsiveLayout()) {
                FormLayoutGenerator.generateEndDiv(writer);
            }
        }
    }
    return out.getBuffer().toString();
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) StringWriter(java.io.StringWriter) IPersist(com.servoy.j2db.persistence.IPersist) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) ArrayList(java.util.ArrayList) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) PrintWriter(java.io.PrintWriter)

Example 8 with IFormElement

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

the class FormLayoutStructureGenerator method generateLayoutContainer.

public static void generateLayoutContainer(LayoutContainer container, Form form, FlattenedSolution fs, PrintWriter writer, DesignProperties design, IFormElementCache cache) {
    WebLayoutSpecification spec = null;
    if (container.getPackageName() != null) {
        PackageSpecification<WebLayoutSpecification> pkg = WebComponentSpecProvider.getSpecProviderState().getLayoutSpecifications().get(container.getPackageName());
        if (pkg != null) {
            spec = pkg.getSpecification(container.getSpecName());
        }
    }
    boolean isCSSPositionContainer = CSSPositionUtils.isCSSPositionContainer(spec);
    writer.print("<");
    writer.print(container.getTagType());
    Set<String> writtenAttributes = new HashSet<>();
    if (design != null) {
        writer.print(" svy-id='");
        writer.print(container.getUUID().toString());
        writer.print("'");
        writer.print(" svy-location='");
        writer.print(container.getLocation().x);
        writer.print("'");
        boolean highSet = false;
        JSONObject ngClass = new JSONObject();
        String layoutStyleClasses = "";
        String solutionStyleClasses = "";
        if (spec != null) {
            writer.print(" svy-layoutname='");
            writer.print(spec.getPackageName() + "." + spec.getName());
            writer.print("'");
            ngClass.put("svy-layoutcontainer", true);
            if (// is this inherited?
            !(container.getAncestor(IRepository.FORMS).getID() == form.getID())) {
                ngClass.put("inheritedElement", true);
            }
            String designClass = spec.getDesignStyleClass() != null && spec.getDesignStyleClass().length() > 0 ? spec.getDesignStyleClass() : "customDivDesign";
            if ("customDivDesign".equals(designClass) && hasSameDesignClassAsParent(container, spec)) {
                designClass = isEvenLayoutContainer(container) ? "customDivDesignOdd" : "customDivDesignEven";
            }
            highSet = true;
            if (design.mainContainer != container.getID()) {
                // added <> tokens so that we can remove quotes around the values so that angular will evaluate at runtime
                ngClass.put(designClass, "<showWireframe<");
            } else {
                writer.print(" data-maincontainer='true'");
                ngClass.put(designClass, "<false<");
            }
            // added <> tokens so that we can remove quotes around the values so that angular will evaluate at runtime
            ngClass.put("highlight_element", "<design_highlight=='highlight_element'<");
            List<String> containerStyleClasses = getStyleClassValues(spec, container.getCssClasses());
            solutionStyleClasses = getSolutionSpecificClasses(spec, container);
            if (!containerStyleClasses.isEmpty()) {
                layoutStyleClasses = containerStyleClasses.stream().collect(Collectors.joining(" "));
                writer.print(" class='" + layoutStyleClasses + "'");
            }
            if (container.getCssClasses() != null && container.getCssClasses().trim().length() > 0) {
                writtenAttributes.add("class");
            }
            if (spec.getAllowedChildren().size() > 0 || spec.getExcludedChildren() != null) {
                // added <> tokens so that we can remove quotes around the values so that angular will evaluate at runtime
                ngClass.put("drop_highlight", "<canContainDraggedElement('" + spec.getPackageName() + "." + spec.getName() + "')<");
            }
        }
        if (!highSet)
            ngClass.put("highlight_element", "<design_highlight=='highlight_element'<");
        if (ngClass.length() > 0) {
            writer.print(" ng-class='" + ngClass.toString().replaceAll("\"<", "").replaceAll("<\"", "").replaceAll("'", "\"") + "'");
        }
        if (writtenAttributes.contains("class")) {
            writer.print(" svy-layout-class='" + layoutStyleClasses + "'");
            writer.print(" svy-solution-layout-class='" + solutionStyleClasses + "'");
        }
        writer.print(" svy-title='" + getLayouContainerTitle(container) + "'");
    }
    if (container.getName() != null) {
        writer.print(" svy-name='");
        writer.print(container.getName());
        writer.print("' ");
    }
    if (container.getElementId() != null) {
        writer.print(" id='");
        writer.print(container.getElementId());
        writer.print("' ");
    }
    writer.print(" svy-autosave ");
    if (isCSSPositionContainer) {
        // we need to specify the height
        writer.print(" style='height:");
        writer.print(container.getSize().height);
        writer.print("px;position: relative;' ");
    }
    Map<String, String> attributes = new HashMap<String, String>(container.getMergedAttributes());
    if (spec != null) {
        for (String propertyName : spec.getAllPropertiesNames()) {
            PropertyDescription pd = spec.getProperty(propertyName);
            if (pd.getDefaultValue() != null && !attributes.containsKey(propertyName)) {
                attributes.put(propertyName, pd.getDefaultValue().toString());
            }
        }
    }
    String classes = attributes.get("class");
    if (classes == null)
        classes = "svy-layoutcontainer";
    else
        classes += " svy-layoutcontainer";
    attributes.put("class", classes);
    for (Entry<String, String> entry : attributes.entrySet()) {
        if (design != null && writtenAttributes.contains(entry.getKey()))
            continue;
        writer.print(" ");
        try {
            StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(entry.getKey(), writer);
            if (entry.getValue() != null && entry.getValue().length() > 0) {
                writer.print("=\"");
                StringEscapeUtils.ESCAPE_ECMASCRIPT.translate(entry.getValue(), writer);
                writer.print("\"");
            }
        } catch (IOException e) {
            Debug.error(e);
        }
    }
    writer.print(">");
    Iterator<IPersist> components = container.getAllObjects(PositionComparator.XY_PERSIST_COMPARATOR);
    while (components.hasNext()) {
        IPersist component = components.next();
        if (component instanceof LayoutContainer) {
            generateLayoutContainer((LayoutContainer) component, form, fs, writer, design, cache);
        } else if (component instanceof IFormElement) {
            FormElement fe = cache.getFormElement((IFormElement) component, fs, null, design != null);
            if (!isSecurityVisible(fs, fe.getPersistIfAvailable(), form)) {
                continue;
            }
            if (isCSSPositionContainer) {
                FormLayoutGenerator.generateFormElementWrapper(writer, fe, form, false);
            }
            FormLayoutGenerator.generateFormElement(writer, fe, form);
            if (isCSSPositionContainer) {
                FormLayoutGenerator.generateEndDiv(writer);
            }
        }
    }
    writer.print("</");
    writer.print(container.getTagType());
    writer.print(">");
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement) PropertyDescription(org.sablo.specification.PropertyDescription) WebLayoutSpecification(org.sablo.specification.WebLayoutSpecification) IFormElement(com.servoy.j2db.persistence.IFormElement) JSONObject(org.json.JSONObject) IPersist(com.servoy.j2db.persistence.IPersist) LayoutContainer(com.servoy.j2db.persistence.LayoutContainer) HashSet(java.util.HashSet)

Example 9 with IFormElement

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

the class ComponentFactory method sortElementsOnPositionAndGroup.

/**
 * Return a new list with the elements of the input list sorted on position. Grouped elements are placed together.
 * @param elements
 */
public static List<IPersist> sortElementsOnPositionAndGroup(List<IPersist> elements) {
    if (elements == null)
        return null;
    // first sort on position, then move all grouped elements together
    List<IPersist> lst = new ArrayList<IPersist>(elements);
    Collections.sort(lst, PositionComparator.XY_PERSIST_COMPARATOR);
    for (int i = 0; i < lst.size(); i++) {
        IPersist element = lst.get(i);
        if (element instanceof IFormElement && ((IFormElement) element).getGroupID() != null) {
            // find other group elements, move them to the left
            for (int j = i + 2; j < lst.size(); j++) {
                IPersist element2 = lst.get(j);
                if (element2 instanceof IFormElement && ((IFormElement) element).getGroupID().equals(((IFormElement) element2).getGroupID())) {
                    // same group, move to the left
                    lst.add(i + 1, lst.remove(j));
                    i++;
                }
            }
        }
    }
    return lst;
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) IPersist(com.servoy.j2db.persistence.IPersist) ArrayList(java.util.ArrayList)

Example 10 with IFormElement

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

the class FormWrapper method getAbsoluteLayoutElements.

public List<FormElement> getAbsoluteLayoutElements() {
    List<FormElement> elements = new ArrayList<FormElement>();
    Collection<IFormElement> components = getBaseComponents();
    if (components != null) {
        for (IFormElement component : components) {
            if (CSSPositionUtils.isInAbsoluteLayoutMode(component)) {
                elements.add(FormElementHelper.INSTANCE.getFormElement(component, context.getSolution(), null, design));
            }
        }
    }
    return elements;
}
Also used : IFormElement(com.servoy.j2db.persistence.IFormElement) ArrayList(java.util.ArrayList) FormElement(com.servoy.j2db.server.ngclient.FormElement) IFormElement(com.servoy.j2db.persistence.IFormElement)

Aggregations

IFormElement (com.servoy.j2db.persistence.IFormElement)42 IPersist (com.servoy.j2db.persistence.IPersist)20 ArrayList (java.util.ArrayList)15 Point (java.awt.Point)14 HashMap (java.util.HashMap)12 JSONObject (org.json.JSONObject)12 PropertyDescription (org.sablo.specification.PropertyDescription)11 Form (com.servoy.j2db.persistence.Form)8 GraphicalComponent (com.servoy.j2db.persistence.GraphicalComponent)8 Part (com.servoy.j2db.persistence.Part)8 FormElement (com.servoy.j2db.server.ngclient.FormElement)8 WebObjectSpecification (org.sablo.specification.WebObjectSpecification)8 Dimension (java.awt.Dimension)7 AbstractBase (com.servoy.j2db.persistence.AbstractBase)6 PropertyPath (com.servoy.j2db.server.ngclient.property.types.PropertyPath)6 BaseComponent (com.servoy.j2db.persistence.BaseComponent)5 LayoutContainer (com.servoy.j2db.persistence.LayoutContainer)5 Portal (com.servoy.j2db.persistence.Portal)4 ServoyJSONObject (com.servoy.j2db.util.ServoyJSONObject)4 IBasicWebObject (com.servoy.j2db.persistence.IBasicWebObject)3