2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.model
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collections
;
14 import java
.util
.Comparator
;
15 import java
.util
.HashSet
;
16 import java
.util
.List
;
19 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
20 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
21 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
22 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
23 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
24 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
25 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
29 * @created Dec 2, 2010
32 public class FeatureNodeContainerTree
{
34 /** Constant <code>comparator</code> */
35 // TODO we will do this with a ViewerSorter
36 public static Comparator
<DescriptionElementBase
> comparator
= new Comparator
<DescriptionElementBase
>() {
39 public int compare(DescriptionElementBase o1
, DescriptionElementBase o2
) {
43 int result
= DescriptionHelper
.getLabel(o1
).compareTo(DescriptionHelper
.getLabel(o2
));
45 return o1
.getUuid().compareTo(o2
.getUuid());
51 private Set
<FeatureNodeContainer
> featureNodeContainers
= new HashSet
<FeatureNodeContainer
>();
53 private FeatureNodeContainer root
;
54 private DescriptionBase description
;
55 private FeatureTree featureTree
;
57 public FeatureNodeContainerTree(DescriptionBase description
, FeatureTree featureTree
){
58 this.description
= description
;
59 this.featureTree
= featureTree
;
61 root
= new FeatureNodeContainer(this);
67 * Traverses the given {@link FeatureNode} and computes a FeatureNodeContainer branch if the
68 * given {@link TaxonDescription} has elements for the given feature node or any of its children
69 * or null if there are no elements.
71 public void buildTree(){
72 List
<FeatureNode
> children
= featureTree
.getRootChildren();
74 // if(description.getElements().isEmpty()){
78 for(FeatureNode featureNode
: children
){
79 root
.findLeaves(featureNode
);
84 * <p>getFeatureNodeContainerForDescriptionElement</p>
86 * @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
87 * @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
89 public FeatureNodeContainer
getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement
) {
90 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
92 for(FeatureNodeContainer container
: leafs
){
93 if(container
.getDescriptionElements().contains(descriptionElement
)){
101 * Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
105 * @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
107 public List
<DescriptionElementBase
> getDescriptionsElementsForFeature(Feature feature
) {
108 List
<DescriptionElementBase
> featureElements
= new ArrayList
<DescriptionElementBase
>();
110 Set
<DescriptionElementBase
> elements
= description
.getElements();
112 if (elements
!= null) {
113 for (DescriptionElementBase element
: elements
) {
114 Feature elementFeature
= HibernateProxyHelper
.deproxy(element
.getFeature());
116 if (feature
.equals(elementFeature
)) {
117 featureElements
.add(element
);
122 if(featureElements
.size() != 0){
123 Collections
.sort(featureElements
, comparator
);
126 return featureElements
;
130 public FeatureNodeContainer
getFeatureNodeContainer(Feature feature
) {
131 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
133 for(FeatureNodeContainer container
: leafs
){
134 if(container
.getFeature().equals(feature
)){
141 public FeatureNodeContainer
getFeatureNodeContainer(FeatureNode featureNode
){
142 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
144 for(FeatureNodeContainer container
: leafs
){
145 if(container
.getFeatureNode().equals(featureNode
)){
152 public void addContainer(FeatureNodeContainer container
){
153 featureNodeContainers
.add(container
);
159 public void removeContainer(FeatureNodeContainer container
) {
160 featureNodeContainers
.remove(container
);
163 public FeatureNodeContainer
getRoot() {
167 public DescriptionBase
getDescription() {
171 public FeatureTree
getFeatureTree() {