Browse Source

load modules

Willi Zschiebsch 4 years ago
parent
commit
85d140dc11

+ 113 - 0
src/main/java/mdd/client/Client.java

@@ -0,0 +1,113 @@
+package mdd.client;
+
+import org.json.simple.JSONObject;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+import org.zeromq.SocketType;
+import org.zeromq.ZMQ;
+import org.zeromq.ZContext;
+
+public class Client implements IGUIEventClient
+{
+    private ZContext context;
+    private ZMQ.Socket req_socket;
+    private ZMQ.Socket sub_socket;
+    private long server_id;
+    private long msg_counter = 0;
+    private Thread listenerThread;
+    private GUIEventHandler _eventHandler;
+
+    private void request_state(){
+        JSONObject json = new JSONObject();
+        json.put("state", "all");
+        JSONObject jmsg = request("get",json);
+        server_id = (long)jmsg.get("serverID");
+        msg_counter = (long) jmsg.get("msgNr");
+        System.out.println("TEST");
+    }
+
+    private void listen(){
+        while (!Thread.currentThread().isInterrupted()) {
+            // Read envelope with address
+            String address = sub_socket.recvStr();
+            // Read message contents
+            String content = sub_socket.recvStr();
+
+            JSONParser jsonParser = new JSONParser();
+            JSONObject jmsg = null;
+            try {
+                jmsg = (JSONObject) jsonParser.parse(content);
+            } catch (ParseException e) {
+                e.printStackTrace();
+            }
+
+            if ((long) jmsg.get("serverID") == server_id){
+                if ((long) jmsg.get("msgNr") == msg_counter) {
+                    _eventHandler.publishEvent("change", jmsg);
+                    System.out.println("[Client] "+ "change: " + jmsg.toString());
+                    ++msg_counter;
+                } else {
+                    request_state();
+                }
+            }
+            System.out.println(address + " : " + content);
+        }
+    }
+
+    public Client(){
+        _eventHandler = GUIEventHandler.getEventHandler();
+        _eventHandler.addEventListener(this);
+        try{
+            context = new ZContext();
+            req_socket = context.createSocket(SocketType.REQ);
+            req_socket.connect("tcp://localhost:5555");
+
+            sub_socket = context.createSocket(SocketType.SUB);
+            sub_socket.connect("tcp://localhost:5556");
+            sub_socket.subscribe("CHANGE".getBytes(ZMQ.CHARSET));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        request_state();
+
+        Runnable runnable = this::listen;
+        listenerThread = new Thread(runnable);
+        listenerThread.start();
+    }
+
+    public JSONObject request(JSONObject json){
+        req_socket.send(json.toString().getBytes(ZMQ.CHARSET), 0);
+        String reply = req_socket.recvStr(0);
+        JSONParser jsonParser = new JSONParser();
+        JSONObject jmsg = null;
+        try {
+            jmsg = (JSONObject) jsonParser.parse(reply);
+        } catch (ParseException e) {
+            e.printStackTrace();
+        }
+        return jmsg;
+    }
+
+    public JSONObject request(String flag, JSONObject json){
+        JSONObject args = new JSONObject();
+        args.put(flag, json);
+        return request(args);
+    }
+
+    @Override
+    public void processGUIEvent(String event, JSONObject args) {
+        if (event.equals("try")){
+            request("try",args);
+        }
+        if (event.equals("get")){
+            _eventHandler.publishEvent("receive", request("get", args));
+        }
+    }
+
+    @Override
+    public void close() throws Exception {
+        listenerThread.join();
+        //close Server
+        _eventHandler.removeEventListener(this);
+    }
+}

+ 10 - 6
src/main/java/mdd/client/FXMLDocumentController.java

@@ -3,25 +3,29 @@ package mdd.client;
 import javafx.fxml.FXML;
 import javafx.fxml.Initializable;
 import javafx.scene.control.SplitPane;
-import javafx.scene.control.Tab;
 import javafx.scene.control.TabPane;
-import javafx.scene.layout.AnchorPane;
-import javafx.scene.paint.Color;
-import javafx.scene.shape.Rectangle;
 
 import java.net.URL;
 import java.util.ResourceBundle;
 
 public class FXMLDocumentController implements Initializable {
     @FXML
-    private Workshop workshop;
+    public Workshop workshop;
     @FXML
-    private SplitPane sPane;
+    public Toolbox toolbox;
+    @FXML
+    public PropertyManager propertymanager;
+    @FXML
+    public SplitPane sPane;
 
     @Override
     public void initialize(URL location, ResourceBundle resources) {
         System.out.println("Loading...");
         workshop.setTabClosingPolicy(TabPane.TabClosingPolicy.ALL_TABS);
+        SplitPane.setResizableWithParent(workshop, false);
+        SplitPane.setResizableWithParent(toolbox, false);
+        SplitPane.setResizableWithParent(propertymanager, false);
+        sPane.setDividerPositions(0.1, 0.9);
     }
 
     //@FXML private TabPane tabPane;

+ 24 - 1
src/main/java/mdd/client/GUIMain.java

@@ -1,9 +1,13 @@
 package mdd.client;
 
 import javafx.application.Application;
+import javafx.beans.value.ChangeListener;
+import javafx.beans.value.ObservableValue;
+import javafx.fxml.FXML;
 import javafx.fxml.FXMLLoader;
 import javafx.scene.Parent;
 import javafx.scene.Scene;
+import javafx.scene.control.SplitPane;
 import javafx.scene.input.KeyCode;
 import javafx.stage.Stage;
 import javafx.scene.input.KeyEvent;
@@ -14,6 +18,7 @@ import java.io.IOException;
 
 public class GUIMain extends Application implements IGUIEventClient{
     private GUIEventHandler _eventHandler;
+    private Client client = new Client();
 
     public static void main(String[] args) {
         launch(args);
@@ -24,8 +29,9 @@ public class GUIMain extends Application implements IGUIEventClient{
         _eventHandler = GUIEventHandler.getEventHandler();
 
         Parent root = null;
+        FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
         try {
-            root = FXMLLoader.load(getClass().getResource("main.fxml"));
+            root = loader.load();
         } catch (IOException e) {
            throw new RuntimeException(e);
         }
@@ -50,12 +56,29 @@ public class GUIMain extends Application implements IGUIEventClient{
             }*/
         });
 
+        FXMLDocumentController controller = loader.getController();
+
         Scene scene = new Scene(root);
         scene.getStylesheets().add(String.valueOf(getClass().getResource("dark-theme.css")));
         primaryStage.setFullScreen(true);
         primaryStage.setMaximized(true);
         primaryStage.setScene(scene);
+
+        primaryStage.showingProperty().addListener(new ChangeListener<Boolean>() {
+
+            @Override
+            public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {
+                if (newValue) {
+                    controller.sPane.setDividerPositions(0.1f, 0.9f);
+                    observable.removeListener(this);
+                }
+            }
+
+        });
         primaryStage.show();
+
+
+
     }
 
     @Override

+ 0 - 30
src/main/java/mdd/client/HelloWorldServer.java

@@ -1,30 +0,0 @@
-package mdd.client;
-
-import org.zeromq.SocketType;
-import org.zeromq.ZMQ;
-import org.zeromq.ZContext;
-
-public class HelloWorldServer
-{
-    public static void main(String[] args) throws Exception
-    {
-        try (ZContext context = new ZContext()) {
-            // Socket to talk to clients
-            ZMQ.Socket socket = context.createSocket(SocketType.REQ);
-            socket.connect("tcp://localhost:5555");
-
-            for (int request = 0; request < 10; request++) {
-
-                String response = "Hello, world!";
-                socket.send(response.getBytes(ZMQ.CHARSET), 0);
-
-                byte[] reply = socket.recv(0);
-
-                // Print the message
-                System.out.println(
-                        "Received: [" + new String(reply, ZMQ.CHARSET) + "]"
-                );
-            }
-        }
-    }
-}

+ 1 - 1
src/main/java/mdd/client/Input.java

@@ -14,7 +14,7 @@ public class Input  extends Connectable {
         super(parent, ConnectableType.INPUT, id);
         _label = new Text("TEST");
         _label.setFont(new Font("Arial", 15));
-        _label.setFill(Color.WHITE);
+        _label.getStyleClass().add("text");
 
         _input = new TextField("{1, 2, 3}");
         _input.setPrefWidth(50);

+ 31 - 19
src/main/java/mdd/client/Module.java

@@ -25,31 +25,16 @@ public class Module extends Group implements IGUIEventClient{
 
     public String ID = "";
     public Vector<String> Prefix;
+    protected String Type = "module";
     public HBox hlayout = new HBox();
     public VBox inputs = new VBox();
     public VBox outputs = new VBox();
     public boolean isSelected = false;
 
-    public static Module DefaultModule(){
-        return new Module(new Vector<String>(), "", Color.DARKBLUE);
-    }
-
-    public Module(Vector<String> prefix, String id, Color color){
+    private void init(Color color){
         setFocusTraversable(true);
         _eventHandler = GUIEventHandler.getEventHandler();
         _eventHandler.addEventListener(this);
-        Prefix = prefix;
-        ID = id;
-        Rectangle titlebar;
-        Rectangle background;
-        Text caption;
-        addInput(new Input(this,"1"));
-        addInput(new Input(this,"2"));
-        addInput(new Input(this,"3"));
-
-        addOutput(new Output(this,"1"));
-        addOutput(new Output(this,"2"));
-        addOutput(new Output(this,"3"));
 
         inputs.setSpacing(10);
 
@@ -62,8 +47,10 @@ public class Module extends Group implements IGUIEventClient{
         hlayout.setTranslateY(30);
 
         // System.out.println("[Module]: pressed: "+ hbox.getPrefWidth());
+        Rectangle titlebar;
+        Rectangle background;
+        Text caption;
 
-        // JSONObject parameters
         titlebar = new Rectangle(0.0f, 0.0f, 150-20, 20.0f);
         titlebar.setArcWidth(10.0f);
         titlebar.setArcHeight(10.0f);
@@ -74,7 +61,7 @@ public class Module extends Group implements IGUIEventClient{
         background.setFill(Color.GRAY);
         caption = new Text("TEST");
         caption.setFont(new Font("Arial", 15));
-        caption.setFill(Color.WHITE);
+        caption.getStyleClass().add("text");
         caption.setX(10);
         caption.setY(15);
 
@@ -111,6 +98,30 @@ public class Module extends Group implements IGUIEventClient{
         });
     }
 
+    public static Module DefaultModule(){
+        return new Module(new Vector<String>(), "", Color.DARKBLUE);
+    }
+
+
+    public Module(JSONObject json){
+        init(Color.DARKCYAN);
+    }
+
+    public Module(Vector<String> prefix, String id, Color color){
+        Prefix = prefix;
+        ID = id;
+
+        addInput(new Input(this,"1"));
+        addInput(new Input(this,"2"));
+        addInput(new Input(this,"3"));
+
+        addOutput(new Output(this,"1"));
+        addOutput(new Output(this,"2"));
+        addOutput(new Output(this,"3"));
+
+        init(color);
+    }
+
     public void addInput(Input in){
         inputs.getChildren().add(in);
     }
@@ -151,6 +162,7 @@ public class Module extends Group implements IGUIEventClient{
         JSONObject json = new JSONObject();
         json.put("prefix", Prefix);
         json.put("id", ID);
+        json.put("type", Type);
         return json;
     }
 

+ 1 - 1
src/main/java/mdd/client/Output.java

@@ -11,7 +11,7 @@ public class Output extends Connectable {
         super(parent, ConnectableType.OUTPUT, id);
         _label = new Text("TEST");
         _label.setFont(new Font("Arial", 15));
-        _label.setFill(Color.WHITE);
+        _label.getStyleClass().add("text");
         _label.setTranslateX(-50);
         _label.setTranslateY(7);
         getChildren().add(_label);

+ 57 - 3
src/main/java/mdd/client/Processor.java

@@ -25,9 +25,8 @@ public class Processor extends Module{
     public Connection temp_connection = new Connection();
     private Connectable connectable;
 
-    public Processor(Vector<String> prefix, String id) {
-        super(prefix, id, Color.DARKVIOLET);
-
+    private void init(){
+        Type = "processor";
         modules.addEventHandler(MouseEvent.DRAG_DETECTED, e -> {
             // System.out.println("[Module]: dragg event detected!");
             if(e.getButton() == MouseButton.PRIMARY) {
@@ -119,6 +118,16 @@ public class Processor extends Module{
         });
     }
 
+    public Processor(JSONObject json){
+        super(json);
+        init();
+    }
+
+    public Processor(Vector<String> prefix, String id) {
+        super(prefix, id, Color.DARKVIOLET);
+        init();
+    }
+
     public Module getModule(String id){
         for (Node node : modules.getChildren()){
             if (node instanceof Module){
@@ -153,6 +162,10 @@ public class Processor extends Module{
     }
 
     public void addModule(Module module){
+        JSONObject json = new JSONObject();
+        json.put("id", getJSONIdentifier());
+        json.put("add", module.getJSONIdentifier());
+        _eventHandler.publishEvent("try", json);
         modules.getChildren().add(module);
     }
 
@@ -200,5 +213,46 @@ public class Processor extends Module{
                 //connections.getChildren().add(temp_connection);
             }
         }
+        if (event.equals("change")){
+            /*
+            "{
+            "id":{...},
+            "add":
+                {
+
+                }
+            }"
+             */
+            if (args.containsKey("id")){
+                System.out.println("[Processor] is " + args.get("id").toString() + " == " + getJSONIdentifier());
+                if (args.get("id").equals(getJSONIdentifier())){
+                    if (args.containsKey("add")){
+
+                        JSONObject jadd = (JSONObject)args.get("add");
+                        switch ((String) ((JSONObject) jadd.get("id")).get("type")) {
+                            case "processor" -> modules.getChildren().add(new Processor(jadd));
+                            case "module" -> modules.getChildren().add(new Module(jadd));
+                            case "connection" -> connections.getChildren().add(new Connection());
+                            default -> System.out.println("[Processor] Unexpected value: " + (String) jadd.get("type"));
+                        }
+                    }
+                }
+            }
+        }
+        if (event.equals("state")){
+            /*
+            "{
+            "id":{...},
+            "modules":
+                [{"id": {"type": "processor"}, "modules":[{},{},{}], "connections":[{},{},{}], "inputs": [{}], "outputs": [], "config":{}},
+                {"type": "module", "id": {}, "inputs": [{}], "outputs": [], "config":{}},
+                {2}],
+            "connections":[{},{}]
+            }"
+             */
+            if (args.get("id").equals(getJSONIdentifier())){
+                System.out.println("[Processor] not implemented load function called!");
+            }
+        }
     }
 }

+ 42 - 0
src/main/java/mdd/client/PropertyManager.java

@@ -0,0 +1,42 @@
+package mdd.client;
+
+import javafx.geometry.Pos;
+import javafx.scene.control.Accordion;
+import javafx.scene.control.Label;
+import javafx.scene.control.ScrollPane;
+import javafx.scene.control.TitledPane;
+import javafx.scene.layout.GridPane;
+import org.json.simple.JSONObject;
+
+public class PropertyManager extends ScrollPane implements IGUIEventClient{
+    private GUIEventHandler _eventhandler;
+
+    public PropertyManager(){
+        super();
+        _eventhandler = GUIEventHandler.getEventHandler();
+        _eventhandler.addEventListener(this);
+        Accordion accordion = new Accordion();
+        GridPane grid = new GridPane();
+        grid.setAlignment(Pos.CENTER);
+        grid.setHgap(10);
+        grid.setVgap(10);
+        TitledPane pane1 = new TitledPane("Properties", new Label("WIP"));
+        TitledPane pane2 = new TitledPane("other Properties", new Label("WIP"));
+
+        accordion.getPanes().add(pane1);
+        accordion.getPanes().add(pane2);
+
+        setContent(accordion);
+        setFitToWidth(true);
+    }
+
+    @Override
+    public void close() throws Exception {
+        _eventhandler.removeEventListener(this);
+    }
+
+    @Override
+    public void processGUIEvent(String event, JSONObject args) {
+
+    }
+}

+ 66 - 0
src/main/java/mdd/client/Toolbox.java

@@ -0,0 +1,66 @@
+package mdd.client;
+
+import javafx.geometry.Pos;
+import javafx.scene.control.Accordion;
+import javafx.scene.control.Label;
+import javafx.scene.control.ScrollPane;
+import javafx.scene.control.TitledPane;
+import javafx.scene.layout.GridPane;
+import org.json.simple.JSONArray;
+import org.json.simple.JSONObject;
+
+public class Toolbox extends ScrollPane implements IGUIEventClient{
+    private GUIEventHandler _eventhandler;
+    private GridPane gridpane_modules = new GridPane();
+    private GridPane gridpane_processors = new GridPane();
+
+    public Toolbox(){
+        super();
+        _eventhandler = GUIEventHandler.getEventHandler();
+        _eventhandler.addEventListener(this);
+        Accordion accordion = new Accordion();
+        GridPane grid = new GridPane();
+        grid.setAlignment(Pos.CENTER);
+        grid.setHgap(10);
+        grid.setVgap(10);
+        TitledPane pane1 = new TitledPane("Modules", gridpane_modules);
+        TitledPane pane2 = new TitledPane("Processors", gridpane_processors);
+
+        accordion.getPanes().add(pane1);
+        accordion.getPanes().add(pane2);
+
+        JSONObject json = new JSONObject();
+        json.put("module", "all");
+        json.put("process", "all");
+
+        _eventhandler.publishEvent("get", json);
+
+        setContent(accordion);
+        setFitToWidth(true);
+    }
+
+    @Override
+    public void close() throws Exception {
+        _eventhandler.removeEventListener(this);
+    }
+
+    @Override
+    public void processGUIEvent(String event, JSONObject args) {
+        if (event.equals("receive")){
+            if (args.containsKey("module")){
+                JSONArray array = (JSONArray) args.get("module");
+                gridpane_modules.getChildren().clear();
+                for (int i = 0; i < array.size(); i++){
+                    gridpane_modules.add(new Label((String)array.get(i)),0,i);
+                }
+            }
+            if (args.containsKey("process")){
+                JSONArray array = (JSONArray) args.get("process");
+                gridpane_processors.getChildren().clear();
+                for (int i = 0; i < array.size(); i++){
+                    gridpane_processors.add(new Label((String)array.get(i)),0,i);
+                }
+            }
+        }
+    }
+}

+ 1 - 0
src/main/java/mdd/client/Workshop.java

@@ -25,6 +25,7 @@ public class Workshop extends TabPane implements IGUIEventClient {
 
     private void loadExample(){
         _main_processor = new Processor(new Vector<String>(),"0");
+        System.out.println("[Workshop] " + _main_processor.getJSONIdentifier().toString());
         _main_processor.addModule(new Module(_main_processor.getAsPrefix(), "1", Color.DODGERBLUE));
         _main_processor.addModule(new Module(_main_processor.getAsPrefix(),"2", Color.DARKRED));
 

+ 6 - 2
src/main/resources/mdd/client/dark-theme.css

@@ -6,12 +6,16 @@
     -fx-control-inner-background-alt: -fx-control-inner-background ;
 }
 
+.text {
+    -fx-fill: white;
+}
+
 .label{
-    -fx-text-fill: lightgray;
+    -fx-text-fill: white;
 }
 
 .text-field {
-    -fx-prompt-text-fill: gray;
+    -fx-prompt-text-fill: white;
 }
 
 .titulo{

+ 6 - 42
src/main/resources/mdd/client/main.fxml

@@ -4,6 +4,8 @@
 <?import javafx.scene.layout.*?>
 
 <?import mdd.client.Workshop?>
+<?import mdd.client.Toolbox?>
+<?import mdd.client.PropertyManager?>
 <VBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="mdd.client.FXMLDocumentController">
    <children>
        <MenuBar>
@@ -14,49 +16,11 @@
            <Menu fx:id="mAbout" text="About">
            </Menu>
        </MenuBar>
-      <SplitPane fx:id="sPane" dividerPositions="0.2, 0.8" prefHeight="${sPane.parent.height}" prefWidth="600.0">
+      <SplitPane fx:id="sPane" dividerPositions="0.1, 0.9" prefHeight="${sPane.parent.height}">
         <items>
-            <Accordion>
-              <panes>
-                <TitledPane animated="false" text="untitled 1">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-                <TitledPane animated="false" text="untitled 2">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-                <TitledPane animated="false" text="untitled 3">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-              </panes>
-            </Accordion>
-            <Workshop fx:id="workshop" prefHeight="238.0" prefWidth="400.0" tabClosingPolicy="UNAVAILABLE">
-
-            </Workshop>
-            <Accordion>
-              <panes>
-                <TitledPane animated="false" text="untitled 1">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-                <TitledPane animated="false" text="untitled 2">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-                <TitledPane animated="false" text="untitled 3">
-                  <content>
-                    <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="100.0" />
-                  </content>
-                </TitledPane>
-              </panes>
-            </Accordion>
+            <Toolbox fx:id="toolbox" minHeight="0.0" minWidth="0.0" prefHeight="238.0" prefWidth="100.0"/>
+            <Workshop fx:id="workshop" minHeight="0.0" minWidth="0.0" prefHeight="238.0" prefWidth="800.0" tabClosingPolicy="UNAVAILABLE"/>
+            <PropertyManager fx:id="propertymanager" minHeight="0.0" minWidth="0.0" prefHeight="238.0" prefWidth="100.0"/>
         </items>
       </SplitPane>
    </children>