Search in sources :

Example 31 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class HTMLComponent method handleInput.

/**
 * Handles the INPUT tag
 *
 * @param element The input element
 * @param align The current aligment
 */
private void handleInput(HTMLElement element, int align) {
    String type = element.getAttributeById(HTMLElement.ATTR_TYPE);
    if (type == null) {
        return;
    }
    int typeID = INPUT_TYPES.indexOf(type.toLowerCase());
    if (typeID == -1) {
        if (htmlCallback != null) {
            if (!htmlCallback.parsingError(HTMLCallback.ERROR_ATTIBUTE_VALUE_INVALID, element.getTagName(), element.getAttributeName(new Integer(HTMLElement.ATTR_TYPE)), type, "Unsupported input type '" + type + "'. Supported types: text, password, checkbox, radio, submit, reset, hidden, image")) {
                cancel();
            }
        }
        return;
    }
    String name = element.getAttributeById(HTMLElement.ATTR_NAME);
    String id = element.getAttributeById(HTMLElement.ATTR_ID);
    String value = element.getAttributeById(HTMLElement.ATTR_VALUE);
    if (value == null) {
        value = "";
    }
    Component cmp = null;
    switch(typeID) {
        case INPUT_CHECKBOX:
            CheckBox cb = new CheckBox();
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                cb.setSelected(true);
            }
            cmp = cb;
            if (curForm != null) {
                curForm.addCheckBox(name, cb, value);
            }
            break;
        case INPUT_HIDDEN:
            if (curForm != null) {
                curForm.addInput(name, value, null);
            }
            break;
        case INPUT_EMAIL:
        case INPUT_TEXT:
        case INPUT_PASSWORD:
            TextField tf = new TextField(value);
            tf.setLeftAndRightEditingTrigger(false);
            if (typeID == INPUT_PASSWORD) {
                tf.setConstraint(TextField.PASSWORD);
            }
            if (typeID == INPUT_EMAIL) {
                tf.setConstraint(TextField.EMAILADDR);
            }
            if (SUPPORT_INPUT_FORMAT) {
                HTMLInputFormat inputFormat = HTMLInputFormat.getInputFormat(element.getAttributeById(HTMLElement.ATTR_FORMAT));
                if (inputFormat != null) {
                    tf = (TextField) inputFormat.applyConstraints(tf);
                    if (curForm != null) {
                        curForm.setInputFormat(tf, inputFormat);
                    }
                }
                String emptyOk = element.getAttributeById(HTMLElement.ATTR_EMPTYOK);
                if ((emptyOk != null) && (curForm != null)) {
                    if (emptyOk.equalsIgnoreCase("true")) {
                        curForm.setEmptyOK(tf, true);
                    } else if (emptyOk.equalsIgnoreCase("false")) {
                        curForm.setEmptyOK(tf, false);
                    }
                }
            }
            int size = getInt(element.getAttributeById(HTMLElement.ATTR_SIZE));
            int maxlen = getInt(element.getAttributeById(HTMLElement.ATTR_MAXLENGTH));
            if (size == 0) {
                size = DEFAULT_TEXTFIELD_SIZE;
            }
            if (maxlen != 0) {
                tf.setMaxSize(maxlen);
                if (size > maxlen) {
                    size = maxlen;
                }
            }
            tf.setPreferredW(tf.getStyle().getFont().stringWidth("W") * size);
            tf.getSelectedStyle().setFont(font.getFont());
            tf.getUnselectedStyle().setFont(font.getFont());
            if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_READONLY) != null)) {
                tf.setEditable(false);
            }
            cmp = tf;
            if (curForm != null) {
                curForm.addInput(name, cmp, value);
                textfieldsToForms.put(tf, curForm);
            }
            break;
        case INPUT_RADIO:
            RadioButton rb = new RadioButton(" ");
            if (element.getAttributeById(HTMLElement.ATTR_CHECKED) != null) {
                rb.setSelected(true);
            }
            cmp = rb;
            if (curForm != null) {
                curForm.addRadioButton(name, rb, value);
            }
            break;
        case INPUT_RESET:
            Command resetCmd = null;
            if (curForm != null) {
                resetCmd = curForm.createResetCommand(value);
            }
            if (resetCmd == null) {
                // dummy command - no form so it won't do anything
                resetCmd = new Command(getUIManager().localize("html.reset", HTMLForm.DEFAULT_RESET_TEXT));
            }
            Button resetButton = new Button(resetCmd);
            cmp = resetButton;
            break;
        case INPUT_BUTTON:
        case INPUT_SUBMIT:
            Command submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            if (submitCmd == null) {
                // dummy command - no form so it won't do anything
                submitCmd = new Command(value.equals("") ? value = getUIManager().localize("html.submit", HTMLForm.DEFAULT_SUBMIT_TEXT) : value);
            }
            Button submitButton = new Button(submitCmd);
            cmp = submitButton;
            break;
        case // Image submit is not officially supported in XHTML-MP 1.0 but was added anyway, but pixel data submission is not supported (i.e. name.x=xx&name.y=yy)
        INPUT_IMAGE:
            submitCmd = null;
            if (curForm != null) {
                submitCmd = curForm.createSubmitCommand(name, value);
            }
            handleImage(element, align, submitCmd);
            break;
    }
    if (cmp != null) {
        if ((!PROCESS_HTML_MP1_ONLY) && (element.getAttributeById(HTMLElement.ATTR_DISABLED) != null)) {
            cmp.setEnabled(false);
        }
        String aKey = element.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
        if ((aKey != null) && (aKey.length() == 1)) {
            // accessKeys.put(new Integer(aKey.charAt(0)), cmp);
            addAccessKey(aKey.charAt(0), cmp, false);
        }
        if (eventsListener != null) {
            eventsListener.registerComponent(cmp, element);
        }
        // Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
        element.setAssociatedComponents(cmp);
        if ((curForm != null) && (curForm.action == null)) {
            // Form that submits to a forbidden link
            cmp.setEnabled(false);
        } else if (firstFocusable == null) {
            firstFocusable = cmp;
        }
        if (id != null) {
            inputFields.put(id, cmp);
        }
    }
    addCmp(cmp, align);
}
Also used : Command(com.codename1.ui.Command) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) CheckBox(com.codename1.ui.CheckBox) TextField(com.codename1.ui.TextField) RadioButton(com.codename1.ui.RadioButton) Component(com.codename1.ui.Component)

Example 32 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class HTMLComponent method processTag.

/**
 * Processes the given tag. This is the main processing method that calls all others and uses itself in a recursive manner.
 *
 * @param element The element to process
 * @param align The current alignment
 */
private void processTag(HTMLElement element, int align) {
    if ((cancelled) && (!cancelledCaught)) {
        return;
    }
    int curAlign = align;
    HTMLFont oldFont = font;
    int oldFontColor = textColor;
    for (int i = 0; i < element.getNumChildren(); i++) {
        if ((cancelled) && (!cancelledCaught)) {
            break;
        }
        HTMLElement child = (HTMLElement) element.getChildAt(i);
        // Process Tag Open
        switch(child.getTagId()) {
            case HTMLElement.TAG_TEXT:
                // String text=child.getAttributeById(HTMLElement.ATTR_TITLE);
                String text = child.getText();
                if ((curComboBox != null) && (optionTag)) {
                    // Text is inside an OPTION tag, i.e. belongs to a ComboBox
                    OptionItem oi = new OptionItem(text, optionValue);
                    curComboBox.addItem(oi);
                    if (optionSelected) {
                        curComboBox.setSelectedItem(oi);
                        if (curForm != null) {
                            curForm.setDefaultValue(curComboBox, oi);
                        }
                    }
                } else if (curTextArea != null) {
                    // Text is inside of a TEXTAREA tag
                    curTextArea.setText(text);
                    if (curForm != null) {
                        curForm.setDefaultValue(curTextArea, text);
                    }
                } else if (element.getTagId() == HTMLElement.TAG_LEGEND) {
                    // Note: this is element, i.e. the child's parent (child is TAG_TEXT, and if parent is TAG_LEGEND then we process this block)
                    if (fieldsets.size() > 0) {
                        Container fset = (Container) fieldsets.lastElement();
                        fset.getStyle().setBorder(Border.createLineBorder(1, text));
                        fset.getStyle().setPadding(Component.TOP, fset.getStyle().getFont().getHeight() + 1);
                    }
                } else if ((curTable != null) && (element.getTagId() == HTMLElement.TAG_CAPTION)) {
                    // Note: this is element, i.e. the child's parent (child is TAG_TEXT, and if parent is TAG_LEGEND then we process this block)
                    curTable.captionTextTag = child;
                } else {
                    // long startTextTime=System.currentTimeMillis();  //debug code for performance measurement
                    Vector comps = null;
                    if (preTagCount != 0) {
                        comps = showPreTagText(text, curAlign);
                    } else {
                        if (FIXED_WIDTH) {
                            comps = showTextFixedWidth(text, curAlign);
                        } else {
                            comps = showText(text, curAlign);
                        }
                    }
                    if (loadCSS) {
                        child.setAssociatedComponents(comps);
                    }
                // textTime+=(System.currentTimeMillis()-startTextTime); //debug code for performance measurement
                }
                break;
            case HTMLElement.TAG_A:
                link = child.getAttributeById(HTMLElement.ATTR_HREF);
                if ((link != null) && (docInfo == null) && (!DocumentInfo.isAbsoluteURL(link))) {
                    if (htmlCallback != null) {
                        htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, child.getTagName(), child.getAttributeName(new Integer(HTMLElement.ATTR_HREF)), link, "Disabling relative link (" + link + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                    }
                    link = null;
                }
                if ((link != null) && (htmlCallback != null)) {
                    int linkProps = htmlCallback.getLinkProperties(this, convertURL(link));
                    if ((linkProps & HTMLCallback.LINK_FORBIDDEN) != 0) {
                        link = null;
                    } else if ((linkProps & HTMLCallback.LINK_VISTED) != 0) {
                        linkVisited = true;
                    }
                }
                anchor = child.getAttributeById(HTMLElement.ATTR_NAME);
                if (link != null) {
                    String aKey = child.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
                    if ((aKey != null) && (aKey.length() == 1)) {
                        accesskey = aKey.charAt(0);
                    }
                }
                break;
            case HTMLElement.TAG_H1:
            case HTMLElement.TAG_H2:
            case HTMLElement.TAG_H3:
            case HTMLElement.TAG_H4:
            case HTMLElement.TAG_H5:
            case HTMLElement.TAG_H6:
                font = (HTMLFont) fonts.get(child.getTagName());
                if (font == null) {
                    font = oldFont;
                }
            // No break here intentionally
            case HTMLElement.TAG_P:
                curAlign = getHorizAlign(child.getAttributeById(HTMLElement.ATTR_ALIGN), align, true);
                adjustAlignment(align, curAlign);
                newLineIfNotEmpty(curAlign);
                newLineIfLastWasNotEmpty(curAlign);
                pushContainer(child);
                break;
            case HTMLElement.TAG_DIV:
            case // CENTER is practically DIV align=CENTER
            HTMLElement.TAG_CENTER:
                curAlign = child.getTagId() == HTMLElement.TAG_DIV ? getHorizAlign(child.getAttributeById(HTMLElement.ATTR_ALIGN), align, true) : Component.CENTER;
                adjustAlignment(align, curAlign);
                newLineIfNotEmpty(curAlign);
                pushContainer(child);
                break;
            case HTMLElement.TAG_FIELDSET:
                newLineIfNotEmpty(curAlign);
                Container newCont = new Container();
                newCont.setUIID("HTMLFieldset");
                if (fieldsets.size() == 0) {
                    // First fieldset shouldn't have margin
                    newCont.getStyle().setMargin(Component.LEFT, 0);
                }
                newCont.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
                curContainer.addComponent(newCont);
                fieldsets.addElement(newCont);
                curContainer = newCont;
                if (loadCSS) {
                    child.setAssociatedComponents(newCont);
                }
                break;
            case HTMLElement.TAG_BR:
                if (loadCSS) {
                    child.setAssociatedComponents(curLine);
                }
                newLine(curAlign);
                break;
            case HTMLElement.TAG_DL:
                newLineIfNotEmpty(curAlign);
                newLine(curAlign);
                pushContainer(child);
                break;
            case HTMLElement.TAG_DT:
                newLineIfNotEmpty(curAlign);
                pushContainer(child);
                break;
            case HTMLElement.TAG_UL:
            case HTMLElement.TAG_DIR:
            case HTMLElement.TAG_MENU:
                newLineIfNotEmpty(curAlign);
                ulLevel++;
                listIndent += INDENT_UL;
                if ((ulLevel == 1) && (olIndex == Integer.MIN_VALUE)) {
                    // newline only if it's the first list
                    newLine(curAlign);
                } else {
                    // extra indentation for level 2 and beyond
                    listIndent += INDENT_UL;
                }
                pushContainer(child);
                break;
            case HTMLElement.TAG_OL:
                newLineIfNotEmpty(curAlign);
                if (olIndex != Integer.MIN_VALUE) {
                    String indexStr = ORDERED_LIST_TYPE_IDENTIFIERS[listType] + "" + olIndex;
                    // new Integer(olIndex));
                    olUpperLevelIndex.addElement(indexStr);
                }
                // olIndex=1;
                olIndex = getInt(child.getAttributeById(HTMLElement.ATTR_START), 1);
                listType = getOrderedListType(child);
                if ((olUpperLevelIndex.size() == 0) && (ulLevel == 0)) {
                    // newline only if it's the first list
                    newLine(curAlign);
                } else {
                    // add indent only for second level - first one already gets it from the numbers alignment to a 4-digit number
                    listIndent += INDENT_OL;
                }
                pushContainer(child);
                break;
            case HTMLElement.TAG_LI:
                Container listItemCont = new Container(new BorderLayout());
                listItemCont.getStyle().setMargin(Component.LEFT, leftIndent + listIndent);
                curContainer.addComponent(listItemCont);
                containers.addElement(curContainer);
                HTMLListItem bullet = null;
                if (((HTMLElement) child.getParent()).getTagId() == HTMLElement.TAG_OL) {
                    olIndex = getInt(child.getAttributeById(HTMLElement.ATTR_VALUE), olIndex);
                    int itemListType = getOrderedListType(child, listType);
                    HTMLListIndex listIndex = new HTMLListIndex(olIndex, itemListType);
                    listIndex.getUnselectedStyle().setFgColor(textColor);
                    listIndex.getSelectedStyle().setFgColor(textColor);
                    listIndex.getUnselectedStyle().setFont(font.getFont());
                    bullet = listIndex;
                    // following aligns short and long numbers (assuming a 4-digit number as the maximum, as other browsers do)
                    bullet.getUnselectedStyle().setAlignment(Component.RIGHT);
                    bullet.setPreferredW(font.getFont().stringWidth("8888. "));
                } else {
                    bullet = new HTMLBullet(getUnorderedListType(child, ulLevel), font.getFont().getHeight(), textColor, this);
                }
                Container bulletCont = new Container(new BorderLayout());
                bulletCont.addComponent(BorderLayout.NORTH, bullet);
                listItemCont.addComponent(BorderLayout.WEST, bulletCont);
                Container listItemText = new Container(new BoxLayout(BoxLayout.Y_AXIS));
                listItemCont.addComponent(BorderLayout.CENTER, listItemText);
                curContainer = listItemText;
                if (loadCSS) {
                    child.setAssociatedComponents(listItemText);
                }
                break;
            case HTMLElement.TAG_BLOCKQUOTE:
                newLineIfNotEmpty(curAlign);
                updateMargin(INDENT_BLOCKQUOTE);
                newLine(curAlign);
                pushContainer(child);
                break;
            case HTMLElement.TAG_DD:
                newLineIfNotEmpty(curAlign);
                updateMargin(INDENT_DD);
                pushContainer(child);
                break;
            case HTMLElement.TAG_HR:
                newLineIfNotEmpty(curAlign);
                Label hr = new Label();
                hr.setUIID("HTMLHR");
                // hr.getStyle().setBorder(Border.createBevelRaised());
                int hrWidth = calcSize(width, child.getAttributeById(HTMLElement.ATTR_WIDTH), width, false);
                int hrHeight = getInt(child.getAttributeById(HTMLElement.ATTR_SIZE), HR_THICKNESS);
                hr.setPreferredW(hrWidth);
                hr.setPreferredH(hrHeight);
                curLine.addComponent(hr);
                newLine(curAlign);
                if (loadCSS) {
                    child.setAssociatedComponents(hr);
                }
                break;
            case HTMLElement.TAG_STYLE:
                break;
            case HTMLElement.TAG_IMG:
                handleImage(child, curAlign, null);
                break;
            case HTMLElement.TAG_PRE:
                preTagCount++;
                pushContainer(child);
            case HTMLElement.TAG_EM:
            case HTMLElement.TAG_STRONG:
            case HTMLElement.TAG_DFN:
            case HTMLElement.TAG_CODE:
            case HTMLElement.TAG_SAMP:
            case HTMLElement.TAG_KBD:
            case HTMLElement.TAG_VAR:
            case HTMLElement.TAG_CITE:
            case HTMLElement.TAG_TT:
                font = (HTMLFont) fonts.get(child.getTagName());
                if (font == null) {
                    font = oldFont;
                }
                break;
            case HTMLElement.TAG_B:
            case HTMLElement.TAG_I:
            case HTMLElement.TAG_BIG:
            case HTMLElement.TAG_SMALL:
                font = getCounterpartFont(child.getTagId(), font);
                break;
            case HTMLElement.TAG_FORM:
                curForm = new HTMLForm(this, child.getAttributeById(HTMLElement.ATTR_ACTION), child.getAttributeById(HTMLElement.ATTR_METHOD), child.getAttributeById(HTMLElement.ATTR_ENCTYPE));
                pushContainer(child);
                break;
            case HTMLElement.TAG_BUTTON:
                handleInput(child, curAlign);
                break;
            case HTMLElement.TAG_INPUT:
                handleInput(child, curAlign);
                break;
            case HTMLElement.TAG_SELECT:
                String multi = child.getAttributeById(HTMLElement.ATTR_MULTIPLE);
                // If a select tag has size specified, it will be shown as an open list, and not as Codename One combo, even if there's no multiple seection allowed
                if ((multi != null) || (child.getAttributeById(HTMLElement.ATTR_SIZE) != null)) {
                    curComboBox = new MultiComboBox(multi != null);
                    Container comboCont = new Container(new BorderLayout());
                    curComboBox.setItemGap(0);
                    comboCont.setUIID("ComboBox");
                    curComboBox.setUIID("List");
                    comboCont.addComponent(BorderLayout.CENTER, curComboBox);
                } else {
                    curComboBox = new HTMLComboBox();
                }
                String name = child.getAttributeById(HTMLElement.ATTR_NAME);
                if (curForm != null) {
                    curForm.addInput(name, curComboBox, null);
                }
                // Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
                child.setAssociatedComponents(curComboBox);
                if (eventsListener != null) {
                    eventsListener.registerComponent(curComboBox, child);
                }
                if ((!PROCESS_HTML_MP1_ONLY) && (child.getAttributeById(HTMLElement.ATTR_DISABLED) != null)) {
                    curComboBox.setEnabled(false);
                }
                break;
            case HTMLElement.TAG_OPTGROUP:
                if (curComboBox != null) {
                    String label = child.getAttributeById(HTMLElement.ATTR_LABEL);
                    if (label != null) {
                        curComboBox.addItem(label);
                    }
                }
                break;
            case HTMLElement.TAG_OPTION:
                optionTag = true;
                optionValue = child.getAttributeById(HTMLElement.ATTR_VALUE);
                if ((curComboBox != null) && (child.getAttributeById(HTMLElement.ATTR_SELECTED) != null)) {
                    optionSelected = true;
                }
                break;
            case HTMLElement.TAG_TEXTAREA:
                curTextArea = new TextArea(getInt(child.getAttributeById(HTMLElement.ATTR_ROWS), DEFAULT_TEXTAREA_ROWS), getInt(child.getAttributeById(HTMLElement.ATTR_COLS), DEFAULT_TEXTAREA_COLS));
                if (!PROCESS_HTML_MP1_ONLY) {
                    if (child.getAttributeById(HTMLElement.ATTR_DISABLED) != null) {
                        curTextArea.setEnabled(false);
                    }
                    if (child.getAttributeById(HTMLElement.ATTR_READONLY) != null) {
                        curTextArea.setEditable(false);
                    }
                }
                addCmp(curTextArea, curAlign);
                if (eventsListener != null) {
                    eventsListener.registerComponent(curTextArea, child);
                }
                // Even if CSS is off, we need to associate it for HTMLElement.getCurentValue
                child.setAssociatedComponents(curTextArea);
                String aKey = element.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
                if ((aKey != null) && (aKey.length() == 1)) {
                    // accessKeys.put(new Integer(aKey.charAt(0)), curTextArea);
                    addAccessKey(aKey.charAt(0), curTextArea, false);
                }
                break;
            case HTMLElement.TAG_Q:
                addQuote(child, curAlign, true);
                quoteTagCount++;
                break;
            case HTMLElement.TAG_TABLE:
                newLineIfNotEmpty(curAlign);
                if (curTable != null) {
                    tables.addElement(curTable);
                    HTMLTableModel newTable = new HTMLTableModel();
                    curTable = newTable;
                } else {
                    curTable = new HTMLTableModel();
                }
                // In fixed width mode we arbitrarily divide the size by a factor knowing that probably there are several cells (If we don't do it, labels inside the cell will be built up to the full width size, leaving no space for others)
                width = width / 2;
                break;
            case HTMLElement.TAG_TR:
                break;
            case HTMLElement.TAG_TH:
            case HTMLElement.TAG_TD:
                if (curTable != null) {
                    handleTableCell(child, curAlign);
                }
                break;
            case HTMLElement.TAG_LABEL:
                labelForID = child.getAttributeById(HTMLElement.ATTR_FOR);
                aKey = child.getAttributeById(HTMLElement.ATTR_ACCESSKEY);
                if ((aKey != null) && (aKey.length() == 1)) {
                    accesskey = aKey.charAt(0);
                }
                break;
            // HTML 4 tags
            case HTMLElement.TAG_FONT:
                // TODO - This will not work for nested font tags, need to either define color as a local parameter or create a vector stack
                textColor = HTMLElement.getColor(child.getAttributeById(HTMLElement.ATTR_COLOR), textColor);
                String family = child.getAttributeById(HTMLElement.ATTR_FACE);
                int size = getInt(child.getAttributeById(HTMLElement.ATTR_SIZE));
                if ((family != null) || (size != 0)) {
                    HTMLFont f = getClosestHTMLFont(family, size, 0, 0);
                    if (f != null) {
                        font = f;
                    }
                }
                break;
            case HTMLElement.TAG_U:
            case // INS (Inserted text) is rendered exactly like U (underline) in most browsers
            HTMLElement.TAG_INS:
                if (underlineCount == 0) {
                    textDecoration |= Style.TEXT_DECORATION_UNDERLINE;
                }
                underlineCount++;
                break;
            case HTMLElement.TAG_S:
            case HTMLElement.TAG_STRIKE:
            case // DEL (Deleted text) is rendered exactly like S (strikethru) in most browsers
            HTMLElement.TAG_DEL:
                if (strikethruCount == 0) {
                    textDecoration |= Style.TEXT_DECORATION_STRIKETHRU;
                }
                strikethruCount++;
                break;
            case HTMLElement.TAG_MAP:
                String mapName = child.getAttributeById(HTMLElement.ATTR_NAME);
                curImageMap = new ImageMapData(mapName);
                break;
            case HTMLElement.TAG_AREA:
                handleImageMapArea(child);
                break;
            case HTMLElement.TAG_SUP:
                superscript++;
                break;
            case HTMLElement.TAG_SUB:
                superscript--;
                break;
            case HTMLElement.TAG_TBODY:
                if (curTable != null) {
                    curTable.startSegment(HTMLTableModel.SEGMENT_TBODY);
                }
                break;
            case HTMLElement.TAG_THEAD:
                if (curTable != null) {
                    curTable.startSegment(HTMLTableModel.SEGMENT_THEAD);
                }
                break;
            case HTMLElement.TAG_TFOOT:
                if (curTable != null) {
                    curTable.startSegment(HTMLTableModel.SEGMENT_TFOOT);
                }
                break;
        }
        if (child.getNumChildren() > 0) {
            processTag(child, curAlign);
        }
        // Process close tag
        switch(child.getTagId()) {
            case HTMLElement.TAG_H1:
            case HTMLElement.TAG_H2:
            case HTMLElement.TAG_H3:
            case HTMLElement.TAG_H4:
            case HTMLElement.TAG_H5:
            case HTMLElement.TAG_H6:
                font = oldFont;
            case HTMLElement.TAG_P:
                // Restore previous alignment
                curAlign = align;
                newLineIfNotEmpty(curAlign);
                popContainer();
                newLine(curAlign);
                break;
            case HTMLElement.TAG_DIV:
            case HTMLElement.TAG_CENTER:
                // Restore previous alignment
                curAlign = align;
                newLineIfNotEmpty(curAlign);
                popContainer();
                break;
            case HTMLElement.TAG_FIELDSET:
                newLineIfNotEmpty(curAlign);
                Container fieldsetContainer = (Container) fieldsets.lastElement();
                curContainer = fieldsetContainer.getParent();
                fieldsets.removeElement(fieldsetContainer);
                break;
            case HTMLElement.TAG_BLOCKQUOTE:
                newLineIfNotEmpty(curAlign);
                newLine(curAlign);
                updateMargin(-INDENT_BLOCKQUOTE);
                popContainer();
                break;
            case HTMLElement.TAG_DT:
                popContainer();
                break;
            case HTMLElement.TAG_DD:
                newLineIfNotEmpty(curAlign);
                updateMargin(-INDENT_DD);
                popContainer();
                break;
            case HTMLElement.TAG_DL:
                newLine(curAlign);
                popContainer();
                break;
            case HTMLElement.TAG_A:
                link = null;
                linkVisited = false;
                mainLink = null;
                anchor = null;
                accesskey = '\0';
                break;
            case HTMLElement.TAG_UL:
            case HTMLElement.TAG_DIR:
            case HTMLElement.TAG_MENU:
                ulLevel--;
                if ((ulLevel == 0) && (olIndex == Integer.MIN_VALUE)) {
                    newLine(curAlign);
                } else {
                    // level 2 and beyond got extra indentation, so we remove it here
                    listIndent -= INDENT_UL;
                }
                listIndent -= INDENT_UL;
                popContainer();
                break;
            case HTMLElement.TAG_OL:
                if (olUpperLevelIndex.size() != 0) {
                    String indexStr = (String) olUpperLevelIndex.lastElement();
                    olUpperLevelIndex.removeElementAt(olUpperLevelIndex.size() - 1);
                    listType = getOrderedListType(indexStr.charAt(0), HTMLListIndex.LIST_NUMERIC);
                    olIndex = getInt(indexStr.substring(1));
                    // First level of ordered list doesn't get indentation, so we substract only if it's nested
                    listIndent -= INDENT_OL;
                } else {
                    olIndex = Integer.MIN_VALUE;
                }
                if ((olIndex == Integer.MIN_VALUE) && (ulLevel == 0)) {
                    // new line only if it is the last nested list
                    newLine(curAlign);
                }
                popContainer();
                break;
            case HTMLElement.TAG_LI:
                if (olIndex != Integer.MIN_VALUE) {
                    olIndex++;
                }
                newLineIfNotEmpty(curAlign);
                // We can't use popContainer, since with LI the container is pushed even when CSS is ignored, to provide the spacing between the list item bullet/number and the text (in a nested way if needed)
                Container prevContainer = (Container) containers.lastElement();
                curContainer = prevContainer;
                containers.removeElement(curContainer);
                // curContainer=listItemParentContainer;
                break;
            case HTMLElement.TAG_PRE:
                preTagCount--;
                popContainer();
            case HTMLElement.TAG_FONT:
                textColor = oldFontColor;
            case HTMLElement.TAG_EM:
            case HTMLElement.TAG_STRONG:
            case HTMLElement.TAG_DFN:
            case HTMLElement.TAG_CODE:
            case HTMLElement.TAG_SAMP:
            case HTMLElement.TAG_KBD:
            case HTMLElement.TAG_VAR:
            case HTMLElement.TAG_CITE:
            case HTMLElement.TAG_B:
            case HTMLElement.TAG_I:
            case HTMLElement.TAG_BIG:
            case HTMLElement.TAG_SMALL:
            case HTMLElement.TAG_TT:
                font = oldFont;
                break;
            case HTMLElement.TAG_FORM:
                if ((curForm != null) && (!curForm.hasSubmitButton) && (curForm.getNumFields() > 0)) {
                    // This is a fix for forms with no submit buttons which can be resulted due to the fact XHTML-MP doesn't support the BUTTON tag and also input type button with javascript
                    Button submitButton = new Button(curForm.createSubmitCommand(null, null));
                    addCmp(submitButton, curAlign);
                }
                curForm = null;
                popContainer();
                break;
            case HTMLElement.TAG_TEXTAREA:
                String name = child.getAttributeById(HTMLElement.ATTR_NAME);
                if (curForm != null) {
                    // This was moved to the end tag to enable auto complete support (i.e. if there's an autocomplete it overrides the default value)
                    curForm.addInput(name, curTextArea, null);
                }
                curTextArea = null;
                break;
            case HTMLElement.TAG_SELECT:
                if (curComboBox instanceof MultiComboBox) {
                    Container comboCont = curComboBox.getParent();
                    int minSize = Math.min(MIN_MULTI_COMBOBOX_ITEMS, curComboBox.size());
                    int maxSize = Math.min(curComboBox.size(), MAX_MULTI_COMBOBOX_ITEMS);
                    int size = Math.min(maxSize, Math.max(getInt(child.getAttributeById(HTMLElement.ATTR_SIZE)), minSize));
                    Component renderCmp = curComboBox.getRenderer().getListCellRendererComponent(curComboBox, "X", 0, false);
                    comboCont.setPreferredH((renderCmp.getPreferredH() + renderCmp.getStyle().getMargin(Component.TOP) + renderCmp.getStyle().getMargin(Component.BOTTOM) + curComboBox.getItemGap()) * size + curComboBox.getStyle().getPadding(Component.TOP) + curComboBox.getStyle().getPadding(Component.BOTTOM));
                    addCmp(comboCont, curAlign);
                } else {
                    addCmp(curComboBox, curAlign);
                }
                curComboBox = null;
                break;
            case HTMLElement.TAG_OPTION:
                optionTag = false;
                optionSelected = false;
                optionValue = null;
                break;
            case HTMLElement.TAG_Q:
                quoteTagCount--;
                addQuote(child, curAlign, false);
                break;
            case HTMLElement.TAG_TABLE:
                newLineIfNotEmpty(curAlign);
                // For a case that TR was not closed properly
                curTable.commitRowIfNotEmpty();
                if (curTable.getRowCount() != 0) {
                    // Don't add an empty table (Creates an exception in TableLayout and useless)
                    /*if (TABLES_LOCK_SIZE) {
                            for(int r=0;r<curTable.getRowCount();r++) {
                                for(int c=0;c<curTable.getColumnCount();c++) {
                                    Component cmp=(Component)curTable.getValueAt(r, c);
                                    if (cmp!=null) { // Can be null for cells that are "spanned over"
                                        cmp.setPreferredSize(cmp.getPreferredSize());
                                    }
                                }
                            }
                        }*/
                    HTMLTable table = new HTMLTable(curTable);
                    table.getStyle().setBgTransparency(0);
                    if (loadCSS) {
                        child.setAssociatedComponents(table);
                    }
                    int borderSize = getInt(child.getAttributeById(HTMLElement.ATTR_BORDER));
                    int[] borderPad = new int[4];
                    if (borderSize > 0) {
                        int frame = PROCESS_HTML_MP1_ONLY ? -1 : HTMLUtils.getStringVal(child.getAttributeById(HTMLElement.ATTR_FRAME), HTMLElement.ALLOWED_TABLE_FRAME_STRINGS);
                        Border border = Border.createLineBorder(borderSize);
                        if (frame == -1) {
                            for (int s = 0; s < borderPad.length; s++) {
                                borderPad[s] = borderSize;
                            }
                        } else {
                            Border[] borders = new Border[4];
                            for (int j = 0; j < HTMLElement.ALLOWED_TABLE_FRAME_VALS[frame].length; j++) {
                                int side = HTMLElement.ALLOWED_TABLE_FRAME_VALS[frame][j];
                                borders[side] = border;
                                borderPad[side] = borderSize;
                            }
                            border = Border.createCompoundBorder(borders[Component.TOP], borders[Component.BOTTOM], borders[Component.LEFT], borders[Component.RIGHT]);
                        }
                        table.getUnselectedStyle().setBorder(border);
                        table.getSelectedStyle().setBorder(border);
                        table.getUnselectedStyle().setPadding(borderPad[Component.TOP], borderPad[Component.BOTTOM], borderPad[Component.LEFT], borderPad[Component.RIGHT]);
                        table.getSelectedStyle().setPadding(borderPad[Component.TOP], borderPad[Component.BOTTOM], borderPad[Component.LEFT], borderPad[Component.RIGHT]);
                    } else {
                        table.getUnselectedStyle().setBorder(null);
                        table.getSelectedStyle().setBorder(null);
                        table.setDrawBorder(false);
                    }
                    if (!PROCESS_HTML_MP1_ONLY) {
                        int rules = HTMLUtils.getStringVal(child.getAttributeById(HTMLElement.ATTR_RULES), HTMLElement.ALLOWED_TABLE_RULES_STRINGS, Table.INNER_BORDERS_ALL);
                        table.setInnerBorderMode(rules);
                        int spacing = getInt(child.getAttributeById(HTMLElement.ATTR_CELLSPACING), -1);
                        if (spacing != -1) {
                            table.setBorderSpacing(spacing, spacing);
                        }
                        int padding = getInt(child.getAttributeById(HTMLElement.ATTR_CELLPADDING), -1);
                        if (padding != -1) {
                            for (int r = 0; r < curTable.getRowCount(); r++) {
                                for (int c = 0; c < curTable.getColumnCount(); c++) {
                                    Component cmp = (Component) curTable.getValueAt(r, c);
                                    if (cmp != null) {
                                        // Can be null for cells that are "spanned over"
                                        cmp.getUnselectedStyle().setPadding(padding, padding, padding, padding);
                                        cmp.getSelectedStyle().setPadding(padding, padding, padding, padding);
                                    }
                                }
                            }
                        }
                    }
                    if (curTable.captionTextTag != null) {
                        Container captionedTable = new Container(new BoxLayout(BoxLayout.Y_AXIS));
                        TextArea caption = new TextArea(curTable.captionTextTag.getText());
                        curTable.captionTextTag.setAssociatedComponents(caption);
                        caption.setUIID("HTMLTableCaption");
                        caption.setEditable(false);
                        caption.setFocusable(false);
                        caption.getStyle().setBorder(null);
                        caption.getStyle().setAlignment(Component.CENTER);
                        captionedTable.addComponent(caption);
                        captionedTable.addComponent(table);
                        addCmp(captionedTable, curAlign);
                    } else {
                        addCmp(table, curAlign);
                    }
                    newLineIfNotEmpty(curAlign);
                }
                if (tables.size() == 0) {
                    curTable = null;
                } else {
                    curTable = (HTMLTableModel) tables.lastElement();
                    tables.removeElement(curTable);
                }
                // In fixed width mode we arbitrarily divide the size by a factor knowing that probably there are several cells - here we restore the size back
                width = width * 2;
                if (width > displayWidth) {
                    width = displayWidth;
                }
                break;
            case HTMLElement.TAG_TR:
                if (curTable != null) {
                    curTable.commitRow();
                }
                break;
            case HTMLElement.TAG_TH:
            case HTMLElement.TAG_TD:
                if (curTable != null) {
                    newLineIfNotEmpty(curAlign);
                    curContainer = (Container) tableCells.lastElement();
                    tableCells.removeElement(curContainer);
                }
                break;
            case HTMLElement.TAG_LABEL:
                labelForID = null;
                accesskey = '\0';
                break;
            // HTML 4 tags
            case HTMLElement.TAG_U:
            case HTMLElement.TAG_INS:
                underlineCount--;
                if (underlineCount == 0) {
                    textDecoration -= Style.TEXT_DECORATION_UNDERLINE;
                }
                break;
            case HTMLElement.TAG_S:
            case HTMLElement.TAG_STRIKE:
            case HTMLElement.TAG_DEL:
                strikethruCount--;
                if (strikethruCount == 0) {
                    textDecoration -= Style.TEXT_DECORATION_STRIKETHRU;
                }
                break;
            case HTMLElement.TAG_MAP:
                if (curImageMap != null) {
                    if (imageMapData == null) {
                        imageMapData = new Hashtable();
                    }
                    imageMapData.put(curImageMap.name, curImageMap);
                    if ((imageMapComponents != null) && (imageMapComponents.containsKey(curImageMap.name))) {
                        HTMLImageMap imageMap = (HTMLImageMap) imageMapComponents.get(curImageMap.name);
                        imageMap.mapData = curImageMap;
                    }
                    curImageMap = null;
                }
                break;
            case HTMLElement.TAG_SUP:
                superscript--;
                break;
            case HTMLElement.TAG_SUB:
                superscript++;
                break;
            case HTMLElement.TAG_TBODY:
            case HTMLElement.TAG_THEAD:
            case HTMLElement.TAG_TFOOT:
                if (curTable != null) {
                    curTable.endSegment();
                }
                break;
        }
    }
}
Also used : TextArea(com.codename1.ui.TextArea) BoxLayout(com.codename1.ui.layouts.BoxLayout) 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) Component(com.codename1.ui.Component) Vector(java.util.Vector) Hashtable(java.util.Hashtable) Border(com.codename1.ui.plaf.Border)

Example 33 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class Border method paint.

void paint(Graphics g, int x, int y, int width, int height, Component c) {
    int originalColor = g.getColor();
    if (!themeColors) {
        g.setColor(colorA);
    }
    int ac = 1;
    if (thickness > 0) {
        if (millimeters) {
            ac = Display.getInstance().convertToPixels(thickness);
        } else {
            ac = (int) thickness;
        }
    }
    switch(type) {
        case TYPE_LINE:
            if (borderTitle == null) {
                if (millimeters) {
                    g.drawRect(x, y, width, height, ac);
                } else {
                    g.drawRect(x, y, width, height, ac);
                }
            } else {
                Font f = c.getStyle().getFont();
                int titleW = f.stringWidth(borderTitle);
                int topPad = c.getStyle().getPaddingTop();
                int topY = y + (topPad - ac) / 2;
                if (c.isRTL()) {
                    // top (segment before the title)
                    g.fillRect(x + width - TITLE_MARGIN, topY, TITLE_MARGIN, ac);
                    // top (segment after the title)
                    g.fillRect(x, topY, width - (TITLE_MARGIN + titleW + TITLE_SPACE * 2), ac);
                    g.drawString(borderTitle, x + width - (TITLE_MARGIN + titleW + TITLE_SPACE), y + (topPad - f.getHeight()) / 2);
                } else {
                    // top (segment before the title)
                    g.fillRect(x, topY, TITLE_MARGIN, ac);
                    // top (segment after the title)
                    g.fillRect(x + TITLE_MARGIN + titleW + TITLE_SPACE * 2, topY, width - (TITLE_MARGIN + titleW + TITLE_SPACE * 2), ac);
                    g.drawString(borderTitle, x + TITLE_MARGIN + TITLE_SPACE, y + (topPad - f.getHeight()) / 2);
                }
                // bottom
                g.fillRect(x, y + height - ac, width, ac);
                // left
                g.fillRect(x, topY, ac, height);
                // right
                g.fillRect(x + width - ac, topY, ac, height);
            }
            break;
        case TYPE_DASHED:
        case TYPE_DOTTED:
            int segWidth = ac;
            if (type == TYPE_DASHED) {
                segWidth = ac * 3;
            }
            int ix = x;
            for (; ix < x + width; ix += segWidth * 2) {
                g.fillRect(ix, y, segWidth, ac);
                g.fillRect(ix, y + height - ac, segWidth, ac);
            }
            if (ix - segWidth < x + width) {
                // fill in the gap if any
                g.fillRect(ix - segWidth, y, x + width - ix + segWidth, ac);
                g.fillRect(ix - segWidth, y + height - ac, x + width - ix + segWidth, ac);
            }
            int iy = y;
            for (; iy < y + height; iy += segWidth * 2) {
                g.fillRect(x, iy, ac, segWidth);
                g.fillRect(x + width - ac, iy, ac, segWidth);
            }
            if (iy - segWidth < y + height) {
                // fill in the gap if any
                g.fillRect(x, iy - segWidth, ac, y + height - iy + segWidth);
                g.fillRect(x + width - ac, iy - segWidth, ac, y + height - iy + segWidth);
            }
            break;
        case TYPE_DOUBLE:
            width--;
            height--;
            for (int iter = 0; iter < ac; iter++) {
                if ((iter * 100 / ac <= 33) || (iter * 100 / ac >= 66)) {
                    g.drawRect(x + iter, y + iter, width, height);
                }
                width -= 2;
                height -= 2;
            }
            break;
        case TYPE_INSET:
        case TYPE_OUTSET:
            for (int i = 0; i < ac; i++) {
                g.drawLine(x + i, y + i, x + i, y + height - i);
                g.drawLine(x + i, y + i, x + width - i, y + i);
            }
            if (type == TYPE_INSET) {
                g.lighterColor(50);
            } else {
                g.darkerColor(50);
            }
            for (int i = 0; i < ac; i++) {
                g.drawLine(x + i, y + height - i, x + width - i, y + height - i);
                g.drawLine(x + width - i, y + i, x + width - i, y + height - i);
            }
            break;
        case TYPE_GROOVE:
        case TYPE_RIDGE:
            for (int i = 0; i < ac / 2; i++) {
                g.drawLine(x + i, y + i, x + i, y + height - i);
                g.drawLine(x + i, y + i, x + width - i, y + i);
            }
            for (int i = ac / 2; i < ac; i++) {
                g.drawLine(x + i, y + height - i, x + width - i, y + height - i);
                g.drawLine(x + width - i, y + i, x + width - i, y + height - i);
            }
            if (type == TYPE_GROOVE) {
                g.lighterColor(50);
            } else {
                g.darkerColor(50);
            }
            for (int i = 0; i < ac / 2; i++) {
                g.drawLine(x + i, y + height - i, x + width - i, y + height - i);
                g.drawLine(x + width - i, y + i, x + width - i, y + height - i);
            }
            for (int i = ac / 2; i < ac; i++) {
                g.drawLine(x + i, y + i, x + i, y + height - i);
                g.drawLine(x + i, y + i, x + width - i, y + i);
            }
            break;
        case TYPE_ROUNDED_PRESSED:
            x++;
            y++;
            width -= 2;
            height -= 2;
        case TYPE_ROUNDED:
            width--;
            height--;
            if (outline) {
                g.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
            }
            break;
        case TYPE_ETCHED_LOWERED:
        case TYPE_ETCHED_RAISED:
            g.drawRect(x, y, width - 2, height - 2);
            if (themeColors) {
                if (type == TYPE_ETCHED_LOWERED) {
                    g.lighterColor(40);
                } else {
                    g.darkerColor(40);
                }
            } else {
                g.setColor(colorB);
            }
            g.drawLine(x + 1, y + height - 3, x + 1, y + 1);
            g.drawLine(x + 1, y + 1, x + width - 3, y + 1);
            g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
            g.drawLine(x + width - 1, y + height - 1, x + width - 1, y);
            break;
        case TYPE_BEVEL_RAISED:
            if (themeColors) {
                g.setColor(getBackgroundColor(c));
                g.lighterColor(50);
            } else {
                g.setColor(colorA);
            }
            g.drawLine(x, y, x, y + height - 2);
            g.drawLine(x + 1, y, x + width - 2, y);
            if (themeColors) {
                g.darkerColor(25);
            } else {
                g.setColor(colorB);
            }
            g.drawLine(x + 1, y + 1, x + 1, y + height - 3);
            g.drawLine(x + 2, y + 1, x + width - 3, y + 1);
            if (themeColors) {
                g.darkerColor(50);
            } else {
                g.setColor(colorC);
            }
            g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
            g.drawLine(x + width - 1, y, x + width - 1, y + height - 2);
            if (themeColors) {
                g.darkerColor(25);
            } else {
                g.setColor(colorD);
            }
            g.drawLine(x + 1, y + height - 2, x + width - 2, y + height - 2);
            g.drawLine(x + width - 2, y + 1, x + width - 2, y + height - 3);
            break;
        case TYPE_UNDERLINE:
            g.fillRect(x, y + height - ac - 1, width, ac);
            break;
        case TYPE_BEVEL_LOWERED:
            if (themeColors) {
                g.setColor(getBackgroundColor(c));
                g.darkerColor(50);
            } else {
                g.setColor(colorD);
            }
            g.drawLine(x, y, x, y + height - 1);
            g.drawLine(x + 1, y, x + width - 1, y);
            if (themeColors) {
                g.lighterColor(25);
            } else {
                g.setColor(colorC);
            }
            g.drawLine(x + 1, y + 1, x + 1, y + height - 2);
            g.drawLine(x + 2, y + 1, x + width - 2, y + 1);
            if (themeColors) {
                g.lighterColor(50);
            } else {
                g.setColor(colorC);
            }
            g.drawLine(x + 1, y + height - 1, x + width - 1, y + height - 1);
            g.drawLine(x + width - 1, y + 1, x + width - 1, y + height - 2);
            if (themeColors) {
                g.lighterColor(25);
            } else {
                g.setColor(colorA);
            }
            g.drawLine(x + 2, y + height - 2, x + width - 2, y + height - 2);
            g.drawLine(x + width - 2, y + 2, x + width - 2, y + height - 3);
            break;
        case TYPE_COMPOUND:
            Style style = c.getStyle();
            boolean drawLeft = true;
            boolean drawRight = true;
            if (c.getUIManager().getLookAndFeel().isRTL()) {
                boolean temp = drawLeft;
                drawLeft = drawRight;
                drawRight = temp;
            }
            Border top = compoundBorders[Component.TOP];
            Border bottom = compoundBorders[Component.BOTTOM];
            Border left = compoundBorders[Component.LEFT];
            Border right = compoundBorders[Component.RIGHT];
            int topThickness = 0;
            int bottomThickness = 0;
            if (top != null) {
                Rectangle clip = saveClip(g);
                // g.pushClip();
                topThickness = (int) top.thickness;
                g.clipRect(x, y, width, topThickness);
                // top.paint(g, c);
                top.paint(g, x, y, width, height, c);
                restoreClip(g, clip);
            // g.popClip();
            }
            if (bottom != null) {
                Rectangle clip = saveClip(g);
                // g.pushClip();
                bottomThickness = (int) bottom.thickness;
                g.clipRect(x, y + height - bottomThickness, width, bottomThickness);
                // bottom.paint(g, c);
                bottom.paint(g, x, y, width, height, c);
                restoreClip(g, clip);
            // g.popClip();
            }
            if ((drawLeft) && (left != null)) {
                Rectangle clip = saveClip(g);
                // g.pushClip();
                g.clipRect(x, y + topThickness, (int) left.thickness, height - topThickness - bottomThickness);
                // left.paint(g, c);
                left.paint(g, x, y, width, height, c);
                restoreClip(g, clip);
            // g.popClip();
            }
            if ((drawRight) && (right != null)) {
                Rectangle clip = saveClip(g);
                // g.pushClip();
                g.clipRect(x + width - (int) right.thickness, y + topThickness, (int) right.thickness, height - topThickness - bottomThickness);
                // right.paint(g, c);
                right.paint(g, x, y, width, height, c);
                restoreClip(g, clip);
            // g.popClip();
            }
            break;
        case TYPE_IMAGE:
        case TYPE_IMAGE_SCALED:
        case TYPE_IMAGE_HORIZONTAL:
        case TYPE_IMAGE_VERTICAL:
            break;
    }
    g.setColor(originalColor);
}
Also used : Rectangle(com.codename1.ui.geom.Rectangle) Font(com.codename1.ui.Font)

Example 34 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class Border method paintBorderBackground.

private void paintBorderBackground(Graphics g, final int xParameter, final int yParameter, final int widthParameter, final int heightParameter, Component c) {
    int originalColor = g.getColor();
    int x = xParameter;
    int y = yParameter;
    int width = widthParameter;
    int height = heightParameter;
    switch(type) {
        case TYPE_ROUNDED_PRESSED:
            x++;
            y++;
            width -= 2;
            height -= 2;
        case TYPE_ROUNDED:
            // Removing this due to issue 301, not sure regarding this...
            // width--;
            // height--;
            // rounded is also responsible for drawing the background
            Style s = c.getStyle();
            if ((s.getBgImage() != null && s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) || s.getBackgroundType() > 1) {
                Object w = s.roundRectCache;
                Image i = null;
                if (w != null) {
                    i = (Image) Display.getInstance().extractHardRef(w);
                }
                if (i != null && i.getWidth() == width && i.getHeight() == height) {
                    g.drawImage(i, x, y);
                } else {
                    // we need to draw a background image!
                    i = ImageFactory.createImage(c, width, height, 0);
                    Graphics imageG = i.getGraphics();
                    imageG.setColor(0);
                    imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                    int[] rgb = i.getRGBCached();
                    int transColor = rgb[0];
                    int[] imageRGB;
                    if (s.getBackgroundType() == Style.BACKGROUND_IMAGE_SCALED) {
                        imageRGB = s.getBgImage().scaled(width, height).getRGBCached();
                    } else {
                        Image bgPaint = ImageFactory.createImage(c, width, height, 0);
                        Painter p = s.getBgPainter();
                        // might occur during temporary conditions in the theme switching
                        if (p == null) {
                            return;
                        }
                        p.paint(bgPaint.getGraphics(), new Rectangle(0, 0, width, height));
                        imageRGB = bgPaint.getRGB();
                    }
                    int rlen = rgb.length;
                    for (int iter = 0; iter < rlen; iter++) {
                        if (rgb[iter] == transColor) {
                            imageRGB[iter] = 0;
                        }
                    }
                    i = Image.createImage(imageRGB, width, height);
                    s.roundRectCache = Display.getInstance().createSoftWeakRef(i);
                    g.drawImage(i, x, y);
                }
            } else {
                int foreground = g.getColor();
                g.setColor(s.getBgColor());
                // Its opaque much easier job!
                if (s.getBgTransparency() == ((byte) 0xff)) {
                    g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                } else {
                    if (g.isAlphaSupported()) {
                        int alpha = g.getAlpha();
                        g.setAlpha(s.getBgTransparency() & 0xff);
                        g.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
                        g.setAlpha(alpha);
                    } else {
                        // translucent... well....
                        if (s.getBgTransparency() != 0) {
                            Image i = ImageFactory.createImage(c, width, height, 0);
                            int[] imageRgb;
                            if (g.getColor() != 0xffffff) {
                                Graphics imageG = i.getGraphics();
                                imageG.setColor(g.getColor());
                                imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                                imageRgb = i.getRGBCached();
                            } else {
                                // background color is white we need to remove a different color
                                // black is the only other "reliable" color on the device
                                Graphics imageG = i.getGraphics();
                                imageG.setColor(0);
                                imageG.fillRect(0, 0, width, height);
                                imageG.setColor(g.getColor());
                                imageG.fillRoundRect(0, 0, width, height, arcWidth, arcHeight);
                                imageRgb = i.getRGBCached();
                            }
                            int removeColor = imageRgb[0];
                            int size = width * height;
                            int alphaInt = ((s.getBgTransparency() & 0xff) << 24) & 0xff000000;
                            for (int iter = 0; iter < size; iter++) {
                                if (removeColor == imageRgb[iter]) {
                                    imageRgb[iter] = 0;
                                    continue;
                                }
                                if ((imageRgb[iter] & 0xff000000) != 0) {
                                    imageRgb[iter] = (imageRgb[iter] & 0xffffff) | alphaInt;
                                }
                            }
                            g.drawImage(new RGBImage(imageRgb, width, height), x, y);
                        }
                    }
                }
                g.setColor(foreground);
            }
            break;
        case TYPE_IMAGE:
            {
                Image topLeft = images[4];
                Image topRight = images[5];
                Image bottomLeft = images[6];
                Image center = images[8];
                x += topLeft.getWidth();
                y += topLeft.getHeight();
                height -= (topLeft.getHeight() + bottomLeft.getHeight());
                width -= (topLeft.getWidth() + topRight.getWidth());
                if (center != null) {
                    g.tileImage(center, x, y, width, height);
                }
                Image top = images[0];
                Image bottom = images[1];
                Image left = images[2];
                Image right = images[3];
                Image bottomRight = images[7];
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                g.drawImage(topLeft, x, y);
                g.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight());
                g.drawImage(topRight, x + width - topRight.getWidth(), y);
                g.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight());
                Image arrowDownImage = null;
                Image arrowUpImage = null;
                Image arrowLeftImage = null;
                Image arrowRightImage = null;
                int arrowPosition = 0;
                // we need to draw an arrow on one of the sides
                if (trackComponent != null && specialTile != null) {
                    int cabsY = c.getAbsoluteY();
                    int trackY = trackComponent.getY();
                    int trackX = trackComponent.getX();
                    int cabsX = c.getAbsoluteX();
                    if (cabsY >= trackY + trackComponent.getHeight()) {
                        // we are below the component
                        arrowUpImage = specialTile[0];
                        arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowUpImage.getWidth() / 2;
                    } else {
                        if (cabsY + c.getHeight() <= trackY) {
                            // we are above the component
                            arrowDownImage = specialTile[1];
                            arrowPosition = (trackX + trackComponent.getWidth() / 2) - cabsX - arrowDownImage.getWidth() / 2;
                        } else {
                            if (cabsX >= trackX + trackComponent.getWidth()) {
                                // we are to the right of the component
                                arrowLeftImage = specialTile[2];
                                arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowLeftImage.getHeight() / 2;
                            } else {
                                if (cabsX + c.getWidth() <= trackX) {
                                    // we are to the left of the component
                                    arrowRightImage = specialTile[3];
                                    arrowPosition = (trackY + trackComponent.getHeight() / 2) - cabsY - arrowRightImage.getHeight() / 2;
                                }
                            }
                        }
                    }
                }
                drawImageBorderLine(g, topLeft, topRight, top, x, y, width, arrowUpImage, arrowPosition, false);
                drawImageBorderLine(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width, arrowDownImage, arrowPosition, true);
                drawImageBorderColumn(g, topLeft, bottomLeft, left, x, y, height, arrowLeftImage, arrowPosition, false);
                drawImageBorderColumn(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height, arrowRightImage, arrowPosition, true);
                break;
            }
        case TYPE_IMAGE_SCALED:
            {
                int clipX = g.getClipX();
                int clipY = g.getClipY();
                int clipWidth = g.getClipWidth();
                int clipHeight = g.getClipHeight();
                // g.pushClip();
                Image topLeft = images[4];
                Image topRight = images[5];
                Image bottomLeft = images[6];
                Image center = images[8];
                x += topLeft.getWidth();
                y += topLeft.getHeight();
                height -= (topLeft.getHeight() + bottomLeft.getHeight());
                width -= (topLeft.getWidth() + topRight.getWidth());
                g.clipRect(x, y, width, height);
                if (center != null && width > 0 && height > 0) {
                    int centerWidth = center.getWidth();
                    int centerHeight = center.getHeight();
                    g.drawImage(center, x, y, width, height);
                }
                Image top = images[0];
                Image bottom = images[1];
                Image left = images[2];
                Image right = images[3];
                Image bottomRight = images[7];
                g.setClip(clipX, clipY, clipWidth, clipHeight);
                // g.popClip();
                // g.pushClip();
                x = xParameter;
                y = yParameter;
                width = widthParameter;
                height = heightParameter;
                g.drawImage(topLeft, x, y);
                g.drawImage(bottomLeft, x, y + height - bottomLeft.getHeight());
                g.drawImage(topRight, x + width - topRight.getWidth(), y);
                g.drawImage(bottomRight, x + width - bottomRight.getWidth(), y + height - bottomRight.getHeight());
                drawImageBorderLineScale(g, topLeft, topRight, top, x, y, width);
                drawImageBorderLineScale(g, bottomLeft, bottomRight, bottom, x, y + height - bottom.getHeight(), width);
                drawImageBorderColumnScale(g, topLeft, bottomLeft, left, x, y, height);
                drawImageBorderColumnScale(g, topRight, bottomRight, right, x + width - right.getWidth(), y, height);
                g.setClip(clipX, clipY, clipWidth, clipHeight);
                // g.popClip();
                break;
            }
        case TYPE_IMAGE_HORIZONTAL:
            {
                Image left = images[0];
                Image right = images[1];
                Image center = images[2];
                Boolean centerAlignHBorderBool = c == null ? null : (Boolean) c.getClientProperty("@centerAlignHBorderBool");
                boolean b = centerAlignHBorderBool == null ? false : centerAlignHBorderBool;
                if (b || c.getUIManager().isThemeConstant("centerAlignHBorderBool", false)) {
                    y += Math.max(0, height / 2 - center.getHeight() / 2);
                }
                g.drawImage(left, x, y);
                g.drawImage(right, x + width - right.getWidth(), y);
                g.tileImage(center, x + left.getWidth(), y, width - left.getWidth() - right.getWidth(), center.getHeight());
                break;
            }
        case TYPE_IMAGE_VERTICAL:
            {
                Image top = images[0];
                Image bottom = images[1];
                Image center = images[2];
                g.drawImage(top, x, y);
                g.drawImage(bottom, x, y + height - bottom.getHeight());
                g.tileImage(center, x, y + top.getHeight(), center.getWidth(), height - top.getHeight() - bottom.getHeight());
                break;
            }
    }
    g.setColor(originalColor);
}
Also used : Graphics(com.codename1.ui.Graphics) Painter(com.codename1.ui.Painter) Rectangle(com.codename1.ui.geom.Rectangle) RGBImage(com.codename1.ui.RGBImage) Image(com.codename1.ui.Image) RGBImage(com.codename1.ui.RGBImage)

Example 35 with Switch

use of com.codename1.components.Switch in project CodenameOne by codenameone.

the class DefaultLookAndFeel method drawTextFieldCursor.

/**
 * {@inheritDoc}
 */
public void drawTextFieldCursor(Graphics g, TextArea ta) {
    Style style = ta.getStyle();
    Font f = style.getFont();
    int cursorY;
    if (ta.isSingleLineTextArea()) {
        switch(ta.getVerticalAlignment()) {
            case Component.BOTTOM:
                cursorY = ta.getY() + ta.getHeight() - f.getHeight();
                break;
            case Component.CENTER:
                cursorY = ta.getY() + ta.getHeight() / 2 - f.getHeight() / 2;
                break;
            default:
                cursorY = ta.getY() + style.getPaddingTop();
                break;
        }
    } else {
        cursorY = ta.getY() + style.getPaddingTop() + ta.getCursorY() * (ta.getRowsGap() + f.getHeight());
    }
    int cursorX = getTextFieldCursorX(ta);
    int align = reverseAlignForBidi(ta);
    int x = 0;
    if (align == Component.RIGHT) {
        String inputMode = ta.getInputMode();
        int inputModeWidth = f.stringWidth(inputMode);
        int baseX = ta.getX() + style.getPaddingLeftNoRTL() + inputModeWidth;
        if (cursorX < baseX) {
            x = baseX - cursorX;
        }
    }
    int oldColor = g.getColor();
    int alpha = g.concatenateAlpha(style.getFgAlpha());
    if (getTextFieldCursorColor() == 0) {
        g.setColor(style.getFgColor());
    } else {
        g.setColor(getTextFieldCursorColor());
    }
    g.drawLine(cursorX + x, cursorY, cursorX + x, cursorY + f.getHeight());
    g.setColor(oldColor);
    g.setAlpha(alpha);
}
Also used : Font(com.codename1.ui.Font)

Aggregations

Component (com.codename1.ui.Component)19 Font (com.codename1.ui.Font)18 Container (com.codename1.ui.Container)15 Form (com.codename1.ui.Form)14 Style (com.codename1.ui.plaf.Style)12 Button (com.codename1.ui.Button)11 Image (com.codename1.ui.Image)11 TextArea (com.codename1.ui.TextArea)11 ArrayList (java.util.ArrayList)11 File (java.io.File)10 IOException (java.io.IOException)10 Hashtable (java.util.Hashtable)10 BorderLayout (com.codename1.ui.layouts.BorderLayout)9 Label (com.codename1.ui.Label)8 FileInputStream (java.io.FileInputStream)8 ActionListener (com.codename1.ui.events.ActionListener)7 TextField (com.codename1.ui.TextField)6 ActionEvent (com.codename1.ui.events.ActionEvent)6 BoxLayout (com.codename1.ui.layouts.BoxLayout)6 EncodedImage (com.codename1.ui.EncodedImage)5