-
Notifications
You must be signed in to change notification settings - Fork 45
/
TreeRenderer.java
97 lines (83 loc) · 4.14 KB
/
TreeRenderer.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
/*******************************************************************************
* Copyright (c) 2019, 2023 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.trees.renderer;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import org.eclipse.sirius.components.representations.VariableManager;
import org.eclipse.sirius.components.trees.Tree;
import org.eclipse.sirius.components.trees.TreeItem;
import org.eclipse.sirius.components.trees.description.TreeDescription;
/**
* Renderer used to create the tree from its description and some variables.
*
* @author hmarchadour
*/
public class TreeRenderer {
public static final String EXPANDED = "expanded";
private VariableManager variableManager;
private TreeDescription treeDescription;
public TreeRenderer(VariableManager variableManager, TreeDescription treeDescription) {
this.variableManager = Objects.requireNonNull(variableManager);
this.treeDescription = Objects.requireNonNull(treeDescription);
}
public Tree render() {
String treeId = this.treeDescription.getIdProvider().apply(this.variableManager);
String label = this.treeDescription.getLabelProvider().apply(this.variableManager);
List<?> rootElements = this.treeDescription.getElementsProvider().apply(this.variableManager);
List<TreeItem> childrenItems = new ArrayList<>(rootElements.size());
for (Object rootElement : rootElements) {
VariableManager rootElementVariableManager = this.variableManager.createChild();
rootElementVariableManager.put(VariableManager.SELF, rootElement);
childrenItems.add(this.renderTreeItem(rootElementVariableManager));
}
// @formatter:off
return Tree.newTree(treeId)
.descriptionId(this.treeDescription.getId())
.label(label)
.children(childrenItems)
.build();
// @formatter:on
}
private TreeItem renderTreeItem(VariableManager treeItemVariableManager) {
String id = this.treeDescription.getTreeItemIdProvider().apply(treeItemVariableManager);
String kind = this.treeDescription.getKindProvider().apply(treeItemVariableManager);
String label = this.treeDescription.getLabelProvider().apply(treeItemVariableManager);
boolean editable = this.treeDescription.getEditableProvider().apply(treeItemVariableManager);
boolean deletable = this.treeDescription.getDeletableProvider().apply(treeItemVariableManager);
boolean selectable = this.treeDescription.getSelectableProvider().apply(treeItemVariableManager);
String imageURL = this.treeDescription.getImageURLProvider().apply(treeItemVariableManager);
Boolean hasChildren = this.treeDescription.getHasChildrenProvider().apply(treeItemVariableManager);
List<?> children = this.treeDescription.getChildrenProvider().apply(treeItemVariableManager);
List<TreeItem> childrenTreeItems = new ArrayList<>(children.size());
boolean expanded = !children.isEmpty();
for (Object child : children) {
VariableManager childVariableManager = treeItemVariableManager.createChild();
childVariableManager.put(VariableManager.SELF, child);
childrenTreeItems.add(this.renderTreeItem(childVariableManager));
}
// @formatter:off
return TreeItem.newTreeItem(id)
.kind(kind)
.label(label)
.editable(editable)
.deletable(deletable)
.selectable(selectable)
.imageURL(imageURL)
.children(childrenTreeItems)
.hasChildren(hasChildren)
.expanded(expanded)
.build();
// @formatter:on
}
}