ref #7095 Refactor RowWrapper
[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
15 import ca.odell.glazedlists.TreeList;
16 import eu.etaxonomy.cdm.model.description.WorkingSet;
17 import eu.etaxonomy.cdm.model.name.Rank;
18 import eu.etaxonomy.cdm.model.taxon.Classification;
19 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
20
21 /**
22 * The tree format defines how to build the hierarchy path for the
23 * taxon column in the character matrix
24 *
25 * @author pplitzner
26 * @date 23.01.2018
27 *
28 */
29 public class DescriptionTreeFormat implements TreeList.Format<Object> {
30
31 private Rank maxRank;
32
33 public DescriptionTreeFormat(WorkingSet workingSet) {
34 this.maxRank = workingSet.getMaxRank();
35 }
36
37 @Override
38 public void getPath(List path, Object element) {
39 if(element instanceof RowWrapper){
40 //TODO: check for multiple taxon nodes in multiple classifications
41 TaxonNode taxonNode = ((RowWrapper) element).getTaxonNode();
42 addPathRecursive(path, taxonNode);
43 }
44 path.add(element);
45 }
46
47 private void addPathRecursive(List path, TaxonNode node){
48 if(node.getParent()!=null
49 && node.getTaxon()!=null
50 && node.getTaxon().getName()!=null
51 && node.getTaxon().getName().getRank()!=null){
52 Rank rank = node.getTaxon().getName().getRank();
53 if(maxRank==null || maxRank.equals(rank)){
54 path.add(node);
55 return;
56 }
57 addPathRecursive(path, node.getParent());
58 path.add(node);
59 }
60 }
61
62 @Override
63 public boolean allowsChildren(Object element) {
64 if(element instanceof TaxonNode){
65 return true;
66 }
67 return false;
68 }
69
70 @Override
71 public Comparator<Object> getComparator(int depth) {
72 return new MatrixRowComparator();
73 }
74
75 }