getString/getColor/getInt + input type overload + adapt existing css

This commit is contained in:
xdrm-brackets 2017-11-15 17:17:45 +01:00
parent f544e03132
commit ebb0c5efb3
6 changed files with 81 additions and 33 deletions

View File

@ -2,19 +2,72 @@ package Classes.css.core;
import java.util.HashMap;
import javafx.scene.paint.Color;
public class Context{
public final static int TYPE_INT = 1;
public final static int TYPE_STR = 2;
public final static int TYPE_COL = 3;
/* Context data (associative array) */
private static HashMap<String, Object> attr = new HashMap<String, Object>();
private static HashMap<String, Object> attr = new HashMap<String, Object>();
private static HashMap<String, Integer> type = new HashMap<String, Integer>();
/* Add data */
public static void bind(String index, Object data){
public static void bind(String index, Integer data){ /* INT */
Context.attr.put(index, data);
Context.type.put(index, Context.TYPE_INT);
}
/* Fetch data */
public static Object get(String index){
return Context.attr.get(index);
public static void bind(String index, String data){ /* String */
Context.attr.put(index, data);
Context.type.put(index, Context.TYPE_STR);
}
public static void bind(String index, int r, int g, int b){ /* Color */
Context.attr.put(index, Color.rgb(r, g, b));
Context.type.put(index, Context.TYPE_COL);
}
/* Fetch data -> String */
public static String getString(String index){
/* (1) If color: return HEX */
if( Context.type.get(index) == Context.TYPE_COL )
return String.format("#%02X%02X%02X",
(int)( ((Color) Context.attr.get(index)).getRed() * 255 ),
(int)( ((Color) Context.attr.get(index)).getGreen() * 255 ),
(int)( ((Color) Context.attr.get(index)).getBlue() * 255 )
);
/* (2) By default -> String auto cast*/
return Context.attr.get(index).toString();
}
/* Fetch data -> Int */
public static Integer getInt(String index) throws Exception{
/* (1) If not INT -> abort */
if( Context.type.get(index) != Context.TYPE_INT )
throw new Exception("Not an int value");
/* (2) By default -> int */
return (Integer) Context.attr.get(index);
}
/* Fetch data -> Color */
public static Color getColor(String index) throws Exception{
/* (1) If not COLOR -> abort */
if( Context.type.get(index) != Context.TYPE_COL )
throw new Exception("Not a color value");
/* (2) By default -> COLOR */
return (Color) Context.attr.get(index);
}
}

View File

@ -14,6 +14,10 @@ public class ContextBuilder{
Context.bind("header-height", 50);
Context.bind("menu-width", 237);
/* (3) Colors */
Context.bind("main-color", 63, 81, 181);
Context.bind("menu-bg", 255, 255, 253);
}
}

View File

@ -15,18 +15,15 @@ public class Header{
// .add("left", 0)
// .add("right", 0)
.add("min-height", Context.get("header-height"))
.add("pref-height", Context.get("header-height"))
.add("max-height", Context.get("header-height"))
.add("min-height", Context.getInt("header-height"))
.add("pref-height", Context.getInt("header-height"))
.add("max-height", Context.getInt("header-height"))
.add("min-width", Context.get("screen-width"))
.add("pref-width", Context.get("screen-width"))
.add("max-width", Context.get("screen-width"))
.add("min-width", Context.getInt("screen-width"))
.add("pref-width", Context.getInt("screen-width"))
.add("max-width", Context.getInt("screen-width"))
.add("background-color", "#3f51b5")
// .add("border-width", "0 0 1 0")
// .add("border-color", "#3240a3")
.add("background-color", Context.getString("main-color"))
.apply();
}

View File

@ -9,23 +9,18 @@ public class MenuContainer{
/* Builds all necessary CSS/layout rules */
public MenuContainer(Node target) throws Exception{
/* (1) Calculate menu height (screen.height - header.height) */
Integer menu_height = Integer.valueOf( Context.get("screen-height").toString() );
menu_height -= Integer.valueOf( Context.get("header-height").toString() );
/* (2) Set rules */
/* (1) Set rules */
Ruleset.load(target)
.add("top", Context.get("header-height"))
.add("top", Context.getInt("header-height"))
.add("left", 0)
.add("min-width", Context.get("menu-width"))
.add("max-width", Context.get("menu-width"))
.add("min-width", Context.getInt("menu-width"))
.add("max-width", Context.getInt("menu-width"))
.add("min-height", menu_height)
.add("max-height", menu_height)
.add("min-height", Context.getInt("screen-height") - Context.getInt("header-height"))
.add("max-height", Context.getInt("screen-height") - Context.getInt("header-height"))
.add("background-color", "#fffffd")
.add("background-color", Context.getString("menu-bg"))
.apply();
}

View File

@ -1,9 +1,8 @@
@import "./constants.css";
/*
#header{
-fx-border-width: 0 0 1 0;
-fx-border-color: #3240a3;
} */
-fx-background-color: #3f51b5;
}
#header_menu{

View File

@ -17,7 +17,7 @@
}
/*
#menu_container{
-fx-min-width: 237;
@ -28,4 +28,4 @@
-fx-background-color: #fffffd;
}*/
}