JavaFX/Classes/css/core/Context.java

79 lines
2.1 KiB
Java

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, Integer> type = new HashMap<String, Integer>();
/* Add data */
public static void bind(String index, Integer data){ /* INT */
Context.attr.put(index, data);
Context.type.put(index, Context.TYPE_INT);
}
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, Color data){ /* Color */
Context.attr.put(index, data);
Context.type.put(index, Context.TYPE_COL);
}
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);
}
}