Search in sources :

Example 1 with StyleInfo

use of com.codename1.ui.plaf.StyleParser.StyleInfo in project CodenameOne by codenameone.

the class StyleParser method getBackgroundTypes.

/**
 * Gets the available background type strings (which can be passed to {@link StyleInfo#setBgType(java.lang.String) }
 * @return
 */
public static List<String> getBackgroundTypes() {
    ArrayList<String> out = new ArrayList<String>();
    out.addAll(bgTypes().keySet());
    Collections.sort(out, new CaseInsensitiveOrder());
    return out;
}
Also used : ArrayList(java.util.ArrayList) CaseInsensitiveOrder(com.codename1.util.CaseInsensitiveOrder)

Example 2 with StyleInfo

use of com.codename1.ui.plaf.StyleParser.StyleInfo in project CodenameOne by codenameone.

the class UIManager method parseStyle.

/**
 * Creates a style by providing style strings in a specific format. This method allows for the use of inline styles
 * to override the styles in {@link com.codename1.ui.Component}
 * @param theme Theme used to retrieve images referenced in the style strings.
 * @param id The style ID (UIID) to use to cache the style inside the theme.
 * @param prefix Prefix to use for styles.  Corresponds to the {@literal prefix} argument in {@link #getComponentStyleImpl(java.lang.String, boolean, java.lang.String)
 * @param baseStyle The style class from which this new style should derive.
 * @param selected True if this is for a selected style.
 * @param styleString Array of style strings to be parsed.  The format is {@literal key1:value1; key2:value2; etc...}.  While this looks similar to CSS, it is important to note that it is NOT
 * CSS.  The keys and values correspond to the properties of {@link com.codename1.ui.plaf.Style} and their associated values.
 * @return A style object representing the styles that were provided in the styleString.
 *
 * <h3>Example Usage</h3>
 *
 * {@code
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *     "fgColor:ff0000; font:18mm; border: 1px solid ff0000; bgType:none; padding: 3mm; margin: 1mm");
 *
 * // Create a 9-piece image border on the fly:
 * Style s = parseStyle(theme, "Button[MyCustomButton]", "", "Button", false,
 *      "border:splicedImage /notes.png 0.3 0.4 0.3 0.4");
 *      // This splices the image found at /notes.png into 9 pieces.  Splice insets are specified by the 4 floating point values
 *      // at the end of the border directive:  [top] [right] [bottom] [left].
 * }
 */
Style parseStyle(Resources theme, String id, String prefix, String baseStyle, boolean selected, String... styleString) {
    String cacheKey = selected ? id + ".sel" : id + "." + prefix;
    String originalId = id;
    if (id == null || id.length() == 0) {
        // if no id return the default style
        id = "";
    } else {
        id = id + ".";
    }
    if (Arrays.toString(styleString).equals(parseCache().get(cacheKey)) && ((selected && selectedStyles.containsKey(id)) || (!selected && this.styles.containsKey(id)))) {
        return getComponentStyleImpl(originalId, selected, prefix);
    }
    parseCache().put(cacheKey, Arrays.toString(styleString));
    Style base = baseStyle != null ? getComponentStyleImpl(baseStyle, selected, prefix) : null;
    Map<String, String> styles = new HashMap<String, String>();
    for (String str : styleString) {
        StyleParser.parseString(styles, str);
    }
    StyleInfo styleInfo = new StyleInfo(styles);
    if (prefix != null && prefix.length() > 0) {
        id += prefix;
    }
    if (themeProps == null) {
        resetThemeProps(null);
    }
    if (baseStyle != null) {
        themeProps.put(id + "derive", baseStyle);
    } else {
        themeProps.remove(id + "derive");
    }
    String val = null;
    Integer bgColor = styleInfo.getBgColor();
    if (bgColor != null) {
        themeProps.put(id + Style.BG_COLOR, Integer.toHexString(bgColor));
    } else {
        themeProps.remove(id + Style.BG_COLOR);
    }
    Integer fgColor = styleInfo.getFgColor();
    if (fgColor != null) {
        themeProps.put(id + Style.FG_COLOR, Integer.toHexString(fgColor));
    } else {
        themeProps.remove(id + Style.FG_COLOR);
    }
    BorderInfo border = styleInfo.getBorder();
    if (border != null) {
        themeProps.put(id + Style.BORDER, border.createBorder(theme));
    } else {
        themeProps.remove(id + Style.BORDER);
    }
    Integer bgType = styleInfo.getBgType();
    if (bgType != null) {
        themeProps.put(id + Style.BACKGROUND_TYPE, bgType.byteValue());
    } else {
        themeProps.remove(id + Style.BACKGROUND_TYPE);
    }
    ImageInfo bgImage = styleInfo.getBgImage();
    if (bgImage != null) {
        themeProps.put(id + Style.BG_IMAGE, bgImage.getImage(theme));
    } else {
        themeProps.remove(id + Style.BG_IMAGE);
    }
    MarginInfo margin = styleInfo.getMargin();
    if (margin != null) {
        float[] marginArr = margin.createMargin(base);
        themeProps.put(id + Style.MARGIN, marginArr[Component.TOP] + "," + marginArr[Component.BOTTOM] + "," + marginArr[Component.LEFT] + "," + marginArr[Component.RIGHT]);
        byte[] unitArr = margin.createMarginUnit(base);
        themeProps.put(id + Style.MARGIN_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.MARGIN);
        themeProps.remove(id + Style.MARGIN_UNIT);
    }
    PaddingInfo padding = styleInfo.getPadding();
    if (padding != null) {
        float[] paddingArr = padding.createPadding(base);
        themeProps.put(id + Style.PADDING, paddingArr[Component.TOP] + "," + paddingArr[Component.BOTTOM] + "," + paddingArr[Component.LEFT] + "," + paddingArr[Component.RIGHT]);
        byte[] unitArr = padding.createPaddingUnit(base);
        themeProps.put(id + Style.PADDING_UNIT, new byte[] { unitArr[Component.TOP], unitArr[Component.BOTTOM], unitArr[Component.LEFT], unitArr[Component.RIGHT] });
    } else {
        themeProps.remove(id + Style.PADDING);
        themeProps.remove(id + Style.PADDING_UNIT);
    }
    Integer transparency = styleInfo.getTransparency();
    if (transparency != null) {
        themeProps.put(id + Style.TRANSPARENCY, String.valueOf(transparency.intValue()));
    } else {
        themeProps.remove(id + Style.TRANSPARENCY);
    }
    Integer opacity = styleInfo.getOpacity();
    if (opacity != null) {
        themeProps.put(id + Style.OPACITY, String.valueOf(opacity.intValue()));
    } else {
        themeProps.remove(id + Style.OPACITY);
    }
    Integer alignment = styleInfo.getAlignment();
    if (alignment != null) {
        themeProps.put(id + Style.ALIGNMENT, alignment);
    } else {
        themeProps.remove(id + Style.ALIGNMENT);
    }
    Integer textDecoration = styleInfo.getTextDecoration();
    if (textDecoration != null) {
        themeProps.put(id + Style.TEXT_DECORATION, textDecoration);
    } else {
        themeProps.remove(id + Style.TEXT_DECORATION);
    }
    FontInfo font = styleInfo.getFont();
    if (font != null) {
        themeProps.put(id + Style.FONT, font.createFont(base));
    } else {
        themeProps.remove(id + Style.FONT);
    }
    if (selected)
        selectedStyles.remove(id);
    else
        this.styles.remove(id);
    return getComponentStyleImpl(originalId, selected, prefix);
}
Also used : HashMap(java.util.HashMap) BorderInfo(com.codename1.ui.plaf.StyleParser.BorderInfo) PaddingInfo(com.codename1.ui.plaf.StyleParser.PaddingInfo) MarginInfo(com.codename1.ui.plaf.StyleParser.MarginInfo) StyleInfo(com.codename1.ui.plaf.StyleParser.StyleInfo) ImageInfo(com.codename1.ui.plaf.StyleParser.ImageInfo) FontInfo(com.codename1.ui.plaf.StyleParser.FontInfo)

Aggregations

BorderInfo (com.codename1.ui.plaf.StyleParser.BorderInfo)1 FontInfo (com.codename1.ui.plaf.StyleParser.FontInfo)1 ImageInfo (com.codename1.ui.plaf.StyleParser.ImageInfo)1 MarginInfo (com.codename1.ui.plaf.StyleParser.MarginInfo)1 PaddingInfo (com.codename1.ui.plaf.StyleParser.PaddingInfo)1 StyleInfo (com.codename1.ui.plaf.StyleParser.StyleInfo)1 CaseInsensitiveOrder (com.codename1.util.CaseInsensitiveOrder)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1