Search in sources :

Example 11 with Constraint

use of com.codename1.ui.table.TableLayout.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 12 with Constraint

use of com.codename1.ui.table.TableLayout.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)

Example 13 with Constraint

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

the class BorderLayout method addLayoutComponent.

/**
 * {@inheritDoc}
 */
public void addLayoutComponent(Object name, Component comp, Container c) {
    // helper check for a common mistake...
    if (name == null) {
        throw new IllegalArgumentException("Cannot add component to BorderLayout Container without constraint parameter");
    }
    // allows us to work with Component constraints too which makes some code simpler
    if (name instanceof Integer) {
        switch(((Integer) name).intValue()) {
            case Component.TOP:
                name = NORTH;
                break;
            case Component.BOTTOM:
                name = SOUTH;
                break;
            case Component.LEFT:
                name = WEST;
                break;
            case Component.RIGHT:
                name = EAST;
                break;
            case Component.CENTER:
                name = CENTER;
                break;
            default:
                throw new IllegalArgumentException("BorderLayout Container expects one of the constraints BorderLayout.NORTH/SOUTH/EAST/WEST/CENTER");
        }
    }
    Component previous = null;
    /* Assign the component to one of the known regions of the layout.
         */
    if (CENTER.equals(name)) {
        previous = portraitCenter;
        portraitCenter = comp;
    } else if (NORTH.equals(name)) {
        previous = portraitNorth;
        portraitNorth = comp;
    } else if (SOUTH.equals(name)) {
        previous = portraitSouth;
        portraitSouth = comp;
    } else if (EAST.equals(name)) {
        previous = portraitEast;
        portraitEast = comp;
    } else if (WEST.equals(name)) {
        previous = portraitWest;
        portraitWest = comp;
    } else if (OVERLAY.equals(name)) {
        previous = overlay;
        overlay = comp;
    } else {
        throw new IllegalArgumentException("cannot add to layout: unknown constraint: " + name);
    }
    if (previous != null && previous != comp) {
        c.removeComponent(previous);
    }
}
Also used : Component(com.codename1.ui.Component)

Example 14 with Constraint

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

the class LayeredLayout method getPreferredSize.

/**
 * {@inheritDoc}
 */
public Dimension getPreferredSize(Container parent) {
    int maxWidth = 0;
    int maxHeight = 0;
    int numOfcomponents = parent.getComponentCount();
    tmpLaidOut.clear();
    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) {
            vInsets += constraint.insets[Component.TOP].preferredValue + constraint.insets[Component.BOTTOM].preferredValue;
            hInsets += constraint.insets[Component.LEFT].preferredValue + constraint.insets[Component.RIGHT].preferredValue;
        }
        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());
    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);
        }
    }
    return d;
}
Also used : Style(com.codename1.ui.plaf.Style) Dimension(com.codename1.ui.geom.Dimension) Component(com.codename1.ui.Component)

Example 15 with Constraint

use of com.codename1.ui.table.TableLayout.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)

Aggregations

Component (com.codename1.ui.Component)8 TextArea (com.codename1.ui.TextArea)6 Style (com.codename1.ui.plaf.Style)5 Container (com.codename1.ui.Container)4 PeerComponent (com.codename1.ui.PeerComponent)4 Point (java.awt.Point)3 BrowserComponent (com.codename1.ui.BrowserComponent)2 ComponentAnimation (com.codename1.ui.animations.ComponentAnimation)2 Dimension (com.codename1.ui.geom.Dimension)2 BorderLayout (com.codename1.ui.layouts.BorderLayout)2 BoxLayout (com.codename1.ui.layouts.BoxLayout)2 Inset (com.codename1.ui.layouts.LayeredLayout.LayeredLayoutConstraint.Inset)2 UITimer (com.codename1.ui.util.UITimer)2 java.awt (java.awt)2 ActionEvent (java.awt.event.ActionEvent)2 ActionListener (java.awt.event.ActionListener)2 AdjustmentListener (java.awt.event.AdjustmentListener)2 FocusEvent (java.awt.event.FocusEvent)2 FocusListener (java.awt.event.FocusListener)2 HierarchyBoundsListener (java.awt.event.HierarchyBoundsListener)2