3 * Copyright (C) 2018 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.editor
.workingSet
.matrix
;
12 import java
.util
.Comparator
;
13 import java
.util
.List
;
16 import ca
.odell
.glazedlists
.TreeList
;
17 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
18 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
19 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNaturalComparator
;
20 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
21 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByNameComparator
;
22 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNodeByRankAndNameComparator
;
23 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
30 public class DescriptionTreeFormat
implements TreeList
.Format
<Object
> {
32 private Comparator
<TaxonNode
> comparator
;
36 public DescriptionTreeFormat(Rank maxRank
) {
37 this.maxRank
= maxRank
;
38 if (PreferencesUtil
.getSortNodesNaturally()){
39 comparator
= new TaxonNaturalComparator();
40 } else if (PreferencesUtil
.getSortNodesStrictlyAlphabetically()){
41 comparator
= new TaxonNodeByNameComparator();
43 comparator
= new TaxonNodeByRankAndNameComparator();
48 public void getPath(List path
, Object element
) {
49 if(element
instanceof RowWrapper
){
50 //TODO: check for multiple taxon nodes in multiple classifications
51 Taxon taxon
= (Taxon
) ((RowWrapper
) element
).getAssociatedTaxa().iterator().next();
52 Set
<TaxonNode
> taxonNodes
= taxon
.getTaxonNodes();
54 TaxonNode node
= taxonNodes
.iterator().next();
55 addPathRecursive(path
, node
);
61 private void addPathRecursive(List path
, TaxonNode node
){
62 if(node
.getParent()!=null
63 && node
.getTaxon()!=null
64 && node
.getTaxon().getName()!=null
65 && node
.getTaxon().getName().getRank()!=null){
66 Rank rank
= node
.getTaxon().getName().getRank();
67 if(maxRank
.equals(rank
)){
71 addPathRecursive(path
, node
.getParent());
77 public boolean allowsChildren(Object element
) {
78 if(element
instanceof TaxonNode
){
85 public Comparator
<Object
> getComparator(int depth
) {
86 return new Comparator
<Object
>() {
89 public int compare(Object o1
, Object o2
) {
90 if(o1
instanceof TaxonNode
&& o2
instanceof TaxonNode
){
91 return comparator
.compare((TaxonNode
)o1
, (TaxonNode
)o2
);
93 return o1
.hashCode()-o2
.hashCode();