Search in sources :

Example 11 with Constraint

use of com.codename1.ui.validation.Constraint in project CodenameOne by codenameone.

the class HTMLComponent method handleTableCell.

/**
 * Handles a single table cell (a TD tag)
 *
 * @param tdTag The TD tag element
 * @param align The current alignment
 */
private void handleTableCell(HTMLElement tdTag, int align) {
    newLineIfNotEmpty(align);
    tableCells.addElement(curContainer);
    Container cell = new Container();
    cell.getStyle().setBgTransparency(0);
    cell.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    // int border=0;
    HTMLElement trTag = (HTMLElement) tdTag.getParent();
    while ((trTag != null) && (trTag.getTagId() != HTMLElement.TAG_TR)) {
        // Though in strict XHTML TR can only contain TD/TH - in some HTMLs TR doesn't have to be the direct parent of the tdTag, i.e.: <tr><b><td>...</td>... </b></tr>
        trTag = (HTMLElement) trTag.getParent();
    }
    // Commented since the table border should not affect cell border
    /*if (trTag!=null) { // Null checks to prevent exceptions for a TD tag without table etc.
                HTMLElement tableTag=(HTMLElement)trTag.getParent();
                while ((tableTag!=null) && (tableTag.getTagId()!=HTMLElement.TAG_TABLE)) { // Though in strict XHTML TABLE can only contain TR - in some HTMLs it might be different
                    tableTag=(HTMLElement)tableTag.getParent();
                }

                if (tableTag!=null) {
                    border=getInt(tableTag.getAttributeById(HTMLElement.ATTR_BORDER));
                }
            }
            cell.getUnselectedStyle().setPadding(border, border, border, border);
            cell.getSelectedStyle().setPadding(border, border, border, border);*/
    // Constraint constraint = new Constraint();
    CellConstraint constraint = new CellConstraint();
    int halign = align;
    int valign = Component.CENTER;
    if (trTag != null) {
        HTMLElement tGroupTag = (HTMLElement) trTag.getParent();
        int tagId = tGroupTag.getTagId();
        if ((tagId == HTMLElement.TAG_TBODY) || (tagId == HTMLElement.TAG_THEAD) || (tagId == HTMLElement.TAG_TFOOT)) {
            // Get the default TR alignment
            halign = getHorizAlign(tGroupTag.getAttributeById(HTMLElement.ATTR_ALIGN), halign, false);
            // Get the default TR valignment
            valign = getVertAlign(tGroupTag.getAttributeById(HTMLElement.ATTR_VALIGN), valign);
        }
        // Get the default TR alignment
        halign = getHorizAlign(trTag.getAttributeById(HTMLElement.ATTR_ALIGN), halign, false);
        // Get the default TR valignment
        valign = getVertAlign(trTag.getAttributeById(HTMLElement.ATTR_VALIGN), valign);
    }
    halign = getHorizAlign(tdTag.getAttributeById(HTMLElement.ATTR_ALIGN), halign, false);
    valign = getVertAlign(tdTag.getAttributeById(HTMLElement.ATTR_VALIGN), valign);
    int colspan = getInt(tdTag.getAttributeById(HTMLElement.ATTR_COLSPAN));
    int rowspan = getInt(tdTag.getAttributeById(HTMLElement.ATTR_ROWSPAN));
    String cWidth = tdTag.getAttributeById(HTMLElement.ATTR_WIDTH);
    int pW = getPercentage(cWidth);
    if ((pW > 0) && (pW < 100)) {
    // constraint.setWidthPercentage(pW); //TODO - Setting a width constraint currently makes the field width 0 - needs to be fixed in TableLayout
    } else {
        pW = getInt(cWidth);
        if (pW != 0) {
            cell.setPreferredW(pW);
        }
    }
    String cHeight = tdTag.getAttributeById(HTMLElement.ATTR_HEIGHT);
    int pH = getPercentage(cHeight);
    if ((pH > 0) && (pH < 100)) {
    // constraint.setHeightPercentage(pH); //TODO - Setting a height constraint currently makes the field height 0 - needs to be fixed in TableLayout
    } else {
        pH = getInt(cHeight);
        if (pH != 0) {
            cell.setPreferredH(pH);
        }
    }
    constraint.setHorizontalAlign(halign);
    constraint.setVerticalAlign(valign);
    if (colspan > 1) {
        constraint.setHorizontalSpan(colspan);
    }
    if (rowspan > 1) {
        constraint.setVerticalSpan(rowspan);
    }
    curContainer = cell;
    if (curTable != null) {
        curTable.addCell(cell, (tdTag.getTagId() == HTMLElement.TAG_TH), constraint);
    }
    if (loadCSS) {
        tdTag.setAssociatedComponents(cell);
        if (trTag != null) {
            trTag.addAssociatedComponent(cell);
        }
    }
}
Also used : Container(com.codename1.ui.Container) BoxLayout(com.codename1.ui.layouts.BoxLayout)

Example 12 with Constraint

use of com.codename1.ui.validation.Constraint in project CodenameOne by codenameone.

the class LayeredLayout method layoutComponent.

/**
 * Lays out the specific component within the container.  This will first lay out any components that it depends on.
 * @param parent The parent container being laid out.
 * @param cmp The component being laid out.
 * @param top
 * @param left
 * @param bottom
 * @param right
 */
private void layoutComponent(Container parent, Component cmp, int top, int left, int bottom, int right) {
    if (tmpLaidOut.contains(cmp)) {
        return;
    }
    tmpLaidOut.add(cmp);
    LayeredLayoutConstraint constraint = (LayeredLayoutConstraint) getComponentConstraint(cmp);
    if (constraint != null) {
        constraint.fixDependencies(parent);
        for (LayeredLayoutConstraint.Inset inset : constraint.insets) {
            if (inset.referenceComponent != null && inset.referenceComponent.getParent() == parent) {
                layoutComponent(parent, inset.referenceComponent, top, left, bottom, right);
            }
        }
    }
    Style s = cmp.getStyle();
    if (constraint != null) {
        // int innerTop = top;
        // int innerBottom = bottom;
        // left = 0;
        // right = parent.getLayoutWidth();
        int leftInset = constraint.insets[Component.LEFT].calculate(cmp, top, left, bottom, right);
        int rightInset = constraint.insets[Component.RIGHT].calculate(cmp, top, left, bottom, right);
        int topInset = constraint.insets[Component.TOP].calculate(cmp, top, left, bottom, right);
        int bottomInset = constraint.insets[Component.BOTTOM].calculate(cmp, top, left, bottom, right);
        cmp.setX(left + leftInset + s.getMarginLeft(parent.isRTL()));
        cmp.setY(top + topInset + s.getMarginTop());
        cmp.setWidth(Math.max(0, right - cmp.getX() - s.getMarginRight(parent.isRTL()) - rightInset));
        // cmp.setWidth(Math.max(0, right - left - s.getHorizontalMargins() - rightInset - leftInset));
        // cmp.setHeight(Math.max(0, bottom - top - s.getVerticalMargins() - bottomInset - topInset));
        cmp.setHeight(Math.max(0, bottom - cmp.getY() - s.getMarginBottom() - bottomInset));
    } else {
        int x = left + s.getMarginLeft(parent.isRTL());
        int y = top + s.getMarginTop();
        int w = right - left - s.getHorizontalMargins();
        int h = bottom - top - s.getVerticalMargins();
        cmp.setX(x);
        cmp.setY(y);
        cmp.setWidth(Math.max(0, w));
        cmp.setHeight(Math.max(0, h));
    // System.out.println("Component laid out "+cmp);
    }
}
Also used : Inset(com.codename1.ui.layouts.LayeredLayout.LayeredLayoutConstraint.Inset) Style(com.codename1.ui.plaf.Style)

Example 13 with Constraint

use of com.codename1.ui.validation.Constraint in project CodenameOne by codenameone.

the class LayeredLayout method getPreferredSize.

/**
 * {@inheritDoc}
 */
@Override
public Dimension getPreferredSize(Container parent) {
    int maxWidth = 0;
    int maxHeight = 0;
    int numOfcomponents = parent.getComponentCount();
    tmpLaidOut.clear();
    boolean requiresSecondPassToCalculatePercentInsets = false;
    for (int i = 0; i < numOfcomponents; i++) {
        Component cmp = parent.getComponentAt(i);
        calcPreferredValues(cmp);
        LayeredLayoutConstraint constraint = (LayeredLayoutConstraint) getComponentConstraint(cmp);
        int vInsets = 0;
        int hInsets = 0;
        if (constraint != null) {
            if (!requiresSecondPassToCalculatePercentInsets) {
                for (Inset ins : constraint.insets) {
                    if (ins.unit == UNIT_PERCENT && ins.referenceComponent == null) {
                        requiresSecondPassToCalculatePercentInsets = true;
                        break;
                    }
                }
            }
            vInsets += constraint.insets[Component.TOP].preferredValue + constraint.insets[Component.BOTTOM].preferredValue;
            hInsets += constraint.insets[Component.LEFT].preferredValue + constraint.insets[Component.RIGHT].preferredValue;
        /*
                // Commenting all this stuff out because the calcPreferredValues() call should
                // take all of this into account already.
                Component topRef = constraint.top().getReferenceComponent();
                LayeredLayoutConstraint currConstraint = constraint;
                int maxIterations = numOfcomponents;
                int iter = 0;
                while (topRef != null) {
                    if (iter++ >= maxIterations) break;
                    vInsets += Math.max(0, topRef.getOuterPreferredH() * currConstraint.top().getReferencePosition());
                    currConstraint = getOrCreateConstraint(topRef);
                    topRef = currConstraint.top().getReferenceComponent();
                }
                Component bottomRef = constraint.bottom().getReferenceComponent();
                currConstraint = constraint;
                iter = 0;
                while (bottomRef != null) {
                    if (iter++ >= maxIterations) break;
                    vInsets += Math.max(0, bottomRef.getOuterPreferredH() * currConstraint.bottom().getReferencePosition());
                    currConstraint = getOrCreateConstraint(bottomRef);
                    bottomRef = currConstraint.bottom().getReferenceComponent();
                }
                
                Component leftRef = constraint.left().getReferenceComponent();
                currConstraint = constraint;
                iter = 0;
                while (leftRef != null) {
                    if (iter++ >= maxIterations) break;
                    hInsets += Math.max(0, leftRef.getOuterPreferredW() * currConstraint.left().getReferencePosition());
                    currConstraint = getOrCreateConstraint(leftRef);
                    leftRef = currConstraint.left().getReferenceComponent();
                }
                
                Component rightRef = constraint.right().getReferenceComponent();
                currConstraint = constraint;
                iter = 0;
                while (rightRef != null) {
                    if (iter++ >= maxIterations) break;
                    hInsets += Math.max(0, rightRef.getOuterPreferredW() * currConstraint.right().getReferencePosition());
                    currConstraint = getOrCreateConstraint(rightRef);
                    rightRef = currConstraint.right().getReferenceComponent();
                }
                */
        }
        maxHeight = Math.max(maxHeight, cmp.getPreferredH() + cmp.getStyle().getMarginTop() + cmp.getStyle().getMarginBottom() + vInsets);
        maxWidth = Math.max(maxWidth, cmp.getPreferredW() + cmp.getStyle().getMarginLeftNoRTL() + cmp.getStyle().getMarginRightNoRTL() + hInsets);
    }
    Style s = parent.getStyle();
    Dimension d = new Dimension(maxWidth + s.getPaddingLeftNoRTL() + s.getPaddingRightNoRTL(), maxHeight + s.getPaddingTop() + s.getPaddingBottom() + parent.getBottomGap());
    if (preferredWidthMM > 0) {
        int minW = Display.getInstance().convertToPixels(preferredWidthMM);
        if (d.getWidth() < minW) {
            d.setWidth(minW);
        }
    }
    if (preferredHeightMM > 0) {
        int minH = Display.getInstance().convertToPixels(preferredHeightMM);
        if (d.getHeight() < Display.getInstance().convertToPixels(preferredHeightMM)) {
            d.setHeight(minH);
        }
    }
    if (requiresSecondPassToCalculatePercentInsets) {
        // We will do a second pass to deal with percent unit insets
        // since these were set to have zero preferred sizes in the calculation.
        // This is still a bit of a hack as it only deals with components that
        // don't depend on any other components.  E.g. If we have a label that is
        // supposed to have a top inset of 75%.  The preferred height should then
        // be 4 times the preferred height of the label rather than just the
        // preferred height of the label itself.
        // This still doesn't deal with the case where there is another label
        // that references that label and has an inset of an additional 20%
        // Ref https://github.com/codenameone/CodenameOne/issues/2720
        float maxHRatio = 0;
        float maxWRatio = 0;
        for (int i = 0; i < numOfcomponents; i++) {
            Component cmp = parent.getComponentAt(i);
            LayeredLayoutConstraint constraint = (LayeredLayoutConstraint) getComponentConstraint(cmp);
            if (constraint != null) {
                float hRatio = 0;
                if (constraint.top().unit == UNIT_PERCENT && constraint.top().referenceComponent == null) {
                    hRatio += constraint.top().value / 100f;
                }
                if (constraint.bottom().unit == UNIT_PERCENT && constraint.bottom().referenceComponent == null) {
                    hRatio += constraint.bottom().value / 100f;
                }
                hRatio = Math.min(1f, hRatio);
                maxHRatio = Math.max(maxHRatio, hRatio);
                float wRatio = 0;
                if (constraint.left().unit == UNIT_PERCENT && constraint.left().referenceComponent == null) {
                    wRatio += constraint.left().value / 100f;
                }
                if (constraint.right().unit == UNIT_PERCENT && constraint.right().referenceComponent == null) {
                    wRatio += constraint.right().value / 100f;
                }
                wRatio = Math.min(1f, wRatio);
                maxWRatio = Math.max(maxWRatio, wRatio);
            }
        }
        if (maxHRatio > 0 && maxHRatio < 1) {
            d.setHeight((int) Math.round(d.getHeight() / (1 - maxHRatio)));
        }
        if (maxWRatio > 0 && maxWRatio < 1) {
            d.setWidth((int) Math.round(d.getWidth() / (1 - maxWRatio)));
        }
    }
    return d;
}
Also used : Inset(com.codename1.ui.layouts.LayeredLayout.LayeredLayoutConstraint.Inset) Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Example 14 with Constraint

use of com.codename1.ui.validation.Constraint in project CodenameOne by codenameone.

the class HTMLInputFormat method applyConstraints.

/**
 * Applies the constrains represented by this object to the given TextArea.
 * After invoking this method the returned TextArea should be used as restrictions are made sometimes on a new object.
 * In case this is a TextField, this method will also set the input modes as needed.
 *
 * @param ta The TextArea to apply the constraints on.
 * @return An instance of TextArea (Either the given one or a new one) with the constraints.
 */
TextArea applyConstraints(TextArea ta) {
    int widestConstraint = 0;
    for (Enumeration e = formatConstraints.elements(); e.hasMoreElements(); ) {
        FormatConstraint constraint = (FormatConstraint) e.nextElement();
        for (int i = 1; i <= 16; i *= 2) {
            if ((constraint.type & i) != 0) {
                widestConstraint |= i;
            }
        }
    }
    if (maxLength != Integer.MAX_VALUE) {
        ta.setMaxSize(maxLength);
    }
    if (widestConstraint == FormatConstraint.TYPE_NUMERIC) {
        ta.setConstraint(ta.getConstraint() | TextArea.NUMERIC);
    }
    if (ta instanceof TextField) {
        TextField tf = (TextField) ta;
        if (((widestConstraint & FormatConstraint.TYPE_SYMBOL) == 0) && ((widestConstraint & FormatConstraint.TYPE_ANY) == 0)) {
            // No symbols allowed
            tf = new TextField(ta.getText()) {

                protected void showSymbolDialog() {
                // Block symbols dialog
                }
            };
            tf.setConstraint(ta.getConstraint());
            ta = tf;
        }
        if ((widestConstraint & FormatConstraint.TYPE_ANY) != 0) {
            if ((widestConstraint & FormatConstraint.TYPE_UPPERCASE) != 0) {
                tf.setInputMode("ABC");
            } else {
                tf.setInputMode("abc");
            }
        } else {
            if ((widestConstraint & FormatConstraint.TYPE_LOWERCASE) == 0) {
                excludeInputMode(tf, "abc");
                excludeInputMode(tf, "Abc");
            }
            if ((widestConstraint & FormatConstraint.TYPE_UPPERCASE) == 0) {
                excludeInputMode(tf, "ABC");
                excludeInputMode(tf, "Abc");
            }
            if ((widestConstraint & FormatConstraint.TYPE_NUMERIC) == 0) {
                excludeInputMode(tf, "123");
            }
        }
    }
    return ta;
}
Also used : Enumeration(java.util.Enumeration) TextField(com.codename1.ui.TextField)

Example 15 with Constraint

use of com.codename1.ui.validation.Constraint in project CodenameOne by codenameone.

the class HTMLTable method createCellConstraint.

/**
 * This method is overriden to fetch the constraints from the associated HTMLTableModel and converts it to TableLayout.Constraint
 *
 * @param value the value of the cell
 * @param row the table row
 * @param column the table column
 * @return the table constraint
 */
protected Constraint createCellConstraint(Object value, int row, int column) {
    CellConstraint cConstraint = ((HTMLTableModel) getModel()).getConstraint(value);
    if (cConstraint == null) {
        // Can be null for cells that were "spanned over"
        return super.createCellConstraint(value, row, column);
    }
    Constraint constraint = new Constraint();
    constraint.setHorizontalAlign(cConstraint.align);
    constraint.setVerticalAlign(cConstraint.valign);
    constraint.setHorizontalSpan(cConstraint.spanHorizontal);
    constraint.setVerticalSpan(cConstraint.spanVertical);
    constraint.setWidthPercentage(cConstraint.width);
    constraint.setHeightPercentage(cConstraint.height);
    return constraint;
}
Also used : Constraint(com.codename1.ui.table.TableLayout.Constraint)

Aggregations

Component (com.codename1.ui.Component)7 TextArea (com.codename1.ui.TextArea)7 Container (com.codename1.ui.Container)5 Style (com.codename1.ui.plaf.Style)5 PeerComponent (com.codename1.ui.PeerComponent)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 Inset (com.codename1.ui.layouts.LayeredLayout.LayeredLayoutConstraint.Inset)3 Point (java.awt.Point)3 java.util (java.util)3 Button (com.codename1.ui.Button)2 Form (com.codename1.ui.Form)2 Label (com.codename1.ui.Label)2 TextField (com.codename1.ui.TextField)2 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)2 Dimension (com.codename1.ui.geom.Dimension)2 BoxLayout (com.codename1.ui.layouts.BoxLayout)2 UITimer (com.codename1.ui.util.UITimer)2 java.awt (java.awt)2 AttributedString (java.text.AttributedString)2 Timer (java.util.Timer)2