6fd9705be3386e56dc803dcb54463a7f07191015
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / workingSet / matrix / DescriptionTreeFormat.java
1 // $Id$
2 /**
3 * Copyright (C) 2018 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10 package eu.etaxonomy.taxeditor.editor.workingSet.matrix;
11
12 import java.util.Comparator;
13 import java.util.List;
14 import java.util.Set;
15
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;
24
25 /**
26 * @author pplitzner
27 * @date 23.01.2018
28 *
29 */
30 public class DescriptionTreeFormat implements TreeList.Format<Object> {
31
32 private Comparator<TaxonNode> comparator;
33 private Rank minRank;
34 private Rank maxRank;
35
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();
42 }else {
43 comparator = new TaxonNodeByRankAndNameComparator();
44 }
45 }
46
47 @Override
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();
53 if(taxonNodes!=null){
54 TaxonNode node = taxonNodes.iterator().next();
55 addPathRecursive(path, node);
56 }
57 }
58 path.add(element);
59 }
60
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)){
68 path.add(node);
69 return;
70 }
71 addPathRecursive(path, node.getParent());
72 path.add(node);
73 }
74 }
75
76 @Override
77 public boolean allowsChildren(Object element) {
78 if(element instanceof TaxonNode){
79 return true;
80 }
81 return false;
82 }
83
84 @Override
85 public Comparator<Object> getComparator(int depth) {
86 return new Comparator<Object>() {
87
88 @Override
89 public int compare(Object o1, Object o2) {
90 if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
91 return comparator.compare((TaxonNode)o1, (TaxonNode)o2);
92 }
93 return o1.hashCode()-o2.hashCode();
94 }
95
96 };
97 }
98 }