Browse Source

big improvements

Willi Zschiebsch 4 years ago
parent
commit
524892db70

+ 27 - 4
src/main/java/mdd/client/Client.java

@@ -65,14 +65,34 @@ public class Client implements IGUIEventClient
                     Platform.runLater(new Runnable() {
                                           @Override
                                           public void run() {
-                                              _eventHandler.publishEvent("change", (JSONObject) finalJmsg.get("change"));//<--do in main Thread
-                                              System.out.println("[Client] " + "change: " + finalJmsg.toString());
+                                              JSONObject jchange = (JSONObject) finalJmsg.get("change");
+                                              String soper = (String)jchange.get("operation");
+                                              if (soper.equals("change")){
+                                                  JSONObject jargs = (JSONObject) jchange.get("args");
+                                                  if (jargs != null){
+                                                      JSONObject jobj = (JSONObject) jargs.get("object");
+                                                      if (jobj != null){
+                                                          if (jobj.containsKey("GUI")){
+                                                              _eventHandler.publishEvent(5,"change", jchange);
+                                                          }
+                                                      }
+                                                  }
+                                              }
+                                              else {
+                                                  _eventHandler.publishEvent("change", jchange);
+                                              }
+                                              //System.out.println("[Client] " + "change: " + finalJmsg.toString());
                                               ++msg_counter;
                                           }
                                       });
                     //<--reopen Thread
                 } else {
-                    request_state();
+                    Platform.runLater(new Runnable() {
+                        @Override
+                        public void run() {
+                            request_state();
+                        }
+                     });
                 }
             }
             System.out.println(address + " : " + content);
@@ -81,7 +101,7 @@ public class Client implements IGUIEventClient
 
     public Client(){
         _eventHandler = GUIEventHandler.getEventHandler();
-        _eventHandler.addEventListener(this);
+        _eventHandler.addEventListener(this, 10);
         try{
             context = new ZContext();
             req_socket = context.createSocket(SocketType.REQ);
@@ -130,6 +150,7 @@ public class Client implements IGUIEventClient
 
     @Override
     public void processGUIEvent(String event, JSONObject args) {
+        long start = System.currentTimeMillis();
         if (event.equals("try")){
             JSONObject jans = request("try",args);
         }
@@ -137,6 +158,8 @@ public class Client implements IGUIEventClient
             JSONObject jans = request("get",  args);
             _eventHandler.publishEvent("receive", (JSONObject)jans.get("receive"));
         }
+        long end = System.currentTimeMillis();
+        System.out.println("[Client]: "+event+": " +(end-start));
     }
 
     @Override

+ 77 - 0
src/main/java/mdd/client/FXMLDocumentController.java

@@ -1,14 +1,21 @@
 package mdd.client;
 
+import javafx.application.Platform;
+import javafx.event.Event;
 import javafx.fxml.FXML;
 import javafx.fxml.Initializable;
 import javafx.scene.control.SplitPane;
 import javafx.scene.control.TabPane;
+import javafx.stage.FileChooser;
+import javafx.stage.Stage;
+import org.json.simple.JSONObject;
 
+import java.io.File;
 import java.net.URL;
 import java.util.ResourceBundle;
 
 public class FXMLDocumentController implements Initializable {
+    private GUIEventHandler _eventhandler = GUIEventHandler.getEventHandler();
     @FXML
     public Workshop workshop;
     @FXML
@@ -18,6 +25,76 @@ public class FXMLDocumentController implements Initializable {
     @FXML
     public SplitPane sPane;
 
+    private Stage primaryStage;
+
+    public void setStage(Stage stage){
+        primaryStage = stage;
+    }
+
+    @FXML
+    public void miNewClicked(Event e){
+        System.out.println("[FXMLDocumentController]: Not implemented!");
+    }
+
+    @FXML
+    public void miOpenClicked(Event e){
+        System.out.println("[FXMLDocumentController]: Not implemented!");
+    }
+
+    @FXML
+    public void miOpen_RecentClicked(Event e){
+        System.out.println("[FXMLDocumentController]: Not implemented!");
+    }
+
+    @FXML
+    public void miClose_ProjectClicked(Event e){
+        System.out.println("[FXMLDocumentController]: Not implemented!");
+    }
+
+    @FXML
+    public void miSettingsClicked(Event e){
+        System.out.println("[FXMLDocumentController]: Not implemented!");
+    }
+
+    @FXML
+    public void miSaveClicked(Event e){
+        JSONObject json = new JSONObject();
+        json.put("operation", "save");
+        json.put("args", "");
+        _eventhandler.publishEvent("try", json);
+    }
+
+    @FXML
+    public void miSave_AsClicked(Event e){
+        FileChooser fileChooser = new FileChooser();
+
+        //Set extension filter for text files
+        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("JSON files (*.json)", "*.json");
+        fileChooser.getExtensionFilters().add(extFilter);
+
+        //Show save file dialog
+        File file = fileChooser.showSaveDialog(primaryStage);
+
+        if (file != null) {
+            //saveTextToFile(sampleText, file);
+            JSONObject json = new JSONObject();
+            json.put("operation", "state");
+            json.put("args", "all");
+
+            _eventhandler.publishEvent("try", json);
+            System.out.println("[FXMLDocumentController]: Not implemented!");
+        }
+    }
+
+    @FXML
+    public void miExitClicked(Event e){
+        Platform.exit();
+        System.exit(0);
+    }
+
+
+
+
     @Override
     public void initialize(URL location, ResourceBundle resources) {
         System.out.println("Loading...");

+ 40 - 9
src/main/java/mdd/client/GUIEventHandler.java

@@ -3,30 +3,61 @@ package mdd.client;
 
 import org.json.simple.JSONObject;
 
-import java.util.Vector;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentSkipListMap;
 
 public class GUIEventHandler {
     private static GUIEventHandler _handler = new GUIEventHandler();
-    private Vector<IGUIEventClient> _clients = new Vector<IGUIEventClient>();
+    private ConcurrentSkipListMap<Integer,Vector<IGUIEventClient>> _priorities = new ConcurrentSkipListMap<>();
 
     public GUIEventHandler(){
 
     }
 
+    public void addEventListener(IGUIEventClient client, Integer level){
+        Vector<IGUIEventClient> clients =  _priorities.get(level);
+        if (clients != null ){
+            clients.add(client);
+        }else{
+            clients = new Vector<>();
+            clients.add(client);
+            _priorities.put(level, clients);
+        }
+    }
+
     public void addEventListener(IGUIEventClient client){
-        _clients.add(client);
+        addEventListener(client, 0);
     }
 
     public void removeEventListener(IGUIEventClient client){
-        _clients.remove(client);
+        for (Map.Entry<Integer,Vector<IGUIEventClient>> entry : _priorities.entrySet()){
+            if (entry.getValue().remove(client)){
+                return;
+            }
+        }
     }
 
-    public void publishEvent(String event, JSONObject args){
-        for (int i = 0; i < _clients.size(); ++i) {
-            _clients.elementAt(i).processGUIEvent(event, args);
-            //System.out.println(i+1 + " | " + _clients.size());
+    public void publishEvent(Integer level, String event, JSONObject args){
+        Integer start_key = _priorities.ceilingKey(level);
+        if (start_key != null){
+            for (Map.Entry<Integer,Vector<IGUIEventClient>> entry : _priorities.entrySet()){
+                if (entry.getKey() >= start_key){
+                    Vector<IGUIEventClient> clients = entry.getValue();
+                    for (int i = 0; i < clients.size(); ++i) {
+                        if (i == 69){
+                            int test = 0;
+                        }
+                        clients.elementAt(i).processGUIEvent(event, args);
+                    }
+                }
+            }
         }
-    };
+    }
+
+    public void publishEvent(String event, JSONObject args){
+        publishEvent(0,event, args);
+    }
 
     public static GUIEventHandler getEventHandler(){
         return _handler;

+ 5 - 4
src/main/java/mdd/client/GUIMain.java

@@ -45,13 +45,14 @@ public class GUIMain extends Application implements IGUIEventClient{
             System.out.println("[Application]: "+e.getText());
             JSONObject json = new JSONObject();
             KeyCode code = e.getCode();
-            if (e.getCode() == KeyCode.ENTER){
+
+            if (code == KeyCode.ENTER){
                 json.put("key", "ENTER");
-            }else{
-                //json.put("key", e.getCharacter().toLowerCase());
+            }else if(code == KeyCode.DELETE){
+                json.put("key", "DELETE");
             }
 
-            _eventHandler.publishEvent("KeyTyped", json);
+            _eventHandler.publishEvent(5, "KeyTyped", json);
             /*if (isSelected) {
                 JSONObject json = new JSONObject();
                 json.put("id", ID);

+ 21 - 4
src/main/java/mdd/client/Module.java

@@ -60,9 +60,10 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
     public boolean isSelected = false;
 
     public Module(JSONObject json){
+        
         setFocusTraversable(true);
         _eventHandler = GUIEventHandler.getEventHandler();
-        _eventHandler.addEventListener(this);
+        _eventHandler.addEventListener(this, 5);
 
         inputs.setSpacing(10);
         inputs.setAlignment(Pos.TOP_LEFT);
@@ -139,7 +140,7 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
                     jargs.put("subject",  getJSONIdentifier());
                     jargs.put("object",   jgui);
                     jmsg.put("args", jargs);
-                    _eventHandler.publishEvent("try", jmsg);
+                    _eventHandler.publishEvent(10, "try", jmsg);
                 }
             }
             if(e.getButton() == MouseButton.SECONDARY) {
@@ -157,6 +158,7 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
             jID = (JSONObject)json.get("ID");
 
             Name = jID.get("name").toString();
+            caption.setText(Name);
             Appendix = (long)jID.get("appendix");
             ID = Name +Appendix;
             Prefix = new Vector<>();
@@ -215,8 +217,12 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
                 jarray = (JSONArray)json.get("outputs");
             }
         }
+        int jsize = 0;
+        if(jarray != null){
+            jsize = jarray.size();
+        }
         //remove
-        int size = connectables.size() - jarray.size();
+        int size = connectables.size() - jsize;
         for (int i = 0; i < size; ++i){
             connectables.remove(connectables.size()-1);
         }
@@ -226,7 +232,7 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
             ((Connectable)connectables.get(i)).configure((JSONObject)jarray.get(i));
         }
         //add
-        size = jarray.size()-connectables.size();
+        size = jsize-connectables.size();
         for (int i = 0; i < size; ++i){
             JSONObject jobj = (JSONObject)jarray.get(i);
             if (ctype == ConnectableType.INPUT){
@@ -280,6 +286,17 @@ public class Module extends Group implements IGUIEventClient, ChangeListener<Bou
 
     @Override
     public void processGUIEvent(String event, JSONObject args) {
+        if (isSelected){
+            if (event.equals("KeyTyped")){
+                if (!args.containsKey("key")){return;}
+                String key = args.get("key").toString();
+                if (key.equals("DELETE")){
+                    _eventHandler.publishEvent(5,"delete", getJSONIdentifier());
+                    System.out.println("[Module]: delete: " + getJSONIdentifier().toString());
+                }
+                return;
+            }
+        }
         if (event == "MOUSE_PRESSED"){
             if (!args.equals(getJSONIdentifier())){
                 isSelected = false;

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

@@ -46,8 +46,9 @@ public class Processor extends Module{
                 JSONObject jout = (JSONObject)jobj.get("output");
                 for (Object ins : (JSONArray) jobj.get("inputs")){
                     JSONObject jin = (JSONObject) ins;
-                    Input in = getModule(((JSONArray)jin.get("prefix")).get(0).toString()).getInput(jin.get("name").toString()+jin.get("appendix").toString());
-                    Output out = getModule(((JSONArray)jout.get("prefix")).get(0).toString()).getOutput(jout.get("name").toString()+jout.get("appendix").toString());
+                    //TODO update prefix
+                    Input in = getModule(((JSONArray)jin.get("prefix")).get(1).toString()).getInput(jin.get("name").toString()+jin.get("appendix").toString());
+                    Output out = getModule(((JSONArray)jout.get("prefix")).get(1).toString()).getOutput(jout.get("name").toString()+jout.get("appendix").toString());
                     connections.getChildren().add(new Connection(in, out));
                 }
             }
@@ -127,7 +128,7 @@ public class Processor extends Module{
                         jconnection.put("output",out.getJSONIdentifier());
                         jargs.put("object", jconnection);
                         jmsg.put("args", jargs);
-                        _eventHandler.publishEvent("try",jmsg);
+                        _eventHandler.publishEvent(10,"try",jmsg);
 
                         success = true;
                     }
@@ -263,6 +264,12 @@ public class Processor extends Module{
                     }
                     break;
             }
+        }else if(event.equals("delete")){
+            if (args.get("prefix").equals(getAsPrefix())){
+                System.out.println("[Processor] delete");
+                //TODO delete
+            }
         }
+
     }
 }

+ 3 - 3
src/main/java/mdd/client/connectable/Connectable.java

@@ -32,8 +32,8 @@ public class Connectable extends GridPane implements IGUIEventClient,ChangeListe
     protected GUIEventHandler _eventHandler;
     private Circle _border;
     private Circle _circle;
-    private boolean _connected = false;
-    private int _connections = 0;
+    protected boolean _connected = false;
+    protected int _connections = 0;
 
     public boolean optimizable = false;
     private Module _parent;
@@ -192,7 +192,7 @@ public class Connectable extends GridPane implements IGUIEventClient,ChangeListe
         _label.setText(Name);
     }
 
-    private void updateColor(){
+    protected void updateColor(){
         if (_connected){
             _circle.setFill(Color.YELLOW);
         } else if (optimizable) {

+ 1 - 1
src/main/java/mdd/client/connectable/Connection.java

@@ -35,7 +35,7 @@ public class Connection extends Line implements IGUIEventClient {
         JSONObject json = new JSONObject();
         json.put("con", this.toString());
         json.put("in", in.toString());
-        json.put("out", in.toString());
+        json.put("out", out.toString());
         _eventHandler.publishEvent("CONNECTED", json);
     }
 

+ 10 - 4
src/main/java/mdd/client/connectable/Input.java

@@ -49,10 +49,16 @@ public class Input  extends Connectable {
         return _input;
     }
 
-    public void setConnected(boolean state){
-        super.setConnected(state);
-        _input.setVisible(!state);
-        _input.setManaged(!state);
+    protected void updateColor(){
+        super.updateColor();
+        if (_connected || optimizable){
+            _input.setVisible(false);
+            _input.setManaged(false);
+        }
+        else{
+            _input.setVisible(true);
+            _input.setManaged(true);
+        }
     }
 
     @Override

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

@@ -9,11 +9,50 @@
 <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>
-           <Menu fx:id="mMain" text="Main">
-               <MenuItem text="Exit" />
+           <Menu fx:id="mFile" text="File">
+               <MenuItem fx:id="miNew" text="New" onAction="#miNewClicked"/>
+               <MenuItem fx:id="miOpen" text="Open" onAction="#miOpenClicked"/>
+               <MenuItem fx:id="miOpen_Recent" text="Open Recent" onAction="#miOpen_RecentClicked"/>
+               <MenuItem fx:id="miClose_Project" text="Close Project" onAction="#miClose_ProjectClicked"/>
+               <MenuItem fx:id="miSettings" text="Settings" onAction="#miSettingsClicked"/>
+               <MenuItem fx:id="miSave" text="Save" onAction="#miSaveClicked"/>
+               <MenuItem fx:id="miSave_As" text="Save As..." onAction="#miSave_AsClicked"/>
+               <MenuItem fx:id="miExit" text="Exit" onAction="#miExitClicked"/>
            </Menu>
 
-           <Menu fx:id="mAbout" text="About">
+           <Menu fx:id="mEdit" text="Edit">
+               <MenuItem text="Undo" />
+               <MenuItem text="Cut" />
+               <MenuItem text="Copy" />
+               <MenuItem text="Past" />
+               <MenuItem text="Delete" />
+               <MenuItem text="Find" />
+           </Menu>
+
+           <Menu fx:id="mRun" text="Run">
+               <MenuItem text="Run" />
+               <MenuItem text="Debug" />
+               <MenuItem text="Profile" />
+               <MenuItem text="Pause" />
+               <MenuItem text="Stop" />
+           </Menu>
+
+           <Menu fx:id="mTools" text="Tools">
+               <MenuItem text="Optimizer" />
+           </Menu>
+
+           <Menu fx:id="mGit" text="Git">
+               <MenuItem text="Commit" />
+               <MenuItem text="Push" />
+               <MenuItem text="Pull" />
+               <MenuItem text="Branches" />
+               <MenuItem text="Merge" />
+               <MenuItem text="Reset" />
+           </Menu>
+
+           <Menu fx:id="mHelp" text="Help">
+               <MenuItem text="Help" />
+               <MenuItem text="About" />
            </Menu>
        </MenuBar>
       <SplitPane fx:id="sPane" dividerPositions="0.1, 0.9" prefHeight="${sPane.parent.height}">