Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
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.HashMap;
14
import java.util.List;
15
import java.util.Map;
16
import java.util.Set;
17

    
18
import ca.odell.glazedlists.TreeList;
19
import eu.etaxonomy.cdm.model.taxon.Taxon;
20
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
21

    
22
/**
23
 * @author pplitzner
24
 * @date 23.01.2018
25
 *
26
 */
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
 public class DescriptionTreeFormat implements TreeList.Format<Object> {
32

    
33
     private Map parentMapping = new HashMap<>();
34

    
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
     @Override
45
     public void getPath(List path, Object element) {
46
         if(element instanceof RowWrapper){
47
             //TODO: check for multiple taxon nodes in multiple classifications
48
             Taxon taxon = (Taxon) ((RowWrapper) element).getAssociatedTaxa().iterator().next();
49
             Set<TaxonNode> taxonNodes = taxon.getTaxonNodes();
50
             if(taxonNodes!=null){
51
                 TaxonNode node = taxonNodes.iterator().next();
52
                 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
             }
73
         }
74
         path.add(element);
75
     }
76

    
77
     private void addPathRecursive(List path, TaxonNode node){
78
         if(node.getParent()!=null){
79
             addPathRecursive(path, node.getParent());
80
             path.add(node);
81
         }
82
     }
83

    
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
     @Override
91
     public boolean allowsChildren(Object element) {
92
         return true;
93
     }
94

    
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
     @Override
103
     public Comparator<Object> getComparator(int depth) {
104
         return new Comparator<Object>() {
105

    
106
             @Override
107
             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());
114
                 }
115
                 return 0;
116
             }
117

    
118
         };
119
     }
120
 }
(5-5/10)