Skip to content

Commit fef67d0

Browse files
hrjakobsenJBcoding
authored andcommitted
Drag and drop between multiple applications now supported
1 parent 69879d5 commit fef67d0

File tree

1 file changed

+48
-7
lines changed

1 file changed

+48
-7
lines changed

src/UI/Flowduino.java

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public class Flowduino extends Application {
5050
protected VariablesMenu variablesMenu;
5151

5252
protected Node clipboard;
53-
protected Node nodeToMove;
53+
protected Node toMove;
5454

5555
protected HashMap<Rectangle, Node> targetNodeMap = new HashMap<>();
5656
protected HashMap<Rectangle, Boolean> targetFirstMap = new HashMap<>();
@@ -336,9 +336,12 @@ public void handle(DragEvent event) {
336336
IfComponent ifComponent = new IfComponent(nodes, cases);
337337
newComponent = ifComponent;
338338
} else if (db.getString().equals("NodeMove")) {
339-
if (insertNodeReletiveToNode(n, nodeToMove, b)) {
340-
deleteNodeFromTree(nodeToMove);
341-
nodeToMove = null;
339+
Node nodeToMove = toMove;
340+
if (nodeToMove == null) {
341+
nodeToMove = loadNode(db.getFiles().get(0));
342+
}
343+
if (nodeToMove != null && insertNodeReletiveToNode(n, nodeToMove, b) && toMove != null) {
344+
deleteNodeFromTree(toMove);
342345
}
343346
}
344347
if (newComponent != null) {
@@ -601,7 +604,6 @@ public boolean saveFile() {
601604
out.close();
602605
fileOut.close();
603606
d.setSavedSinceLastChange(true);
604-
605607
} catch (Exception e) {
606608
System.out.println("Save failed");
607609
e.printStackTrace();
@@ -712,13 +714,26 @@ public void handle(MouseEvent mouseEvent) {
712714
/* put a string on dragboard */
713715
ClipboardContent content = new ClipboardContent();
714716
content.putString("NodeMove");
717+
try {
718+
File temp = File.createTempFile("clipboard", ".tmp");
719+
saveNode(temp, n);
720+
toMove = n;
721+
content.putFiles(new ArrayList<File>(){{add(temp);}});
722+
723+
} catch(IOException e) {
724+
e.printStackTrace();
725+
}
715726
db.setContent(content);
716727

717-
nodeToMove = n;
718-
719728
mouseEvent.consume();
720729
}
721730
});
731+
p.setOnDragDone(new EventHandler<DragEvent>() {
732+
@Override
733+
public void handle(DragEvent mouseEvent) {
734+
toMove = null;
735+
}
736+
});
722737
p.getStyleClass().add(blocktype);
723738
p.getStyleClass().add("block-image");
724739
programView.getChildren().add(p);
@@ -751,4 +766,30 @@ public boolean insertNodeReletiveToNode(Node n, Node newNodeToInsert, boolean be
751766
return false;
752767
}
753768
}
769+
770+
public void saveNode(File f, Node n) {
771+
try {
772+
FileOutputStream fileOut = new FileOutputStream(f.getAbsoluteFile());
773+
ObjectOutputStream out = new ObjectOutputStream(fileOut);
774+
out.writeObject(n);
775+
out.close();
776+
fileOut.close();
777+
} catch (Exception e) {
778+
e.printStackTrace();
779+
}
780+
}
781+
782+
public Node loadNode(File f) {
783+
try {
784+
FileInputStream fileIn = new FileInputStream(f.getAbsoluteFile());
785+
ObjectInputStream in = new ObjectInputStream(fileIn);
786+
Node n = (Node)in.readObject();
787+
in.close();
788+
fileIn.close();
789+
return n;
790+
} catch(Exception e) {
791+
e.printStackTrace();
792+
}
793+
return null;
794+
}
754795
}

0 commit comments

Comments
 (0)