Revision 4ca3e4f3
Added by Patrick Plitzner over 6 years ago
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/DescriptionTreeFormat.java | ||
---|---|---|
10 | 10 |
package eu.etaxonomy.taxeditor.editor.workingSet.matrix; |
11 | 11 |
|
12 | 12 |
import java.util.Comparator; |
13 |
import java.util.HashMap; |
|
14 | 13 |
import java.util.List; |
15 |
import java.util.Map; |
|
16 | 14 |
import java.util.Set; |
17 | 15 |
|
18 | 16 |
import ca.odell.glazedlists.TreeList; |
19 | 17 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
18 |
import eu.etaxonomy.cdm.model.taxon.TaxonNaturalComparator; |
|
20 | 19 |
import eu.etaxonomy.cdm.model.taxon.TaxonNode; |
20 |
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByNameComparator; |
|
21 |
import eu.etaxonomy.cdm.model.taxon.TaxonNodeByRankAndNameComparator; |
|
22 |
import eu.etaxonomy.taxeditor.preference.PreferencesUtil; |
|
21 | 23 |
|
22 | 24 |
/** |
23 | 25 |
* @author pplitzner |
24 | 26 |
* @date 23.01.2018 |
25 | 27 |
* |
26 | 28 |
*/ |
27 |
/* |
|
28 |
* Using a String directly as the tree item has the possible disadvantage of |
|
29 |
* haven non-unique items in the tree within subtrees. |
|
30 |
*/ |
|
31 | 29 |
public class DescriptionTreeFormat implements TreeList.Format<Object> { |
32 | 30 |
|
33 |
private Map parentMapping = new HashMap<>(); |
|
31 |
private Comparator<TaxonNode> comparator; |
|
32 |
|
|
33 |
public DescriptionTreeFormat() { |
|
34 |
if (PreferencesUtil.getSortNodesNaturally()){ |
|
35 |
comparator = new TaxonNaturalComparator(); |
|
36 |
} else if (PreferencesUtil.getSortNodesStrictlyAlphabetically()){ |
|
37 |
comparator = new TaxonNodeByNameComparator(); |
|
38 |
}else { |
|
39 |
comparator = new TaxonNodeByRankAndNameComparator(); |
|
40 |
} |
|
41 |
} |
|
34 | 42 |
|
35 |
/** |
|
36 |
* Populate path with a list describing the path from a root node to |
|
37 |
* this element. Upon returning, the list must have size >= 1, where the |
|
38 |
* provided element identical to the list's last element. This |
|
39 |
* implementation will use the first object found for a last name as |
|
40 |
* root node by storing it within a map. If there is already an object |
|
41 |
* stored for the lastname of the given element, it will be used as root |
|
42 |
* for the path. |
|
43 |
*/ |
|
44 | 43 |
@Override |
45 | 44 |
public void getPath(List path, Object element) { |
46 | 45 |
if(element instanceof RowWrapper){ |
... | ... | |
50 | 49 |
if(taxonNodes!=null){ |
51 | 50 |
TaxonNode node = taxonNodes.iterator().next(); |
52 | 51 |
addPathRecursive(path, node); |
53 |
// Object rowWrapperParentNode = parentMapping.get(node); |
|
54 |
// if(rowWrapperParentNode!=null){ |
|
55 |
// path.add(rowWrapperParentNode); |
|
56 |
// } |
|
57 |
// else{ |
|
58 |
// TaxonNode parentNode = HibernateProxyHelper.deproxy(node.getParent(), TaxonNode.class); |
|
59 |
// while(parentNode!=null){ |
|
60 |
// path.add(parentNode); |
|
61 |
// parentMapping.put(node, parentNode); |
|
62 |
// if(parentNode instanceof TaxonNode){ |
|
63 |
// if(parentMapping.get(parentNode)!=null){ |
|
64 |
// path.add(parentMapping).get(parentNode); |
|
65 |
// } |
|
66 |
// else{ |
|
67 |
// parentNode = HibernateProxyHelper.deproxy(parentNode.getParent(), TaxonNode.class); |
|
68 |
// } |
|
69 |
// } |
|
70 |
// } |
|
71 |
// } |
|
72 | 52 |
} |
73 | 53 |
} |
74 | 54 |
path.add(element); |
... | ... | |
81 | 61 |
} |
82 | 62 |
} |
83 | 63 |
|
84 |
/** |
|
85 |
* Simply always return true. |
|
86 |
* |
|
87 |
* @return true if this element can have child elements, or |
|
88 |
* false if it is always a leaf node. |
|
89 |
*/ |
|
90 | 64 |
@Override |
91 | 65 |
public boolean allowsChildren(Object element) { |
92 |
return true; |
|
66 |
if(element instanceof TaxonNode){ |
|
67 |
return true; |
|
68 |
} |
|
69 |
return false; |
|
93 | 70 |
} |
94 | 71 |
|
95 |
/** |
|
96 |
* Returns the comparator used to order path elements of the specified |
|
97 |
* depth. If enforcing order at this level is not intended, this method |
|
98 |
* should return null. We do a simple sorting of the last |
|
99 |
* names of the persons to show so the tree nodes are sorted in |
|
100 |
* alphabetical order. |
|
101 |
*/ |
|
102 | 72 |
@Override |
103 | 73 |
public Comparator<Object> getComparator(int depth) { |
104 | 74 |
return new Comparator<Object>() { |
105 | 75 |
|
106 | 76 |
@Override |
107 | 77 |
public int compare(Object o1, Object o2) { |
108 |
// if(o1 instanceof RowWrapper && o2 instanceof RowWrapper){ |
|
109 |
// return ((RowWrapper) o1).getSpecimenDescription().getId()-(((RowWrapper) o2).getSpecimenDescription().getId()); |
|
110 |
// } |
|
111 |
// else |
|
112 |
if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){ |
|
113 |
return ((TaxonNode) o1).getTaxon().getName().getNameCache().compareTo(((TaxonNode) o2).getTaxon().getName().getNameCache()); |
|
78 |
if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){ |
|
79 |
return comparator.compare((TaxonNode)o1, (TaxonNode)o2); |
|
114 | 80 |
} |
115 |
return 0;
|
|
81 |
return o1.hashCode()-o2.hashCode();
|
|
116 | 82 |
} |
117 | 83 |
|
118 | 84 |
}; |
Also available in: Unified diff
ref #7095 correctly sort tree hierarchy via taxon nodes sort prefs