2017-11-15 13:23:13 +00:00
|
|
|
package Classes.css.core;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
2017-11-15 16:17:45 +00:00
|
|
|
import javafx.scene.paint.Color;
|
|
|
|
|
2017-11-15 13:23:13 +00:00
|
|
|
public class Context{
|
2017-11-15 16:17:45 +00:00
|
|
|
|
|
|
|
public final static int TYPE_INT = 1;
|
|
|
|
public final static int TYPE_STR = 2;
|
|
|
|
public final static int TYPE_COL = 3;
|
2017-11-15 13:23:13 +00:00
|
|
|
|
|
|
|
/* Context data (associative array) */
|
2017-11-15 16:17:45 +00:00
|
|
|
private static HashMap<String, Object> attr = new HashMap<String, Object>();
|
|
|
|
private static HashMap<String, Integer> type = new HashMap<String, Integer>();
|
2017-11-15 13:23:13 +00:00
|
|
|
|
|
|
|
/* Add data */
|
2017-11-15 16:17:45 +00:00
|
|
|
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 */
|
2017-11-15 13:23:13 +00:00
|
|
|
Context.attr.put(index, data);
|
2017-11-15 16:17:45 +00:00
|
|
|
Context.type.put(index, Context.TYPE_STR);
|
2017-11-15 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:17:45 +00:00
|
|
|
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);
|
2017-11-15 13:23:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 16:17:45 +00:00
|
|
|
/* 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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-11-15 13:23:13 +00:00
|
|
|
}
|