/
NodeAdapter.java
216 lines (188 loc) · 5.81 KB
/
NodeAdapter.java
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package de.emsw.fx;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javafx.beans.property.StringProperty;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.control.Accordion;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.Control;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.SplitPane;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TitledPane;
import javafx.scene.control.ToolBar;
public class NodeAdapter {
static enum RootType {
NODE, MENU_ITEM, TAB
}
public static NodeAdapter adapt(Object fx) {
if (fx instanceof Node) {
return new NodeAdapter((Node)fx);
} else if (fx instanceof MenuItem) {
return new NodeAdapter((MenuItem)fx);
} else if (fx instanceof Tab) {
return new NodeAdapter((Tab)fx);
}
return null;
}
private Object fxObject;
private RootType type;
public NodeAdapter(Node node) {
this.fxObject = node;
this.type = RootType.NODE;
}
public NodeAdapter(MenuItem menuItem) {
this.fxObject = menuItem;
this.type = RootType.MENU_ITEM;
}
public NodeAdapter(Tab tab) {
this.fxObject = tab;
this.type = RootType.TAB;
}
public Object getFXObject() {
return fxObject;
}
public RootType getRootType() {
return type;
}
public Node getNode() {
if (type != RootType.NODE)
return null;
return (Node)fxObject;
}
public MenuItem getMenuItem() {
if (type != RootType.MENU_ITEM)
return null;
return (MenuItem)fxObject;
}
public Tab getTab() {
if (type != RootType.TAB)
return null;
return (Tab)fxObject;
}
public StringProperty idProperty() {
switch (type) {
case NODE:
return getNode().idProperty();
case MENU_ITEM:
return getMenuItem().idProperty();
case TAB:
return getTab().idProperty();
}
return null;
}
public ObservableList<String> getStyleClass() {
switch (type) {
case NODE:
return getNode().getStyleClass();
case MENU_ITEM:
return getMenuItem().getStyleClass();
case TAB:
return getTab().getStyleClass();
}
return null;
}
public Object getUserData() {
switch (type) {
case NODE:
return getNode().getUserData();
case MENU_ITEM:
return getMenuItem().getUserData();
case TAB:
return getTab().getUserData();
}
return null;
}
public void setUserData(Object data) {
switch (type) {
case NODE:
getNode().setUserData(data);
break;
case MENU_ITEM:
getMenuItem().setUserData(data);
break;
case TAB:
getTab().setUserData(data);
break;
}
}
public List<NodeAdapter> getChildrenUnmodifiable() {
List<NodeAdapter> result = new ArrayList<NodeAdapter>();
// This returns the same thing as Parent.getChildrenUnmodifiable()
// if (fxObject instanceof Node) {
// ((Node)fxObject).lookupAll("*").forEach(node -> {
// if (!node.equals(fxObject)) {
// result.add(new NodeAdapter(node));
// }
// });
// }
// primary parent type derived from the root type
if (fxObject instanceof Parent) {
((Parent)fxObject).getChildrenUnmodifiable().forEach(node -> result.add(new NodeAdapter(node)));
} else if (fxObject instanceof Menu) {
((Menu)fxObject).getItems().forEach(item -> result.add(new NodeAdapter(item)));
} else if (fxObject instanceof Tab) {
Node content = ((Tab)fxObject).getContent();
if (content != null)
result.add(new NodeAdapter(content));
}
// extended parent types
if (fxObject instanceof MenuBar) {
((MenuBar)fxObject).getMenus().forEach(menu -> result.add(new NodeAdapter(menu)));
} else if (fxObject instanceof TabPane) {
((TabPane)fxObject).getTabs().forEach(tab -> result.add(new NodeAdapter(tab)));
} else if (fxObject instanceof TitledPane) {
Node content = ((TitledPane)fxObject).getContent();
if (content != null)
result.add(new NodeAdapter(content));
} else if (fxObject instanceof ScrollPane) {
Node content = ((ScrollPane)fxObject).getContent();
if (content != null)
result.add(new NodeAdapter(content));
} else if (fxObject instanceof Accordion) {
((Accordion)fxObject).getPanes().forEach(pane -> result.add(new NodeAdapter(pane)));
} else if (fxObject instanceof SplitPane) {
((SplitPane)fxObject).getItems().forEach(node -> result.add(new NodeAdapter(node)));
} else if (fxObject instanceof ToolBar) {
((ToolBar)fxObject).getItems().forEach(node -> result.add(new NodeAdapter(node)));
} else if (fxObject instanceof ButtonBar) {
((ButtonBar)fxObject).getButtons().forEach(button -> result.add(new NodeAdapter(button)));
}
// context menu
if (fxObject instanceof Control) {
ContextMenu contextMenu = ((Control)fxObject).getContextMenu();
if (contextMenu != null) {
contextMenu.getItems().forEach(item -> result.add(new NodeAdapter(item)));
}
}
return Collections.unmodifiableList(result);
}
public NodeAdapter getAdapterForChild(Object node) {
List<NodeAdapter> result = getChildrenUnmodifiable()
.stream()
.filter(child -> child.fxObject.equals(node))
.collect(Collectors.toList());
if (result.size() == 0)
return null;
return result.get(0);
}
public void accept(INodeVisitor visitor) {
boolean result = visitor.visit(this);
if (!result)
return;
getChildrenUnmodifiable().forEach(nodeAdapter -> nodeAdapter.accept(visitor));
}
@Override
public String toString() {
return "NodeAdapter [type=" + type + ", fxObject=" + fxObject + "]";
}
}