package mdd.client; import javafx.beans.property.DoubleProperty; import javafx.beans.property.SimpleDoubleProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.event.EventHandler; import javafx.scene.Group; import javafx.scene.control.TextField; import javafx.scene.input.*; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.shape.Circle; import javafx.scene.text.Font; import javafx.scene.text.Text; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; import javax.xml.stream.EventFilter; public class Connectable extends Group implements IGUIEventClient ,ChangeListener{ private GUIEventHandler _eventHandler = new GUIEventHandler(); private Circle _border; private Circle _circle; private boolean _connected = false; private boolean _optimizable = false; private Module _parent; private ConnectableType _type; public String ID = ""; public DoubleProperty x = new SimpleDoubleProperty(); public DoubleProperty y = new SimpleDoubleProperty(); public Connectable(Module parent, ConnectableType type, String id){ _parent = parent; _type = type; ID = id; _parent.translateXProperty().addListener(this); _parent.translateYProperty().addListener(this); _parent.hlayout.translateXProperty().addListener(this); _parent.hlayout.translateYProperty().addListener(this); if (_type == ConnectableType.INPUT){ _parent.inputs.layoutXProperty().addListener(this); _parent.inputs.layoutYProperty().addListener(this); } else{ _parent.outputs.layoutXProperty().addListener(this); _parent.outputs.layoutYProperty().addListener(this); } layoutXProperty().addListener(this); layoutYProperty().addListener(this); _border = new Circle(0,0, 7); _border.setFill(Color.BLACK); _circle = new Circle(0,0,5); _circle.setFill(Color.WHITE); _circle.setOnMouseClicked(new EventHandler() { @Override public void handle(MouseEvent e) { if(e.getButton() == MouseButton.SECONDARY) { if (!_connected){ _optimizable = !_optimizable; updateColor(); } } } }); getChildren().addAll(_border, _circle); _circle.addEventHandler(MouseEvent.DRAG_DETECTED, e -> { Dragboard db = startDragAndDrop(TransferMode.MOVE); ClipboardContent content = new ClipboardContent(); JSONObject json = generateJSON(); content.putString(json.toString()); System.out.println("[Connectable]: started drag with id = " + json.toString()); db.setContent(content); //e.consume(); }); _circle.addEventHandler(DragEvent.DRAG_OVER, e -> { Dragboard db = e.getDragboard(); if (db.hasString()) { e.acceptTransferModes(TransferMode.MOVE); } e.consume(); }); _circle.addEventHandler(DragEvent.DRAG_DROPPED, e -> { Dragboard db = e.getDragboard(); boolean success = false; if (db.hasString()) { JSONParser parser = new JSONParser(); JSONObject json = null; try { json = (JSONObject)parser.parse(db.getString()); } catch (ParseException parseException) { parseException.printStackTrace(); } JSONArray jsonList = new JSONArray(); jsonList.add(json); jsonList.add(generateJSON()); ClipboardContent content = new ClipboardContent(); content.putString(jsonList.toString()); db.setContent(content); System.out.println("Dropped: " + db.getString()); success = true; } //e.setDropCompleted(success); //e.consume(); }); } private void updateColor(){ if (_connected){ _circle.setFill(Color.YELLOW); } else if (_optimizable) { _circle.setFill(Color.GREEN); } else { _circle.setFill(Color.WHITE); } } private JSONObject generateJSON(){ JSONObject json = new JSONObject(); if (_type == ConnectableType.INPUT){ json.put("type", "INPUT"); }else if(_type == ConnectableType.OUTPUT){ json.put("type", "OUTPUT"); }else{ json.put("type", "ANYTHING"); } JSONArray listID = new JSONArray(); listID.add(ID); listID.add(_parent.ID); json.put("ID", listID); return json; } public void setConnected(boolean state){ if (state != _connected){ _connected = state; updateColor(); } } @Override public void changed(ObservableValue observable, Number oldValue, Number newValue) { if (_type == ConnectableType.INPUT){ x.setValue(_parent.getTranslateX()+_parent.inputs.getLayoutX()+_parent.hlayout.getTranslateX()+getLayoutX()); y.setValue(_parent.getTranslateY()+_parent.inputs.getLayoutY()+_parent.hlayout.getTranslateY()+getLayoutY()); } else{ x.setValue(_parent.getTranslateX()+_parent.outputs.getLayoutX()+_parent.hlayout.getTranslateX()+getLayoutX()); y.setValue(_parent.getTranslateY()+_parent.outputs.getLayoutY()+_parent.hlayout.getTranslateY()+getLayoutY()); } } @Override public GUIEventHandler getGUIEventHandler() { return _eventHandler; } @Override public void setGUIEventHandler(GUIEventHandler eventHandler) { _eventHandler = eventHandler; _eventHandler.addEventListener(this); } @Override public void processGUIEvent(String event, JSONObject args) { } }