Revision a6e7790d
Added by Patrick Plitzner over 7 years ago
src/main/java/eu/etaxonomy/cdm/vaadin/container/TaxonNodeContainer.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.util.ArrayList; |
4 | 4 |
import java.util.Arrays; |
5 |
import java.util.Collection; |
|
6 | 5 |
import java.util.List; |
7 | 6 |
|
8 | 7 |
import com.vaadin.data.Item; |
9 | 8 |
import com.vaadin.data.util.HierarchicalContainer; |
10 |
import com.vaadin.ui.Tree; |
|
11 | 9 |
|
12 | 10 |
import eu.etaxonomy.cdm.api.service.IClassificationService; |
13 | 11 |
import eu.etaxonomy.cdm.model.name.Rank; |
... | ... | |
19 | 17 |
|
20 | 18 |
private static final long serialVersionUID = 102401340698963360L; |
21 | 19 |
public static final String LABEL = "label"; |
22 |
|
|
23 |
private Tree tree; |
|
24 |
|
|
20 |
|
|
25 | 21 |
/** |
26 | 22 |
* Creates a new taxon node container |
27 | 23 |
* @param parentNode the parent node which will <b>not</b> be included |
28 | 24 |
* in the result but only its child nodes |
29 | 25 |
*/ |
30 |
public TaxonNodeContainer(TaxonNode parentNode, Tree tree) { |
|
31 |
this.tree = tree; |
|
32 |
List<TaxonNode> taxonNodeList = getTaxonNodeList(parentNode, tree); |
|
26 |
public TaxonNodeContainer(TaxonNode parentNode) { |
|
33 | 27 |
addContainerProperty(LABEL, String.class, "[no taxon]"); |
34 |
for (TaxonNode taxonNode : taxonNodeList) { |
|
35 |
Item item = addItem(taxonNode); |
|
36 |
if(taxonNode.getTaxon()!=null){ |
|
37 |
item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache()); |
|
38 |
} |
|
39 |
else if(taxonNode.getClassification()!=null){ |
|
40 |
item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText()); |
|
41 |
} |
|
42 |
} |
|
28 |
getTaxonNodeList(parentNode); |
|
43 | 29 |
} |
44 |
|
|
45 |
public List<TaxonNode> getTaxonNodeList(TaxonNode parentNode, Tree tree) {
|
|
30 |
|
|
31 |
public void getTaxonNodeList(TaxonNode parentNode) {
|
|
46 | 32 |
List<TaxonNode> nodes = new ArrayList<TaxonNode>(); |
47 |
|
|
33 |
|
|
48 | 34 |
List<String> nodeInitStrategy = Arrays.asList(new String[]{ |
49 | 35 |
"taxon.sec", |
50 | 36 |
"taxon.name", |
... | ... | |
56 | 42 |
IClassificationService classificationService = CdmSpringContextHelper.getClassificationService(); |
57 | 43 |
List<Classification> classificationList = classificationService.listClassifications(null, null, null, nodeInitStrategy); |
58 | 44 |
for (Classification classification : classificationList) { |
59 |
TaxonNode rootNode = classification.getRootNode(); |
|
45 |
TaxonNode rootNode = classification.getRootNode(); |
|
46 |
Item item = addItem(rootNode); |
|
60 | 47 |
setChildrenAllowed(rootNode, false); |
61 | 48 |
nodes.add(rootNode); |
49 |
if(rootNode.getClassification()!=null){ |
|
50 |
item.getItemProperty(LABEL).setValue(rootNode.getClassification().getName().getText()); |
|
51 |
} |
|
62 | 52 |
} |
63 | 53 |
} |
64 | 54 |
else{ |
65 | 55 |
//load child nodes |
66 |
nodes.addAll(addChildNodes(parentNode, tree));
|
|
56 |
addChildNodes(parentNode);
|
|
67 | 57 |
} |
68 |
return nodes; |
|
69 | 58 |
} |
70 | 59 |
|
71 |
private Collection<? extends TaxonNode> addChildNodes(TaxonNode parentNode, Tree tree) { |
|
72 |
tree.addItem(parentNode); |
|
73 |
List<TaxonNode> nodes = new ArrayList<TaxonNode>(); |
|
60 |
private void addChildNodes(TaxonNode parentNode) { |
|
74 | 61 |
List<TaxonNode> childNodes = parentNode.getChildNodes(); |
75 | 62 |
setChildrenAllowed(parentNode, !childNodes.isEmpty()); |
63 |
boolean hasValidChildren = false; |
|
76 | 64 |
for (TaxonNode taxonNode : childNodes) { |
77 | 65 |
if(taxonNode!=null && taxonNode.getTaxon()!=null && taxonNode.getTaxon().getName()!=null){ |
78 | 66 |
Rank rank = taxonNode.getTaxon().getName().getRank(); |
79 | 67 |
if(rank!=null && rank.isHigher(Rank.SPECIES())){ |
80 |
nodes.add(taxonNode); |
|
81 |
tree.setParent(taxonNode, parentNode); |
|
82 |
addChildNodes(taxonNode, tree); |
|
83 |
} |
|
84 |
else{ |
|
85 |
tree.setChildrenAllowed(parentNode, false); |
|
68 |
Item item = addItem(taxonNode); |
|
69 |
setParent(taxonNode, parentNode); |
|
70 |
if(taxonNode.getTaxon()!=null){ |
|
71 |
item.getItemProperty(LABEL).setValue(taxonNode.getTaxon().getName().getTitleCache()); |
|
72 |
} |
|
73 |
else if(taxonNode.getClassification()!=null){ |
|
74 |
item.getItemProperty(LABEL).setValue(taxonNode.getClassification().getName().getText()); |
|
75 |
} |
|
76 |
hasValidChildren = true; |
|
77 |
|
|
78 |
addChildNodes(taxonNode); |
|
86 | 79 |
} |
87 | 80 |
} |
88 | 81 |
} |
89 |
return nodes;
|
|
82 |
setChildrenAllowed(parentNode, hasValidChildren);
|
|
90 | 83 |
} |
91 |
|
|
84 |
|
|
92 | 85 |
} |
Also available in: Unified diff
ref #5458 Add taxon tree instead of combo box