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.singlesource.editor.key.polytomous;
|
12
|
|
13
|
import org.apache.log4j.Logger;
|
14
|
import org.eclipse.jface.viewers.CellLabelProvider;
|
15
|
import org.eclipse.jface.viewers.ViewerCell;
|
16
|
import org.eclipse.swt.SWT;
|
17
|
import org.eclipse.swt.graphics.Color;
|
18
|
|
19
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
20
|
import eu.etaxonomy.cdm.model.description.KeyStatement;
|
21
|
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
|
22
|
import eu.etaxonomy.taxeditor.singlesource.org.eclipse.swt.widgets.DisplayProxy;
|
23
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
24
|
|
25
|
/**
|
26
|
* @author n.hoffmann
|
27
|
* @created Apr 18, 2011
|
28
|
* @version 1.0
|
29
|
*/
|
30
|
public class PolytomousKeyListLabelProviderFacadeImpl extends CellLabelProvider implements IPolytomousKeyListLabelProvider {
|
31
|
|
32
|
private static final Logger logger = Logger.getLogger(PolytomousKeyListLabelProviderFacadeImpl.class);
|
33
|
|
34
|
private static Color colorLink = DisplayProxy.getDefault().getSystemColor(
|
35
|
SWT.COLOR_BLUE);
|
36
|
private static final String EMPTY = "";
|
37
|
// TODO make this configurable via preferences
|
38
|
private static final String INCREMENTOR_CHARACTER = "'";
|
39
|
|
40
|
public PolytomousKeyListLabelProviderFacadeImpl() {
|
41
|
super();
|
42
|
}
|
43
|
|
44
|
public Object getInstanceInternal()
|
45
|
{
|
46
|
PolytomousKeyListLabelProviderFacadeImpl INST = new PolytomousKeyListLabelProviderFacadeImpl();
|
47
|
return INST;
|
48
|
}
|
49
|
|
50
|
/*
|
51
|
* (non-Javadoc)
|
52
|
*
|
53
|
* @see
|
54
|
* org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.
|
55
|
* jface.viewers.ViewerCell)
|
56
|
*/
|
57
|
@Override
|
58
|
public void update(ViewerCell cell) {
|
59
|
Object element = cell.getElement();
|
60
|
int columnIndex = cell.getColumnIndex();
|
61
|
|
62
|
if (element instanceof PolytomousKeyNode) {
|
63
|
|
64
|
String text = getTextForColumnIndex((PolytomousKeyNode) element,
|
65
|
columnIndex);
|
66
|
cell.setText(text);
|
67
|
}
|
68
|
}
|
69
|
|
70
|
private String getTextForColumnIndex(PolytomousKeyNode node, int columnIndex) {
|
71
|
switch (columnIndex) {
|
72
|
case 0:
|
73
|
return getItemNumber(node);
|
74
|
case 1:
|
75
|
return getItemQuestion(node);
|
76
|
case 2:
|
77
|
return getItemStatement(node);
|
78
|
case 3:
|
79
|
return getItemLink(node);
|
80
|
}
|
81
|
return EMPTY;
|
82
|
}
|
83
|
|
84
|
private String getItemNumber(PolytomousKeyNode node) {
|
85
|
if (isParentRoot(node)) {
|
86
|
return "root";
|
87
|
} else {
|
88
|
PolytomousKeyNode parent = getParent(node);
|
89
|
String itemNumber = parent.getNodeNumber() != null ? parent
|
90
|
.getNodeNumber().toString() : EMPTY;
|
91
|
|
92
|
int index = parent.getChildren().indexOf(node);
|
93
|
|
94
|
for (int i = 0; i < index; i++) {
|
95
|
itemNumber += INCREMENTOR_CHARACTER;
|
96
|
}
|
97
|
|
98
|
return itemNumber;
|
99
|
}
|
100
|
}
|
101
|
|
102
|
private String getItemQuestion(PolytomousKeyNode node) {
|
103
|
if (isParentRoot(node)) {
|
104
|
return "";
|
105
|
} else {
|
106
|
KeyStatement question = getParent(node).getQuestion();
|
107
|
return question != null ? question.getLabelText(CdmStore
|
108
|
.getDefaultLanguage()) : EMPTY;
|
109
|
}
|
110
|
|
111
|
}
|
112
|
|
113
|
private String getItemStatement(PolytomousKeyNode node) {
|
114
|
KeyStatement statement = node.getStatement();
|
115
|
return statement != null ? CdmUtils.Nz(statement.getLabelText(CdmStore
|
116
|
.getDefaultLanguage())) : EMPTY;
|
117
|
}
|
118
|
|
119
|
private String getItemLink(PolytomousKeyNode node) {
|
120
|
String taxonString = node.getTaxon() != null ? node.getTaxon()
|
121
|
.getName().getTitleCache() : EMPTY;
|
122
|
|
123
|
return node.getChildren().isEmpty() ? taxonString : node
|
124
|
.getNodeNumber().toString();
|
125
|
}
|
126
|
|
127
|
private PolytomousKeyNode getParent(PolytomousKeyNode node) {
|
128
|
return node.getParent();
|
129
|
}
|
130
|
|
131
|
private boolean isParentRoot(PolytomousKeyNode node) {
|
132
|
return getParent(node) == null;
|
133
|
}
|
134
|
|
135
|
}
|