3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.model
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
20 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
21 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
22 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
23 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
24 import eu
.etaxonomy
.cdm
.model
.description
.FeatureNode
;
25 import eu
.etaxonomy
.cdm
.model
.description
.FeatureTree
;
26 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
30 * @created Dec 2, 2010
33 public class FeatureNodeContainerTree
{
35 /** Constant <code>comparator</code> */
36 // TODO we will do this with a ViewerSorter
37 public static Comparator
<DescriptionElementBase
> comparator
= new Comparator
<DescriptionElementBase
>() {
40 public int compare(DescriptionElementBase o1
, DescriptionElementBase o2
) {
41 return DescriptionHelper
.getLabel(o1
).compareTo(DescriptionHelper
.getLabel(o2
));
45 private Set
<FeatureNodeContainer
> featureNodeContainers
= new HashSet
<FeatureNodeContainer
>();
47 private FeatureNodeContainer root
;
48 private DescriptionBase description
;
49 private FeatureTree featureTree
;
51 public FeatureNodeContainerTree(DescriptionBase description
, FeatureTree featureTree
){
52 this.description
= description
;
53 this.featureTree
= featureTree
;
55 root
= new FeatureNodeContainer(this);
61 * Traverses the given {@link FeatureNode} and computes a FeatureNodeContainer branch if the
62 * given {@link TaxonDescription} has elements for the given feature node or any of its children
63 * or null if there are no elements.
65 public void buildTree(){
66 List
<FeatureNode
> children
= featureTree
.getRootChildren();
68 // if(description.getElements().isEmpty()){
72 for(FeatureNode featureNode
: children
){
73 root
.findLeaves(featureNode
);
78 * <p>getFeatureNodeContainerForDescriptionElement</p>
80 * @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
81 * @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
83 public FeatureNodeContainer
getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement
) {
84 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
86 for(FeatureNodeContainer container
: leafs
){
87 if(container
.getDescriptionElements().contains(descriptionElement
)){
95 * Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
99 * @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
101 public List
<DescriptionElementBase
> getDescriptionsElementsForFeature(Feature feature
) {
102 List
<DescriptionElementBase
> featureElements
= new ArrayList
<DescriptionElementBase
>();
104 Set
<DescriptionElementBase
> elements
= description
.getElements();
106 if (elements
!= null) {
107 for (DescriptionElementBase element
: elements
) {
108 Feature elementFeature
= (Feature
) HibernateProxyHelper
.deproxy(element
.getFeature());
110 if (feature
.equals(elementFeature
)) {
111 featureElements
.add(element
);
116 if(featureElements
.size() != 0){
117 Collections
.sort(featureElements
, comparator
);
120 return featureElements
;
124 public FeatureNodeContainer
getFeatureNodeContainer(Feature feature
) {
125 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
127 for(FeatureNodeContainer container
: leafs
){
128 if(container
.getFeature().equals(feature
)){
135 public FeatureNodeContainer
getFeatureNodeContainer(FeatureNode featureNode
){
136 List
<FeatureNodeContainer
> leafs
= root
.getLeafs();
138 for(FeatureNodeContainer container
: leafs
){
139 if(container
.getFeatureNode().equals(featureNode
)){
146 public void addContainer(FeatureNodeContainer container
){
147 featureNodeContainers
.add(container
);
153 public void removeContainer(FeatureNodeContainer container
) {
154 featureNodeContainers
.remove(container
);
157 public FeatureNodeContainer
getRoot() {
161 public DescriptionBase
getDescription() {
165 public FeatureTree
getFeatureTree() {