From f06377004a387ab64138c9db08758f7b347ef66c Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Tue, 14 Nov 2017 16:53:47 +0100 Subject: [PATCH] Over-writable CSS interpreter --- Classes/css/core/Rule.java | 9 +++ Classes/css/core/Ruleset.java | 104 +++++++++++++++++++++++++++ Classes/css/core/rule/JavaFX.java | 35 +++++++++ Classes/css/core/rule/Padding.java | 108 ++++++++++++++++++++++++++++ Classes/css/core/rule/Position.java | 56 +++++++++++++++ 5 files changed, 312 insertions(+) create mode 100644 Classes/css/core/Rule.java create mode 100644 Classes/css/core/Ruleset.java create mode 100644 Classes/css/core/rule/JavaFX.java create mode 100644 Classes/css/core/rule/Padding.java create mode 100644 Classes/css/core/rule/Position.java diff --git a/Classes/css/core/Rule.java b/Classes/css/core/Rule.java new file mode 100644 index 0000000..81ccbf8 --- /dev/null +++ b/Classes/css/core/Rule.java @@ -0,0 +1,9 @@ +package Classes.css.core; + +import javafx.scene.Node; + +public interface Rule{ + + public void apply(Node target) throws Exception; + +} diff --git a/Classes/css/core/Ruleset.java b/Classes/css/core/Ruleset.java new file mode 100644 index 0000000..065abb8 --- /dev/null +++ b/Classes/css/core/Ruleset.java @@ -0,0 +1,104 @@ +package Classes.css.core; + +import java.util.ArrayList; + +import Classes.css.core.rule.JavaFX; +import Classes.css.core.rule.Padding; +import Classes.css.core.rule.Position; +import javafx.scene.Node; + +public class Ruleset { + + /* Attributes */ + private Node target; + private ArrayList rule; + + + /* Builder -> static instance builder */ + public static Ruleset load(Node target) { + + /* (1) Create instance */ + Ruleset instance = new Ruleset(target); + + /* (2) Chain design pattern */ + return instance; + + } + + + /* Constructor */ + private Ruleset(Node target) { + + /* (1) Store target */ + this.target = target; + + /* (2) Init. rule list */ + this.rule = new ArrayList(); + + } + + + /* Add a rule */ + public Ruleset add(String l_side, Object... r_side) throws Exception{ + + + /* (1) Init. + ------------------------------------ */ + /* (1) If no argument -> abort */ + if( r_side.length == 0 ) + throw new Exception("No @r_side given"); + + /* (2) Init. rule container */ + Rule new_rule = null; + + + /* (1) Manage Rule Type (auto Builder) + ------------------------------------ */ + /* (1) Padding*/ + if( l_side == "padding" ) + new_rule = (Rule) new Padding(r_side); + + /* (2) Position */ + else if( l_side == "top" || l_side == "left" || l_side == "right" || l_side == "bottom" ) + new_rule = (Rule) new Position(l_side, (int) r_side[0]); + + + /* (x) If nothing custom -> suppose java fx rule */ + else + new_rule = (Rule) new JavaFX(l_side, (String) r_side[0]); + + + + /* (2) Add the rule to the ruleset + ------------------------------------ */ + /* (1) Add the rule to the ruleset (only if not null) */ + if( new_rule != null ) + this.rule.add( new_rule ); + + /* (2) Chain design pattern */ + return this; + + } + + + /* Apply all rules */ + public void apply() throws Exception{ + + /* (1) Init. meter */ + int i = 0; + int il = this.rule.size(); + + /* (2) Apply each rule */ + for( ; i < il ; i++ ) + this.rule.get(i).apply(this.target); + + /* (3) Stop the Chain design pattern */ + return; + + + } + + + + +} diff --git a/Classes/css/core/rule/JavaFX.java b/Classes/css/core/rule/JavaFX.java new file mode 100644 index 0000000..4abdf99 --- /dev/null +++ b/Classes/css/core/rule/JavaFX.java @@ -0,0 +1,35 @@ +package Classes.css.core.rule; + +import Classes.css.core.Rule; +import javafx.scene.Node; + +public class JavaFX implements Rule{ + + /* Attributes */ + private String l_side; + private String r_side; + + + /* Constructor -> Dispatch attributes */ + public JavaFX(String l_side, String r_side){ + + /* (1) Initialize values */ + this.l_side = l_side; + this.r_side = r_side; + + } + + + public void apply(Node target){ + + /* (1) Prefix with '-fx-' */ + String css_prop = "-fx-"+this.l_side+": "+this.r_side+";"; // "name: val;" + + /* (2) Apply to target */ + target.setStyle(css_prop); + + + } + + +} diff --git a/Classes/css/core/rule/Padding.java b/Classes/css/core/rule/Padding.java new file mode 100644 index 0000000..8a4720c --- /dev/null +++ b/Classes/css/core/rule/Padding.java @@ -0,0 +1,108 @@ +package Classes.css.core.rule; + +import java.util.ArrayList; + +import Classes.css.core.Rule; +import javafx.geometry.Insets; +import javafx.scene.Node; +import javafx.scene.layout.Region; + +public class Padding implements Rule{ + + /* Attributes */ + private Integer top; + private Integer right; + private Integer bottom; + private Integer left; + + + /* Constructor -> Dispatch attributes */ + public Padding(Object[] r_side) throws Exception{ + + + /* (1) Initialization + ------------------------------------------*/ + /* (1) Initialize values */ + this.top = null; + this.left = null; + this.bottom = null; + this.right = null; + + /* (2) Cast Object[] to ArrayList */ + ArrayList_r_side = this.auto_cast(r_side); + + + + /* (2) Manage/store values + ------------------------------------------*/ + switch(r_side.length){ + + /* (1) If 4 -> (top, right, bottom, left) */ + case 4: + this.top = _r_side.get(0); + this.right = _r_side.get(1); + this.bottom = _r_side.get(2); + this.left = _r_side.get(3); + break; + + /* (2) If 3 -> (top, right+left, bottom) */ + case 3: + this.top = _r_side.get(0); + this.right = _r_side.get(1); + this.left = _r_side.get(1); + this.bottom = _r_side.get(2); + break; + + /* (3) If 2 -> (top+botton, right+left) */ + case 2: + this.top = _r_side.get(0); + this.bottom = _r_side.get(0); + this.right = _r_side.get(1); + this.left = _r_side.get(1); + break; + + /* (4) If 1 -> (top+botton+right+left) */ + case 1: + this.top = _r_side.get(0); + this.bottom = _r_side.get(0); + this.right = _r_side.get(0); + this.left = _r_side.get(0); + break; + + } + + } + + + + public void apply(Node target){ + + /* (1) Set padding inset */ + Insets pad_inset = new Insets(this.top, this.right, this.bottom, this.left); + + /* (2) Apply to target */ + Region _target = (Region) target; + _target.setPadding(pad_inset); + + } + + + + + + /* Cast from Object[] to Integer[] */ + private ArrayList auto_cast(Object[] cast_in) throws Exception{ + + /* (1) Init. output */ + ArrayList cast_out = new ArrayList(); + + /* (2) Parse input to try to parse values */ + for( Object obj_in : cast_in ) + cast_out.add( Integer.valueOf(obj_in.toString()) ); + + /* (3) Return casted array */ + return cast_out; + } + + +} diff --git a/Classes/css/core/rule/Position.java b/Classes/css/core/rule/Position.java new file mode 100644 index 0000000..8c48564 --- /dev/null +++ b/Classes/css/core/rule/Position.java @@ -0,0 +1,56 @@ +package Classes.css.core.rule; + +import Classes.css.core.Rule; +import javafx.scene.Node; +import javafx.scene.layout.AnchorPane; + +public class Position implements Rule{ + + /* Attributes */ + private String direction; + private Integer offset; + + + /* Constructor -> Dispatch attributes */ + public Position(String l_side, int r_side){ + + this.direction = l_side; + this.offset = r_side; + + } + + + public void apply(Node target) throws Exception{ + + /* (1) Initialization + ------------------------------------ */ + /* (1) If not anchor pane -> abort */ + if( !(target.getParent() instanceof AnchorPane) ) + throw new Exception("Target's parent is not an 'AnchorPane'"); + + /* (2) Cast for readability */ + double _offset = this.offset; + + + /* (2) Manage directions + ------------------------------------ */ + /* (1) Direction: TOP */ + if( this.direction == "top" ) + AnchorPane.setTopAnchor(target, _offset); + + /* (2) Direction: LEFT */ + else if( this.direction == "left" ) + AnchorPane.setLeftAnchor(target, _offset); + + /* (3) Direction: RIGHT */ + else if( this.direction == "right" ) + AnchorPane.setRightAnchor(target, _offset); + + /* (4) Direction: BOTTOM */ + else if( this.direction == "bottom" ) + AnchorPane.setBottomAnchor(target, _offset); + + } + + +}