1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.key.polytomous;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.List;
|
8
|
|
9
|
import org.eclipse.jface.viewers.IBaseLabelProvider;
|
10
|
import org.eclipse.jface.viewers.IContentProvider;
|
11
|
import org.eclipse.jface.viewers.LabelProvider;
|
12
|
import org.eclipse.jface.viewers.Viewer;
|
13
|
import org.eclipse.zest.core.viewers.IGraphContentProvider;
|
14
|
|
15
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
16
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
17
|
import eu.etaxonomy.cdm.model.description.PolytomousKey;
|
18
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
19
|
import eu.etaxonomy.taxeditor.editor.key.AbstractKeyEditor;
|
20
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
21
|
|
22
|
/**
|
23
|
* @author n.hoffmann
|
24
|
*
|
25
|
*/
|
26
|
public class PolytomousKeyEditor extends AbstractKeyEditor<PolytomousKey> {
|
27
|
|
28
|
private class PolytomousKeyRelationship {
|
29
|
private Object destination;
|
30
|
private Object source;
|
31
|
|
32
|
PolytomousKeyRelationship(Object source, Object destination){
|
33
|
this.source = HibernateProxyHelper.deproxy(source);
|
34
|
this.destination = HibernateProxyHelper.deproxy(destination);
|
35
|
}
|
36
|
}
|
37
|
|
38
|
private class ContentProvider implements IGraphContentProvider{
|
39
|
|
40
|
private List<PolytomousKeyRelationship> relations;
|
41
|
|
42
|
@Override
|
43
|
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
|
44
|
|
45
|
}
|
46
|
|
47
|
@Override
|
48
|
public void dispose() {
|
49
|
|
50
|
}
|
51
|
|
52
|
@Override
|
53
|
public Object[] getElements(Object inputElement) {
|
54
|
relations = new ArrayList<PolytomousKeyRelationship>();
|
55
|
|
56
|
if(inputElement instanceof PolytomousKey){
|
57
|
getTreeRecursively(relations, inputElement);
|
58
|
}
|
59
|
return relations.toArray();
|
60
|
}
|
61
|
|
62
|
private void getTreeRecursively(List<PolytomousKeyRelationship> relations, Object parent){
|
63
|
List<PolytomousKeyNode> children;
|
64
|
|
65
|
if(parent instanceof PolytomousKeyNode){
|
66
|
children = ((PolytomousKeyNode) parent).getChildren();
|
67
|
}else if(parent instanceof PolytomousKey){
|
68
|
children = ((PolytomousKey) parent).getRoot().getChildren();
|
69
|
}else{
|
70
|
throw new RuntimeException("Parent element has to be PolytomousKeyNode or PolytomousKey, but was: " + parent.getClass());
|
71
|
}
|
72
|
|
73
|
for(PolytomousKeyNode childNode : children){
|
74
|
relations.add(new PolytomousKeyRelationship(parent, childNode));
|
75
|
getTreeRecursively(relations, childNode);
|
76
|
}
|
77
|
}
|
78
|
|
79
|
|
80
|
@Override
|
81
|
public Object getSource(Object rel) {
|
82
|
return ((PolytomousKeyRelationship) rel).source;
|
83
|
}
|
84
|
|
85
|
@Override
|
86
|
public Object getDestination(Object rel) {
|
87
|
return ((PolytomousKeyRelationship) rel).destination;
|
88
|
}
|
89
|
|
90
|
};
|
91
|
|
92
|
private class PolytomousKeyLabelProvider extends LabelProvider{
|
93
|
@Override
|
94
|
public String getText(Object element) {
|
95
|
if(element instanceof PolytomousKey){
|
96
|
return ((PolytomousKey) element).getTitleCache();
|
97
|
}else if(element instanceof PolytomousKeyNode){
|
98
|
PolytomousKeyNode keyNode = (PolytomousKeyNode) element;
|
99
|
|
100
|
if(keyNode.isLeaf()){
|
101
|
if(keyNode.getTaxon() != null){
|
102
|
return keyNode.getTaxon().getName().getTitleCache();
|
103
|
}
|
104
|
return "leaf but no taxon";
|
105
|
}
|
106
|
|
107
|
if(keyNode.getNodeNumber() != null){
|
108
|
return keyNode.getNodeNumber().toString();
|
109
|
}else{
|
110
|
return "No node number set";
|
111
|
}
|
112
|
}else if(element instanceof PolytomousKeyRelationship){
|
113
|
PolytomousKeyRelationship relationship = (PolytomousKeyRelationship) element;
|
114
|
if(relationship.source instanceof PolytomousKeyNode){
|
115
|
PolytomousKeyNode sourceNode = (PolytomousKeyNode) relationship.source;
|
116
|
if(sourceNode.getQuestion() != null && sourceNode.getQuestion().getLabelText(CdmStore.getDefaultLanguage()) != null){
|
117
|
return sourceNode.getQuestion().getLabelText(CdmStore.getDefaultLanguage());
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
return "";
|
122
|
}
|
123
|
};
|
124
|
|
125
|
public static final String ID = "eu.etaxonomy.taxeditor.editor.key.polytomous";
|
126
|
|
127
|
/**
|
128
|
*
|
129
|
*/
|
130
|
public PolytomousKeyEditor() {
|
131
|
super();
|
132
|
}
|
133
|
|
134
|
|
135
|
@Override
|
136
|
public ConversationHolder getConversationHolder() {
|
137
|
return ((PolytomousKeyEditorInput) getEditorInput()).getConversationHolder();
|
138
|
}
|
139
|
|
140
|
@Override
|
141
|
public PolytomousKey getKey() {
|
142
|
return ((PolytomousKeyEditorInput) getEditorInput()).getKey();
|
143
|
}
|
144
|
|
145
|
|
146
|
@Override
|
147
|
protected IBaseLabelProvider getLabelProvider() {
|
148
|
return new PolytomousKeyLabelProvider();
|
149
|
}
|
150
|
|
151
|
|
152
|
@Override
|
153
|
protected IContentProvider getContentProvider() {
|
154
|
return new ContentProvider();
|
155
|
}
|
156
|
}
|