Search in sources :

Example 1 with Thing

use of com.codename1.rad.schemas.Thing in project CodenameOne by codenameone.

the class CSSEngine method applyStyleToUIElement.

// //////
// CSS2 additions end
// /////
/**
 * Applies the given CSS directives to the component
 *
 * @param ui The component representing (part of) the element that the style should be applied to
 * @param selector The style attributes relating to this element
 * @param element The element the style should be applied to
 * @param htmlC The HTMLComponent to which this element belongs to
 * @param focus true if the style should be applied only to the selected state iof the ui (a result of pseudo-class selector a:focus etc.)
 */
private void applyStyleToUIElement(Component ui, CSSElement selector, HTMLElement element, HTMLComponent htmlC) {
    // count++;
    // This is relevant only for non recursive types - otherwise we need to recheck everytime since it depends on the specific UI component class
    int styles = getApplicableStyles(ui, selector);
    // White spaces
    if (HTMLComponent.FIXED_WIDTH) {
        // This works well only in fixed width mode (Since we cannot "force" a newline in FlowLayout)
        // TODO - enable in FIXED_WIDTH for pre vs. normal/nowrap
        int space = selector.getAttrVal(CSSElement.CSS_WHITE_SPACE);
        if (space != -1) {
            switch(space) {
                case WHITE_SPACE_NORMAL:
                    setWrapRecursive(element, htmlC);
                    break;
                case WHITE_SPACE_NOWRAP:
                    setNowrapRecursive(element);
                    break;
                case WHITE_SPACE_PRE:
                    // TODO - Not implemented yet
                    break;
            }
        }
    }
    // Input format
    String v = selector.getAttributeById(CSSElement.CSS_WAP_INPUT_FORMAT);
    if ((v != null) && ((element.getTagId() == HTMLElement.TAG_TEXTAREA) || (element.getTagId() == HTMLElement.TAG_INPUT)) && (ui instanceof TextArea)) {
        v = omitQuotesIfExist(v);
        // This may return a new instance of TextField taht has to be updated in the tree. This is alos the reason why input format is the first thing checked - see HTMLInputFormat.applyConstraints
        ui = htmlC.setInputFormat((TextArea) ui, v);
        element.setAssociatedComponents(ui);
    }
    // Input emptyOK
    int inputRequired = selector.getAttrVal(CSSElement.CSS_WAP_INPUT_REQUIRED);
    if ((inputRequired != -1) && ((element.getTagId() == HTMLElement.TAG_TEXTAREA) || (element.getTagId() == HTMLElement.TAG_INPUT)) && (ui instanceof TextArea)) {
        if (inputRequired == INPUT_REQUIRED_TRUE) {
            htmlC.setInputRequired(((TextArea) ui), true);
        } else if (inputRequired == INPUT_REQUIRED_FALSE) {
            htmlC.setInputRequired(((TextArea) ui), false);
        }
    }
    // Display
    int disp = selector.getAttrVal(CSSElement.CSS_DISPLAY);
    switch(disp) {
        case DISPLAY_NONE:
            if (ui.getParent() != null) {
                ui.getParent().removeComponent(ui);
            } else {
                // special case for display in the BODY tag
                if (ui instanceof Container) {
                    ((Container) ui).removeAll();
                }
            }
            return;
        case // Animate component (ticker-like)
        DISPLAY_MARQUEE:
            htmlC.marqueeComponents.addElement(ui);
            break;
    }
    // Visibility
    int visibility = selector.getAttrVal(CSSElement.CSS_VISIBILITY);
    if (visibility != -1) {
        boolean visible = (visibility == VISIBILITY_VISIBLE);
        setVisibleRecursive(ui, visible);
        if (!visible) {
            // Don't waste time on processing hidden elements, though technically the size of the element is still reserved and should be according to style
            return;
        } else {
            // Need to turn on visibility of all component's parents, in case they were declared hidden
            setParentsVisible(ui);
        }
    }
    // 
    // Dimensions
    // 
    // TODO - debug: Width and Height don't always work - for simple components they usually do, but for containers either they don't have any effect or some inner components (with size restrictions) disappear
    // We use the entire display width and height as reference since htmlC still doesn't have a preferred size or actual size
    // Width
    // TODO - Width/Height is disabled currently, since it causes a lot of side effects, making some components disappear
    /*
        int width=selector.getAttrLengthVal(CSSElement.CSS_WIDTH,ui,Display.getInstance().getDisplayWidth());

        // Height
        int height=selector.getAttrLengthVal(CSSElement.CSS_HEIGHT,ui,Display.getInstance().getDisplayHeight());

        if (!HTMLComponent.PROCESS_HTML_MP1_ONLY) {
            int minWidth=selector.getAttrLengthVal(CSSElement.CSS_MIN_WIDTH,ui,Display.getInstance().getDisplayWidth());
            int maxWidth=selector.getAttrLengthVal(CSSElement.CSS_MAX_WIDTH,ui,Display.getInstance().getDisplayWidth());
            int minHeight=selector.getAttrLengthVal(CSSElement.CSS_MIN_HEIGHT,ui,Display.getInstance().getDisplayHeight());
            int maxHeight=selector.getAttrLengthVal(CSSElement.CSS_MAX_HEIGHT,ui,Display.getInstance().getDisplayHeight());

            if (width==-1) { // process min/max only if exact was not specified
                if ((minWidth!=-1) && (minWidth>ui.getPreferredW())) {
                    width=minWidth;
                }
                if ((maxWidth!=-1) && (maxWidth<ui.getPreferredW())) {
                    width=maxWidth;
                }
            }
            if (height==-1) { // process min/max only if exact was not specified
                if ((minHeight!=-1) && (minHeight>ui.getPreferredH())) {
                    height=minHeight;
                }
                if ((maxHeight!=-1) && (maxHeight<ui.getPreferredH())) {
                    height=maxHeight;
                }
            }
        }

        if ((width!=-1) || (height!=-1)) {
            if (width==-1) {
                width=ui.getPreferredW();
            }
            if (height==-1) {
                height=ui.getPreferredH();
            }
            ui.setPreferredSize(new Dimension(width,height));
        }
        */
    // 
    // Colors
    // 
    // Background Color
    int bgColor = selector.getAttrVal(CSSElement.CSS_BACKGROUND_COLOR);
    if (bgColor != -1) {
        if ((styles & STYLE_UNSELECTED) != 0) {
            ui.getUnselectedStyle().setBgColor(bgColor);
            ui.getUnselectedStyle().setBgTransparency(255);
        }
        if ((styles & STYLE_SELECTED) != 0) {
            ui.getSelectedStyle().setBgColor(bgColor);
            ui.getSelectedStyle().setBgTransparency(255);
        }
        if ((styles & STYLE_PRESSED) != 0) {
            ((HTMLLink) ui).getPressedStyle().setBgColor(bgColor);
            ((HTMLLink) ui).getPressedStyle().setBgTransparency(255);
        }
    }
    // Foreground color
    int fgColor = selector.getAttrVal(CSSElement.CSS_COLOR);
    if (fgColor != -1) {
        setColorRecursive(ui, fgColor, selector);
    }
    // Background Image
    v = selector.getAttributeById(CSSElement.CSS_BACKGROUND_IMAGE);
    if (v != null) {
        String url = getCSSUrl(v);
        if (url != null) {
            // Setting an alternative bgPainter that can support CSS background properties
            CSSBgPainter bgPainter = new CSSBgPainter(ui);
            // Background tiling
            byte bgType = (byte) selector.getAttrVal(CSSElement.CSS_BACKGROUND_REPEAT);
            if (bgType == -1) {
                // default value
                bgType = Style.BACKGROUND_IMAGE_TILE_BOTH;
            }
            // Note that we don't set transparency to 255, since the image may have its own transparency/opaque areas - we don't want to block the entire component/container entirely
            if ((styles & STYLE_SELECTED) != 0) {
                ui.getSelectedStyle().setBgPainter(bgPainter);
                ui.getSelectedStyle().setBackgroundType(bgType);
            }
            if ((styles & STYLE_UNSELECTED) != 0) {
                ui.getUnselectedStyle().setBgPainter(bgPainter);
                ui.getUnselectedStyle().setBackgroundType(bgType);
            }
            if ((styles & STYLE_PRESSED) != 0) {
                ((HTMLLink) ui).getPressedStyle().setBgPainter(bgPainter);
                ((HTMLLink) ui).getPressedStyle().setBackgroundType(bgType);
            }
            // The background image itself
            if (htmlC.showImages) {
                if (htmlC.getDocumentInfo() != null) {
                    htmlC.getThreadQueue().addBgImage(ui, htmlC.convertURL(url), styles);
                } else {
                    if (DocumentInfo.isAbsoluteURL(url)) {
                        htmlC.getThreadQueue().addBgImage(ui, url, styles);
                    } else {
                        if (htmlC.getHTMLCallback() != null) {
                            htmlC.getHTMLCallback().parsingError(HTMLCallback.ERROR_NO_BASE_URL, selector.getTagName(), selector.getAttributeName(new Integer(CSSElement.CSS_BACKGROUND_IMAGE)), url, "Ignoring background image file referred in a CSS file/segment (" + url + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                        }
                    }
                }
            }
            for (int i = CSSElement.CSS_BACKGROUND_POSITION_X; i <= CSSElement.CSS_BACKGROUND_POSITION_Y; i++) {
                int pos = selector.getAttrVal(i);
                if (pos != -1) {
                    bgPainter.setPosition(i, pos);
                }
            }
            // or 'scroll' (default) which means the it moves with scrolling (Like usually in CodenameOne backgrounds)
            if (selector.getAttrVal((CSSElement.CSS_BACKGROUND_ATTACHMENT)) == BG_ATTACHMENT_FIXED) {
                bgPainter.setFixed();
            }
        }
    }
    // TODO - float: none/left/right
    // TODO - clear: none/left/right/both
    // Margin
    Component marginComp = ui;
    if (ui instanceof Label) {
        // If this is a Label/HTMLLink we do not apply the margin individually to each word, but rather to the whole block
        marginComp = ui.getParent();
    } else if ((element.getTagId() == HTMLElement.TAG_LI) && (ui.getParent().getLayout() instanceof BorderLayout)) {
        marginComp = ui.getParent();
    }
    for (int i = CSSElement.CSS_MARGIN_TOP; i <= CSSElement.CSS_MARGIN_RIGHT; i++) {
        int marginPixels = -1;
        if ((i == CSSElement.CSS_MARGIN_TOP) || (i == CSSElement.CSS_MARGIN_BOTTOM)) {
            // Here the used component is ui and not marginComp, since we're interested in the font size (which will be corrent in Labels not in their containers)
            marginPixels = selector.getAttrLengthVal(i, ui, htmlC.getHeight());
        } else {
            marginPixels = selector.getAttrLengthVal(i, ui, htmlC.getWidth());
        }
        if (marginPixels >= 0 && marginComp != null) {
            if ((styles & STYLE_SELECTED) != 0) {
                marginComp.getSelectedStyle().setMargin(i - CSSElement.CSS_MARGIN_TOP, marginPixels);
                // parent when the link focuses
                if ((ui instanceof HTMLLink) && (styles == STYLE_SELECTED)) {
                    ((HTMLLink) ui).setParentChangesOnFocus();
                }
            }
            if ((styles & STYLE_UNSELECTED) != 0) {
                marginComp.getUnselectedStyle().setMargin(i - CSSElement.CSS_MARGIN_TOP, marginPixels);
            }
        // Since we don't apply the margin/padding on the component but rather on its parent
        // There is no point in setting the PRESSED style since we don't have a pressed event from Button, nor do we have a pressedStyle for containers
        // That's why we can't do the same trick as in selected style, and the benefit of this rather "edge" case (That is anyway not implemented in all browsers) seems rather small
        // if ((styles & STYLE_PRESSED)!=0) {
        // ((HTMLLink)ui).getPressedStyle().setMargin(i-CSSElement.CSS_MARGIN_TOP, marginPixels);
        // }
        }
    }
    Component padComp = ui;
    if (ui instanceof Label) {
        padComp = ui.getParent();
    } else if ((element.getTagId() == HTMLElement.TAG_LI) && (ui.getParent().getLayout() instanceof BorderLayout)) {
        padComp = ui.getParent();
    }
    for (int i = CSSElement.CSS_PADDING_TOP; i <= CSSElement.CSS_PADDING_RIGHT; i++) {
        int padPixels = -1;
        if ((i == CSSElement.CSS_PADDING_TOP) || (i == CSSElement.CSS_PADDING_BOTTOM)) {
            padPixels = selector.getAttrLengthVal(i, ui, htmlC.getHeight());
        } else {
            padPixels = selector.getAttrLengthVal(i, ui, htmlC.getWidth());
        }
        if (padPixels >= 0) {
            // Only positive or 0
            if ((styles & STYLE_SELECTED) != 0) {
                if (padComp != null) {
                    padComp.getSelectedStyle().setPadding(i - CSSElement.CSS_PADDING_TOP, padPixels);
                }
                if ((ui instanceof HTMLLink) && (styles == STYLE_SELECTED)) {
                    // See comment on margins
                    ((HTMLLink) ui).setParentChangesOnFocus();
                }
            }
            if ((styles & STYLE_UNSELECTED) != 0) {
                if (padComp != null) {
                    padComp.getUnselectedStyle().setPadding(i - CSSElement.CSS_PADDING_TOP, padPixels);
                }
            }
        // See comment in margin on why PRESSED was dropped
        // if ((styles & STYLE_PRESSED)!=0) {
        // ((HTMLLink)padComp).getPressedStyle().setPadding(i-CSSElement.CSS_PADDING_TOP, padPixels);
        // }
        }
    }
    // 
    // Text
    // 
    // Text Alignment
    int align = selector.getAttrVal(CSSElement.CSS_TEXT_ALIGN);
    if (align != -1) {
        switch(element.getTagId()) {
            case HTMLElement.TAG_TD:
            case HTMLElement.TAG_TH:
                setTableCellAlignment(element, ui, align, true);
                break;
            case HTMLElement.TAG_TR:
                setTableCellAlignmentTR(element, ui, align, true);
                break;
            case HTMLElement.TAG_TABLE:
                setTableAlignment(ui, align, true);
                break;
            default:
                // TODO - this sometimes may collide with the HTML align attribute. If the style of the same tag has alignment it overrides the align attribute, but if it is inherited, the align tag prevails
                setTextAlignmentRecursive(ui, align);
        }
    }
    // Vertical align
    int valign = selector.getAttrVal(CSSElement.CSS_VERTICAL_ALIGN);
    if (valign != -1) {
        switch(element.getTagId()) {
            case HTMLElement.TAG_TD:
            case HTMLElement.TAG_TH:
                setTableCellAlignment(element, ui, valign, false);
                break;
            case HTMLElement.TAG_TR:
                setTableCellAlignmentTR(element, ui, valign, false);
                break;
            // break;
            default:
        }
    }
    // Text Transform
    int transform = selector.getAttrVal(CSSElement.CSS_TEXT_TRANSFORM);
    if (transform != -1) {
        setTextTransformRecursive(ui, transform);
    }
    // Text indentation
    int indent = selector.getAttrLengthVal(CSSElement.CSS_TEXT_INDENT, ui, htmlC.getWidth());
    if (indent >= 0) {
        // Only positive (0 also as it may cancel previous margins)
        setTextIndentationRecursive(ui, indent);
    }
    // 
    // Font
    // 
    // Font family
    String fontFamily = selector.getAttributeById(CSSElement.CSS_FONT_FAMILY);
    if (fontFamily != null) {
        int index = fontFamily.indexOf(',');
        if (index != -1) {
            // Currently we ignore font families fall back (i.e. Arial,Helvetica,Sans-serif) since even finding a match for one font is quite expensive performance-wise
            fontFamily = fontFamily.substring(0, index);
        }
    }
    // Font Style
    int fontStyle = selector.getAttrVal(CSSElement.CSS_FONT_STYLE);
    // Font Weight
    int fontWeight = selector.getAttrVal(CSSElement.CSS_FONT_WEIGHT);
    int fontSize = selector.getAttrLengthVal(CSSElement.CSS_FONT_SIZE, ui, ui.getStyle().getFont().getHeight());
    if (fontSize < -1) {
        int curSize = ui.getStyle().getFont().getHeight();
        if (fontSize == CSSElement.FONT_SIZE_LARGER) {
            fontSize = curSize + 2;
        } else if (fontSize == CSSElement.FONT_SIZE_SMALLER) {
            fontSize = curSize - 2;
        }
    }
    // Since J2ME doesn't support small-caps fonts, when a small-caps font varinat is requested
    // the font-family is changed to "smallcaps" which should be loaded to HTMLComponent and the theme as a bitmap font
    // If no smallcaps font is found at all, then the family stays the same, but if even only one is found - the best match will be used.
    int fontVariant = selector.getAttrVal(CSSElement.CSS_FONT_VARIANT);
    if ((fontVariant == FONT_VARIANT_SMALLCAPS) && (htmlC.isSmallCapsFontAvailable())) {
        fontFamily = CSSElement.SMALL_CAPS_STRING;
    }
    // Process font only if once of the font CSS properties was mentioned and valid
    if ((fontFamily != null) || (fontSize != -1) || (fontStyle != -1) || (fontWeight != -1)) {
        setFontRecursive(htmlC, ui, fontFamily, fontSize, fontStyle, fontWeight, selector);
    }
    // List style
    int listType = -1;
    String listImg = null;
    Component borderUi = ui;
    if ((element.getTagId() == HTMLElement.TAG_LI) || (element.getTagId() == HTMLElement.TAG_UL) || (element.getTagId() == HTMLElement.TAG_OL) || (element.getTagId() == HTMLElement.TAG_DIR) || (element.getTagId() == HTMLElement.TAG_MENU)) {
        int listPos = selector.getAttrVal(CSSElement.CSS_LIST_STYLE_POSITION);
        if (listPos == LIST_STYLE_POSITION_INSIDE) {
            // Padding and not margin since background color should affect also the indented space
            ui.getStyle().setPadding(Component.LEFT, ui.getStyle().getMargin(Component.LEFT) + INDENT_LIST_STYLE_POSITION);
            Container parent = ui.getParent();
            if (parent.getLayout() instanceof BorderLayout) {
                borderUi = parent;
            }
        }
        listType = selector.getAttrVal(CSSElement.CSS_LIST_STYLE_TYPE);
        listImg = getCSSUrl(selector.getAttributeById(CSSElement.CSS_LIST_STYLE_IMAGE));
    }
    // Border
    Border[] borders = new Border[4];
    // Used to prevent drawing a border in the middle of two words in the same segment
    boolean leftBorder = false;
    // Used to prevent drawing a border in the middle of two words in the same segment
    boolean rightBorder = false;
    boolean hasBorder = false;
    if ((borderUi == ui) && (element.getUi().size() > 1)) {
        if (element.getUi().firstElement() == borderUi) {
            leftBorder = true;
        } else if (element.getUi().lastElement() == borderUi) {
            rightBorder = true;
        }
    } else {
        leftBorder = true;
        rightBorder = true;
    }
    for (int i = Component.TOP; i <= Component.RIGHT; i++) {
        if ((i == Component.BOTTOM) || (i == Component.TOP) || ((i == Component.LEFT) && (leftBorder)) || ((i == Component.RIGHT) && (rightBorder))) {
            borders[i] = createBorder(selector, borderUi, i, styles, BORDER);
            if (borders[i] != null) {
                hasBorder = true;
            }
        }
    }
    if (hasBorder) {
        Border curBorder = borderUi.getUnselectedStyle().getBorder();
        if (((styles & STYLE_SELECTED) != 0) && ((styles & STYLE_UNSELECTED) == 0)) {
            curBorder = borderUi.getSelectedStyle().getBorder();
        }
        if ((styles & STYLE_PRESSED) != 0) {
            curBorder = ((HTMLLink) borderUi).getSelectedStyle().getBorder();
        }
        // In case this element was assigned a top border for instance, and then by belonging to another tag/class/id it has also a bottom border - this merges the two (and gives priority to the new one)
        if ((curBorder != null) && (curBorder.getCompoundBorders() != null)) {
            // TODO - This doesn't cover the case of having another border (i.e. table/fieldset?) - Can also assign the non-CSS border to the other corners?
            // curBorder.
            Border[] oldBorders = curBorder.getCompoundBorders();
            for (int i = Component.TOP; i <= Component.RIGHT; i++) {
                if (borders[i] == null) {
                    borders[i] = oldBorders[i];
                }
            }
        }
        Border border = Border.createCompoundBorder(borders[Component.TOP], borders[Component.BOTTOM], borders[Component.LEFT], borders[Component.RIGHT]);
        if (border != null) {
            if ((styles & STYLE_SELECTED) != 0) {
                borderUi.getSelectedStyle().setBorder(border);
            }
            if ((styles & STYLE_UNSELECTED) != 0) {
                borderUi.getUnselectedStyle().setBorder(border);
            }
            if ((styles & STYLE_PRESSED) != 0) {
                ((HTMLLink) borderUi).getPressedStyle().setBorder(border);
            }
            if (borderUi.getParent() != null) {
                borderUi.getParent().revalidate();
            } else if (borderUi instanceof Container) {
                ((Container) borderUi).revalidate();
            }
        }
    }
    // 
    // Specific elements styling
    // 
    // Access keys
    v = selector.getAttributeById(CSSElement.CSS_WAP_ACCESSKEY);
    if ((v != null) && (v.length() >= 1) && (// These are the only tags that can accpet an access key
    (element.getTagId() == HTMLElement.TAG_INPUT) || (element.getTagId() == HTMLElement.TAG_TEXTAREA) || (element.getTagId() == HTMLElement.TAG_LABEL) || // For A tags this is applied only to the first word, no need to apply it to each word of the link
    ((element.getTagId() == HTMLElement.TAG_A) && (ui instanceof HTMLLink) && ((HTMLLink) ui).parentLink == null))) {
        // The accesskey string may consist fallback assignments (comma seperated) and multiple assignments (space seperated) and any combination of those
        // For example: "send *, #" (meaning: assign both the send and * keys, and if failed to assign one of those assign the # key instead)
        int index = v.indexOf(',');
        boolean assigned = false;
        while (index != -1) {
            // Handle fallback access keys
            String key = v.substring(0, index).trim();
            v = v.substring(index + 1);
            assigned = processAccessKeys(key, htmlC, ui);
            if (assigned) {
                // comma denotes fallback, and once we succeeded assigning the accesskey, the others are irrelevant
                break;
            }
            index = v.indexOf(',');
        }
        if (!assigned) {
            processAccessKeys(v.trim(), htmlC, ui);
        }
    }
    if (!HTMLComponent.PROCESS_HTML_MP1_ONLY) {
        // Text decoration (In HTML-MP1 the only mandatory decoration is 'none')
        int decoration = selector.getAttrVal(CSSElement.CSS_TEXT_DECORATION);
        if (decoration == TEXT_DECOR_NONE) {
            removeTextDecorationRecursive(ui, selector);
        } else if (decoration == TEXT_DECOR_UNDERLINE) {
            setTextDecorationRecursive(ui, Style.TEXT_DECORATION_UNDERLINE, selector);
        } else if (decoration == TEXT_DECOR_LINETHROUGH) {
            setTextDecorationRecursive(ui, Style.TEXT_DECORATION_STRIKETHRU, selector);
        } else if (decoration == TEXT_DECOR_OVERLINE) {
            setTextDecorationRecursive(ui, Style.TEXT_DECORATION_OVERLINE, selector);
        }
        // Word spacing
        if (!HTMLComponent.FIXED_WIDTH) {
            // The relative dimension is 0, since percentage doesn't work with word-spacing in browsers
            int wordSpace = selector.getAttrLengthVal(CSSElement.CSS_WORD_SPACING, ui, 0);
            if (wordSpace != -1) {
                setWordSpacingRecursive(ui, wordSpace);
            }
        }
        // Line height
        // Technically the font height should be queried when actually resizing the line (since it may differ for a big block) - but since this would be ery time consuming and also major browsers don't take it into account - we'll do the same
        // int lineHeight=selector.getAttrLengthVal(CSSElement.CSS_LINE_HEIGHT, ui, ui.getStyle().getFont().getHeight());
        int lineHeight = selector.getAttrLengthVal(CSSElement.CSS_LINE_HEIGHT, ui, ui.getStyle().getFont().getHeight());
        if (lineHeight != -1) {
            // 100% means normal line height (don't add margin). Sizes below will not work, even they do in regular browsers
            lineHeight = Math.max(0, lineHeight - ui.getStyle().getFont().getHeight());
            setLineHeightRecursive(ui, lineHeight / 2);
        }
        // Quotes
        String quotesStr = selector.getAttributeById(CSSElement.CSS_QUOTES);
        if (quotesStr != null) {
            Vector quotes = htmlC.getWords(quotesStr, Component.LEFT, false);
            int size = quotes.size();
            if ((size == 2) || (size == 4)) {
                String[] quotesArr = new String[4];
                for (int i = 0; i < size; i++) {
                    quotesArr[i] = omitQuotesIfExist((String) quotes.elementAt(i));
                }
                if (size == 2) {
                    // If only 2 quotes are specified they are used both as primary and secondary
                    quotesArr[2] = quotesArr[0];
                    quotesArr[3] = quotesArr[1];
                }
                setQuotesRecursive(ui, quotesArr);
            }
        }
        // Outline
        Border outline = createBorder(selector, borderUi, 0, styles, OUTLINE);
        if (outline != null) {
            if ((styles & STYLE_SELECTED) != 0) {
                addOutlineToStyle(borderUi.getSelectedStyle(), outline);
            }
            if ((styles & STYLE_UNSELECTED) != 0) {
                addOutlineToStyle(borderUi.getUnselectedStyle(), outline);
            }
            if ((styles & STYLE_PRESSED) != 0) {
                addOutlineToStyle(((HTMLLink) borderUi).getPressedStyle(), outline);
            }
            if (borderUi.getParent() != null) {
                borderUi.getParent().revalidate();
            } else if (borderUi instanceof Container) {
                ((Container) borderUi).revalidate();
            }
        }
        // Direction
        int dir = selector.getAttrVal(CSSElement.CSS_DIRECTION);
        if (dir != -1) {
            setDirectionRecursive(ui, dir == DIRECTION_RTL);
        }
        // Table properties
        if (ui instanceof HTMLTable) {
            int tableProp = selector.getAttrVal(CSSElement.CSS_BORDER_COLLAPSE);
            if (tableProp != -1) {
                ((HTMLTable) ui).setCollapseBorder(tableProp == BORDER_COLLAPSE_COLLAPSE);
            }
            tableProp = selector.getAttrVal(CSSElement.CSS_EMPTY_CELLS);
            if (tableProp != -1) {
                ((HTMLTable) ui).setDrawEmptyCellsBorder(tableProp == EMPTY_CELLS_SHOW);
            }
            // bottom = 0 , top = 1
            tableProp = selector.getAttrVal(CSSElement.CSS_CAPTION_SIDE);
            if (tableProp != -1) {
                Container tableParentCont = ui.getParent();
                // should result in 0 when the caption is at the bottom, and 1 when the caption is on top
                int tablePos = tableParentCont.getComponentIndex(ui);
                if (tableProp != tablePos) {
                    Component caption = tableParentCont.getComponentAt((tablePos + 1) % 2);
                    tableParentCont.removeComponent(caption);
                    tableParentCont.addComponent(tablePos, caption);
                }
            }
            String spacing = selector.getAttributeById(CSSElement.CSS_BORDER_SPACING);
            if (spacing != null) {
                spacing = spacing.trim();
                int index = spacing.indexOf(' ');
                int spaceH = 0;
                int spaceV = 0;
                if (index == -1) {
                    // one value only
                    spaceH = CSSElement.convertLengthVal(CSSElement.convertUnitsOrPercentage(spacing), ui, ui.getPreferredW());
                    spaceV = spaceH;
                } else {
                    String spaceHoriz = spacing.substring(0, index);
                    String spaceVert = spacing.substring(index + 1);
                    spaceH = CSSElement.convertLengthVal(CSSElement.convertUnitsOrPercentage(spaceHoriz), ui, ui.getPreferredW());
                    spaceV = CSSElement.convertLengthVal(CSSElement.convertUnitsOrPercentage(spaceVert), ui, ui.getPreferredH());
                }
                ((HTMLTable) ui).setBorderSpacing(spaceH, spaceV);
            }
        }
    }
    // This is since in some cases other elements can come between a OL/UL and its LI items (Though illegal in HTML, it can occur)
    if ((listType != -1) || (listImg != null)) {
        if (element.getTagId() == HTMLElement.TAG_LI) {
            if (ui instanceof Container) {
                Container liCont = (Container) ui;
                Container liParent = liCont.getParent();
                Component firstComp = liParent.getComponentAt(0);
                if (firstComp instanceof Container) {
                    Container bulletCont = (Container) firstComp;
                    if (bulletCont.getComponentCount() > 0) {
                        Component listItemCmp = bulletCont.getComponentAt(0);
                        if (listItemCmp instanceof Component) {
                            HTMLListItem listItem = ((HTMLListItem) listItemCmp);
                            listItem.setStyleType(listType);
                            listItem.setImage(listImg);
                        }
                    }
                }
            }
        } else if ((element.getTagId() == HTMLElement.TAG_UL) || (element.getTagId() == HTMLElement.TAG_OL) || (element.getTagId() == HTMLElement.TAG_DIR) || (element.getTagId() == HTMLElement.TAG_MENU)) {
            Container ulCont = (Container) ui;
            for (int i = 0; i < ulCont.getComponentCount(); i++) {
                Component cmp = ulCont.getComponentAt(i);
                if (cmp instanceof Container) {
                    Container liCont = (Container) cmp;
                    if (liCont.getComponentCount() >= 1) {
                        cmp = liCont.getComponentAt(0);
                        if (cmp instanceof Container) {
                            Container liContFirstLine = (Container) cmp;
                            if (liContFirstLine.getComponentCount() >= 1) {
                                cmp = liContFirstLine.getComponentAt(0);
                                if (cmp instanceof HTMLListItem) {
                                    HTMLListItem listItem = (HTMLListItem) cmp;
                                    listItem.setStyleType(listType);
                                    listItem.setImage(listImg);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Component(com.codename1.ui.Component) Border(com.codename1.ui.plaf.Border) Vector(java.util.Vector)

Example 2 with Thing

use of com.codename1.rad.schemas.Thing in project CodenameOne by codenameone.

the class UIBuilderOverride method createInstance.

/**
 * Create a component instance from XML
 */
private Component createInstance(final ComponentEntry root, final EditableResources res, Container rootCnt, final Container parentContainer, final ArrayList<Runnable> postCreateTasks) {
    try {
        final Component c = createComponentType(root.getType());
        if (rootCnt == null) {
            rootCnt = (Container) c;
        }
        final Container rootContainer = rootCnt;
        if (root.getBaseForm() != null) {
            c.putClientProperty("%base_form%", root.getBaseForm());
        }
        c.putClientProperty(TYPE_KEY, root.getType());
        c.setName(root.getName());
        String clientProps = root.getClientProperties();
        if (clientProps != null && clientProps.length() > 0) {
            String[] props = clientProps.split(",");
            StringBuilder b = new StringBuilder();
            for (String p : props) {
                String[] keyVal = p.split("=");
                c.putClientProperty(keyVal[0], keyVal[1]);
                if (b.length() > 0) {
                    b.append(",");
                }
                b.append(keyVal[0]);
            }
            c.putClientProperty("cn1$Properties", b.toString());
        }
        rootContainer.putClientProperty("%" + root.getName() + "%", c);
        // layout must be first since we might need to rely on it later on with things such as constraints
        if (root.getLayout() != null) {
            modifyingProperty(c, PROPERTY_LAYOUT);
            Layout l;
            if (root.getLayout().equals("BorderLayout")) {
                l = new BorderLayout();
                if (root.isBorderLayoutAbsoluteCenter() != null) {
                    ((BorderLayout) l).setAbsoluteCenter(root.isBorderLayoutAbsoluteCenter().booleanValue());
                }
                if (root.getBorderLayoutSwapCenter() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.CENTER, root.getBorderLayoutSwapCenter());
                }
                if (root.getBorderLayoutSwapNorth() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.NORTH, root.getBorderLayoutSwapNorth());
                }
                if (root.getBorderLayoutSwapSouth() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.SOUTH, root.getBorderLayoutSwapSouth());
                }
                if (root.getBorderLayoutSwapEast() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.EAST, root.getBorderLayoutSwapEast());
                }
                if (root.getBorderLayoutSwapWest() != null) {
                    ((BorderLayout) l).defineLandscapeSwap(BorderLayout.WEST, root.getBorderLayoutSwapWest());
                }
            } else {
                if (root.getLayout().equals("FlowLayout")) {
                    l = new FlowLayout();
                    ((FlowLayout) l).setFillRows(root.isFlowLayoutFillRows());
                    ((FlowLayout) l).setAlign(root.getFlowLayoutAlign());
                    ((FlowLayout) l).setValign(root.getFlowLayoutValign());
                } else {
                    if (root.getLayout().equals("GridLayout")) {
                        l = new GridLayout(root.getGridLayoutRows().intValue(), root.getGridLayoutColumns().intValue());
                    } else {
                        if (root.getLayout().equals("BoxLayout")) {
                            if (root.getBoxLayoutAxis().equals("X")) {
                                l = new BoxLayout(BoxLayout.X_AXIS);
                            } else {
                                l = new BoxLayout(BoxLayout.Y_AXIS);
                            }
                        } else {
                            if (root.getLayout().equals("TableLayout")) {
                                l = new TableLayout(root.getTableLayoutRows(), root.getTableLayoutColumns());
                            } else {
                                l = new LayeredLayout();
                            }
                        }
                    }
                }
            }
            ((Container) c).setLayout(l);
        }
        if (parentContainer != null && root.getLayoutConstraint() != null) {
            modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
            if (parentContainer.getLayout() instanceof BorderLayout) {
                c.putClientProperty("layoutConstraint", root.getLayoutConstraint().getValue());
            } else {
                TableLayout tl = (TableLayout) parentContainer.getLayout();
                TableLayout.Constraint con = tl.createConstraint(root.getLayoutConstraint().getRow(), root.getLayoutConstraint().getColumn());
                con.setHeightPercentage(root.getLayoutConstraint().getHeight());
                con.setWidthPercentage(root.getLayoutConstraint().getWidth());
                con.setHorizontalAlign(root.getLayoutConstraint().getAlign());
                con.setHorizontalSpan(root.getLayoutConstraint().getSpanHorizontal());
                con.setVerticalAlign(root.getLayoutConstraint().getValign());
                con.setVerticalSpan(root.getLayoutConstraint().getSpanVertical());
                c.putClientProperty("layoutConstraint", con);
            }
        }
        if (root.getEmbed() != null && root.getEmbed().length() > 0) {
            modifyingProperty(c, PROPERTY_EMBED);
            rootContainer.putClientProperty(EMBEDDED_FORM_FLAG, "");
            ((EmbeddedContainer) c).setEmbed(root.getEmbed());
            Container embed = createContainer(res, root.getEmbed(), (EmbeddedContainer) c);
            if (embed != null) {
                if (embed instanceof Form) {
                    embed = formToContainer((Form) embed);
                }
                ((EmbeddedContainer) c).addComponent(BorderLayout.CENTER, embed);
                // this isn't exactly the "right thing" but its the best we can do to make all
                // use cases work
                beforeShowContainer(embed);
                postShowContainer(embed);
            }
        }
        if (root.isToggle() != null) {
            modifyingProperty(c, PROPERTY_TOGGLE_BUTTON);
            ((Button) c).setToggle(root.isToggle().booleanValue());
        }
        if (root.getGroup() != null) {
            modifyingProperty(c, PROPERTY_RADIO_GROUP);
            ((RadioButton) c).setGroup(root.getGroup());
        }
        if (root.isSelected() != null) {
            modifyingProperty(c, PROPERTY_SELECTED);
            if (c instanceof RadioButton) {
                ((RadioButton) c).setSelected(root.isSelected().booleanValue());
            } else {
                ((CheckBox) c).setSelected(root.isSelected().booleanValue());
            }
        }
        if (root.isScrollableX() != null) {
            modifyingProperty(c, PROPERTY_SCROLLABLE_X);
            ((Container) c).setScrollableX(root.isScrollableX().booleanValue());
        }
        if (root.isScrollableY() != null) {
            modifyingProperty(c, PROPERTY_SCROLLABLE_Y);
            ((Container) c).setScrollableY(root.isScrollableY().booleanValue());
        }
        if (root.isTensileDragEnabled() != null) {
            modifyingProperty(c, PROPERTY_TENSILE_DRAG_ENABLED);
            c.setTensileDragEnabled(root.isTensileDragEnabled().booleanValue());
        }
        if (root.isTactileTouch() != null) {
            modifyingProperty(c, PROPERTY_TACTILE_TOUCH);
            c.setTactileTouch(root.isTactileTouch().booleanValue());
        }
        if (root.isSnapToGrid() != null) {
            modifyingProperty(c, PROPERTY_SNAP_TO_GRID);
            c.setSnapToGrid(root.isSnapToGrid().booleanValue());
        }
        if (root.isFlatten() != null) {
            modifyingProperty(c, PROPERTY_FLATTEN);
            c.setFlatten(root.isFlatten().booleanValue());
        }
        if (root.getText() != null) {
            modifyingProperty(c, PROPERTY_TEXT);
            if (c instanceof Label) {
                ((Label) c).setText(root.getText());
            } else {
                ((TextArea) c).setText(root.getText());
            }
        }
        if (root.getMaxSize() != null) {
            modifyingProperty(c, PROPERTY_TEXT_MAX_LENGTH);
            ((TextArea) c).setMaxSize(root.getMaxSize().intValue());
        }
        if (root.getConstraint() != null) {
            modifyingProperty(c, PROPERTY_TEXT_CONSTRAINT);
            ((TextArea) c).setConstraint(root.getConstraint().intValue());
        }
        if (root.getAlignment() != null) {
            modifyingProperty(c, PROPERTY_ALIGNMENT);
            if (c instanceof Label) {
                ((Label) c).setAlignment(root.getAlignment().intValue());
            } else {
                ((TextArea) c).setAlignment(root.getAlignment().intValue());
            }
        }
        if (root.isGrowByContent() != null) {
            modifyingProperty(c, PROPERTY_TEXT_AREA_GROW);
            ((TextArea) c).setGrowByContent(root.isGrowByContent().booleanValue());
        }
        if (root.getTabPlacement() != null) {
            modifyingProperty(c, PROPERTY_TAB_PLACEMENT);
            ((Tabs) c).setTabPlacement(root.getTabPlacement().intValue());
        }
        if (root.getTabTextPosition() != null) {
            modifyingProperty(c, PROPERTY_TAB_TEXT_POSITION);
            ((Tabs) c).setTabTextPosition(root.getTabTextPosition().intValue());
        }
        if (root.getUiid() != null) {
            modifyingProperty(c, PROPERTY_UIID);
            c.setUIID(root.getUiid());
        }
        if (root.getDialogUIID() != null) {
            modifyingProperty(c, PROPERTY_DIALOG_UIID);
            ((Dialog) c).setDialogUIID(root.getDialogUIID());
        }
        if (root.isDisposeWhenPointerOutOfBounds() != null) {
            modifyingProperty(c, PROPERTY_DISPOSE_WHEN_POINTER_OUT);
            ((Dialog) c).setDisposeWhenPointerOutOfBounds(root.isDisposeWhenPointerOutOfBounds());
        }
        if (root.getCloudBoundProperty() != null) {
            modifyingProperty(c, PROPERTY_CLOUD_BOUND_PROPERTY);
            c.setCloudBoundProperty(root.getCloudBoundProperty());
        }
        if (root.getCloudDestinationProperty() != null) {
            modifyingProperty(c, PROPERTY_CLOUD_DESTINATION_PROPERTY);
            c.setCloudDestinationProperty(root.getCloudDestinationProperty());
        }
        if (root.getDialogPosition() != null && root.getDialogPosition().length() > 0) {
            modifyingProperty(c, PROPERTY_DIALOG_POSITION);
            ((Dialog) c).setDialogPosition(root.getDialogPosition());
        }
        if (root.isFocusable() != null) {
            modifyingProperty(c, PROPERTY_FOCUSABLE);
            c.setFocusable(root.isFocusable().booleanValue());
        }
        if (root.isEnabled() != null) {
            modifyingProperty(c, PROPERTY_ENABLED);
            c.setEnabled(root.isEnabled().booleanValue());
        }
        if (root.isScrollVisible() != null) {
            modifyingProperty(c, PROPERTY_SCROLL_VISIBLE);
            c.setScrollVisible(root.isScrollVisible().booleanValue());
        }
        if (root.getIcon() != null) {
            modifyingProperty(c, PROPERTY_ICON);
            ((Label) c).setIcon(res.getImage(root.getIcon()));
        }
        if (root.getRolloverIcon() != null) {
            modifyingProperty(c, PROPERTY_ROLLOVER_ICON);
            ((Button) c).setRolloverIcon(res.getImage(root.getRolloverIcon()));
        }
        if (root.getPressedIcon() != null) {
            modifyingProperty(c, PROPERTY_PRESSED_ICON);
            ((Button) c).setPressedIcon(res.getImage(root.getPressedIcon()));
        }
        if (root.getDisabledIcon() != null) {
            modifyingProperty(c, PROPERTY_DISABLED_ICON);
            ((Button) c).setDisabledIcon(res.getImage(root.getDisabledIcon()));
        }
        if (root.getGap() != null) {
            modifyingProperty(c, PROPERTY_GAP);
            ((Label) c).setGap(root.getGap().intValue());
        }
        if (root.getVerticalAlignment() != null) {
            modifyingProperty(c, PROPERTY_VERTICAL_ALIGNMENT);
            if (c instanceof Label) {
                ((Label) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
            } else {
                ((TextArea) c).setVerticalAlignment(root.getVerticalAlignment().intValue());
            }
        }
        if (root.getTextPosition() != null) {
            modifyingProperty(c, PROPERTY_TEXT_POSITION);
            ((Label) c).setTextPosition(root.getTextPosition().intValue());
        }
        if (root.getTitle() != null) {
            modifyingProperty(c, PROPERTY_TITLE);
            ((Form) c).setTitle(root.getTitle());
        }
        // components should be added when we've set everything else up
        if (root.getComponent() != null) {
            modifyingProperty(c, PROPERTY_COMPONENTS);
            if (c instanceof Tabs) {
                for (ComponentEntry ent : root.getComponent()) {
                    Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
                    ((Tabs) c).addTab(ent.getTabTitle(), newCmp);
                }
            } else {
                for (ComponentEntry ent : root.getComponent()) {
                    Component newCmp = createInstance(ent, res, rootContainer, (Container) c, postCreateTasks);
                    Object cons = newCmp.getClientProperty("layoutConstraint");
                    if (cons != null) {
                        modifyingProperty(c, PROPERTY_LAYOUT_CONSTRAINT);
                        ((Container) c).addComponent(cons, newCmp);
                    } else {
                        ((Container) c).addComponent(newCmp);
                    }
                }
            }
        }
        if (root.getColumns() != null) {
            modifyingProperty(c, PROPERTY_COLUMNS);
            ((TextArea) c).setColumns(root.getColumns().intValue());
        }
        if (root.getRows() != null) {
            modifyingProperty(c, PROPERTY_ROWS);
            ((TextArea) c).setRows(root.getRows().intValue());
        }
        if (root.getHint() != null) {
            modifyingProperty(c, PROPERTY_HINT);
            if (c instanceof List) {
                ((List) c).setHint(root.getHint());
            } else {
                ((TextArea) c).setHint(root.getHint());
            }
        }
        if (root.getHintIcon() != null) {
            modifyingProperty(c, PROPERTY_HINT_ICON);
            if (c instanceof List) {
                ((List) c).setHintIcon(res.getImage(root.getHint()));
            } else {
                ((TextArea) c).setHintIcon(res.getImage(root.getHint()));
            }
        }
        if (root.getItemGap() != null) {
            modifyingProperty(c, PROPERTY_ITEM_GAP);
            ((List) c).setItemGap(root.getItemGap().intValue());
        }
        if (root.getFixedSelection() != null) {
            modifyingProperty(c, PROPERTY_LIST_FIXED);
            ((List) c).setFixedSelection(root.getFixedSelection().intValue());
        }
        if (root.getOrientation() != null) {
            modifyingProperty(c, PROPERTY_LIST_ORIENTATION);
            ((List) c).setOrientation(root.getOrientation().intValue());
        }
        if (c instanceof com.codename1.ui.List && !(c instanceof com.codename1.components.RSSReader)) {
            modifyingProperty(c, PROPERTY_LIST_ITEMS);
            if (root.getStringItem() != null && root.getStringItem().length > 0) {
                String[] arr = new String[root.getStringItem().length];
                for (int iter = 0; iter < arr.length; iter++) {
                    arr[iter] = root.getStringItem()[iter].getValue();
                }
                ((List) c).setModel(new DefaultListModel<String>(arr));
            } else {
                if (root.getMapItems() != null && root.getMapItems().length > 0) {
                    Hashtable[] arr = new Hashtable[root.getMapItems().length];
                    for (int iter = 0; iter < arr.length; iter++) {
                        arr[iter] = new Hashtable();
                        if (root.getMapItems()[iter].getActionItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                Command cmd = createCommandImpl((String) v.getValue(), null, -1, v.getValue(), false, "");
                                cmd.putClientProperty(COMMAND_ACTION, (String) v.getValue());
                                arr[iter].put(v.getKey(), cmd);
                            }
                        }
                        if (root.getMapItems()[iter].getStringItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                arr[iter].put(v.getKey(), v.getValue());
                            }
                        }
                        if (root.getMapItems()[iter].getImageItem() != null) {
                            for (Val v : root.getMapItems()[iter].getActionItem()) {
                                arr[iter].put(v.getKey(), res.getImage(v.getValue()));
                            }
                        }
                    }
                    ((List) c).setModel(new DefaultListModel<java.util.Map>(arr));
                }
            }
        }
        if (root.getSelectedRenderer() != null) {
            modifyingProperty(c, PROPERTY_LIST_RENDERER);
            GenericListCellRenderer g;
            if (root.getSelectedRendererEven() == null) {
                Component selected = createContainer(res, root.getSelectedRenderer());
                Component unselected = createContainer(res, root.getUnselectedRenderer());
                g = new GenericListCellRenderer(selected, unselected);
                g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
            } else {
                Component selected = createContainer(res, root.getSelectedRenderer());
                Component unselected = createContainer(res, root.getUnselectedRenderer());
                Component even = createContainer(res, root.getSelectedRendererEven());
                Component evenU = createContainer(res, root.getUnselectedRendererEven());
                g = new GenericListCellRenderer(selected, unselected, even, evenU);
                g.setFisheye(!root.getSelectedRenderer().equals(root.getUnselectedRenderer()));
            }
            if (c instanceof ContainerList) {
                ((ContainerList) c).setRenderer(g);
            } else {
                ((List) c).setRenderer(g);
            }
        }
        if (root.getNextForm() != null && root.getNextForm().length() > 0) {
            modifyingProperty(c, PROPERTY_NEXT_FORM);
            setNextForm(c, root.getNextForm(), res, rootContainer);
        }
        if (root.getCommand() != null) {
            modifyingProperty(c, PROPERTY_COMMANDS);
            for (CommandEntry cmd : root.getCommand()) {
                Command currentCommand = createCommandImpl(cmd.getName(), res.getImage(cmd.getIcon()), cmd.getId(), cmd.getAction(), cmd.isBackCommand(), cmd.getArgument());
                if (cmd.getRolloverIcon() != null && cmd.getRolloverIcon().length() > 0) {
                    currentCommand.setRolloverIcon(res.getImage(cmd.getRolloverIcon()));
                }
                if (cmd.getPressedIcon() != null && cmd.getPressedIcon().length() > 0) {
                    currentCommand.setPressedIcon(res.getImage(cmd.getPressedIcon()));
                }
                if (cmd.getDisabledIcon() != null && cmd.getDisabledIcon().length() > 0) {
                    currentCommand.setDisabledIcon(res.getImage(cmd.getDisabledIcon()));
                }
                if (cmd.isBackCommand()) {
                    ((Form) c).setBackCommand(currentCommand);
                }
                ((Form) c).addCommand(currentCommand);
                currentCommand.putClientProperty(COMMAND_ARGUMENTS, cmd.getArgument());
                currentCommand.putClientProperty(COMMAND_ACTION, cmd.getAction());
            }
        }
        if (root.isCyclicFocus() != null) {
            modifyingProperty(c, PROPERTY_CYCLIC_FOCUS);
            ((Form) c).setCyclicFocus(root.isCyclicFocus().booleanValue());
        }
        if (root.isRtl() != null) {
            modifyingProperty(c, PROPERTY_RTL);
            c.setRTL(root.isRtl().booleanValue());
        }
        if (root.getThumbImage() != null) {
            modifyingProperty(c, PROPERTY_SLIDER_THUMB);
            ((Slider) c).setThumbImage(res.getImage(root.getThumbImage()));
        }
        if (root.isInfinite() != null) {
            modifyingProperty(c, PROPERTY_INFINITE);
            ((Slider) c).setInfinite(root.isInfinite().booleanValue());
        }
        if (root.getProgress() != null) {
            modifyingProperty(c, PROPERTY_PROGRESS);
            ((Slider) c).setProgress(root.getProgress().intValue());
        }
        if (root.isVertical() != null) {
            modifyingProperty(c, PROPERTY_VERTICAL);
            ((Slider) c).setVertical(root.isVertical().booleanValue());
        }
        if (root.isEditable() != null) {
            modifyingProperty(c, PROPERTY_EDITABLE);
            if (c instanceof TextArea) {
                ((TextArea) c).setEditable(root.isEditable().booleanValue());
            } else {
                ((Slider) c).setEditable(root.isEditable().booleanValue());
            }
        }
        if (root.getIncrements() != null) {
            modifyingProperty(c, PROPERTY_INCREMENTS);
            ((Slider) c).setIncrements(root.getIncrements().intValue());
        }
        if (root.isRenderPercentageOnTop() != null) {
            modifyingProperty(c, PROPERTY_RENDER_PERCENTAGE_ON_TOP);
            ((Slider) c).setRenderPercentageOnTop(root.isRenderPercentageOnTop().booleanValue());
        }
        if (root.getMaxValue() != null) {
            modifyingProperty(c, PROPERTY_MAX_VALUE);
            ((Slider) c).setMaxValue(root.getMaxValue().intValue());
        }
        if (root.getMinValue() != null) {
            modifyingProperty(c, PROPERTY_MIN_VALUE);
            ((Slider) c).setMinValue(root.getMinValue().intValue());
        }
        if (root.getCommandName() != null) {
            modifyingProperty(c, PROPERTY_COMMAND);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    Command cmd = createCommandImpl(root.getCommandName(), res.getImage(root.getCommandIcon()), root.getCommandId().intValue(), root.getCommandAction(), root.isCommandBack().booleanValue(), root.getCommandArgument());
                    if (c instanceof Container) {
                        Button b = (Button) ((Container) c).getLeadComponent();
                        b.setCommand(cmd);
                        return;
                    }
                    ((Button) c).setCommand(cmd);
                }
            });
        }
        if (root.getLabelFor() != null) {
            modifyingProperty(c, PROPERTY_LABEL_FOR);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    ((Label) c).setLabelForComponent((Label) findByName(root.getLabelFor(), rootContainer));
                }
            });
        }
        if (root.getLeadComponent() != null) {
            modifyingProperty(c, PROPERTY_LEAD_COMPONENT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    ((Container) c).setLeadComponent(findByName(root.getLeadComponent(), rootContainer));
                }
            });
        }
        if (root.getNextFocusUp() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_UP);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusUp(findByName(root.getNextFocusUp(), rootContainer));
                }
            });
        }
        if (root.getNextFocusDown() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_DOWN);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusDown(findByName(root.getNextFocusDown(), rootContainer));
                }
            });
        }
        if (root.getNextFocusLeft() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_LEFT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusLeft(findByName(root.getNextFocusLeft(), rootContainer));
                }
            });
        }
        if (root.getNextFocusRight() != null) {
            modifyingProperty(c, PROPERTY_NEXT_FOCUS_RIGHT);
            postCreateTasks.add(new Runnable() {

                public void run() {
                    c.setNextFocusRight(findByName(root.getNextFocusRight(), rootContainer));
                }
            });
        }
        // custom settings are always last after all other properties
        if (root.getCustom() != null && root.getCustom().length > 0) {
            modifyingProperty(c, PROPERTY_CUSTOM);
            for (Custom cust : root.getCustom()) {
                modifyingCustomProperty(c, cust.getName());
                Object value = null;
                Class customType = UserInterfaceEditor.getPropertyCustomType(c, cust.getName());
                if (customType.isArray()) {
                    if (customType == String[].class) {
                        if (cust.getStr() != null) {
                            String[] arr = new String[cust.getStr().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = cust.getStr()[iter].getValue();
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == String[][].class) {
                        if (cust.getArr() != null) {
                            String[][] arr = new String[cust.getArr().length][];
                            for (int iter = 0; iter < arr.length; iter++) {
                                if (cust.getArr()[iter] != null && cust.getArr()[iter].getValue() != null) {
                                    arr[iter] = new String[cust.getArr()[iter].getValue().length];
                                    for (int inter = 0; inter < arr[iter].length; inter++) {
                                        arr[iter][inter] = cust.getArr()[iter].getValue()[inter].getValue();
                                    }
                                }
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == com.codename1.ui.Image[].class) {
                        if (cust.getStr() != null) {
                            com.codename1.ui.Image[] arr = new com.codename1.ui.Image[cust.getStr().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = res.getImage(cust.getStr()[iter].getValue());
                            }
                            c.setPropertyValue(cust.getName(), arr);
                        } else {
                            c.setPropertyValue(cust.getName(), null);
                        }
                        continue;
                    }
                    if (customType == Object[].class) {
                        if (cust.getStringItem() != null) {
                            String[] arr = new String[cust.getStringItem().length];
                            for (int iter = 0; iter < arr.length; iter++) {
                                arr[iter] = cust.getStringItem()[iter].getValue();
                            }
                            c.setPropertyValue(cust.getName(), arr);
                            continue;
                        } else {
                            if (cust.getMapItems() != null) {
                                Hashtable[] arr = new Hashtable[cust.getMapItems().length];
                                for (int iter = 0; iter < arr.length; iter++) {
                                    arr[iter] = new Hashtable();
                                    if (cust.getMapItems()[iter].getActionItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            Command cmd = createCommandImpl(v.getValue(), null, -1, v.getValue(), false, "");
                                            cmd.putClientProperty(COMMAND_ACTION, v.getValue());
                                            value = cmd;
                                            arr[iter].put(v.getKey(), cmd);
                                        }
                                    }
                                    if (cust.getMapItems()[iter].getStringItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            arr[iter].put(v.getKey(), v.getValue());
                                        }
                                    }
                                    if (cust.getMapItems()[iter].getImageItem() != null) {
                                        for (Val v : cust.getMapItems()[iter].getActionItem()) {
                                            arr[iter].put(v.getKey(), res.getImage(v.getValue()));
                                        }
                                    }
                                }
                                c.setPropertyValue(cust.getName(), arr);
                                continue;
                            }
                        }
                        c.setPropertyValue(cust.getName(), null);
                        continue;
                    }
                }
                if (customType == String.class) {
                    c.setPropertyValue(cust.getName(), cust.getValue());
                    continue;
                }
                if (customType == Integer.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Integer.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Long.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Long.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Double.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Double.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Date.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), new Date(Long.parseLong(cust.getValue())));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Float.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Float.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Byte.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Byte.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Character.class) {
                    if (cust.getValue() != null && ((String) cust.getValue()).length() > 0) {
                        c.setPropertyValue(cust.getName(), new Character(((String) cust.getValue()).charAt(0)));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == Boolean.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), Boolean.valueOf(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == com.codename1.ui.Image.class) {
                    if (cust.getValue() != null) {
                        c.setPropertyValue(cust.getName(), res.getImage(cust.getValue()));
                    } else {
                        c.setPropertyValue(cust.getName(), null);
                    }
                    continue;
                }
                if (customType == com.codename1.ui.Container.class) {
                    // resource might have been removed we need to fail gracefully
                    String[] uiNames = res.getUIResourceNames();
                    for (int iter = 0; iter < uiNames.length; iter++) {
                        if (uiNames[iter].equals(cust.getName())) {
                            c.setPropertyValue(cust.getName(), createContainer(res, cust.getName()));
                            continue;
                        }
                    }
                    c.setPropertyValue(cust.getName(), null);
                    continue;
                }
                if (customType == com.codename1.ui.list.CellRenderer.class) {
                    if (cust.getUnselectedRenderer() != null) {
                        GenericListCellRenderer g;
                        if (cust.getSelectedRendererEven() == null) {
                            Component selected = createContainer(res, cust.getSelectedRenderer());
                            Component unselected = createContainer(res, cust.getUnselectedRenderer());
                            g = new GenericListCellRenderer(selected, unselected);
                            g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
                        } else {
                            Component selected = createContainer(res, cust.getSelectedRenderer());
                            Component unselected = createContainer(res, cust.getUnselectedRenderer());
                            Component even = createContainer(res, cust.getSelectedRendererEven());
                            Component evenU = createContainer(res, cust.getUnselectedRendererEven());
                            g = new GenericListCellRenderer(selected, unselected, even, evenU);
                            g.setFisheye(!cust.getSelectedRenderer().equals(cust.getUnselectedRenderer()));
                        }
                        c.setPropertyValue(cust.getName(), g);
                        continue;
                    }
                    c.setPropertyValue(cust.getName(), null);
                    continue;
                }
            }
        }
        return c;
    } catch (Throwable t) {
        t.printStackTrace();
        JOptionPane.showMessageDialog(java.awt.Frame.getFrames()[0], "Error creating component: " + root.getName() + "\n" + t.toString() + "\ntrying to recover...", "Error", JOptionPane.ERROR_MESSAGE);
        return null;
    }
}
Also used : Val(com.codename1.ui.util.xml.Val) TextArea(com.codename1.ui.TextArea) Label(com.codename1.ui.Label) Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Dialog(com.codename1.ui.Dialog) ComponentEntry(com.codename1.ui.util.xml.comps.ComponentEntry) ArrayList(java.util.ArrayList) ContainerList(com.codename1.ui.list.ContainerList) List(com.codename1.ui.List) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) List(com.codename1.ui.List) RadioButton(com.codename1.ui.RadioButton) CheckBox(com.codename1.ui.CheckBox) Tabs(com.codename1.ui.Tabs) FlowLayout(com.codename1.ui.layouts.FlowLayout) Slider(com.codename1.ui.Slider) Form(com.codename1.ui.Form) BoxLayout(com.codename1.ui.layouts.BoxLayout) ContainerList(com.codename1.ui.list.ContainerList) GridLayout(com.codename1.ui.layouts.GridLayout) Component(com.codename1.ui.Component) TableLayout(com.codename1.ui.table.TableLayout) GenericListCellRenderer(com.codename1.ui.list.GenericListCellRenderer) Hashtable(java.util.Hashtable) Custom(com.codename1.ui.util.xml.comps.Custom) Date(java.util.Date) CommandEntry(com.codename1.ui.util.xml.comps.CommandEntry) BoxLayout(com.codename1.ui.layouts.BoxLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) GridLayout(com.codename1.ui.layouts.GridLayout) Layout(com.codename1.ui.layouts.Layout) FlowLayout(com.codename1.ui.layouts.FlowLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) TableLayout(com.codename1.ui.table.TableLayout) ActionCommand(com.codename1.designer.ActionCommand) Command(com.codename1.ui.Command)

Example 3 with Thing

use of com.codename1.rad.schemas.Thing in project CodenameOne by codenameone.

the class LayoutTests method flowLayoutTests.

public boolean flowLayoutTests() throws Exception {
    System.out.println("FlowLayout Tests");
    boolean rtl = UIManager.getInstance().getLookAndFeel().isRTL();
    try {
        UIManager.getInstance().getLookAndFeel().setRTL(false);
        FlowLayout fl = new FlowLayout();
        Container cnt = new Container(fl);
        cnt.setRTL(false);
        Label lbl = new Label("Test");
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0);
        lbl.setRTL(false);
        cnt.add(lbl);
        int cntWidth = 500;
        int cntHeight = 500;
        int labelPreferredWidth = lbl.getPreferredW();
        int labelPreferredHeight = lbl.getPreferredH();
        cnt.setWidth(cntWidth);
        cnt.setHeight(cntHeight);
        // Start with NO margin, NO padding, default FlowLayout, with single child label also with no padding or margin
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "Label should be laid out with its preferred height");
        assertEqual(0, lbl.getX(), "Label should have x=0 in default FlowLayout");
        assertEqual(0, lbl.getY(), "Label should have y=0 in default FlowLayout");
        // Now try with Change to RTL.  FlowLayout should respect RTL so that left padding/margin/align is interpreted
        // as the opposite.
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "Label should be aligned right default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "Label should have y=0 in default FlowLayout in RTL");
        // Now add left Margin to the label.  This should be applied to right side in RTL
        lbl.getStyle().setMarginLeft(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LM10: Label should be laid out with its preferred width in RTL ");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 10, lbl.getX(), "LM10: Label should be aligned right default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now change to LTR
        cnt.setRTL(false);
        lbl.setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "LM10: Label should be aligned left with 10 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "LM10: Label should have y=0 in default FlowLayout");
        // Now add left padding to the container.  In RTL this should be applied on right
        cnt.getStyle().setPaddingLeft(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(22, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout");
        // Now change back to RTL
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 22, lbl.getX(), "CP12,LM10: Label should be aligned right with 22 offset default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now change padding to right.  in RTL this is applied to left.  This shouldn't affect the layout at all
        cnt.getStyle().setPaddingLeft(0);
        cnt.getStyle().setPaddingRight(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - lbl.getWidth() - 10, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout in RTL");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout in RTL");
        // Now in LTR
        cnt.setRTL(false);
        lbl.setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(0, lbl.getY(), "CP12,LM10: Label should have y=0 in default FlowLayout");
        // Now add some top padding
        cnt.getStyle().setPaddingTop(5);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "TP5,CP12,LM10: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "TP5,CP12,LM10: Label should be laid out with its preferred height");
        assertEqual(10, lbl.getX(), "TP5,CP12,LM10: Label should be aligned left with 22 offset default FlowLayout");
        assertEqual(5, lbl.getY(), "TP5,CP12,LM10: Label should have y=5 in default FlowLayout");
        // And in RTL
        cnt.setRTL(true);
        lbl.setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "TP5,CP12,LM10: Label should be laid out with its preferred width in RTL");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "TP5,CP12,LM10: Label should be laid out with its preferred height in RTL");
        assertEqual(cntWidth - 10 - lbl.getWidth(), lbl.getX(), "TP5,CP12,LM10: Label should be aligned right with 10 offset default FlowLayout in RTL");
        assertEqual(5, lbl.getY(), "TP5,CP12,LM10: Label should have y=5 in default FlowLayout in RTL");
        // Now let's test center alignment with a single child.
        // All margins and padding back to ZERO and LTR
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0).setRTL(false);
        fl.setAlign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR Center: Label should be laid out with its preferred height");
        assertEqual((cntWidth - lbl.getWidth()) / 2, lbl.getX(), "LTR Center: Label should be aligned  center");
        assertEqual(0, lbl.getY(), "LTR Center: Label should have y=0 in default FlowLayout");
        // Now let's add some margin to the child, and padding to the parent
        // In this case, it should align center in the *inner* bounds of the container (i.e. inside the padding box).
        // But because the child has a left margin of 10, it should actually be 10 to the right of absolute center of this box.
        lbl.getStyle().setMarginLeft(10);
        cnt.getStyle().setPaddingLeft(5);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,P5,M10, Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR,P5,M10, Center: Label should be laid out with its preferred height");
        assertEqual((cnt.getInnerWidth() - lbl.getOuterWidth()) / 2 + 15, lbl.getX(), "LTR,P5,M10, Center: Label should be aligned center");
        assertEqual(0, lbl.getY(), "LTR,P5,M10, Center: Label should have y=0 in default FlowLayout");
        // Now check RTL
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "RTL,P5,M10, Center: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "RTL,P5,M10, Center: Label should be laid out with its preferred height");
        // Expected value:  We subtract LTR expected value from the container width - which should take us to the right edge
        // of the label - so we additionally subtract the label width to take us to the left edge.
        assertEqual(cntWidth - ((cnt.getInnerWidth() - lbl.getOuterWidth()) / 2 + 15) - lbl.getWidth(), lbl.getX(), "RTL,P5,M10, Center: Label should be aligned center");
        assertEqual(0, lbl.getY(), "RTL,P5,M10, Center: Label should have y=0 in default FlowLayout");
        // Now let's test right alignment with a single child.
        fl.setAlign(RIGHT);
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "LTR,Right: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "LTR,Right: Label should be laid out with its preferred height");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "LTR,Right: Label should be aligned right");
        assertEqual(0, lbl.getY(), "LTR,Right: Label should have y=0 in default FlowLayout");
        // Now in RTL
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(labelPreferredWidth, lbl.getWidth(), "RTL,Right: Label should be laid out with its preferred width");
        assertEqual(labelPreferredHeight, lbl.getHeight(), "RTL,Right: Label should be laid out with its preferred height");
        assertEqual(0, lbl.getX(), "RTL,Right: Label should be aligned right");
        assertEqual(0, lbl.getY(), "RTL,Right: Label should have y=0 in default FlowLayout");
        // Now add some padding to the left.  This should have no effect on this test because we are aligned right.
        cnt.getStyle().setPaddingLeft(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getX(), "RTL,P10,Right: Label should be aligned right");
        $(lbl, cnt).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "LTR,Right: Label should be aligned right");
        // Now let's test vertical alignment
        $(cnt, lbl).selectAllStyles().setPadding(0).setMargin(0);
        fl.setValign(BOTTOM);
        $(cnt, lbl).setRTL(false);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight(), lbl.getY(), "Should be aligned bottom");
        // RTL should have no effect on the vertical alignment
        $(cnt, lbl).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight(), lbl.getY(), "Should be aligned bottom");
        // Add some padding to the bottom
        cnt.getStyle().setPaddingBottom(10);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight() - 10, lbl.getY(), "Should be aligned bottom");
        // Add bottom margin to label.  This should effectively move it up.
        lbl.getStyle().setMarginBottom(12);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(cntHeight - lbl.getHeight() - 22, lbl.getY(), "Should be aligned bottom");
        // Vertical align center.
        fl.setValign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((cnt.getInnerHeight() - lbl.getOuterHeight()) / 2, lbl.getY(), "Should be valigned middle");
        // Now valign by row.  With only a single component, this should cause the children to be rendered aligned TOP
        fl.setValignByRow(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Check valign=BOTTOM now.  Should be same as center when aligning by row, since it is its own row.
        fl.setValign(BOTTOM);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Baseline should be same as others since there is only a single child.
        fl.setValign(BASELINE);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        // Now let's try laying out a 2nd child.
        Component spacer1 = createEmptyComponent(100, 100);
        $(spacer1, lbl, cnt).setPadding(0).setMargin(0).setRTL(false);
        cnt.add(spacer1);
        fl.setValign(TOP);
        fl.setAlign(LEFT);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        assertEqual(0, spacer1.getY(), "Spacer should be aligned to top");
        assertEqual(0, lbl.getX(), "Label should be aligned to left edge");
        assertEqual(lbl.getWidth(), spacer1.getX(), "Spacer should be aligned to right edge of label");
        assertEqual(lbl.getPreferredW(), lbl.getWidth(), "Label should be rendered to its preferred width");
        assertEqual(lbl.getPreferredH(), lbl.getHeight(), "Lable should be rendered to its preferred height");
        assertEqual(100, spacer1.getWidth(), "Spacer should be its preferred width");
        assertEqual(100, spacer1.getHeight(), "Spacer should be its preferred height");
        // Let's try aligning bottom by row with the two of them.
        fl.setValign(BOTTOM);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        int rowH = Math.max(100, lbl.getOuterHeight());
        assertEqual(rowH - lbl.getHeight(), lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual(rowH - 100, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now let's valign center
        fl.setValign(CENTER);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((rowH - lbl.getHeight()) / 2, lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual((rowH - 100) / 2, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now same thing with RTL
        $(cnt, lbl, spacer1).setRTL(true);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual((rowH - lbl.getHeight()) / 2, lbl.getY(), "Label should be aligned with the bottom of its row.");
        assertEqual((rowH - 100) / 2, spacer1.getY(), "Spacer shoudl be aligned with the bottom of its row");
        // Now align top left but still RTL.  Make sure everything is Kosher
        fl.setValign(TOP);
        fl.setAlign(LEFT);
        cnt.setShouldCalcPreferredSize(true);
        cnt.layoutContainer();
        assertEqual(0, lbl.getY(), "Should be valigned top, when valignbyrow is true");
        assertEqual(0, spacer1.getY(), "Spacer should be aligned to top");
        assertEqual(cntWidth - lbl.getWidth(), lbl.getX(), "Label should be aligned to left edge");
        assertEqual(cntWidth - lbl.getWidth() - spacer1.getWidth(), spacer1.getX(), "Spacer should be aligned to right edge of label");
        assertEqual(lbl.getPreferredW(), lbl.getWidth(), "Label should be rendered to its preferred width");
        assertEqual(lbl.getPreferredH(), lbl.getHeight(), "Lable should be rendered to its preferred height");
        assertEqual(100, spacer1.getWidth(), "Spacer should be its preferred width");
        assertEqual(100, spacer1.getHeight(), "Spacer should be its preferred height");
    } finally {
        UIManager.getInstance().getLookAndFeel().setRTL(rtl);
    }
    return true;
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout)

Example 4 with Thing

use of com.codename1.rad.schemas.Thing in project CodenameOne by codenameone.

the class LazyValueC method createComponent.

private Component createComponent(DataInputStream in, Container parent, Container root, Resources res, Hashtable componentListeners, EmbeddedContainer embedded) throws Exception {
    String name = in.readUTF();
    int property = in.readInt();
    // special case for the base form
    if (property == PROPERTY_BASE_FORM) {
        String baseFormName = name;
        initBaseForm(baseFormName);
        if (!ignorBaseForm) {
            Form base = (Form) createContainer(res, baseFormName);
            Container destination = (Container) findByName("destination", base);
            // try finding an appropriate empty container if no "fixed" destination is defined
            if (destination == null) {
                destination = findEmptyContainer(base.getContentPane());
                if (destination == null) {
                    System.out.println("Couldn't find appropriate 'destination' container in base form: " + baseFormName);
                    return null;
                }
            }
            root = base;
            Component cmp = createComponent(in, destination, root, res, componentListeners, embedded);
            if (destination.getLayout() instanceof BorderLayout) {
                destination.addComponent(BorderLayout.CENTER, cmp);
            } else {
                destination.addComponent(cmp);
            }
            return root;
        } else {
            name = in.readUTF();
            property = in.readInt();
        }
    }
    Component cmp = createComponentType(name);
    if (componentListeners != null) {
        Object listeners = componentListeners.get(name);
        if (listeners != null) {
            if (listeners instanceof Vector) {
                Vector v = (Vector) listeners;
                for (int iter = 0; iter < v.size(); iter++) {
                    bindListenerToComponent(cmp, v.elementAt(iter));
                }
            } else {
                bindListenerToComponent(cmp, listeners);
            }
        }
    }
    Component actualLead = cmp;
    if (actualLead instanceof Container) {
        Container cnt = (Container) actualLead;
        actualLead = cnt.getLeadComponent();
        if (actualLead == null) {
            actualLead = cmp;
        }
    }
    if (actualLead instanceof Button) {
        ActionListener l = getFormListenerInstance(root, embedded);
        if (l != null) {
            ((Button) actualLead).addActionListener(l);
        }
    } else {
        if (actualLead instanceof TextArea) {
            ActionListener l = getFormListenerInstance(root, embedded);
            if (l != null) {
                ((TextArea) actualLead).addActionListener(l);
            }
        } else {
            if (actualLead instanceof List) {
                ActionListener l = getFormListenerInstance(root, embedded);
                if (l != null) {
                    ((List) actualLead).addActionListener(l);
                }
            } else {
                if (actualLead instanceof ContainerList) {
                    ActionListener l = getFormListenerInstance(root, embedded);
                    if (l != null) {
                        ((ContainerList) actualLead).addActionListener(l);
                    }
                } else {
                    if (actualLead instanceof com.codename1.ui.Calendar) {
                        ActionListener l = getFormListenerInstance(root, embedded);
                        if (l != null) {
                            ((com.codename1.ui.Calendar) actualLead).addActionListener(l);
                        }
                    }
                }
            }
        }
    }
    cmp.putClientProperty(TYPE_KEY, name);
    if (root == null) {
        root = (Container) cmp;
    }
    while (property != -1) {
        modifyingProperty(cmp, property);
        switch(property) {
            case PROPERTY_CUSTOM:
                String customPropertyName = in.readUTF();
                modifyingCustomProperty(cmp, customPropertyName);
                boolean isNull = in.readBoolean();
                if (isNull) {
                    cmp.setPropertyValue(customPropertyName, null);
                    break;
                }
                boolean cl = cmp instanceof ContainerList;
                String[] propertyNames = cmp.getPropertyNames();
                for (int iter = 0; iter < propertyNames.length; iter++) {
                    if (propertyNames[iter].equals(customPropertyName)) {
                        Class type = cmp.getPropertyTypes()[iter];
                        String[] typeNames = cmp.getPropertyTypeNames();
                        String typeName = null;
                        if (typeNames != null && typeNames.length > iter) {
                            typeName = typeNames[iter];
                        }
                        Object value = readCustomPropertyValue(in, type, typeName, res, propertyNames[iter]);
                        if (cl && customPropertyName.equals("ListItems") && setListModel((ContainerList) cmp)) {
                            break;
                        }
                        cmp.setPropertyValue(customPropertyName, value);
                        break;
                    }
                }
                break;
            case PROPERTY_EMBED:
                root.putClientProperty(EMBEDDED_FORM_FLAG, "");
                ((EmbeddedContainer) cmp).setEmbed(in.readUTF());
                Container embed = createContainer(res, ((EmbeddedContainer) cmp).getEmbed(), (EmbeddedContainer) cmp);
                if (embed != null) {
                    if (embed instanceof Form) {
                        embed = formToContainer((Form) embed);
                    }
                    ((EmbeddedContainer) cmp).addComponent(BorderLayout.CENTER, embed);
                    // this isn't exactly the "right thing" but its the best we can do to make all
                    // use cases work
                    beforeShowContainer(embed);
                    postShowContainer(embed);
                }
                break;
            case PROPERTY_TOGGLE_BUTTON:
                ((Button) cmp).setToggle(in.readBoolean());
                break;
            case PROPERTY_RADIO_GROUP:
                ((RadioButton) cmp).setGroup(in.readUTF());
                break;
            case PROPERTY_SELECTED:
                boolean isSelected = in.readBoolean();
                if (cmp instanceof RadioButton) {
                    ((RadioButton) cmp).setSelected(isSelected);
                } else {
                    ((CheckBox) cmp).setSelected(isSelected);
                }
                break;
            case PROPERTY_SCROLLABLE_X:
                ((Container) cmp).setScrollableX(in.readBoolean());
                break;
            case PROPERTY_SCROLLABLE_Y:
                ((Container) cmp).setScrollableY(in.readBoolean());
                break;
            case PROPERTY_TENSILE_DRAG_ENABLED:
                cmp.setTensileDragEnabled(in.readBoolean());
                break;
            case PROPERTY_TACTILE_TOUCH:
                cmp.setTactileTouch(in.readBoolean());
                break;
            case PROPERTY_SNAP_TO_GRID:
                cmp.setSnapToGrid(in.readBoolean());
                break;
            case PROPERTY_FLATTEN:
                cmp.setFlatten(in.readBoolean());
                break;
            case PROPERTY_TEXT:
                if (cmp instanceof Label) {
                    ((Label) cmp).setText(in.readUTF());
                } else {
                    ((TextArea) cmp).setText(in.readUTF());
                }
                break;
            case PROPERTY_TEXT_MAX_LENGTH:
                ((TextArea) cmp).setMaxSize(in.readInt());
                break;
            case PROPERTY_TEXT_CONSTRAINT:
                ((TextArea) cmp).setConstraint(in.readInt());
                if (cmp instanceof TextField) {
                    int cons = ((TextArea) cmp).getConstraint();
                    if ((cons & TextArea.NUMERIC) == TextArea.NUMERIC) {
                        ((TextField) cmp).setInputModeOrder(new String[] { "123" });
                    }
                }
                break;
            case PROPERTY_ALIGNMENT:
                if (cmp instanceof Label) {
                    ((Label) cmp).setAlignment(in.readInt());
                } else {
                    ((TextArea) cmp).setAlignment(in.readInt());
                }
                break;
            case PROPERTY_TEXT_AREA_GROW:
                ((TextArea) cmp).setGrowByContent(in.readBoolean());
                break;
            case PROPERTY_LAYOUT:
                Layout layout = null;
                switch(in.readShort()) {
                    case LAYOUT_BORDER_LEGACY:
                        layout = new BorderLayout();
                        break;
                    case LAYOUT_BORDER_ANOTHER_LEGACY:
                        {
                            BorderLayout b = new BorderLayout();
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.NORTH, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.EAST, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.WEST, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.SOUTH, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.CENTER, in.readUTF());
                            }
                            layout = b;
                            break;
                        }
                    case LAYOUT_BORDER:
                        {
                            BorderLayout b = new BorderLayout();
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.NORTH, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.EAST, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.WEST, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.SOUTH, in.readUTF());
                            }
                            if (in.readBoolean()) {
                                b.defineLandscapeSwap(BorderLayout.CENTER, in.readUTF());
                            }
                            b.setAbsoluteCenter(in.readBoolean());
                            layout = b;
                            break;
                        }
                    case LAYOUT_BOX_X:
                        layout = new BoxLayout(BoxLayout.X_AXIS);
                        break;
                    case LAYOUT_BOX_Y:
                        layout = new BoxLayout(BoxLayout.Y_AXIS);
                        break;
                    case LAYOUT_FLOW_LEGACY:
                        layout = new FlowLayout();
                        break;
                    case LAYOUT_FLOW:
                        FlowLayout f = new FlowLayout();
                        f.setFillRows(in.readBoolean());
                        f.setAlign(in.readInt());
                        f.setValign(in.readInt());
                        layout = f;
                        break;
                    case LAYOUT_LAYERED:
                        layout = new LayeredLayout();
                        break;
                    case LAYOUT_GRID:
                        layout = new GridLayout(in.readInt(), in.readInt());
                        break;
                    case LAYOUT_TABLE:
                        layout = new TableLayout(in.readInt(), in.readInt());
                        break;
                }
                ((Container) cmp).setLayout(layout);
                break;
            case PROPERTY_TAB_PLACEMENT:
                ((Tabs) cmp).setTabPlacement(in.readInt());
                break;
            case PROPERTY_TAB_TEXT_POSITION:
                ((Tabs) cmp).setTabTextPosition(in.readInt());
                break;
            case PROPERTY_PREFERRED_WIDTH:
                cmp.setPreferredW(in.readInt());
                break;
            case PROPERTY_PREFERRED_HEIGHT:
                cmp.setPreferredH(in.readInt());
                break;
            case PROPERTY_UIID:
                cmp.setUIID(in.readUTF());
                break;
            case PROPERTY_DIALOG_UIID:
                ((Dialog) cmp).setDialogUIID(in.readUTF());
                break;
            case PROPERTY_DISPOSE_WHEN_POINTER_OUT:
                ((Dialog) cmp).setDisposeWhenPointerOutOfBounds(in.readBoolean());
                break;
            case PROPERTY_CLOUD_BOUND_PROPERTY:
                cmp.setCloudBoundProperty(in.readUTF());
                break;
            case PROPERTY_CLOUD_DESTINATION_PROPERTY:
                cmp.setCloudDestinationProperty(in.readUTF());
                break;
            case PROPERTY_DIALOG_POSITION:
                String pos = in.readUTF();
                if (pos.length() > 0) {
                    ((Dialog) cmp).setDialogPosition(pos);
                }
                break;
            case PROPERTY_FOCUSABLE:
                cmp.setFocusable(in.readBoolean());
                break;
            case PROPERTY_ENABLED:
                cmp.setEnabled(in.readBoolean());
                break;
            case PROPERTY_SCROLL_VISIBLE:
                cmp.setScrollVisible(in.readBoolean());
                break;
            case PROPERTY_ICON:
                ((Label) cmp).setIcon(res.getImage(in.readUTF()));
                break;
            case PROPERTY_ROLLOVER_ICON:
                ((Button) cmp).setRolloverIcon(res.getImage(in.readUTF()));
                break;
            case PROPERTY_PRESSED_ICON:
                ((Button) cmp).setPressedIcon(res.getImage(in.readUTF()));
                break;
            case PROPERTY_DISABLED_ICON:
                ((Button) cmp).setDisabledIcon(res.getImage(in.readUTF()));
                break;
            case PROPERTY_GAP:
                ((Label) cmp).setGap(in.readInt());
                break;
            case PROPERTY_VERTICAL_ALIGNMENT:
                if (cmp instanceof TextArea) {
                    ((TextArea) cmp).setVerticalAlignment(in.readInt());
                } else {
                    ((Label) cmp).setVerticalAlignment(in.readInt());
                }
                break;
            case PROPERTY_TEXT_POSITION:
                ((Label) cmp).setTextPosition(in.readInt());
                break;
            case PROPERTY_CLIENT_PROPERTIES:
                int count = in.readInt();
                StringBuilder sb = new StringBuilder();
                for (int iter = 0; iter < count; iter++) {
                    String k = in.readUTF();
                    String v = in.readUTF();
                    cmp.putClientProperty(k, v);
                    sb.append(k);
                    if (iter < count - 1) {
                        sb.append(",");
                    }
                }
                cmp.putClientProperty("cn1$Properties", sb.toString());
                break;
            case PROPERTY_NAME:
                String componentName = in.readUTF();
                cmp.setName(componentName);
                root.putClientProperty("%" + componentName + "%", cmp);
                break;
            case PROPERTY_LAYOUT_CONSTRAINT:
                if (parent.getLayout() instanceof BorderLayout) {
                    cmp.putClientProperty("layoutConstraint", in.readUTF());
                } else {
                    TableLayout tl = (TableLayout) parent.getLayout();
                    TableLayout.Constraint con = tl.createConstraint(in.readInt(), in.readInt());
                    con.setHeightPercentage(in.readInt());
                    con.setWidthPercentage(in.readInt());
                    con.setHorizontalAlign(in.readInt());
                    con.setHorizontalSpan(in.readInt());
                    con.setVerticalAlign(in.readInt());
                    con.setVerticalSpan(in.readInt());
                    cmp.putClientProperty("layoutConstraint", con);
                }
                break;
            case PROPERTY_TITLE:
                ((Form) cmp).setTitle(in.readUTF());
                break;
            case PROPERTY_COMPONENTS:
                int componentCount = in.readInt();
                if (cmp instanceof Tabs) {
                    for (int iter = 0; iter < componentCount; iter++) {
                        String tab = in.readUTF();
                        Component child = createComponent(in, (Container) cmp, root, res, componentListeners, embedded);
                        ((Tabs) cmp).addTab(tab, child);
                    }
                } else {
                    for (int iter = 0; iter < componentCount; iter++) {
                        Component child = createComponent(in, (Container) cmp, root, res, componentListeners, embedded);
                        Object con = child.getClientProperty("layoutConstraint");
                        if (con != null) {
                            ((Container) cmp).addComponent(con, child);
                        } else {
                            ((Container) cmp).addComponent(child);
                        }
                    }
                }
                break;
            case PROPERTY_COLUMNS:
                ((TextArea) cmp).setColumns(in.readInt());
                break;
            case PROPERTY_ROWS:
                ((TextArea) cmp).setRows(in.readInt());
                break;
            case PROPERTY_HINT:
                if (cmp instanceof List) {
                    ((List) cmp).setHint(in.readUTF());
                } else {
                    ((TextArea) cmp).setHint(in.readUTF());
                }
                break;
            case PROPERTY_HINT_ICON:
                if (cmp instanceof List) {
                    ((List) cmp).setHintIcon(res.getImage(in.readUTF()));
                } else {
                    ((TextArea) cmp).setHintIcon(res.getImage(in.readUTF()));
                }
                break;
            case PROPERTY_ITEM_GAP:
                ((List) cmp).setItemGap(in.readInt());
                break;
            case PROPERTY_LIST_FIXED:
                ((List) cmp).setFixedSelection(in.readInt());
                break;
            case PROPERTY_LIST_ORIENTATION:
                ((List) cmp).setOrientation(in.readInt());
                break;
            case PROPERTY_LIST_ITEMS_LEGACY:
                String[] items = new String[in.readInt()];
                for (int iter = 0; iter < items.length; iter++) {
                    items[iter] = in.readUTF();
                }
                if (!setListModel(((List) cmp))) {
                    ((List) cmp).setModel(new DefaultListModel((Object[]) items));
                }
                break;
            case PROPERTY_LIST_ITEMS:
                Object[] elements = readObjectArrayForListModel(in, res);
                if (!setListModel(((List) cmp))) {
                    ((List) cmp).setModel(new DefaultListModel(elements));
                }
                break;
            case PROPERTY_LIST_RENDERER:
                if (cmp instanceof ContainerList) {
                    ((ContainerList) cmp).setRenderer(readRendererer(res, in));
                } else {
                    ((List) cmp).setRenderer(readRendererer(res, in));
                }
                break;
            case PROPERTY_NEXT_FORM:
                String nextForm = in.readUTF();
                setNextForm(cmp, nextForm, res, root);
                break;
            case PROPERTY_COMMANDS:
                readCommands(in, cmp, res, false);
                break;
            case PROPERTY_COMMANDS_LEGACY:
                readCommands(in, cmp, res, true);
                break;
            case PROPERTY_CYCLIC_FOCUS:
                ((Form) cmp).setCyclicFocus(in.readBoolean());
                break;
            case PROPERTY_RTL:
                cmp.setRTL(in.readBoolean());
                break;
            case PROPERTY_SLIDER_THUMB:
                ((Slider) cmp).setThumbImage(res.getImage(in.readUTF()));
                break;
            case PROPERTY_INFINITE:
                ((Slider) cmp).setInfinite(in.readBoolean());
                break;
            case PROPERTY_PROGRESS:
                ((Slider) cmp).setProgress(in.readInt());
                break;
            case PROPERTY_VERTICAL:
                ((Slider) cmp).setVertical(in.readBoolean());
                break;
            case PROPERTY_EDITABLE:
                if (cmp instanceof TextArea) {
                    ((TextArea) cmp).setEditable(in.readBoolean());
                } else {
                    ((Slider) cmp).setEditable(in.readBoolean());
                }
                break;
            case PROPERTY_INCREMENTS:
                ((Slider) cmp).setIncrements(in.readInt());
                break;
            case PROPERTY_RENDER_PERCENTAGE_ON_TOP:
                ((Slider) cmp).setRenderPercentageOnTop(in.readBoolean());
                break;
            case PROPERTY_MAX_VALUE:
                ((Slider) cmp).setMaxValue(in.readInt());
                break;
            case PROPERTY_MIN_VALUE:
                ((Slider) cmp).setMinValue(in.readInt());
                break;
        }
        property = in.readInt();
    }
    postCreateComponent(cmp);
    return cmp;
}
Also used : FlowLayout(com.codename1.ui.layouts.FlowLayout) Slider(com.codename1.ui.Slider) Form(com.codename1.ui.Form) TextArea(com.codename1.ui.TextArea) ContainerList(com.codename1.ui.list.ContainerList) BoxLayout(com.codename1.ui.layouts.BoxLayout) Label(com.codename1.ui.Label) DefaultListModel(com.codename1.ui.list.DefaultListModel) Container(com.codename1.ui.Container) GridLayout(com.codename1.ui.layouts.GridLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Dialog(com.codename1.ui.Dialog) TextField(com.codename1.ui.TextField) List(com.codename1.ui.List) ContainerList(com.codename1.ui.list.ContainerList) Component(com.codename1.ui.Component) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) Vector(java.util.Vector) TableLayout(com.codename1.ui.table.TableLayout) RadioButton(com.codename1.ui.RadioButton) ActionListener(com.codename1.ui.events.ActionListener) BoxLayout(com.codename1.ui.layouts.BoxLayout) LayeredLayout(com.codename1.ui.layouts.LayeredLayout) GridLayout(com.codename1.ui.layouts.GridLayout) FlowLayout(com.codename1.ui.layouts.FlowLayout) BorderLayout(com.codename1.ui.layouts.BorderLayout) Layout(com.codename1.ui.layouts.Layout) TableLayout(com.codename1.ui.table.TableLayout) CheckBox(com.codename1.ui.CheckBox) Tabs(com.codename1.ui.Tabs)

Aggregations

Component (com.codename1.ui.Component)3 Container (com.codename1.ui.Container)3 Label (com.codename1.ui.Label)3 TextArea (com.codename1.ui.TextArea)3 BorderLayout (com.codename1.ui.layouts.BorderLayout)3 FlowLayout (com.codename1.ui.layouts.FlowLayout)3 Button (com.codename1.ui.Button)2 CheckBox (com.codename1.ui.CheckBox)2 Dialog (com.codename1.ui.Dialog)2 Form (com.codename1.ui.Form)2 List (com.codename1.ui.List)2 RadioButton (com.codename1.ui.RadioButton)2 Slider (com.codename1.ui.Slider)2 Tabs (com.codename1.ui.Tabs)2 BoxLayout (com.codename1.ui.layouts.BoxLayout)2 GridLayout (com.codename1.ui.layouts.GridLayout)2 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)2 Layout (com.codename1.ui.layouts.Layout)2 ContainerList (com.codename1.ui.list.ContainerList)2 TableLayout (com.codename1.ui.table.TableLayout)2