2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.editor
.key
.polytomous
;
13 import org
.apache
.commons
.lang3
.StringUtils
;
14 import org
.eclipse
.jface
.viewers
.StyledCellLabelProvider
;
15 import org
.eclipse
.jface
.viewers
.StyledString
;
16 import org
.eclipse
.jface
.viewers
.StyledString
.Styler
;
17 import org
.eclipse
.jface
.viewers
.ViewerCell
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.custom
.StyleRange
;
20 import org
.eclipse
.swt
.graphics
.Color
;
21 import org
.eclipse
.swt
.graphics
.TextStyle
;
22 import org
.eclipse
.swt
.widgets
.Display
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
26 import eu
.etaxonomy
.cdm
.model
.common
.LanguageString
;
27 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
28 import eu
.etaxonomy
.cdm
.model
.description
.KeyStatement
;
29 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKeyNode
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
34 * @created Apr 18, 2011
36 public class PolytomousKeyListLabelProvider
extends StyledCellLabelProvider
{
38 private static Color colorLink
= Display
.getCurrent().getSystemColor(
40 private Styler styler
;
42 private static final String EMPTY
= ""; //$NON-NLS-1$
43 // TODO make this configurable via preferences
44 private static final String INCREMENTOR_CHARACTER
= "'"; //$NON-NLS-1$
47 public void update(ViewerCell cell
) {
48 Object element
= cell
.getElement();
49 int columnIndex
= cell
.getColumnIndex();
51 if (element
instanceof PolytomousKeyNode
) {
53 String text
= getTextForColumnIndex((PolytomousKeyNode
) element
,
59 if (columnIndex
== 4 || columnIndex
== 5) {
60 StyledString styledString
= new StyledString(text
, getStyler());
61 StyleRange
[] styleRanges
;
62 styleRanges
= styledString
.getStyleRanges();
63 cell
.setStyleRanges(styleRanges
);
70 private String
getTextForColumnIndex(PolytomousKeyNode node
, int columnIndex
) {
71 switch (columnIndex
) {
73 return getItemNumber(node
);
75 return getItemQuestion(node
);
77 return getItemEdgeNumber(node
);
79 return getItemStatement(node
);
81 return getItemLink(node
);
83 return getItemTaxon(node
);
88 private String
getFeature(PolytomousKeyNode node
) {
89 if (isParentRoot(node
)) {
90 return ""; //$NON-NLS-1$
92 Feature parentFeature
= getParent(node
).getFeature();
93 if (parentFeature
== null){
96 String statementLabel
= CdmUtils
.Nz(parentFeature
.getLabel(CdmStore
.getDefaultLanguage()));
97 if(CdmUtils
.isBlank(statementLabel
)){
98 statementLabel
= parentFeature
.getLabel();
100 return statementLabel
;
104 private Styler
getStyler() {
105 if (styler
== null) {
106 styler
= new Styler() {
108 public void applyStyles(TextStyle textStyle
) {
109 textStyle
.underline
= true;
110 textStyle
.underlineColor
= colorLink
;
111 textStyle
.foreground
= colorLink
;
112 textStyle
.underlineStyle
= SWT
.UNDERLINE_SINGLE
;
119 private String
getItemNumber(PolytomousKeyNode node
) {
120 if (isParentRoot(node
)) {
121 return "root"; //$NON-NLS-1$
124 String itemNumber
= (node
.getParent().getNodeNumber() != null) ? node
.getParent().getNodeNumber().toString() : EMPTY
;
129 private String
getItemEdgeNumber(PolytomousKeyNode node
) {
130 String itemEdgeNumber
= node
.getParent().getNodeNumber() != null ? node
.getParent().getNodeNumber().toString() : EMPTY
;
131 PolytomousKeyNode parent
= getParent(node
);
132 int index
= parent
.getChildren().indexOf(node
);
134 char numberChar
= 'a';
135 //FIXME: Currently this numbering works only until 'z', after which all siblings will be named with 'z'
136 for (int i
= 0; i
< index
; i
++) {
142 //itemEdgeNumber += INCREMENTOR_CHARACTER;
144 return itemEdgeNumber
+ numberChar
;
147 private String
getItemQuestion(PolytomousKeyNode node
) {
148 String questionString
= EMPTY
;
149 if (!isParentRoot(node
)) {
150 KeyStatement question
= getParent(node
).getQuestion();
151 if (question
!= null){
152 questionString
= CdmUtils
.Nz(question
.getLabelText(CdmStore
.getDefaultLanguage()));
153 if (StringUtils
.isBlank(questionString
)){
154 Map
<Language
, LanguageString
> labels
= question
.getLabel();
155 if (labels
!= null && !labels
.isEmpty()){
156 for (LanguageString langString
: labels
.values()){
157 if (StringUtils
.isNotBlank(langString
.getText())){
158 questionString
= langString
.getText();
166 String featureString
= getFeature(node
);
167 return StringUtils
.join(new String
[]{questionString
, featureString
});
170 private String
getItemStatement(PolytomousKeyNode node
) {
171 KeyStatement statement
= node
.getStatement();
172 if(statement
==null) {
175 String statementLabel
= CdmUtils
.Nz(statement
.getLabelText(CdmStore
176 .getDefaultLanguage()));
177 if (StringUtils
.isBlank(statementLabel
)){
178 Map
<Language
, LanguageString
> labels
= statement
.getLabel();
179 if (labels
!= null && !labels
.isEmpty()){
180 for (LanguageString langString
: labels
.values()){
181 if (StringUtils
.isNotBlank(langString
.getText())){
182 statementLabel
= langString
.getText();
188 return statementLabel
!= null ? statementLabel
: EMPTY
;
191 private String
getItemLink(PolytomousKeyNode node
) {
192 String linkString
= node
.getChildren().isEmpty() ? EMPTY
: node
.getNodeNumber().toString();
197 private String
getItemTaxon(PolytomousKeyNode node
) {
198 String taxonString
= node
.getTaxon() != null ? node
.getTaxon().getName().getTitleCache() : EMPTY
;
203 private PolytomousKeyNode
getParent(PolytomousKeyNode node
) {
204 return node
.getParent();
207 private boolean isParentRoot(PolytomousKeyNode node
) {
208 return getParent(node
) == null;