Project

General

Profile

Download (4.72 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 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

    
11
package eu.etaxonomy.taxeditor.model;
12

    
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;
18
import java.util.Set;
19

    
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;
27

    
28
/**
29
 * @author n.hoffmann
30
 * @created Dec 2, 2010
31
 * @version 1.0
32
 */
33
public class FeatureNodeContainerTree {
34

    
35
	/** Constant <code>comparator</code> */
36
	// TODO we will do this with a ViewerSorter
37
	public static Comparator<DescriptionElementBase> comparator = new Comparator<DescriptionElementBase>() {
38

    
39
		@Override
40
		public int compare(DescriptionElementBase o1, DescriptionElementBase o2) {
41
			return DescriptionHelper.getLabel(o1).compareTo(DescriptionHelper.getLabel(o2));
42
		}
43
	};
44
	
45
	private Set<FeatureNodeContainer> featureNodeContainers = new HashSet<FeatureNodeContainer>();
46
	
47
	private FeatureNodeContainer root;
48
	private DescriptionBase description;
49
	private FeatureTree featureTree;
50
	
51
	public FeatureNodeContainerTree(DescriptionBase description, FeatureTree featureTree){
52
		this.description = description;
53
		this.featureTree = featureTree;
54
		
55
		root = new FeatureNodeContainer(this);
56
		
57
		buildTree();
58
	}
59
	
60
	/**
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.
64
	 */
65
	public void buildTree(){
66
		List<FeatureNode> children = featureTree.getRootChildren();
67
		
68
//		if(description.getElements().isEmpty()){
69
//			return;
70
//		}
71
		
72
		for(FeatureNode featureNode : children){
73
			root.findLeaves(featureNode);
74
		}
75
	}
76
	
77
	/**
78
	 * <p>getFeatureNodeContainerForDescriptionElement</p>
79
	 *
80
	 * @param descriptionElement a {@link eu.etaxonomy.cdm.model.description.DescriptionElementBase} object.
81
	 * @return a {@link eu.etaxonomy.taxeditor.model.FeatureNodeContainer} object.
82
	 */
83
	public FeatureNodeContainer getFeatureNodeContainerForDescriptionElement (DescriptionElementBase descriptionElement) {
84
		List<FeatureNodeContainer> leafs = root.getLeafs();
85
		
86
		for(FeatureNodeContainer container : leafs){
87
			if(container.getDescriptionElements().contains(descriptionElement)){
88
				return container;
89
			}
90
		}
91
		return null;
92
	}
93
	
94
	/**
95
	 * Returns all elements contained in the given {@link DescriptionBase description} that have the given {@link Feature feature}.
96
	 * 
97
	 * @param description
98
	 * @param feature
99
	 * @return a list of description elements with the given feature alphabetically sorted by {@link DescriptionHelper#getLabel(Object)}
100
	 */
101
	public List<DescriptionElementBase> getDescriptionsElementsForFeature(Feature feature) {
102
		List<DescriptionElementBase> featureElements = new ArrayList<DescriptionElementBase>();
103
		
104
		Set<DescriptionElementBase> elements = description.getElements();
105
		
106
		if (elements != null) {
107
			for (DescriptionElementBase element : elements) {
108
				Feature elementFeature = (Feature) HibernateProxyHelper.deproxy(element.getFeature());
109
				
110
				if (feature.equals(elementFeature)) {
111
					featureElements.add(element);
112
				}
113
			}
114
		}
115
		
116
		if(featureElements.size() != 0){
117
			Collections.sort(featureElements, comparator);
118
		}
119
		
120
		return featureElements;
121
	}
122
	
123
	
124
	public FeatureNodeContainer getFeatureNodeContainer(Feature feature) {		
125
		List<FeatureNodeContainer> leafs = root.getLeafs();
126
		
127
		for(FeatureNodeContainer container : leafs){
128
			if(container.getFeature().equals(feature)){
129
				return container;
130
			}
131
		}
132
		return null;
133
	}
134
	
135
	public FeatureNodeContainer getFeatureNodeContainer(FeatureNode featureNode){
136
		List<FeatureNodeContainer> leafs = root.getLeafs();
137
		
138
		for(FeatureNodeContainer container : leafs){
139
			if(container.getFeatureNode().equals(featureNode)){
140
				return container;
141
			}
142
		}
143
		return null;
144
	}
145
	
146
	public void addContainer(FeatureNodeContainer container){
147
		featureNodeContainers.add(container);
148
	}
149

    
150
	/**
151
	 * 
152
	 */
153
	public void removeContainer(FeatureNodeContainer container) {
154
		featureNodeContainers.remove(container);
155
	}
156
	
157
	public FeatureNodeContainer getRoot() {
158
		return root;
159
	}
160

    
161
	public DescriptionBase getDescription() {
162
		return description;
163
	}
164

    
165
	public FeatureTree getFeatureTree() {
166
		return featureTree;
167
	}
168
	
169
}
(14-14/38)