Class StyleParser

java.lang.Object
com.codename1.ui.plaf.StyleParser

public abstract class StyleParser extends Object

Parses Style strings into StyleInfo objects, which can be converted to Style objects at runtime.

This class is the basis for the inline style functionality:

  • Component#setInlineAllStyles(java.lang.String)

  • Component#getInlineAllStyles()

  • Component#setInlineSelectedStyles(java.lang.String)

  • Component#getInlineSelectedStyles()

  • etc..

Style strings are strings which describe a style, and are in a particular format that StyleParser knows how to parse. The general format of a style string is key1:value1; key2:value2; ... keyn:valuen;. I.e. a set of key-value pairs with pairs separated by semi-colons, and keys and values separated by a colon. This is very similar to CSS, but it is NOT CSS. Style string keys and values are closely related to the properties of the Style class and their associated values.

Supported Keys

The following keys are supported:

  • fgColor - The foreground color as a hex string. E.g. ff0000.

  • bgColor - The background color as a hex string. E.g. ff0000.

  • transparency - The background transparency as an integer. 0-255.

  • textDecoration - The text decoration. One of underline, overline, 3d, 3d_lowered, 3d_shadow_north, strikethru, or none.

  • opacity - The opacity as an integer. 0-255

  • padding - The padding as a sequence of 1, 2, 3, or 4 values. See "Padding and Margin Strings" below for details on the format.

  • margin - The margin as a sequence of 1, 2, 3, or 4, values. See "Padding and Margin Strings" below for details on the format.

  • font - The font. See "Font Strings" below for details on the format.

  • border - The border. See "Border Strings" below for details on the format.

  • bgType - The background type. See "Background Type Values" below for details on the available options.

Padding and Margin Strings

The padding and margin keys can take values the same format as is used for CSS margin and padding directives. That is the value can be expressed as a space-separated sequence of scalar values (i.e. float values with a unit suffix). Some examples:

  • padding:0px - Sets all padding to zero pixels.

  • padding:2mm 1px - Sets vertical padding to 2 millimetres, and horizontal padding to 1 pixel.

  • padding:2mm 1px inherit - Sets top padding to 1 millimetres, horizontal padding to 1 pixel, and bottom padding to inherit the parent style's bottom padding.

  • padding:1mm 2px inherit 4mm - Top padding=1 millimetre. Right padding=2 pixels. Bottom padding inherits parent style's bottom padding. Left padding=4 millimetres.

All of the examples above use padding, but the same format is used for margin. They demonstrate the use of 1, 2, 3, and 4 value sequences, and their meaning. In general terms these formats can be described as:

    • Sets padding on all sites to
    • Top and bottom padding set to . Left and right padding set to
    • Top=. Left and right = . Bottom = bottom.
  • - Top=. Right=right. Bottom=bottom. Left=left.  In other words, values applied clock-wise, starting on top side.
    

Font Strings

Fonts strings can take any of the following formats:

    • E.g. 3mm Arial.ttf /Arial.ttf or 12px native:MainRegular native:MainRegular
    • E.g. 3mm Arial.ttf or 3mm native:ItalicBlack.
    • E.g. 3mm or 12px. When only specifying the size, the font family will be dictated by the parent style.
    • E.g. Arial.ttf /Arial.ttf or native:MainBold native:MainBold. When omitting font size (as this format does), the size is dictated by the parent style.
  • | - E.g. Arial.ttf or /Arial.ttf, or native:MainRegular Strings starting with a / are assumed to be files. The corresponding font name is then derived by removing the slash and the trailing .ttf. When omitting font size (as this format does), the size is dictated by the parent style.

Border Strings

The border property accepts several different formats for its value. This is due to the many different kinds of borders that can be created. The following are some of the formats.

Line Border

solid - E.g. 1mm solid ff0000. should be expressed as a scalar value with unit. E.g. 1mm, or 2px. should be an RGB hex string. E.g ff0000 for red.

Dashed Border

dashed - E.g. 1mm dashed ff0000. should be expressed as a scalar value with unit. E.g. 1mm, or 2px. should be an RGB hex string. E.g ff0000 for red.

Dotted Border

dotted - E.g. 1mm dotted ff0000. should be expressed as a scalar value with unit. E.g. 1mm, or 2px. should be an RGB hex string. E.g ff0000 for red.

Underline Border

underline - E.g. 1mm underline ff0000. should be expressed as a scalar value with unit. E.g. 1mm, or 2px. should be an RGB hex string. E.g ff0000 for red.

Image Border

image ... - A 9-piece image border. The .. values are strings which refer to images either on the classpath, or in the theme resource file. If the image string starts with /, then it is assumed to be on the classpath. The order of the images corresponds to the parameters of com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image, com.codename1.ui.Image).

image - A 9-piece image border, but with the images corresponding to the parameters of com.codename1.ui.Image, com.codename1.ui.Image).

horizontalImage - A 3-piece horizontal image border. Image parameters correspond with com.codename1.ui.Image, com.codename1.ui.Image) parameters.

verticalImage - A 3-piece horizontal image border. Image parameters correspond with com.codename1.ui.Image, com.codename1.ui.Image) parameters.

splicedImage - A 9-piece image border that is generated from a single image, but with inset values specifying where the image should be sliced to create the 9 sub-images.

Parameters:

  • The image to use. If this begins with /, then the image will be found on the classpath. Otherwise it will be found in the theme resource file.

  • , , , - The insets along which is sliced to generate the 9-subimages. These values are expressed as a floating point number between 0.0 and 1.0, where 1.0 is the full width or height of the image depending on the orientation (horizontal or vertical) or the inset. If image is 100 pixels by 100 pixels, then a top inset of 0.4 would cause a slice to occur at 40 pixels from the top of the image (i.e. the top-left, top, and top-right slices would each be 40 pixels high.

  • Field Details

  • Constructor Details

    • StyleParser

      public StyleParser()
  • Method Details

    • getBackgroundTypes

      public static List<String> getBackgroundTypes()
      Gets the available background type strings (which can be passed to StyleInfo#setBgType(java.lang.String)
    • validateScalarValue

      public static boolean validateScalarValue(String val)

      Checks if a string is a valid scalar value. A scalar value should be in the format where is an integer or decimal number, and is a unit - one of mm, px, or %.

      There is one special value: "inherit" which indicates that the scalar value just inherits from its parent.

      Parameters
      • val: String value to validate.
      Returns

      True if the value is a valid scalar value.

    • parseScalarValue

      public static StyleParser.ScalarValue parseScalarValue(String val)

      Parses a string into a scalar value. A scalar value should be in the format where is an integer or decimal number, and is a unit - one of mm, px, or %.

      There is one special value: "inherit" which indicates that the scalar value just inherits from its parent.

      Parameters
      • val: String that should be a valid scalar value.
    • getSupportedBackgroundTypes

      public static List<String> getSupportedBackgroundTypes()
      Gets a list of the background types that are supported.