-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathStatementBlock.java
More file actions
140 lines (120 loc) · 6.47 KB
/
StatementBlock.java
File metadata and controls
140 lines (120 loc) · 6.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.gmail.visualbukkit.blocks;
import com.gmail.visualbukkit.VisualBukkitApp;
import com.gmail.visualbukkit.project.*;
import com.gmail.visualbukkit.ui.ActionMenuItem;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javafx.scene.input.ClipboardContent;
import javafx.scene.input.Dragboard;
import javafx.scene.input.MouseButton;
import javafx.scene.input.TransferMode;
import javafx.scene.paint.Color;
import org.apache.commons.lang3.RandomStringUtils;
import org.json.JSONObject;
public non-sealed abstract class StatementBlock extends Block {
public StatementBlock() {
getStyleClass().add("statement-block");
ActionMenuItem copyItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.copy"), e -> CopyPasteManager.copyStatement(this));
ActionMenuItem cutItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.cut"), e -> {
CopyPasteManager.copyStatement(this);
UndoManager.current().execute(this::delete);
});
ActionMenuItem deleteItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.delete"), e -> UndoManager.current().execute(this::delete));
ActionMenuItem copyStackItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.copy_stack"), e -> CopyPasteManager.copyStatements(getParentStatementHolder().getStack(this)));
ActionMenuItem cutStackItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.cut_stack"), e -> {
CopyPasteManager.copyStatements(getParentStatementHolder().getStack(this));
UndoManager.current().execute(() -> getParentStatementHolder().removeStack(this));
});
ActionMenuItem deleteStackItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.delete_stack"), e -> UndoManager.current().execute(() -> getParentStatementHolder().removeStack(this)));
ActionMenuItem pasteBeforeItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.paste_before"), e -> UndoManager.current().execute(() -> getParentStatementHolder().addBefore(this, CopyPasteManager.pasteStatement())));
ActionMenuItem pasteAfterItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.paste_after"), e -> UndoManager.current().execute(() -> getParentStatementHolder().addAfter(this, CopyPasteManager.pasteStatement())));
ActionMenuItem javadocsItem = new ActionMenuItem(VisualBukkitApp.localizedText("context_menu.javadocs"), e -> openJavadocs());
pasteBeforeItem.disableProperty().bind(CopyPasteManager.statementCopiedProperty().not());
pasteAfterItem.disableProperty().bind(pasteBeforeItem.disableProperty());
getContextMenu().getItems().addAll(copyItem, cutItem, deleteItem, new SeparatorMenuItem(), copyStackItem, cutStackItem, deleteStackItem, new SeparatorMenuItem(), pasteBeforeItem, pasteAfterItem, new SeparatorMenuItem(), javadocsItem);
setOnDragDetected(e -> {
if (e.getButton() == MouseButton.PRIMARY) {
Dragboard dragboard = startDragAndDrop(TransferMode.ANY);
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setFill(Color.TRANSPARENT);
Image image = snapshot(snapshotParameters, new WritableImage((int) Math.min(getWidth(), 500), (int) Math.min(getHeight(), 500)));
dragboard.setDragView(image);
ClipboardContent content = new ClipboardContent();
content.putString("");
dragboard.setContent(content);
setVisible(false);
setManaged(false);
e.consume();
}
});
setOnDragDone(e -> {
StatementConnector.hideCurrent();
setVisible(true);
setManaged(true);
e.consume();
});
setOnDragOver(e -> {
if (e.getGestureSource() instanceof StatementSource || e.getGestureSource() instanceof StatementBlock) {
getParentStatementHolder().showConnector(this, 2 * e.getY() > getHeight());
e.acceptTransferModes(TransferMode.ANY);
e.consume();
}
});
setOnDragDropped(e -> {
StatementBlock block = e.getGestureSource() instanceof StatementSource s ? s.getFactory().newBlock() : (StatementBlock) e.getGestureSource();
StatementConnector.current().accept(block);
e.setDropCompleted(true);
e.consume();
});
}
@SafeVarargs
public final void checkForPrevious(Class<? extends StatementBlock>... classes) {
StatementBlock block = getParentStatementHolder().getPrevious(this);
if (block != null) {
for (Class<?> clazz : classes) {
if (clazz.isAssignableFrom(block.getClass())) {
return;
}
}
}
pseudoClassStateChanged(INVALID_STYLE_CLASS, true);
}
public String generateDebugJava(BuildInfo buildInfo) {
String exceptionVar = RandomStringUtils.randomAlphanumeric(16);
String reportMethod = "Class.forName(\"com.gmail.visualbukkit.plugin.VisualBukkitPlugin\").getDeclaredMethod(\"reportException\", String.class, Throwable.class)";
return "try { %s } catch (Exception $%s) { %s.invoke(null, \"%s\", $%s); }".formatted(generateJava(buildInfo), exceptionVar, reportMethod, getUUID(), exceptionVar);
}
public abstract String generateJava(BuildInfo buildInfo);
@Override
public void delete() {
if (getParentStatementHolder() != null) {
getParentStatementHolder().remove(this);
}
}
public StatementHolder getParentStatementHolder() {
return (StatementHolder) getParent();
}
@BlockDefinition(id = "unknown-statement", name = "Unknown Statement")
public static class Unknown extends StatementBlock {
private JSONObject json = new JSONObject();
@Override
public void updateState() {
super.updateState();
pseudoClassStateChanged(INVALID_STYLE_CLASS, true);
}
@Override
public String generateJava(BuildInfo buildInfo) {
return "";
}
@Override
public JSONObject serialize() {
return json;
}
@Override
public void deserialize(JSONObject json) {
this.json = json;
}
}
}