1
|
/**
|
2
|
* Copyright (C) 2018 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.debug;
|
10
|
|
11
|
import java.util.List;
|
12
|
|
13
|
import com.vaadin.data.util.HierarchicalContainer;
|
14
|
import com.vaadin.data.util.IndexedContainer;
|
15
|
import com.vaadin.data.util.filter.SimpleStringFilter;
|
16
|
import com.vaadin.shared.ui.label.ContentMode;
|
17
|
import com.vaadin.ui.CustomComponent;
|
18
|
import com.vaadin.ui.Label;
|
19
|
import com.vaadin.ui.TextField;
|
20
|
import com.vaadin.ui.Tree;
|
21
|
import com.vaadin.ui.VerticalLayout;
|
22
|
import com.vaadin.ui.VerticalSplitPanel;
|
23
|
|
24
|
import eu.etaxonomy.cdm.cache.CdmTransientEntityCacher;
|
25
|
import eu.etaxonomy.cdm.cache.EntityCacherDebugResult;
|
26
|
import eu.etaxonomy.cdm.cache.EntityCacherDebugResult.CdmEntityInfo;
|
27
|
import eu.etaxonomy.cdm.vaadin.component.TextFieldNFix;
|
28
|
import eu.etaxonomy.cdm.vaadin.view.name.CachingPresenter;
|
29
|
|
30
|
/**
|
31
|
* @author a.kohlbecker
|
32
|
* @since Jan 22, 2018
|
33
|
*
|
34
|
*/
|
35
|
public class EntityCacheDebuggerComponent extends CustomComponent {
|
36
|
|
37
|
CachingPresenter presenter;
|
38
|
CdmTransientEntityCacher cacher;
|
39
|
EntityCacherDebugResult debugResults;
|
40
|
|
41
|
VerticalLayout layout = new VerticalLayout();
|
42
|
VerticalSplitPanel splitPanel = new VerticalSplitPanel();
|
43
|
|
44
|
private Tree entityTree;
|
45
|
|
46
|
public EntityCacheDebuggerComponent(CachingPresenter presenter){
|
47
|
|
48
|
this.presenter = presenter;
|
49
|
this.cacher = (CdmTransientEntityCacher) presenter.getCache();
|
50
|
debugResults = new EntityCacherDebugResult(cacher, presenter.getRootEntities());
|
51
|
initContent();
|
52
|
}
|
53
|
|
54
|
private void initContent() {
|
55
|
|
56
|
|
57
|
TextField filterField = new TextFieldNFix();
|
58
|
filterField.setInputPrompt("Enter filter text");
|
59
|
filterField.addTextChangeListener(e -> filterTree(e.getText()));
|
60
|
filterField.setWidth("100%");
|
61
|
|
62
|
entityTree = new Tree("Cache Content");
|
63
|
buildTree(entityTree, debugResults.getRootElements());
|
64
|
|
65
|
Label debugInformation = new Label(); // );
|
66
|
debugInformation.setCaption("Debug Information");
|
67
|
debugInformation.setValue(debugResults.toString());
|
68
|
debugInformation.setContentMode(ContentMode.PREFORMATTED);
|
69
|
|
70
|
setCompositionRoot(layout);
|
71
|
|
72
|
entityTree.setSizeUndefined();
|
73
|
debugInformation.setSizeUndefined();
|
74
|
debugInformation.setReadOnly(true);
|
75
|
splitPanel.setFirstComponent(entityTree);
|
76
|
splitPanel.setSecondComponent(debugInformation);
|
77
|
splitPanel.setSplitPosition(50, Unit.PERCENTAGE);
|
78
|
splitPanel.setSizeFull();
|
79
|
|
80
|
layout.addComponents(filterField, splitPanel);
|
81
|
layout.setSizeFull();
|
82
|
layout.setExpandRatio(splitPanel, 1.0f);
|
83
|
|
84
|
|
85
|
}
|
86
|
|
87
|
/**
|
88
|
* @param value
|
89
|
* @return
|
90
|
*/
|
91
|
private void filterTree(String text) {
|
92
|
IndexedContainer indexedContainer = (IndexedContainer)entityTree.getContainerDataSource();
|
93
|
indexedContainer.removeAllContainerFilters();
|
94
|
|
95
|
if(!text.isEmpty()){
|
96
|
SimpleStringFilter filter = new SimpleStringFilter("label", text, true, false);
|
97
|
indexedContainer.addContainerFilter(filter);
|
98
|
((HierarchicalContainer)entityTree.getContainerDataSource()).rootItemIds().forEach(rid -> entityTree.expandItemsRecursively(rid));
|
99
|
}
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @param entityTree
|
104
|
* @param rootElements
|
105
|
*/
|
106
|
private void buildTree(Tree tree, List<CdmEntityInfo> childElements) {
|
107
|
|
108
|
HierarchicalContainer container = new HierarchicalContainer();
|
109
|
|
110
|
container.addContainerProperty("label", String.class, "");
|
111
|
tree.setItemCaptionPropertyId("label");
|
112
|
buildTree(container, childElements, null);
|
113
|
|
114
|
tree.setContainerDataSource(container);
|
115
|
|
116
|
container.setItemSorter(new CdmEntityInfoSorter());
|
117
|
|
118
|
}
|
119
|
|
120
|
private void buildTree(HierarchicalContainer container, List<CdmEntityInfo> childElements, Object parentItemId) {
|
121
|
for(CdmEntityInfo cei : childElements){
|
122
|
String itemId = cei.getLabel();
|
123
|
container.addItem(itemId);
|
124
|
container.getItem(itemId).getItemProperty("label").setValue(itemId);
|
125
|
if(parentItemId != null){
|
126
|
container.setParent(itemId, parentItemId);
|
127
|
}
|
128
|
if(cei.getChildren().isEmpty()){
|
129
|
container.setChildrenAllowed(itemId, false);
|
130
|
} else {
|
131
|
buildTree(container, cei.getChildren(), itemId);
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
|
137
|
}
|