|
@@ -0,0 +1,159 @@
|
|
|
+package mdd.client.optimization;
|
|
|
+
|
|
|
+import javafx.beans.value.ChangeListener;
|
|
|
+import javafx.beans.value.ObservableValue;
|
|
|
+import javafx.geometry.Bounds;
|
|
|
+import javafx.scene.Group;
|
|
|
+import javafx.scene.control.Label;
|
|
|
+import javafx.scene.layout.GridPane;
|
|
|
+import javafx.scene.layout.VBox;
|
|
|
+import javafx.scene.paint.Color;
|
|
|
+import javafx.scene.shape.Rectangle;
|
|
|
+import mdd.client.GUIEventHandler;
|
|
|
+import mdd.client.IGUIEventClient;
|
|
|
+import org.json.simple.JSONArray;
|
|
|
+import org.json.simple.JSONObject;
|
|
|
+
|
|
|
+public class Permutation extends Group implements IGUIEventClient, ChangeListener<Bounds> {
|
|
|
+ private GridPane header = new GridPane();
|
|
|
+ private VBox dna = new VBox();
|
|
|
+ private Rectangle titlebar;
|
|
|
+ private Rectangle background;
|
|
|
+
|
|
|
+ protected GUIEventHandler _eventHandler;
|
|
|
+ protected JSONObject _jprocessor;
|
|
|
+ protected JSONArray _dna;
|
|
|
+ protected long _gen;
|
|
|
+
|
|
|
+ Label _status = new Label();
|
|
|
+ Label _fitness = new Label();
|
|
|
+ Label _time = new Label();
|
|
|
+
|
|
|
+ /*
|
|
|
+ staus: " " | fitness: | time:
|
|
|
+ dna:
|
|
|
+ */
|
|
|
+ public Permutation(long gen, JSONObject json){
|
|
|
+ _gen = gen;
|
|
|
+ _eventHandler = GUIEventHandler.getEventHandler();
|
|
|
+ _eventHandler.addEventListener(this, 5);
|
|
|
+ setFocusTraversable(true);
|
|
|
+ dna.setSpacing(10);
|
|
|
+ dna.setTranslateX(5);
|
|
|
+ dna.setTranslateY(30);
|
|
|
+
|
|
|
+ titlebar = new Rectangle(0.0f, 0.0f, 1.0, 20f);
|
|
|
+ titlebar.setArcWidth(10.0f);
|
|
|
+ titlebar.setArcHeight(10.0f);
|
|
|
+
|
|
|
+ //background = new Rectangle(0.0f, 0.0f, 150-20, 150 + 40);
|
|
|
+ background = new Rectangle(0.0f, 0.0f, 1.0, 1.0);
|
|
|
+ background.setArcWidth(10.0f);
|
|
|
+ background.setArcHeight(10.0f);
|
|
|
+ background.setFill(Color.GRAY);
|
|
|
+ header.setTranslateX(10);
|
|
|
+ Color color = Color.DARKCYAN;
|
|
|
+ titlebar.setFill(color);
|
|
|
+
|
|
|
+ header.add(_status,0,0);
|
|
|
+ header.add(_fitness,1,0);
|
|
|
+ header.add(_time,2,0);
|
|
|
+
|
|
|
+ header.setHgap(5.0f);
|
|
|
+
|
|
|
+ configure(json);
|
|
|
+ getChildren().clear();
|
|
|
+ getChildren().addAll( background, titlebar, header, dna);
|
|
|
+ dna.layoutBoundsProperty().addListener(this);
|
|
|
+ header.layoutBoundsProperty().addListener(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected void configure(JSONObject json){
|
|
|
+ if(json.containsKey("status")){
|
|
|
+ _status.setText("status: "+ json.get("status").toString());
|
|
|
+ }
|
|
|
+ if(json.containsKey("fitness")){
|
|
|
+ _fitness.setText("fitness: "+ json.get("fitness").toString());
|
|
|
+ }
|
|
|
+ if(json.containsKey("time")){
|
|
|
+ _time.setText("time: "+ json.get("time").toString());
|
|
|
+ }
|
|
|
+ if (_dna == null){
|
|
|
+ if(json.containsKey("dna")){
|
|
|
+ dna.getChildren().clear();
|
|
|
+ _dna = (JSONArray)json.get("dna");
|
|
|
+ for (int i = 0; i < _dna.size(); ++i){
|
|
|
+ dna.getChildren().add(new Label(_dna.get(i).toString()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(json.containsKey("processor")){
|
|
|
+ _jprocessor = (JSONObject) json.get("processor");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void changed(ObservableValue<? extends Bounds> observableValue, Bounds oldValue, Bounds newValue) {
|
|
|
+ double width = header.getWidth();
|
|
|
+ if (newValue.getWidth() > width){
|
|
|
+ width =newValue.getWidth();
|
|
|
+ width += 10;
|
|
|
+ }else{
|
|
|
+ width += 10;
|
|
|
+ width +=10;
|
|
|
+ }
|
|
|
+ titlebar.setWidth(width);
|
|
|
+ titlebar.setHeight(20.0f);
|
|
|
+ background.setWidth(width);
|
|
|
+ background.setHeight(dna.getTranslateY()+dna.getHeight()+10);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void processGUIEvent(String event, JSONObject args) {
|
|
|
+ if (event.equals("change")) {
|
|
|
+ if (!args.containsKey("args")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject jargs = (JSONObject) args.get("args");
|
|
|
+ if (!jargs.containsKey("subject")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject jsub = (JSONObject) jargs.get("subject");
|
|
|
+ if (!jsub.get("type").equals("optimizer")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!args.containsKey("operation")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!"change".equals((String) args.get("operation"))) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject jobj = (JSONObject) jargs.get("object");
|
|
|
+ if (!jobj.containsKey("step")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (_gen!=(long)jobj.get("step")){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!jobj.containsKey("permutation")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ JSONObject jind = (JSONObject)jobj.get("permutation");
|
|
|
+ //System.out.println("Permutation: " + _dna.toString());
|
|
|
+ if (!_dna.equals((JSONArray)jind.get("dna"))) {
|
|
|
+ return;
|
|
|
+ }else{
|
|
|
+ if (!_dna.toString().equals(jind.get("dna").toString())){
|
|
|
+ System.out.println("It happened!");
|
|
|
+ }
|
|
|
+ configure(jind);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void close() throws Exception {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|