-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathParentProgramNodeContribution.java
More file actions
160 lines (140 loc) · 5.31 KB
/
ParentProgramNodeContribution.java
File metadata and controls
160 lines (140 loc) · 5.31 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.jbm.urcap.customAPI.impl;
import com.jbm.urcap.customAPI.impl.MyCustomAPI.MyColor;
import com.ur.urcap.api.contribution.ProgramNodeContribution;
import com.ur.urcap.api.contribution.program.ProgramAPIProvider;
import com.ur.urcap.api.domain.program.ProgramModel;
import com.ur.urcap.api.domain.program.nodes.ProgramNodeFactory;
import com.ur.urcap.api.domain.program.nodes.contributable.URCapProgramNode;
import com.ur.urcap.api.domain.program.structure.ProgramNodeVisitor;
import com.ur.urcap.api.domain.program.structure.TreeNode;
import com.ur.urcap.api.domain.program.structure.TreeStructureException;
import com.ur.urcap.api.domain.script.ScriptWriter;
import com.ur.urcap.api.domain.undoredo.UndoRedoManager;
import com.ur.urcap.api.domain.undoredo.UndoableChanges;
public class ParentProgramNodeContribution implements ProgramNodeContribution{
private final ProgramAPIProvider apiProvider;
private final ParentProgramNodeView view;
private final UndoRedoManager undoRedoManager;
private static MyColor[] colors = {MyColor.RED, MyColor.GREEN, MyColor.BLUE};
public ParentProgramNodeContribution(ProgramAPIProvider apiProvider, ParentProgramNodeView view) {
this.apiProvider = apiProvider;
this.view = view;
this.undoRedoManager = this.apiProvider.getProgramAPI().getUndoRedoManager();
}
public void requestToAddChildNode() {
if(!childNodeAlreadyExists()) {
addChildNode();
openView();
}
}
public void requestToReColorChildNode(MyColor color) {
if(childNodeAlreadyExists()) {
setChildNodeColor(color);
}
}
private void addChildNode() {
ProgramModel programModel = apiProvider.getProgramAPI().getProgramModel();
final ProgramNodeFactory nf = programModel.getProgramNodeFactory();
final TreeNode root = programModel.getRootTreeNode(this);
undoRedoManager.recordChanges(new UndoableChanges() {
@Override
public void executeChanges() {
try {
root.addChild(nf.createURCapProgramNode(ChildProgramNodeService.class));
} catch (TreeStructureException e) {
// TODO: handle exception
}
}
});
}
private boolean childNodeAlreadyExists() {
final boolean[] foundChild = new boolean[1];
foundChild[0] = false;
TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);
root.traverse(new ProgramNodeVisitor() {
@Override
public void visit(URCapProgramNode programNode, int index, int depth) {
if(programNode.canGetAs(MyCustomAPI.class)) {
foundChild[0] = true;
}
// super.visit(programNode, index, depth);
}
});
return foundChild[0];
}
/*****
* Method to SET the MyColor of the Child node, using the URCap Custom API
* The Child node must implement the MyCustomAPI interface in the Contribution
* @param color the MyColor to set in the Child
*/
private void setChildNodeColor(final MyColor color) {
// Get the root-node (this program node)
TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);
// Traverse the program tree, using the ProgramNodeVisitor
root.traverse(new ProgramNodeVisitor() {
// Override "visit" looking for "URCapProgramNode" nodes
// Visit will be called for every URCapProgramNode in sub-tree
@Override
public void visit(URCapProgramNode programNode, int index, int depth) {
// Check if the found program node implements the "MyCustomAPI" interface
if(programNode.canGetAs(MyCustomAPI.class)) {
// If it did, we found the right node
// Now we can use "getAs" since we already tested with "canGetAs"
// After "getAs" we can now call the interface methods
programNode.getAs(MyCustomAPI.class).setColor(color);
}
}
});
}
/*****
* Method to GET the MyColor of the Child node, using the URCap Custom API
* The Child node must implement the MyCustomAPI interface in the Contribution
* @return the MyColor configured for the Child
*/
private MyColor getChildNodeColor() {
final MyColor[] color = new MyColor[1];
// Get the root-node (this program node)
TreeNode root = apiProvider.getProgramAPI().getProgramModel().getRootTreeNode(this);
// Traverse the program tree, using the ProgramNodeVisitor
root.traverse(new ProgramNodeVisitor() {
// Override "visit" looking for "URCapProgramNode" nodes
// Visit will be called for every URCapProgramNode in sub-tree
@Override
public void visit(URCapProgramNode programNode, int index, int depth) {
// Check if the found program node implements the "MyCustomAPI" interface
if(programNode.canGetAs(MyCustomAPI.class)) {
// If it did, we found the right node
// Now we can use "getAs" since we already tested with "canGetAs"
// After "getAs" we can now call the interface methods
color[0] = programNode.getAs(MyCustomAPI.class).getColor();
}
}
});
return color[0];
}
@Override
public void openView() {
view.setColorComboBoxItems(colors);
boolean childExists = childNodeAlreadyExists();
view.setImplementButtonEnabled(!childExists);
view.setColorComboBoxEnabled(childExists);
if(childExists) {
view.setColorComboBoxSelectedItem(getChildNodeColor());
}
}
@Override
public void closeView() {
}
@Override
public String getTitle() {
return "Parent Node";
}
@Override
public boolean isDefined() {
return true;
}
@Override
public void generateScript(ScriptWriter writer) {
writer.writeChildren();
}
}