Project

General

Profile

Download (3.63 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.remote.vaadin.components;
2

    
3
import java.io.Serializable;
4
import java.util.ArrayList;
5
import java.util.Collection;
6
import java.util.Collections;
7
import java.util.Comparator;
8

    
9
import org.springframework.context.annotation.Scope;
10

    
11
import com.vaadin.ui.CustomComponent;
12
import com.vaadin.ui.Tree;
13
import com.vaadin.ui.Window;
14

    
15
import eu.etaxonomy.cdm.model.common.Language;
16
import eu.etaxonomy.cdm.model.description.CategoricalData;
17
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
18
import eu.etaxonomy.cdm.model.description.Distribution;
19
import eu.etaxonomy.cdm.model.description.StateData;
20
import eu.etaxonomy.cdm.model.description.TextData;
21
import eu.etaxonomy.cdm.model.taxon.Taxon;
22

    
23

    
24
@Scope("request")
25
public class DetailWindow extends CustomComponent implements Serializable{
26

    
27
	
28
	private Collection<DescriptionElementBase> listDescriptions;
29
	private Taxon taxon;
30

    
31
	public DetailWindow(Taxon taxon, Collection<DescriptionElementBase> listDescriptions) {
32
		this.taxon = taxon;
33
		this.listDescriptions = listDescriptions;
34
		
35
	}
36
	
37
	public Window createWindow(){
38
		Window window = new Window();
39
		window.setHeight("600px");
40
		window.setWidth("400px");
41
		window.setCaption(taxon.getName().getTitleCache());
42
		window.setContent(constructDescriptionTree(taxon));
43
		return window;
44
	}
45
	
46
	private Tree constructDescriptionTree(Taxon taxon){
47
		Tree tree = new Tree();
48
		tree.setSizeUndefined();
49
		String parent = "Descriptive Data";
50
		tree.setValue(parent);
51
		initDescriptionTree(tree, listDescriptions, parent);
52
		return tree;
53
	}
54
	
55
	private void initDescriptionTree(Tree tree, Collection<DescriptionElementBase>listDescriptions, Object parent) {
56
		//TODO: sorting List
57
//		ArrayList<DescriptionElementBase> lDesc = new ArrayList<DescriptionElementBase>(listDescriptions);
58
//		sort(lDesc);
59
		for (DescriptionElementBase deb : listDescriptions){
60
			tree.addItem(deb.getFeature());
61
			tree.setItemCaption(deb.getFeature(), deb.getFeature().getTitleCache());
62
			tree.setParent(deb.getFeature(), parent);
63
			tree.setChildrenAllowed(deb.getFeature(), true);
64
			
65
			if(deb instanceof CategoricalData){
66
				CategoricalData cd = (CategoricalData) deb;
67
				if(cd.getStatesOnly().size() <= 1){
68
					for(StateData st  : cd.getStateData()){
69
						tree.addItem(st);
70
						tree.setItemCaption(st, st.getState().getTitleCache());
71
						tree.setParent(st, deb.getFeature());
72
						tree.setChildrenAllowed(st, false);
73
					}
74
				}else{
75
					//TODO: implement recursion
76
				}
77
			}else if(deb instanceof TextData){
78
				TextData td = (TextData) deb;
79
				tree.addItem(td);
80
				tree.setItemCaption(td, td.getText(Language.GERMAN()));
81
				tree.setParent(td, deb.getFeature());
82
				tree.setChildrenAllowed(td, false);
83
			}else if(deb instanceof Distribution){
84
				Distribution db = (Distribution) deb;
85
				
86
				tree.addItem(db.toString());
87
				tree.setParent(db.toString(), deb.getFeature());
88
				tree.setChildrenAllowed(db.toString(), true);
89
				
90
				tree.addItem(db.getStatus().toString());
91
				tree.setParent(db.getStatus().toString(), db.toString());
92
				tree.setChildrenAllowed(db.getStatus().toString(), false);
93
			}
94
			tree.expandItemsRecursively(parent);
95
		}
96

    
97
	}
98

    
99
	private void sort(ArrayList<DescriptionElementBase> lDesc) {
100
		Collections.sort(lDesc, new Comparator<DescriptionElementBase>() {
101
			@Override
102
			public int compare(DescriptionElementBase o1,
103
					DescriptionElementBase o2) {
104
			    if (o1.toString() == null && o2.toString() == null) {
105
			        return 0;
106
			      }
107
			      if (o1.toString() == null) {
108
			        return 1;
109
			      }
110
			      if (o2.toString() == null) {
111
			        return -1;
112
			      }
113
			      return o2.toString().compareTo(o2.toString());
114
			}
115
		});
116
	}
117

    
118
}
(4-4/8)