Search in sources :

Example 66 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class HTMLComponent method handleTableCell.

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

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

Example 67 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class HTMLComponent method newLine.

/**
 * Adds the container representing the current line to the current container and creates a new one
 */
private void newLine(int align) {
    if (curLine.getComponentCount() == 0) {
        // If no components are present, create a vertical spacing in the size of the font height
        curLine.setPreferredH(font.getHeight());
    } else if (maxSuperscript != 0) {
        // NOTE: handling this is the line level is not an ideal implementation, but other approaches will make it too complicated
        for (int i = 0; i < curLine.getComponentCount(); i++) {
            Component cmp = curLine.getComponentAt(i);
            Style style = cmp.getStyle();
            style.setMargin(Component.TOP, style.getMargin(Component.TOP) + maxSuperscript - style.getMargin(Component.BOTTOM));
            style.setMargin(Component.BOTTOM, 0);
            if (cmp instanceof HTMLLink) {
                style = cmp.getSelectedStyle();
                style.setMargin(Component.TOP, style.getMargin(Component.TOP) + maxSuperscript - style.getMargin(Component.BOTTOM));
                style.setMargin(Component.BOTTOM, 0);
                style = ((HTMLLink) cmp).getPressedStyle();
                style.setMargin(Component.TOP, style.getMargin(Component.TOP) + maxSuperscript - style.getMargin(Component.BOTTOM));
                style.setMargin(Component.BOTTOM, 0);
            }
        }
        maxSuperscript = 0;
    }
    lastWasEmpty = (curLine.getComponentCount() == 0);
    curContainer.addComponent(curLine);
    curLine = new Container();
    curLine.getStyle().setBgTransparency(0);
    if (!FIXED_WIDTH) {
        FlowLayout fl = new FlowLayout(align);
        fl.setValign(Component.BOTTOM);
        fl.setValignByRow(true);
        curLine.setLayout(fl);
    } else {
        FlowLayout fl = (FlowLayout) curLine.getLayout();
        fl.setValign(Component.BOTTOM);
    }
    curLine.setScrollableX(false);
    curLine.getStyle().setMargin(Component.LEFT, leftIndent);
    x = leftIndent;
}
Also used : Container(com.codename1.ui.Container) FlowLayout(com.codename1.ui.layouts.FlowLayout) Style(com.codename1.ui.plaf.Style) Component(com.codename1.ui.Component)

Example 68 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class HTMLComponent method addString.

/**
 * Adds the given text to the container as a label or a link.
 * The string given here does not need line breaking as this was calculated before in the calling method.
 *
 * @param str The text to display
 * @param align The current horizontal alignment
 */
private Label addString(String str, int align) {
    Label lbl = null;
    int color = textColor;
    if ((curLine.getComponentCount() == 0) && (str.startsWith(" "))) {
        // First word in paragraph ignores all spaces
        str = str.substring(1);
        if (str.length() == 0) {
            return null;
        }
    }
    if (link != null) {
        lbl = new HTMLLink(str, link, this, mainLink, linkVisited);
        color = linkColor;
        if (linkVisited) {
            color = COLOR_VISITED_LINKS;
        }
        lbl.getSelectedStyle().setFont(font.getFont());
        ((HTMLLink) lbl).getPressedStyle().setFont(font.getFont());
        if (mainLink == null) {
            mainLink = (HTMLLink) lbl;
        }
        if (accesskey != '\0') {
            // accessKeys.put(new Integer(accesskey), lbl);
            addAccessKey(accesskey, lbl, false);
            // To prevent the access key from adding again to all words of the link
            accesskey = '\0';
        }
        lbl.getSelectedStyle().setMargin(0, 0, 0, 0);
        lbl.getSelectedStyle().setPadding(0, 0, 0, 0);
        ((HTMLLink) lbl).getPressedStyle().setMargin(0, 0, 0, 0);
        ((HTMLLink) lbl).getPressedStyle().setPadding(0, 0, 0, 0);
        lbl.getSelectedStyle().setTextDecoration(textDecoration);
        ((HTMLLink) lbl).getPressedStyle().setTextDecoration(textDecoration);
    } else {
        if (labelForID != null) {
            lbl = new ForLabel(str, this, labelForID);
            if (accesskey != '\0') {
                // accessKeys.put(new Integer(accesskey), lbl);
                addAccessKey(accesskey, lbl, false);
                // To prevent the access key from adding again to all words of the link
                accesskey = '\0';
            }
            labelForID = null;
        } else {
            lbl = new Label(str);
        }
    }
    lbl.getStyle().setMargin(0, 0, 0, 0);
    lbl.getStyle().setPadding(0, 0, 0, 0);
    if (superscript > 0) {
        int margin = font.getHeight() * superscript / 2;
        lbl.getStyle().setMargin(Component.BOTTOM, margin);
        if (link != null) {
            lbl.getSelectedStyle().setMargin(Component.BOTTOM, margin);
            ((HTMLLink) lbl).getPressedStyle().setMargin(Component.BOTTOM, margin);
        }
        if (margin > maxSuperscript) {
            // The height superscript margin is saved for a later adjustment at newLine
            maxSuperscript = margin;
        }
    } else if (superscript < 0) {
        int margin = -font.getHeight() * superscript / 2;
        lbl.getStyle().setMargin(Component.TOP, margin);
        if (link != null) {
            lbl.getSelectedStyle().setMargin(Component.TOP, margin);
            ((HTMLLink) lbl).getPressedStyle().setMargin(Component.TOP, margin);
        }
    }
    lbl.getUnselectedStyle().setFgColor(color);
    lbl.getSelectedStyle().setFgColor(color);
    // lbl.setVerticalAlignment(Component.BOTTOM); //TODO - This still doesn't align as label alignment in Codename One refers to the text alignment in relation to its icon (if exists)
    lbl.getUnselectedStyle().setFont(font.getFont());
    lbl.getUnselectedStyle().setBgTransparency(0);
    lbl.setGap(0);
    lbl.setTickerEnabled(false);
    lbl.setEndsWith3Points(false);
    lbl.getUnselectedStyle().setTextDecoration(textDecoration);
    if (align != JUSTIFY) {
        // Regular Codename One labels do not support justify. We acheive justification in fixed width mode below
        lbl.setAlignment(align);
    }
    curLine.addComponent(lbl);
    if (anchor != null) {
        anchors.put(anchor, lbl);
    }
    if (FIXED_WIDTH) {
        if (align != Component.LEFT) {
            if (align == JUSTIFY) {
                // Text justification algorithm
                Vector words = getWords(str, align, false);
                if (words.size() > 1) {
                    int spaceW = font.getFont().stringWidth(" ");
                    int spacesToAdd = (width - lbl.getPreferredW()) / spaceW;
                    int spacesPerWord = spacesToAdd / (words.size() - 1);
                    int addtlSpaces = spacesToAdd % (words.size() - 1);
                    String newStr = (String) words.elementAt(0);
                    for (int i = 1; i < words.size(); i++) {
                        for (int j = 0; j < spacesPerWord; j++) {
                            newStr += ' ';
                        }
                        if (i <= addtlSpaces) {
                            newStr += ' ';
                        }
                        newStr += ' ' + (String) words.elementAt(i);
                    }
                    lbl.setText(newStr);
                }
            } else {
                lbl.setPreferredW(width);
            }
            x = width;
            newLine(align);
        } else {
            x += lbl.getPreferredW();
        }
    }
    return lbl;
}
Also used : Label(com.codename1.ui.Label) Vector(java.util.Vector)

Example 69 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class CSSEngine method setTextIndentationRecursive.

/**
 * Sets the given text indentation to the component and all its children
 * Note: This doesn't really work well with HTMLComponent.FIXED_WIDTH mode since labels there are not single words but rather the whole line, so they get pushed out of the screen
 *
 * @param cmp The component to set the indentation on
 * @param indent The indentation in pixels
 */
private void setTextIndentationRecursive(Component cmp, int indent) {
    if (cmp instanceof Container) {
        Container cont = (Container) cmp;
        if ((cont.getLayout() instanceof FlowLayout) && (cont.getComponentCount() > 0)) {
            // Note that we don't need to consider the "applicable" styles, as this is a container and will always return selected+unselected
            cont.getComponentAt(0).getUnselectedStyle().setMargin(Component.LEFT, indent);
            cont.getComponentAt(0).getSelectedStyle().setMargin(Component.LEFT, indent);
        }
        for (int i = 0; i < cont.getComponentCount(); i++) {
            setTextIndentationRecursive(cont.getComponentAt(i), indent);
        }
    }
}
Also used : Container(com.codename1.ui.Container) FlowLayout(com.codename1.ui.layouts.FlowLayout)

Example 70 with Container

use of com.codename1.ui.Container in project CodenameOne by codenameone.

the class CSSEngine method setParentsVisible.

/**
 * Turns on the visibilty of all ancestors of the given component
 *
 * @param cmp The component to work on
 */
private void setParentsVisible(Component cmp) {
    Container cont = cmp.getParent();
    while (cont != null) {
        cont.setVisible(true);
        cont = cont.getParent();
    }
}
Also used : Container(com.codename1.ui.Container)

Aggregations

Container (com.codename1.ui.Container)85 Component (com.codename1.ui.Component)65 BorderLayout (com.codename1.ui.layouts.BorderLayout)46 Style (com.codename1.ui.plaf.Style)38 ActionEvent (com.codename1.ui.events.ActionEvent)29 Form (com.codename1.ui.Form)26 Label (com.codename1.ui.Label)21 BoxLayout (com.codename1.ui.layouts.BoxLayout)21 Dimension (com.codename1.ui.geom.Dimension)20 ActionListener (com.codename1.ui.events.ActionListener)19 Button (com.codename1.ui.Button)15 FlowLayout (com.codename1.ui.layouts.FlowLayout)15 ArrayList (java.util.ArrayList)14 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)12 TextArea (com.codename1.ui.TextArea)11 Point (com.codename1.ui.geom.Point)11 Rectangle (com.codename1.ui.geom.Rectangle)11 Dialog (com.codename1.ui.Dialog)10 RadioButton (com.codename1.ui.RadioButton)10 Vector (java.util.Vector)9