Project

General

Profile

Download (4.72 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.model;
11

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

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

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

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

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

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

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

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