Project

General

Profile

« Previous | Next » 

Revision 65611564

Added by Patrick Plitzner about 6 years ago

ref #7095 Refactoring

  • Extracted DescriptionTreeFormat class to own class file

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/workingSet/matrix/CharacterMatrix.java
16 16
import java.util.Arrays;
17 17
import java.util.Collection;
18 18
import java.util.Collections;
19
import java.util.Comparator;
20 19
import java.util.HashMap;
21 20
import java.util.HashSet;
22 21
import java.util.List;
......
123 122
import eu.etaxonomy.cdm.model.description.State;
124 123
import eu.etaxonomy.cdm.model.description.WorkingSet;
125 124
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
126
import eu.etaxonomy.cdm.model.taxon.Taxon;
127
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
128 125
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
129 126
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical.CategoricalDataCellEditor;
130 127
import eu.etaxonomy.taxeditor.editor.workingSet.matrix.categorical.CategoricalDataDisplayConverter;
......
919 916
        return propertyMap;
920 917
    }
921 918

  
922
    /*
923
    * Using a String directly as the tree item has the possible disadvantage of
924
    * haven non-unique items in the tree within subtrees.
925
    */
926
    private class DescriptionTreeFormat implements TreeList.Format<Object> {
927

  
928
        private Map parentMapping = new HashMap<>();
929

  
930
        /**
931
         * Populate path with a list describing the path from a root node to
932
         * this element. Upon returning, the list must have size >= 1, where the
933
         * provided element identical to the list's last element. This
934
         * implementation will use the first object found for a last name as
935
         * root node by storing it within a map. If there is already an object
936
         * stored for the lastname of the given element, it will be used as root
937
         * for the path.
938
         */
939
        @Override
940
        public void getPath(List path, Object element) {
941
            if(element instanceof RowWrapper){
942
                //TODO: check for multiple taxon nodes in multiple classifications
943
                Taxon taxon = (Taxon) ((RowWrapper) element).getAssociatedTaxa().iterator().next();
944
                Set<TaxonNode> taxonNodes = taxon.getTaxonNodes();
945
                if(taxonNodes!=null){
946
                    TaxonNode node = taxonNodes.iterator().next();
947
                    addPathRecursive(path, node);
948
//                    Object rowWrapperParentNode = parentMapping.get(node);
949
//                    if(rowWrapperParentNode!=null){
950
//                        path.add(rowWrapperParentNode);
951
//                    }
952
//                    else{
953
//                        TaxonNode parentNode = HibernateProxyHelper.deproxy(node.getParent(), TaxonNode.class);
954
//                        while(parentNode!=null){
955
//                            path.add(parentNode);
956
//                            parentMapping.put(node, parentNode);
957
//                            if(parentNode instanceof TaxonNode){
958
//                                if(parentMapping.get(parentNode)!=null){
959
//                                    path.add(parentMapping).get(parentNode);
960
//                                }
961
//                                else{
962
//                                    parentNode = HibernateProxyHelper.deproxy(parentNode.getParent(), TaxonNode.class);
963
//                                }
964
//                            }
965
//                        }
966
//                    }
967
                }
968
            }
969
            path.add(element);
970
        }
971

  
972
        private void addPathRecursive(List path, TaxonNode node){
973
            if(node.getParent()!=null){
974
                addPathRecursive(path, node.getParent());
975
                path.add(node);
976
            }
977
        }
978

  
979
        /**
980
         * Simply always return true.
981
         *
982
         * @return true if this element can have child elements, or
983
         *         false if it is always a leaf node.
984
         */
985
        @Override
986
        public boolean allowsChildren(Object element) {
987
            return true;
988
        }
989

  
990
        /**
991
         * Returns the comparator used to order path elements of the specified
992
         * depth. If enforcing order at this level is not intended, this method
993
         * should return null. We do a simple sorting of the last
994
         * names of the persons to show so the tree nodes are sorted in
995
         * alphabetical order.
996
         */
997
        @Override
998
        public Comparator<Object> getComparator(int depth) {
999
            return new Comparator<Object>() {
1000

  
1001
                @Override
1002
                public int compare(Object o1, Object o2) {
1003
//                    if(o1 instanceof RowWrapper && o2 instanceof RowWrapper){
1004
//                        return ((RowWrapper) o1).getSpecimenDescription().getId()-(((RowWrapper) o2).getSpecimenDescription().getId());
1005
//                    }
1006
//                    else
1007
                        if(o1 instanceof TaxonNode && o2 instanceof TaxonNode){
1008
                        return ((TaxonNode) o1).getTaxon().getName().getNameCache().compareTo(((TaxonNode) o2).getTaxon().getName().getNameCache());
1009
                    }
1010
                    return 0;
1011
                }
1012

  
1013
            };
1014
        }
1015
    }
1016

  
1017

  
1018 919
}
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.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
 }

Also available in: Unified diff