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
) {
40 return DescriptionHelper
.getLabel(o1
).compareTo(DescriptionHelper
.getLabel(o2
));
44 private Set
<FeatureNodeContainer
> featureNodeContainers
= new HashSet
<FeatureNodeContainer
>();
46 private FeatureNodeContainer root
;
47 private DescriptionBase description
;
48 private FeatureTree featureTree
;
50 public FeatureNodeContainerTree(DescriptionBase description
, FeatureTree featureTree
){
51 this.description
= description
;
52 this.featureTree
= featureTree
;
54 root
= new FeatureNodeContainer(this);
60 * Traverses the given {@link FeatureNode} and computes a FeatureNodeContainer branch if the
61 * given {@link TaxonDescription} has elements for the given feature node or any of its children
62 * or null if there are no elements.
64 public void buildTree(){
65 List
<FeatureNode
> children
= featureTree
.getRootChildren();
67 // if(description.getElements().isEmpty()){
71 for(FeatureNode featureNode
: children
){
72 root
.findLeaves(featureNode
);
77 * <p>getFeatureNodeContainerForDescriptionElement</p>
79 * @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
80 * @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
82 public FeatureNodeContainer
getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement
) {
83 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
85 for(FeatureNodeContainer container
: leafs
){
86 if(container
.getDescriptionElements().contains(descriptionElement
)){
94 * Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
98 * @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
100 public List
<DescriptionElementBase
> getDescriptionsElementsForFeature(Feature feature
) {
101 List
<DescriptionElementBase
> featureElements
= new ArrayList
<DescriptionElementBase
>();
103 Set
<DescriptionElementBase
> elements
= description
.getElements();
105 if (elements
!= null) {
106 for (DescriptionElementBase element
: elements
) {
107 Feature elementFeature
= (Feature
) HibernateProxyHelper
.deproxy(element
.getFeature());
109 if (feature
.equals(elementFeature
)) {
110 featureElements
.add(element
);
115 if(featureElements
.size() != 0){
116 Collections
.sort(featureElements
, comparator
);
119 return featureElements
;
123 public FeatureNodeContainer
getFeatureNodeContainer(Feature feature
) {
124 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
126 for(FeatureNodeContainer container
: leafs
){
127 if(container
.getFeature().equals(feature
)){
134 public FeatureNodeContainer
getFeatureNodeContainer(FeatureNode featureNode
){
135 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
137 for(FeatureNodeContainer container
: leafs
){
138 if(container
.getFeatureNode().equals(featureNode
)){
145 public void addContainer(FeatureNodeContainer container
){
146 featureNodeContainers
.add(container
);
152 public void removeContainer(FeatureNodeContainer container
) {
153 featureNodeContainers
.remove(container
);
156 public FeatureNodeContainer
getRoot() {
160 public DescriptionBase
getDescription() {
164 public FeatureTree
getFeatureTree() {