Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8091202

CSS Style Object Model in Java

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 7-pool
    • javafx
    • None

      This one may be too big and perhaps might not be as popular as I would hope, but I'm going to raise it just in case it gets some interest.

      I would really like to see the Styles as defined by CSS be exposed as a Java Object Model. Ideally there would be a Java object for for each style 'class' defined in the CSS reference guide (http://download.oracle.com/javafx/2.0/api/javafx/scene/doc-files/cssref.html), with each one extending a base Style class.

      Basically I want the core CSS model to be Java-based, with CSS just being a syntactic sugar for loading and populating these Java Objects. This is very similar to the FXML model, where the Nodes all exists as Java Objects and the XML is just a modelling language that makes defining scenes of Nodes a little simpler.

      This would give us some subtle, yet powerful advantages over the pure CSS approached used at the moment:

      * The ability to interrogate and manipulate styles via code. for example I could find out what Style object is applicable to my Node (and derive it's border or background color, etc) or I could create a Style in code on the fly based on data attributes, etc.

      * Type safety when working with styles in the code. Instead of Node.setStyle(String) we would have Node.setStyle(Style), where attributes of Style are set via setters and using enums, etc.

      * The ability to go beyond CSS for special styling cases - e.g. you could use Groovy for defining styles. As we see the addition of animations done via the styles, I personally feel CSS will start to quickly hinder us (it's not built for procedural stuff like this). You could also have you Style classes stored as Hibernate beans in a Database, etc, etc.

      * The ability to easily add custom styles for custom components - developers would just extend the Style class and add their own attributes (e.g. a carousel component might have a '-fx-rotation-speed' style attribute). A simple CSS-name-to-Java-property convention (like that used for FXML attribute setting) would allow this custom attribute to be used in the CSS in a very simple and intuitive way.

      These are the immediate things I can think of, but my experience is that when we have the core model in Java people will come up with some cool stuff that we haven't even thought of yet. Having it confined to a CSS model under the hood limits our creative options before we even get out the gate.

      So just to clarify, as an example of what I mean we would have classes like NodeStyle, LabelledStyle, VBoxStyle, TextStyle, etc, with TextStyle for example looking something like the following:

        public class TextStyle extends Style {
            private Font font;
            private boolean strikethrough;
            private TextAlignment textAlignment; // enum
            private TextOrigin textOrigin; //enum
            private boolean underline;

            // ... getters and setters
        }

      Then I would really like to see a StyleSheet class exist. This would hold all the styles and the mapping between their relevant selectors (i.e. it is a Java representation of the StyleSheet.css file) and provide methods for accessing these. Something like:

        public class StyleSheet {

            public void setStyleForId(String id, Style style);
       
            public void setStyleForClass(String id, Style style);
       
            public Style getStyleForId(String nodeId);

            public Style getStyleForClass(String styleClass);

            public Style[] getStylesForNode(Node node);
        }

      The Nodes (or the renderers or whatever it is that styles currently) would use this internally instead of whatever they currently do to work out their style.

      When loading a Style, we would just have a StyleSheetLoader, or a static method on StyleSheet to load this from a file. So instead of this:

              scene.getStylesheets().add("styles.css");

      We would have:

              scene.getStylesheets().add(StyleSheet.load("styles.css"));


      That's the idea anyway. It would need some deeper analysis if the idea gets any traction.

      There may be a lot of reasons why doing this is not a goer (i.e. too hard, performance issues, or not popular enough) but for me this would be a really powerful and neat part of JFX that would open the door to a whole lot of cool things in the future. JFX1.x suffered from using a scripting language instead of pure Java as its core engine for building scenes. It fixed this by flipping it so the core is Java, and the scripting (FXML) is just an add-on. To me the same rationale for this change is just as valid for the CSS side of things.

      Cheers,
      zonski





            Unassigned Unassigned
            dzwolenskjfx Daniel Zwolenski (Inactive)
            Votes:
            25 Vote for this issue
            Watchers:
            19 Start watching this issue

              Created:
              Updated:
              Imported: