Search in sources :

Example 71 with Label

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

the class HTMLComponent method addQuote.

/**
 * Adds a quote according to the current quote count
 *
 * @param quoteElement The quote element (TAG_Q)
 * @param curAlign The current horizontal alignment
 */
private void addQuote(HTMLElement quoteElement, int curAlign, boolean isStartTag) {
    String quote = null;
    // 0 is the start tag of primary tag and 1 its closing tag. 2 is the start of secondary tag and 3 the closing tag (Used for CSS_QUOTES)
    int quoteNum = isStartTag ? 0 : 1;
    if (quoteTagCount == 0) {
        quote = "\"";
    } else {
        quote = "'";
        quoteNum += 2;
    }
    if ((FIXED_WIDTH) && (width - x < font.stringWidth(quote))) {
        newLine(curAlign);
    }
    Label quoteLabel = addString(quote, curAlign);
    quoteLabel.putClientProperty(CLIENT_PROPERTY_QUOTE, new Integer(quoteNum));
    if (loadCSS) {
        quoteElement.addAssociatedComponent(quoteLabel);
    }
}
Also used : Label(com.codename1.ui.Label)

Example 72 with Label

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

the class HTMLComponent method handleImage.

/**
 * Handles the IMG tag. This includes calculating its size (if available), applying any links/accesskeys and adding it to the download queue
 *
 * @param imgElement the IMG element
 * @param align th current alignment
 * @param cmd The submit command of a form, used only for INPUT type="image"
 */
private void handleImage(HTMLElement imgElement, int align, Command cmd) {
    String imageUrl = imgElement.getAttributeById(HTMLElement.ATTR_SRC);
    Label imgLabel = null;
    if (imageUrl != null) {
        String alignStr = imgElement.getAttributeById(HTMLElement.ATTR_ALIGN);
        // Image width and height
        int iWidth = calcSize(getWidth(), imgElement.getAttributeById(HTMLElement.ATTR_WIDTH), 0, false);
        int iHeight = calcSize(getHeight(), imgElement.getAttributeById(HTMLElement.ATTR_HEIGHT), 0, false);
        // Whitespace on the image sides (i.e. Margins)
        int hspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_HSPACE));
        int vspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_VSPACE));
        int totalWidth = iWidth + hspace * 2;
        if ((FIXED_WIDTH) && (x + totalWidth >= width)) {
            newLine(align);
        }
        // Alternative image text, shown until image is loaded.
        String altText = imgElement.getAttributeById(HTMLElement.ATTR_ALT);
        String imageMap = imgElement.getAttributeById(HTMLElement.ATTR_USEMAP);
        if (link != null) {
            // This image is inside an A tag with HREF attribute
            imgLabel = new HTMLLink(altText, link, this, mainLink, false);
            if (mainLink == null) {
                mainLink = (HTMLLink) imgLabel;
            }
            if (accesskey != '\0') {
                // accessKeys.put(new Integer(accesskey), imgLabel);
                addAccessKey(accesskey, imgLabel, false);
            }
            if (!PROCESS_HTML_MP1_ONLY) {
                ((HTMLLink) imgLabel).isMap = (imgElement.getAttributeById(HTMLElement.ATTR_ISMAP) != null);
            }
        } else if (cmd != null) {
            // Special case of an image submit button
            imgLabel = new Button(cmd);
            if ((altText != null) && (!altText.equals(""))) {
                imgLabel.setText(altText);
            }
            if (firstFocusable == null) {
                firstFocusable = imgLabel;
            }
        } else if (imageMap != null) {
            // Image Map
            imgLabel = new HTMLImageMap(this);
            if (imageMapComponents == null) {
                imageMapComponents = new Hashtable();
            }
            if (imageMap.startsWith("#")) {
                // Image map are denoted by # and then the map name (But we also tolerate if map is specified without #)
                imageMap = imageMap.substring(1);
            }
            imageMapComponents.put(imageMap, imgLabel);
            if ((imageMapData != null) && (imageMapData.containsKey(imageMap))) {
                ImageMapData data = (ImageMapData) imageMapData.get(imageMap);
                ((HTMLImageMap) imgLabel).mapData = data;
            }
        } else {
            imgLabel = new Label(altText);
        }
        if ((iWidth != 0) || (iHeight != 0)) {
            // reserve space while loading image if either width or height are specified, otherwise we don't know how much to reserve
            iWidth += imgLabel.getStyle().getPadding(Component.LEFT) + imgLabel.getStyle().getPadding(Component.RIGHT);
            iHeight += imgLabel.getStyle().getPadding(Component.TOP) + imgLabel.getStyle().getPadding(Component.BOTTOM);
            imgLabel.setPreferredSize(new Dimension(iWidth, iHeight));
        } else {
            // If no space is reserved, make a minimal text, otherwise Codename One won't calculate the size right after the image loads
            if ((imgLabel.getText() == null) || (imgLabel.getText().equals(""))) {
                imgLabel.setText(" ");
            }
        }
        // It is important that the padding of the image component itself will be all 0
        // This is because when the image is loaded, its preferred size is checked to see if its width/height were preset by the width/height attribute
        imgLabel.getSelectedStyle().setPadding(0, 0, 0, 0);
        imgLabel.getUnselectedStyle().setPadding(0, 0, 0, 0);
        imgLabel.getSelectedStyle().setFont(font.getFont());
        imgLabel.getUnselectedStyle().setFont(font.getFont());
        int borderSize = getInt(imgElement.getAttributeById(HTMLElement.ATTR_BORDER));
        if (borderSize != 0) {
            imgLabel.putClientProperty(CLIENT_PROPERTY_IMG_BORDER, new Integer(borderSize));
        } else {
            borderSize = 1;
        }
        imgLabel.getUnselectedStyle().setBorder(Border.createLineBorder(borderSize));
        imgLabel.getSelectedStyle().setBorder(Border.createLineBorder(borderSize));
        imgLabel.getUnselectedStyle().setBgTransparency(0);
        imgLabel.getSelectedStyle().setBgTransparency(0);
        Container imgCont = new Container(new BorderLayout());
        imgCont.addComponent(BorderLayout.CENTER, imgLabel);
        imgCont.getSelectedStyle().setMargin(vspace, vspace, hspace, hspace);
        imgCont.getUnselectedStyle().setMargin(vspace, vspace, hspace, hspace);
        curLine.addComponent(imgCont);
        x += totalWidth;
        // Alignment
        imgLabel.setAlignment(getHorizAlign(alignStr, align, false));
        imgLabel.setVerticalAlignment(getVertAlign(alignStr, Component.CENTER));
        if (showImages) {
            if (docInfo != null) {
                imageUrl = docInfo.convertURL(imageUrl);
                threadQueue.add(imgLabel, imageUrl);
            } else {
                if (DocumentInfo.isAbsoluteURL(imageUrl)) {
                    threadQueue.add(imgLabel, imageUrl);
                } else {
                    if (htmlCallback != null) {
                        htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, imgElement.getTagName(), imgElement.getAttributeName(new Integer(HTMLElement.ATTR_SRC)), imageUrl, "Ignoring Image file referred in an IMG tag (" + imageUrl + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
                    }
                }
            }
        }
        if (loadCSS) {
            imgElement.setAssociatedComponents(imgCont);
        }
    }
}
Also used : Container(com.codename1.ui.Container) BorderLayout(com.codename1.ui.layouts.BorderLayout) Button(com.codename1.ui.Button) RadioButton(com.codename1.ui.RadioButton) Hashtable(java.util.Hashtable) Label(com.codename1.ui.Label) Dimension(com.codename1.ui.geom.Dimension)

Example 73 with Label

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

the class FontImage method setMaterialIcon.

/**
 * <p>Applies a material design icon (one of the MATERIAL_* icon constants) to the given label using the
 * styling of the label. Notice that when the argument is a button the pressed/selected &amp; disabled states
 * will be set appropriately.</p>
 * <script src="https://gist.github.com/codenameone/8cf6f70188959524474b.js"></script>
 *
 * @param l a label or subclass (e.g. Button etc.)
 * @param icon one of the MATERIAL_* icons
 * @param size the size of the icon in millimeters
 */
public static void setMaterialIcon(Label l, char icon, float size) {
    if (Font.isTrueTypeFileSupported()) {
        Style s = new Style(l.getUnselectedStyle());
        s.setFont(getMaterialDesignFont().derive(rightSize(s, size), Font.STYLE_PLAIN));
        l.setIcon(FontImage.create("" + icon, s));
        if (l instanceof Button) {
            Button b = (Button) l;
            Style sel = b.getSelectedStyle();
            Style pre = b.getPressedStyle();
            Style dis = b.getDisabledStyle();
            if (sel.getFgColor() != s.getFgColor() || (sel.getBgColor() != s.getBgColor()) || (sel.getBgTransparency() != s.getBgTransparency())) {
                sel = new Style(sel);
                sel.setFont(getMaterialDesignFont().derive(rightSize(sel, size), Font.STYLE_PLAIN));
                b.setRolloverIcon(FontImage.create("" + icon, sel));
            }
            if (pre.getFgColor() != s.getFgColor() || (pre.getBgColor() != s.getBgColor()) || (pre.getBgTransparency() != s.getBgTransparency())) {
                pre = new Style(pre);
                pre.setFont(getMaterialDesignFont().derive(rightSize(pre, size), Font.STYLE_PLAIN));
                b.setPressedIcon(FontImage.create("" + icon, pre));
                b.setRolloverPressedIcon(FontImage.create("" + icon, pre));
            }
            if (dis.getFgColor() != s.getFgColor() || (dis.getBgColor() != s.getBgColor()) || (dis.getBgTransparency() != s.getBgTransparency())) {
                dis = new Style(dis);
                dis.setFont(getMaterialDesignFont().derive(rightSize(dis, size), Font.STYLE_PLAIN));
                b.setDisabledIcon(FontImage.create("" + icon, dis));
            }
        }
    }
}
Also used : SpanButton(com.codename1.components.SpanButton) MultiButton(com.codename1.components.MultiButton) Style(com.codename1.ui.plaf.Style)

Example 74 with Label

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

the class FontImage method setMaterialIcon.

/**
 * <p>Applies a material design icon (one of the MATERIAL_* icons above) to the given component using the
 * styling of the label</p>
 * <script src="https://gist.github.com/codenameone/8cf6f70188959524474b.js"></script>
 * @param l a SpanLabel
 * @param icon one of the MATERIAL_* icons
 * @param size the size of the icon in millimeters
 */
public static void setMaterialIcon(SpanLabel l, char icon, float size) {
    if (Font.isTrueTypeFileSupported()) {
        Style s = new Style(l.getUnselectedStyle());
        s.setFont(getMaterialDesignFont().derive(rightSize(s, size), Font.STYLE_PLAIN));
        l.setIcon(FontImage.create("" + icon, s));
    }
}
Also used : Style(com.codename1.ui.plaf.Style)

Example 75 with Label

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

the class InputComponent method errorMessage.

/**
 * Sets the text of the error label
 * @param errorMessage the text
 * @return this for chaining calls E.g. {@code TextComponent tc = new TextComponent().text("Text").label("Label"); }
 */
public InputComponent errorMessage(String errorMessage) {
    String col = getUIManager().getThemeConstant("textComponentErrorColor", null);
    if (errorMessage == null || errorMessage.length() == 0) {
        // no need for double showing of error
        if (this.errorMessage.getText().length() == 0) {
            return this;
        }
        // clear the error mode
        this.errorMessage.setText("");
        if (col != null) {
            lbl.setUIID(lbl.getUIID());
            getEditor().setUIID(getEditor().getUIID());
        }
    } else {
        this.errorMessage.setText(errorMessage);
        if (col != null) {
            int val = Integer.parseInt(col, 16);
            lbl.getAllStyles().setFgColor(val);
            Border b = Border.createUnderlineBorder(2, val);
            getEditor().getAllStyles().setBorder(b);
        }
    }
    refreshForGuiBuilder();
    return this;
}
Also used : Border(com.codename1.ui.plaf.Border)

Aggregations

Label (com.codename1.ui.Label)41 BorderLayout (com.codename1.ui.layouts.BorderLayout)22 Container (com.codename1.ui.Container)21 Component (com.codename1.ui.Component)16 Form (com.codename1.ui.Form)15 Button (com.codename1.ui.Button)14 TextArea (com.codename1.ui.TextArea)14 Style (com.codename1.ui.plaf.Style)13 ActionListener (com.codename1.ui.events.ActionListener)12 ActionEvent (com.codename1.ui.events.ActionEvent)11 Image (com.codename1.ui.Image)10 Dimension (com.codename1.ui.geom.Dimension)10 Vector (java.util.Vector)10 BoxLayout (com.codename1.ui.layouts.BoxLayout)9 EncodedImage (com.codename1.ui.EncodedImage)8 RadioButton (com.codename1.ui.RadioButton)7 LayeredLayout (com.codename1.ui.layouts.LayeredLayout)6 Hashtable (java.util.Hashtable)6 Paint (com.codename1.charts.compat.Paint)5 Font (com.codename1.ui.Font)5