Search in sources :

Example 66 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawComponent.

private void drawComponent(Graphics g, Label l, Image icon, Image stateIcon, int preserveSpaceForState) {
    setFG(g, l);
    int alpha = g.concatenateAlpha(l.getStyle().getFgAlpha());
    int gap = l.getGap();
    int stateIconSize = 0;
    int stateIconYPosition = 0;
    String text = l.getText();
    Style style = l.getStyle();
    int cmpX = l.getX();
    int cmpY = l.getY();
    int cmpHeight = l.getHeight();
    int cmpWidth = l.getWidth();
    boolean rtl = l.isRTL();
    int leftPadding = style.getPaddingLeft(rtl);
    int rightPadding = style.getPaddingRight(rtl);
    int topPadding = style.getPaddingTop();
    int bottomPadding = style.getPaddingBottom();
    Font font = style.getFont();
    int fontHeight = 0;
    if (text == null) {
        text = "";
    }
    if (text.length() > 0) {
        fontHeight = font.getHeight();
    }
    int x = cmpX + leftPadding;
    int y = cmpY + topPadding;
    boolean opposite = false;
    boolean stateButtonOnLeft = false;
    if (stateIcon != null) {
        // square image width == height
        stateIconSize = stateIcon.getWidth();
        preserveSpaceForState = stateIconSize + gap;
        stateIconYPosition = cmpY + topPadding + (cmpHeight - topPadding - bottomPadding) / 2 - stateIconSize / 2;
        int tX = cmpX;
        if (((Button) l).isOppositeSide()) {
            if (rtl) {
                tX += leftPadding;
                stateButtonOnLeft = true;
                x = cmpX + leftPadding + preserveSpaceForState;
            } else {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            }
            // cmpWidth -= leftPadding - stateIconSize;
            // preserveSpaceForState = 0;
            opposite = true;
        } else {
            if (rtl) {
                tX = tX + cmpWidth - leftPadding - stateIconSize;
            } else {
                x = cmpX + leftPadding + preserveSpaceForState;
                tX += leftPadding;
                stateButtonOnLeft = true;
            }
        }
        g.drawImage(stateIcon, tX, stateIconYPosition);
    }
    // default for bottom left alignment
    int align = reverseAlignForBidi(l, style.getAlignment());
    int textPos = reverseAlignForBidi(l, l.getTextPosition());
    // set initial x,y position according to the alignment and textPosition
    if (align == Component.LEFT) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                if (textPos == Label.RIGHT && stateIcon == null) {
                    x += preserveSpaceForState;
                }
                break;
            case Label.BOTTOM:
            case Label.TOP:
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    } else if (align == Component.CENTER) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + ((icon != null) ? icon.getWidth() + l.getGap() : 0) + l.getStringWidth(font))) / 2;
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                break;
            case Label.BOTTOM:
            case Label.TOP:
                x = x + (cmpWidth - (preserveSpaceForState + leftPadding + rightPadding + Math.max(((icon != null) ? icon.getWidth() + l.getGap() : 0), l.getStringWidth(font)))) / 2;
                if (!opposite) {
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                } else {
                    x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    } else if (align == Component.RIGHT) {
        switch(textPos) {
            case Label.LEFT:
            case Label.RIGHT:
                x = cmpX + cmpWidth - rightPadding - (((icon != null) ? (icon.getWidth() + gap) : 0) + l.getStringWidth(font));
                x = stateButtonOnLeft ? x : x - preserveSpaceForState;
                y = y + (cmpHeight - (topPadding + bottomPadding + Math.max(((icon != null) ? icon.getHeight() : 0), fontHeight))) / 2;
                break;
            case Label.BOTTOM:
            case Label.TOP:
                x = cmpX + cmpWidth - rightPadding - (Math.max(((icon != null) ? (icon.getWidth()) : 0), l.getStringWidth(font)));
                if (!opposite) {
                    x = Math.max(x, cmpX + leftPadding + preserveSpaceForState);
                } else {
                    x = Math.min(x, cmpX + leftPadding + preserveSpaceForState);
                }
                y = y + (cmpHeight - (topPadding + bottomPadding + ((icon != null) ? icon.getHeight() + gap : 0) + fontHeight)) / 2;
                break;
        }
    }
    int textSpaceW = cmpWidth - rightPadding - leftPadding;
    int textSpaceX = cmpX + leftPadding;
    if (icon != null && (textPos == Label.RIGHT || textPos == Label.LEFT)) {
        textSpaceW = textSpaceW - icon.getWidth();
        textSpaceX = (textPos == Label.RIGHT) ? textSpaceX + icon.getWidth() : textSpaceX;
    }
    textSpaceW = textSpaceW - preserveSpaceForState;
    textSpaceX = stateButtonOnLeft ? textSpaceX + preserveSpaceForState : textSpaceX;
    if (icon == null) {
        // no icon only string
        drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
    } else {
        int strWidth = l.getStringWidth(font);
        int iconWidth = icon.getWidth();
        int iconHeight = icon.getHeight();
        int iconStringWGap;
        int iconStringHGap;
        switch(textPos) {
            case Label.LEFT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    strWidth = drawLabelStringValign(g, l, text, x, y, iconStringHGap, iconHeight, textSpaceX, textSpaceW, fontHeight);
                    g.drawImage(icon, x + strWidth + gap, y);
                } else {
                    strWidth = drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
                    drawLabelImageValign(g, l, icon, x + strWidth + gap, y, fontHeight, iconHeight);
                }
                break;
            case Label.RIGHT:
                if (iconHeight > fontHeight) {
                    iconStringHGap = (iconHeight - fontHeight) / 2;
                    g.drawImage(icon, x, y);
                    drawLabelStringValign(g, l, text, x + iconWidth + gap, y, iconStringHGap, iconHeight, textSpaceX, textSpaceW, fontHeight);
                } else {
                    drawLabelImageValign(g, l, icon, x, y, fontHeight, iconHeight);
                    drawLabelString(g, l, text, x + iconWidth + gap, y, textSpaceX, textSpaceW);
                }
                break;
            case Label.BOTTOM:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    g.drawImage(icon, x, y);
                    drawLabelString(g, l, text, x + iconStringWGap, y + iconHeight + gap, textSpaceX, textSpaceW);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    g.drawImage(icon, x + iconStringWGap, y);
                    drawLabelString(g, l, text, x, y + iconHeight + gap, textSpaceX, textSpaceW);
                }
                break;
            case Label.TOP:
                if (iconWidth > strWidth) {
                    // center align the smaller
                    iconStringWGap = (iconWidth - strWidth) / 2;
                    drawLabelString(g, l, text, x + iconStringWGap, y, textSpaceX, textSpaceW);
                    g.drawImage(icon, x, y + fontHeight + gap);
                } else {
                    iconStringWGap = (Math.min(strWidth, textSpaceW) - iconWidth) / 2;
                    drawLabelString(g, l, text, x, y, textSpaceX, textSpaceW);
                    g.drawImage(icon, x + iconStringWGap, y + fontHeight + gap);
                }
                break;
        }
    }
    String badgeText = l.getBadgeText();
    if (badgeText != null && badgeText.length() > 0) {
        Component badgeCmp = l.getBadgeStyleComponent();
        int badgePaddingTop = CN.convertToPixels(1);
        int badgePaddingBottom = badgePaddingTop;
        int badgePaddingLeft = badgePaddingTop;
        int badgePaddingRight = badgePaddingTop;
        int fgColor = 0xffffff;
        int bgColor = 0x666666;
        int strokeColor = bgColor;
        Font badgeFont = null;
        Style badgeStyle;
        if (badgeCmp == null) {
            badgeStyle = l.getUIManager().getComponentStyle("Badge");
        } else {
            badgeStyle = badgeCmp.getStyle();
        }
        if (badgeStyle != null) {
            fgColor = badgeStyle.getFgColor();
            bgColor = badgeStyle.getBgColor();
            if (badgeStyle.getBorder() instanceof RoundBorder) {
                strokeColor = ((RoundBorder) badgeStyle.getBorder()).getStrokeColor();
            } else {
                strokeColor = bgColor;
            }
            badgeFont = badgeStyle.getFont();
            badgePaddingTop = badgeStyle.getPaddingTop();
            badgePaddingBottom = badgeStyle.getPaddingBottom();
            badgePaddingLeft = badgeStyle.getPaddingLeftNoRTL();
            badgePaddingRight = badgeStyle.getPaddingRightNoRTL();
        }
        if (badgeFont == null) {
            if (Font.isNativeFontSchemeSupported()) {
                badgeFont = Font.createTrueTypeFont(Font.NATIVE_MAIN_LIGHT).derive(fontHeight / 2, 0);
            } else {
                badgeFont = Font.createSystemFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL);
            }
        }
        int badgeFontHeight = badgeFont.getHeight();
        int badgeTextWidth = badgeFont.stringWidth(badgeText);
        GeneralPath path = new GeneralPath();
        Rectangle rect = new Rectangle(cmpX + cmpWidth - badgeTextWidth - badgePaddingLeft - badgePaddingRight, cmpY, badgePaddingLeft + badgePaddingRight + badgeTextWidth, badgePaddingTop + badgePaddingBottom + badgeFontHeight);
        if (rect.getWidth() < rect.getHeight()) {
            rect.setX(cmpX + cmpWidth - rect.getHeight());
            rect.setWidth(rect.getHeight());
        }
        path.moveTo(rect.getX() + rect.getHeight() / 2, rect.getY());
        path.lineTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY());
        path.arcTo(rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getWidth() - rect.getHeight() / 2, rect.getY() + rect.getHeight(), true);
        path.lineTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight());
        path.arcTo(rect.getX() + rect.getHeight() / 2, rect.getY() + rect.getHeight() / 2, rect.getX() + rect.getHeight() / 2, rect.getY(), true);
        path.closePath();
        int col = g.getColor();
        boolean antialias = g.isAntiAliased();
        g.setAntiAliased(true);
        g.setColor(bgColor);
        g.fillShape(path);
        if (bgColor != strokeColor) {
            g.setColor(strokeColor);
            int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
            g.drawShape(path, new Stroke(1, Stroke.CAP_SQUARE, Stroke.JOIN_MITER, 1f));
            g.setAlpha(alpha2);
        }
        g.setColor(fgColor);
        g.setFont(badgeFont);
        int alpha2 = g.concatenateAlpha(badgeStyle.getFgAlpha());
        g.drawString(badgeText, rect.getX() + rect.getWidth() / 2 - badgeTextWidth / 2, rect.getY() + badgePaddingTop);
        g.setAlpha(alpha2);
        g.setColor(col);
        g.setAntiAliased(antialias);
    }
    g.setAlpha(alpha);
}
Also used : Stroke(com.codename1.ui.Stroke) GeneralPath(com.codename1.ui.geom.GeneralPath) Button(com.codename1.ui.Button) Rectangle(com.codename1.ui.geom.Rectangle) Component(com.codename1.ui.Component) Font(com.codename1.ui.Font)

Example 67 with Label

use of com.codename1.rad.models.Property.Label 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 68 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class HTMLForm method submit.

/**
 * Called when the a form submit is needed.
 * This querys all form fields, creates a URL accordingly and sets it to the HTMLComponent
 */
void submit(String submitKey, String submitVal) {
    if (action == null) {
        return;
    }
    // If this is turned to true anywhere, the form will not be submitted
    boolean error = false;
    String url = action;
    String params = null;
    if (comps.size() > 0) {
        params = "";
        for (Enumeration e = comps.keys(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            Object input = comps.get(key);
            key = HTMLUtils.encodeString(key);
            String value = "";
            if (input instanceof String) {
                // hidden
                value = HTMLUtils.encodeString((String) input);
                params += key + "=" + value + "&";
            } else if (input instanceof Hashtable) {
                // checkbox / radiobutton
                Hashtable options = (Hashtable) input;
                for (Enumeration e2 = options.keys(); e2.hasMoreElements(); ) {
                    Button b = (Button) e2.nextElement();
                    if (b.isSelected()) {
                        params += key + "=" + HTMLUtils.encodeString((String) options.get(b)) + "&";
                    }
                }
            } else if (input instanceof TextArea) {
                // catches both textareas and text input fields
                TextArea tf = ((TextArea) input);
                String text = tf.getText();
                String errorMsg = null;
                if (HTMLComponent.SUPPORT_INPUT_FORMAT) {
                    boolean ok = false;
                    if (text.equals("")) {
                        // check empty - Note that emptyok/-wap-input-required overrides input format
                        if (emptyNotOk.contains(tf)) {
                            errorMsg = htmlC.getUIManager().localize("html.format.emptynotok", "Field can't be empty");
                            error = true;
                        } else if (emptyOk.contains(tf)) {
                            ok = true;
                        }
                    }
                    if ((!error) && (!ok)) {
                        // If there's already an error or it has been cleared by the emptyOK field, no need to check
                        HTMLInputFormat inputFormat = (HTMLInputFormat) inputFormats.get(tf);
                        if ((inputFormat != null) && (!inputFormat.verifyString(text))) {
                            String emptyStr = "";
                            if (emptyOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.oremptyok", " or an empty string");
                            } else if (emptyNotOk.contains(tf)) {
                                emptyStr = htmlC.getUIManager().localize("html.format.andemptynotok", " and cannot be an empty string");
                            }
                            errorMsg = htmlC.getUIManager().localize("html.format.errordesc", "Malformed text. Correct value: ") + inputFormat.toString() + emptyStr;
                            error = true;
                        }
                    }
                }
                if (htmlC.getHTMLCallback() != null) {
                    int type = HTMLCallback.FIELD_TEXT;
                    if ((tf.getConstraint() & TextArea.PASSWORD) != 0) {
                        type = HTMLCallback.FIELD_PASSWORD;
                    }
                    text = htmlC.getHTMLCallback().fieldSubmitted(htmlC, tf, url, key, text, type, errorMsg);
                }
                if (errorMsg == null) {
                    params += key + "=" + HTMLUtils.encodeString(text) + "&";
                }
            } else if (input instanceof ComboBox) {
                // drop down lists (single selection)
                Object item = ((ComboBox) input).getSelectedItem();
                if (item instanceof OptionItem) {
                    value = ((OptionItem) item).getValue();
                    params += key + "=" + HTMLUtils.encodeString(value) + "&";
                }
            // if not - value may be an OPTGROUP label in an only optgroup combobox
            } else if (input instanceof MultiComboBox) {
                // drop down lists (multiple selection)
                Vector selected = ((MultiComboBox) input).getSelected();
                for (int i = 0; i < selected.size(); i++) {
                    Object item = selected.elementAt(i);
                    if (item instanceof OptionItem) {
                        value = ((OptionItem) item).getValue();
                        params += key + "=" + HTMLUtils.encodeString(value) + "&";
                    }
                // if not - value may be an OPTGROUP label in an only optgroup combobox
                }
            }
        }
        if (params.endsWith("&")) {
            // trim the extra &
            params = params.substring(0, params.length() - 1);
        }
    }
    // Add the submit button param, only if the key is non-null (unnamed submit buttons are not passed as parameters)
    if (submitKey != null) {
        if (params == null) {
            params = "";
        }
        if (!params.equals("")) {
            params = params + "&";
        }
        params = params + HTMLUtils.encodeString(submitKey) + "=" + HTMLUtils.encodeString(submitVal);
    }
    if (!error) {
        DocumentInfo docInfo = new DocumentInfo(url, params, isPostMethod);
        if ((encType != null) && (!encType.equals(""))) {
            docInfo.setEncoding(encType);
        }
        htmlC.setPage(docInfo);
    }
}
Also used : Enumeration(java.util.Enumeration) TextArea(com.codename1.ui.TextArea) Hashtable(java.util.Hashtable) ComboBox(com.codename1.ui.ComboBox) RadioButton(com.codename1.ui.RadioButton) Button(com.codename1.ui.Button) Vector(java.util.Vector)

Example 69 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class GenericListCellRenderer method setComponentValue.

/**
 * Initializes the given component with the given value
 *
 * @param cmp one of the components that is or is a part of the renderer
 * @param value the value to install into the component
 */
private void setComponentValue(Component cmp, Object value, Component parent, Component rootRenderer) {
    // process so renderer selected/unselected styles are applied
    if (cmp.getName().toLowerCase().endsWith("fixed")) {
        return;
    }
    if (cmp instanceof Label) {
        if (value instanceof Image) {
            Image i = (Image) value;
            if (i.isAnimation()) {
                if (pendingAnimations == null) {
                    pendingAnimations = new ArrayList<Image>();
                }
                if (!pendingAnimations.contains(i)) {
                    pendingAnimations.add(i);
                    if (parentList == null) {
                        parentList = parent;
                    }
                    if (parentList != null) {
                        Form f = parentList.getComponentForm();
                        if (f != null) {
                            f.registerAnimated(mon);
                            waitingForRegisterAnimation = false;
                        } else {
                            waitingForRegisterAnimation = true;
                        }
                    }
                } else {
                    if (waitingForRegisterAnimation) {
                        if (parentList != null) {
                            Form f = parentList.getComponentForm();
                            if (f != null) {
                                f.registerAnimated(mon);
                                waitingForRegisterAnimation = false;
                            }
                        }
                    }
                }
            }
            Image oldImage = ((Label) cmp).getIcon();
            ((Label) cmp).setIcon(i);
            ((Label) cmp).setText("");
            if (oldImage == null || oldImage.getWidth() != i.getWidth() || oldImage.getHeight() != i.getHeight()) {
                ((Container) rootRenderer).revalidate();
            }
            return;
        } else {
            ((Label) cmp).setIcon(null);
        }
        if (cmp instanceof CheckBox) {
            ((CheckBox) cmp).setSelected(isSelectedValue(value));
            return;
        }
        if (cmp instanceof RadioButton) {
            ((RadioButton) cmp).setSelected(isSelectedValue(value));
            return;
        }
        if (cmp instanceof Slider) {
            ((Slider) cmp).setProgress(((Integer) value).intValue());
            return;
        }
        Label l = (Label) cmp;
        if (value == null) {
            l.setText("");
        } else {
            if (value instanceof Label) {
                l.setText(((Label) value).getText());
                l.setIcon(((Label) value).getIcon());
            } else {
                l.setText(value.toString());
            }
        }
        if (firstCharacterRTL) {
            String t = l.getText();
            if (t.length() > 0) {
                l.setRTL(Display.getInstance().isRTL(t.charAt(0)));
            }
        }
        return;
    }
    if (cmp instanceof TextArea) {
        if (value == null) {
            ((TextArea) cmp).setText("");
        } else {
            ((TextArea) cmp).setText(value.toString());
        }
    }
}
Also used : Container(com.codename1.ui.Container) Slider(com.codename1.ui.Slider) Form(com.codename1.ui.Form) TextArea(com.codename1.ui.TextArea) CheckBox(com.codename1.ui.CheckBox) Label(com.codename1.ui.Label) RadioButton(com.codename1.ui.RadioButton) Image(com.codename1.ui.Image) EncodedImage(com.codename1.ui.EncodedImage) URLImage(com.codename1.ui.URLImage)

Example 70 with Label

use of com.codename1.rad.models.Property.Label in project CodenameOne by codenameone.

the class GenericListCellRenderer method findComponentsOfInterest.

private void findComponentsOfInterest(Component cmp, ArrayList dest) {
    if (cmp instanceof Container) {
        Container c = (Container) cmp;
        int count = c.getComponentCount();
        for (int iter = 0; iter < count; iter++) {
            findComponentsOfInterest(c.getComponentAt(iter), dest);
        }
        return;
    }
    // performance optimization for fixed images in lists
    if (cmp.getName() != null) {
        if (cmp instanceof Label) {
            Label l = (Label) cmp;
            if (l.getName().toLowerCase().endsWith("fixed") && l.getIcon() != null) {
                l.getIcon().lock();
            }
            dest.add(cmp);
            return;
        }
        if (cmp instanceof TextArea) {
            dest.add(cmp);
            return;
        }
    }
}
Also used : Container(com.codename1.ui.Container) TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label)

Aggregations

Label (com.codename1.ui.Label)129 Form (com.codename1.ui.Form)95 Button (com.codename1.ui.Button)50 BorderLayout (com.codename1.ui.layouts.BorderLayout)50 Container (com.codename1.ui.Container)49 Component (com.codename1.ui.Component)27 SpanLabel (com.codename1.components.SpanLabel)26 Style (com.codename1.ui.plaf.Style)24 BoxLayout (com.codename1.ui.layouts.BoxLayout)19 TextArea (com.codename1.ui.TextArea)18 ActionEvent (com.codename1.ui.events.ActionEvent)18 IOException (java.io.IOException)18 Image (com.codename1.ui.Image)17 Dimension (com.codename1.ui.geom.Dimension)16 TextField (com.codename1.ui.TextField)15 ActionListener (com.codename1.ui.events.ActionListener)15 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)15 Toolbar (com.codename1.ui.Toolbar)13 FlowLayout (com.codename1.ui.layouts.FlowLayout)12 EncodedImage (com.codename1.ui.EncodedImage)11