From 3aa27887cb7ad0568014424d0bf5c280afb32222 Mon Sep 17 00:00:00 2001 From: Ivan Olexyn Date: Mon, 13 Apr 2020 21:44:50 +0200 Subject: [PATCH] add bridge - adding directories/collections --- doc/ui-design.uxf | 392 ++++++++++++++++++++--- doc/ui-id-naming.uxf | 104 +++--- src/com/olexyn/ensync/Flow.java | 27 +- src/com/olexyn/ensync/Main.java | 13 +- src/com/olexyn/ensync/ui/Bridge.java | 56 ++++ src/com/olexyn/ensync/ui/Controller.java | 114 +++---- 6 files changed, 521 insertions(+), 185 deletions(-) create mode 100644 src/com/olexyn/ensync/ui/Bridge.java diff --git a/doc/ui-design.uxf b/doc/ui-design.uxf index d0e53d5..fa04db8 100644 --- a/doc/ui-design.uxf +++ b/doc/ui-design.uxf @@ -4,8 +4,8 @@ UMLClass - 340 - 300 + 530 + 550 160 30 @@ -17,8 +17,8 @@ bg=#90CAF9 UMLClass - 340 - 260 + 530 + 510 160 30 @@ -31,8 +31,8 @@ bg=#90CAF9 UMLClass - 20 - 260 + 210 + 510 130 30 @@ -45,8 +45,8 @@ bg=#90CAF9 UMLClass - 340 - 340 + 530 + 590 160 30 @@ -58,8 +58,8 @@ bg=#80CBC4 UMLClass - 340 - 180 + 530 + 430 160 30 @@ -71,8 +71,8 @@ bg=#80CBC4 UMLClass - 210 - 260 + 400 + 510 70 30 @@ -84,8 +84,8 @@ bg=#90CAF9 UMLClass - 210 - 180 + 400 + 430 70 30 @@ -97,8 +97,8 @@ bg=#80CBC4 UMLClass - 340 - 220 + 530 + 470 160 30 @@ -110,8 +110,8 @@ bg=#90CAF9 UMLClass - 210 - 220 + 400 + 470 70 30 @@ -123,8 +123,8 @@ bg=#90CAF9 UMLClass - 20 - 220 + 210 + 470 130 30 @@ -136,8 +136,8 @@ bg=#90CAF9 UMLClass - 340 - 140 + 530 + 390 160 30 @@ -148,8 +148,8 @@ halign=left UMLClass - 340 - 100 + 530 + 350 160 30 @@ -160,8 +160,8 @@ halign=left UMLClass - 20 - 100 + 210 + 350 130 30 @@ -172,8 +172,8 @@ halign=left UMLClass - 340 - 20 + 530 + 270 160 30 @@ -185,8 +185,8 @@ bg=#80CBC4 UMLClass - 210 - 100 + 400 + 350 70 30 @@ -197,8 +197,8 @@ halign=left UMLClass - 210 - 20 + 400 + 270 70 30 @@ -210,8 +210,8 @@ bg=#80CBC4 UMLClass - 340 - 60 + 530 + 310 160 30 @@ -222,8 +222,8 @@ halign=left UMLClass - 210 - 60 + 400 + 310 70 30 @@ -234,8 +234,8 @@ halign=left UMLClass - 20 - 60 + 210 + 310 130 30 @@ -246,8 +246,8 @@ halign=left UMLClass - 20 - 300 + 210 + 550 130 30 @@ -255,4 +255,316 @@ halign=left halign=left + + UMLClass + + 480 + 590 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 550 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 510 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 470 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 430 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 390 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 350 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 310 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 480 + 270 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 590 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 550 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 510 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 470 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 430 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 390 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 350 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 310 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 350 + 270 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 400 + 590 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 400 + 550 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 210 + 590 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 400 + 390 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 210 + 390 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 210 + 430 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 210 + 270 + 40 + 30 + + +bg=#F6F6F6 + + + + UMLClass + + 210 + 640 + 40 + 30 + + +bg=#F6F6F6 + + diff --git a/doc/ui-id-naming.uxf b/doc/ui-id-naming.uxf index 2e74fa0..db94de4 100644 --- a/doc/ui-id-naming.uxf +++ b/doc/ui-id-naming.uxf @@ -4,8 +4,8 @@ UMLClass - 630 - 870 + 580 + 820 160 30 @@ -16,8 +16,8 @@ halign=left Relation - 780 - 870 + 730 + 820 100 30 @@ -27,20 +27,20 @@ halign=left UMLNote - 860 - 870 - 230 + 810 + 820 + 280 30 - addDirectoryButton + addDirectoryButton-name1234 fg=red UMLClass - 630 - 800 + 580 + 750 160 30 @@ -51,8 +51,8 @@ halign=left UMLNote - 860 - 800 + 810 + 750 320 30 @@ -63,8 +63,8 @@ fg=red UMLClass - 310 - 800 + 260 + 750 130 30 @@ -75,8 +75,8 @@ halign=left UMLNote - 860 - 740 + 810 + 690 320 30 @@ -87,8 +87,8 @@ fg=red Relation - 430 - 740 + 380 + 690 450 90 @@ -98,8 +98,8 @@ fg=red Relation - 780 - 800 + 730 + 750 100 30 @@ -109,8 +109,8 @@ fg=red UMLClass - 630 - 940 + 580 + 890 160 30 @@ -121,8 +121,8 @@ halign=left UMLNote - 860 - 770 + 810 + 720 320 30 @@ -133,8 +133,8 @@ fg=red UMLClass - 630 - 680 + 580 + 630 160 30 @@ -145,8 +145,8 @@ halign=left UMLClass - 500 - 800 + 450 + 750 70 30 @@ -157,8 +157,8 @@ halign=left UMLClass - 500 - 680 + 450 + 630 70 30 @@ -169,8 +169,8 @@ halign=left UMLNote - 860 - 940 + 810 + 890 190 30 @@ -181,8 +181,8 @@ fg=red Relation - 780 - 940 + 730 + 890 100 30 @@ -192,8 +192,8 @@ fg=red UMLNote - 860 - 680 + 810 + 630 300 30 @@ -204,8 +204,8 @@ fg=red Relation - 560 - 770 + 510 + 720 320 60 @@ -215,8 +215,8 @@ fg=red Relation - 780 - 680 + 730 + 630 100 30 @@ -226,8 +226,8 @@ fg=red UMLNote - 860 - 650 + 810 + 600 300 30 @@ -238,8 +238,8 @@ fg=red Relation - 560 - 650 + 510 + 600 320 60 @@ -249,8 +249,8 @@ fg=red UMLClass - 310 - 870 + 260 + 820 130 30 @@ -261,20 +261,20 @@ halign=left UMLNote - 860 - 840 - 230 + 810 + 790 + 280 30 - addDirectoryTextField + addDirectoryTextField-name1234 fg=red Relation - 430 - 840 + 380 + 790 450 60 diff --git a/src/com/olexyn/ensync/Flow.java b/src/com/olexyn/ensync/Flow.java index 7dea000..277c9a7 100644 --- a/src/com/olexyn/ensync/Flow.java +++ b/src/com/olexyn/ensync/Flow.java @@ -4,10 +4,8 @@ import com.olexyn.ensync.artifacts.SyncDirectory; import com.olexyn.ensync.artifacts.SyncMap; import java.io.File; -import java.util.Map; - -public class Flow implements Runnable{ +public class Flow implements Runnable { private String state; @@ -17,14 +15,13 @@ public class Flow implements Runnable{ - initialize(); - + while (true) { - for (var syncMapEntry : Main.SYNC.entrySet()) { + initialize(); + for (var syncMapEntry : Main.SYNC.entrySet()) { - while (true) { for (var syncDirectoryEntry : syncMapEntry.getValue().syncDirectories.entrySet()) { @@ -47,19 +44,21 @@ public class Flow implements Runnable{ } - try { - System.out.println("Pausing..."); - Thread.sleep(2000); - } catch (InterruptedException ignored) { - } } + try { + System.out.println("Pausing..."); + Thread.sleep(2000); + } catch (InterruptedException ignored) { + + } + } } public String getState() { - return state==null ? "NONE" : state; + return state == null ? "NONE" : state; } @@ -67,7 +66,7 @@ public class Flow implements Runnable{ * For every single SyncDirectory try to read it's StateFile.

* If the StateFile is missing, then create a StateFile. */ - private void initialize(){ + private void initialize() { for (var syncMapEntry : Main.SYNC.entrySet()) { SyncMap syncMap = syncMapEntry.getValue(); state = syncMap.toString(); diff --git a/src/com/olexyn/ensync/Main.java b/src/com/olexyn/ensync/Main.java index 18a033c..0080952 100644 --- a/src/com/olexyn/ensync/Main.java +++ b/src/com/olexyn/ensync/Main.java @@ -4,7 +4,6 @@ import com.olexyn.ensync.artifacts.SyncMap; import com.olexyn.ensync.ui.UI; import java.util.HashMap; -import java.util.Map; public class Main{ @@ -14,23 +13,23 @@ public class Main{ - final public static Thread uiThread = new Thread(new UI(), "ui"); + final public static Thread UI_THREAD = new Thread(new UI(), "ui"); - final public static Thread flowThread = new Thread(new Flow(), "flow"); + final public static Thread FLOW_THREAD = new Thread(new Flow(), "flow"); - final public static Map SYNC = new HashMap<>(); + final public static HashMap SYNC = new HashMap<>(); public static void main(String[] args) { - SYNC.put("test", new SyncMap("test")); - uiThread.start(); - flowThread.start(); + UI_THREAD.start(); + + FLOW_THREAD.start(); diff --git a/src/com/olexyn/ensync/ui/Bridge.java b/src/com/olexyn/ensync/ui/Bridge.java new file mode 100644 index 0000000..806216b --- /dev/null +++ b/src/com/olexyn/ensync/ui/Bridge.java @@ -0,0 +1,56 @@ +package com.olexyn.ensync.ui; + + +import com.olexyn.ensync.artifacts.SyncMap; + +import static com.olexyn.ensync.Main.SYNC; +import static com.olexyn.ensync.Main.FLOW_THREAD; +import static com.olexyn.ensync.Main.UI_THREAD; + +import java.io.File; + +/** + * Connect the Controller and the Flow + */ +public class Bridge { + + + + void newCollection(String collectionName){ + SYNC.put(collectionName, new SyncMap(collectionName)); + } + + + void removeCollection(String collectionName){ + SYNC.remove(collectionName); + } + + + void addDirectory(String collectionName, File diretory){ + SYNC.get(collectionName).addDirectory(diretory.getAbsolutePath()); + //TODO pause syning when adding + } + + + /** + * This works, because a directory, which here is an unique ablsolute path, + * is only supposed to present once across entire SYNC. + */ + void removeDirectory(String directoryAbsolutePath){ + //TODO fix ConcurrentModificationException. This will possibly resolve further errors. + while (true) { + if (FLOW_THREAD.getState().equals(Thread.State.TIMED_WAITING)) { + try { + FLOW_THREAD.wait(); + } catch (InterruptedException e) { + for (var syncMap : SYNC.entrySet()){ + syncMap.getValue().removeDirectory(directoryAbsolutePath); + } + FLOW_THREAD.notify(); + break; + } + } + } + + } +} diff --git a/src/com/olexyn/ensync/ui/Controller.java b/src/com/olexyn/ensync/ui/Controller.java index 3629136..55e8504 100644 --- a/src/com/olexyn/ensync/ui/Controller.java +++ b/src/com/olexyn/ensync/ui/Controller.java @@ -1,7 +1,6 @@ package com.olexyn.ensync.ui; -import com.olexyn.ensync.Main; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.Node; @@ -28,6 +27,10 @@ public class Controller implements Initializable { final static int COLUMN_COUNT = 5; // How many columns should the GridPane have? Adjust if necessary. final static Text SPACE = new Text(""); // Placeholder int collection_count = 0; + Bridge bridge = new Bridge(); + + @FXML + protected GridPane gridPane; @Override public void initialize(URL url, ResourceBundle resourceBundle) { @@ -53,14 +56,14 @@ public class Controller implements Initializable { } + private void newCollection() { - - private void newCollection() { - String collectionName = "name" + collection_count++; + bridge.newCollection(collectionName); + TextField collectionStateTextField = new TextField(); collectionStateTextField.setText("STATE"); @@ -85,45 +88,43 @@ public class Controller implements Initializable { List payload = new ArrayList<>(); payload.addAll(Arrays.asList(new Text(""), new Text(""), collectionStateTextField, new Text(""), removeCollectionButton)); payload.addAll(Arrays.asList(addDirectoryTextField, new Text(""), new Text(""), new Text(""), addDirectoryButton)); + insertPayload(nodeList, payload, "newCollectionButton", -4); redraw(gridPane, nodeList); } - @FXML - protected GridPane gridPane; + + /** + * For the order & number of Nodes see ui-design.png . + * Remove the "Remove-Collection-Line" and the consecutive lines until and including the "Add-Directory-Line". + * The !=null expression protects from Text("") placeholders, who have id==null. + */ private void removeCollection(String collectionName) { - List nodeList = new ArrayList<>(gridPane.getChildren()); + bridge.removeCollection(collectionName); - here : for (Node node : nodeList) { + List nodeList = new ArrayList<>(gridPane.getChildren()); + + here: + for (Node node : nodeList) { if (node.getId() != null && node.getId().equals("removeCollectionButton-" + collectionName)) { int i = nodeList.indexOf(node) - 4; - while (i fillBlanks() { - List out = new ArrayList<>(); - - - return out; - + redraw(gridPane, nodeList); } @@ -138,26 +139,28 @@ public class Controller implements Initializable { directoryChooser.setTitle("Select Directory."); directoryChooser.setInitialDirectory(new File(System.getProperty("user.home"))); - File dir = directoryChooser.showDialog(stage); + File directory = directoryChooser.showDialog(stage); + + if (directory != null) { + + bridge.addDirectory(collectionName, directory); - if (dir != null) { TextField directoryPathTextField = new TextField(); - directoryPathTextField.setText(dir.getAbsolutePath()); + directoryPathTextField.setText(directory.getAbsolutePath()); directoryPathTextField.setDisable(true); - directoryPathTextField.setId("directoryPathTextField-" + dir.getAbsolutePath()); + directoryPathTextField.setId("directoryPathTextField-" + directory.getAbsolutePath()); + - // TODO for now there is only one SyncMap "test". - // Main.SYNC.get("test").addDirectory(dir.getAbsolutePath()); TextField directoryStateTextField = new TextField(); directoryStateTextField.setText("STATE"); directoryStateTextField.setStyle("-fx-text-fill: green"); directoryStateTextField.setDisable(true); - directoryStateTextField.setId("directoryStateTextField-" + dir.getAbsolutePath()); + directoryStateTextField.setId("directoryStateTextField-" + directory.getAbsolutePath()); Button removeDirectoryButton = new Button("Remove"); - removeDirectoryButton.setId("removeDirectoryButton-" + dir.getAbsolutePath()); - removeDirectoryButton.setOnAction(event -> { this.removeDirectory(removeDirectoryButton.getId());}); + removeDirectoryButton.setId("removeDirectoryButton-" + directory.getAbsolutePath()); + removeDirectoryButton.setOnAction(event -> { this.removeDirectory(directory.getAbsolutePath());}); List nodeList = new ArrayList<>(gridPane.getChildren()); @@ -196,32 +199,28 @@ public class Controller implements Initializable { private void redraw(GridPane gridPane, List nodeList) { gridPane.getChildren().clear(); - int col = 0; - int row = 0; + int col = 0, row = 0; for (Node node : nodeList) { - if ((nodeList.indexOf(node) + 0) % COLUMN_COUNT == 0) { + if (nodeList.indexOf(node) % COLUMN_COUNT == 0) { row++; col = 0; } gridPane.add(node, col, row); col++; - - } } - private void removeDirectory(String id) { + private void removeDirectory(String directoryAbsolutePath) { + bridge.removeDirectory(directoryAbsolutePath); List nodeList = new ArrayList<>(gridPane.getChildren()); - //TODO fix ConcurrentModificationException. This will possibly resolve further errors. - for (Node node : nodeList) { - if (node.getId() != null && node.getId().equals(id)) { + if (node.getId() != null && node.getId().equals("removeDirectoryButton-" +directoryAbsolutePath)) { int i = nodeList.indexOf(node) - 4; for (int j = 0; j < 5; j++) { nodeList.remove(i); @@ -229,39 +228,10 @@ public class Controller implements Initializable { break; } } - - redraw(gridPane,nodeList); - - /* - String path = id.replace("removeButton", ""); - while (true) { - if (Main.flowThread.getState().equals(Thread.State.TIMED_WAITING)) { - try { - Main.flowThread.wait(); - } catch (InterruptedException e) { - Main.SYNC.get("test").removeDirectory(path); - Main.flowThread.notify(); - break; - } - } - } - - */ - - + redraw(gridPane, nodeList); } } - class Placeholder extends Text{ - static int count =0; - - Placeholder(String label, String name){ - - super(label); - this.setId("placeholder-"+name+"-"+count++); - } - -}