Class Styles

java.lang.Object
com.simsilica.lemur.style.Styles

public class Styles extends Object
Provides support for automatically configuring GUI elements based on a style attribute system similar to cascading style sheets.

The attributes that are available for a given GUI element are based on the StyleAttribute annotations specified on the class. StyleAttribute setters expose style attributes that can be set in an Attributes set.

Attributes are accessed by selector that is a simplified version of the way cascading style sheets work. A selector can either by the raw style name, a specific element ID for a style, or a parent/child relationship for a style. A particular element may inherit from one or more of these depending on how its element ID is setup.

In normal cascading style sheets, the parent/child relationship between elements is based on the full page hierarchy. In Lemur's Style system, parent/child relationships are limited to the dotted notation with an element ID itself. So an element ID of "button" has no parent/child relationship. An element ID of "slider.thumb.button" has direct parent/child relationships between "slider"/"thumb" and "thumb"/"button", and an intrinsic parent/child relationship between "slider"/"button".

Attribute sets are built in such a way that they inherit attributes up this parent/child hierarchy. Most specific styles override less specific ones. The precendence goes roughly in the following order:

  • Specific element ID, fully qualified
  • For every dotted part, from right to left, all parent/child relationships and then the element ID for that dotted part.
  • The global settings for the particular style.

For example, an ElementId of "A.B.C" would refer to an Attribute hierarchy from most specific to least specific, as:

  • A.B.C
  • B contains C
  • A contains C
  • C
  • A contains B
  • B
  • style-defaults

This is "font" is accessed for "A.B.C" the configured Attribute sets are checked for a "font" attribute in the order listed above. The first one found wins.

The values of the attributes are set based on a selector similar to the specific steps listed above. For example, a style's attributes can be specifically set for "A.B.C" or can be set for any case of "A contains C". These attribute sets are accessed using the getSelector() methods and return a non-hierarchical, selector-specific attribute set.

To retrieve attributes for a GUI element, the getAttributes() methods are used. These return a full hierarchy of attribute sets as described above.

Using this system, an application could easily configure style-global attributes while also overriding the defaults for specific cases. For example, if the application is defining a "beveled" style, it might set the default "font" for all style="beveled" elements. It might then set the default background for all style="beveled", element="button" elements. At that point, all labels, text fields, and text entry fields would be using the specified font. All buttons, whether they are in sliders, scroll bars, or on their own, would have the specified background. The application could then define settings for "any button in a slider" with getSelector( "slider", "button", "beveled" ) or even get more specific and target "slider"/"thumb" directly or any "thumb"/"button" that might exist in sliders or scrollbars, etc..

  • Field Details

  • Constructor Details

    • Styles

      public Styles()
  • Method Details

    • setDefaultStyle

      public void setDefaultStyle(String style)
      Sets the default style that is used by GUI elements when no other style is specified. This is an easy way to provide a common look to the entire user interface without having to pass styles to every created component. Defaults to the root style.
    • getDefaultStyle

      public String getDefaultStyle()
    • clearCache

      public void clearCache()
    • setDefault

      public void setDefault(Object value)
    • getDefault

      public <T> T getDefault(Class<T> type)
    • getAttributes

      public Attributes getAttributes(ElementId elementId)
      Retrieves the compiled attributes for the specified element ID and default style. The attributes are compiled based on the selector rules and attributes setup prior to this call.
    • getAttributes

      public Attributes getAttributes(ElementId elementId, String style)
      Retrieves the compiled attributes for the specified element ID and style. The attributes are compiled based on the selector rules and attributes setup prior to this call.
    • getAttributes

      public Attributes getAttributes(String elementId)
      Retrieves the compiled attributes for the specified element ID and default style. The attributes are compiled based on the selector rules and attributes setup prior to this call.
    • getAttributes

      public Attributes getAttributes(String elementId, String style)
      Retrieves the compiled attributes for the specified element ID and style. The attributes are compiled based on the selector rules and attributes setup prior to this call.
    • styleKey

      protected String styleKey(ElementId elementId, String style)
    • getTree

      protected StyleTree getTree(String style, boolean create)
    • getSelector

      public Attributes getSelector(String style)
    • getSelector

      public Attributes getSelector(ElementId id, String style)
    • getSelector

      public Attributes getSelector(String id, String style)
    • getSelector

      public Attributes getSelector(ElementId parent, ElementId child, String style)
    • getSelector

      public Attributes getSelector(ElementId parent, String child, String style)
    • getSelector

      public Attributes getSelector(String parent, ElementId child, String style)
    • getSelector

      public Attributes getSelector(String parent, String child, String style)
    • main

      public static void main(String... args)
    • initializeStyles

      public void initializeStyles(Class c)
    • getExistingValue

      protected Object getExistingValue(Object o, Method m)
    • getStyleAttributeMethods

      protected static List<Method> getStyleAttributeMethods(Class c)
    • applyStyles

      @Deprecated public void applyStyles(Object o, String elementId)
      Deprecated.
    • applyStyles

      @Deprecated public void applyStyles(Object o, String elementId, String style)
      Deprecated.
    • applyStyles

      public void applyStyles(Object o, ElementId elementId)
    • applyStyles

      public void applyStyles(Object o, ElementId elementId, String style)
    • clone

      protected Object clone(Object value, Cloner cloner)
    • cloneMap

      protected Map<Object,Object> cloneMap(Map<Object,Object> source, Cloner cloner)
    • cloneList

      protected List<Object> cloneList(List<Object> source, Cloner cloner)