Project

General

Profile

Download (4.77 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.editor.key.polytomous;
11

    
12
import org.eclipse.jface.viewers.StyledCellLabelProvider;
13
import org.eclipse.jface.viewers.StyledString;
14
import org.eclipse.jface.viewers.StyledString.Styler;
15
import org.eclipse.jface.viewers.ViewerCell;
16
import org.eclipse.swt.SWT;
17
import org.eclipse.swt.custom.StyleRange;
18
import org.eclipse.swt.graphics.Color;
19
import org.eclipse.swt.graphics.TextStyle;
20
import org.eclipse.swt.widgets.Display;
21

    
22
import eu.etaxonomy.cdm.common.CdmUtils;
23
import eu.etaxonomy.cdm.model.common.Language;
24
import eu.etaxonomy.cdm.model.description.KeyStatement;
25
import eu.etaxonomy.cdm.model.description.PolytomousKeyNode;
26
import eu.etaxonomy.taxeditor.store.CdmStore;
27

    
28
/**
29
 * @author n.hoffmann
30
 * @created Apr 18, 2011
31
 * @version 1.0
32
 */
33
public class PolytomousKeyListLabelProvider extends StyledCellLabelProvider {
34

    
35
	private static Color colorLink = Display.getCurrent().getSystemColor(
36
			SWT.COLOR_BLUE);
37
	private Styler styler;
38

    
39
	private static final String EMPTY = ""; //$NON-NLS-1$
40
	// TODO make this configurable via preferences
41
	private static final String INCREMENTOR_CHARACTER = "'"; //$NON-NLS-1$
42

    
43
	/*
44
	 * (non-Javadoc)
45
	 * 
46
	 * @see
47
	 * org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.
48
	 * jface.viewers.ViewerCell)
49
	 */
50
	@Override
51
	public void update(ViewerCell cell) {
52
		Object element = cell.getElement();
53
		int columnIndex = cell.getColumnIndex();
54

    
55
		if (element instanceof PolytomousKeyNode) {
56

    
57
			String text = getTextForColumnIndex((PolytomousKeyNode) element,
58
					columnIndex);
59
			
60
			
61
			cell.setText(text);
62

    
63
			if (columnIndex == 4 || columnIndex == 5) {
64
				StyledString styledString = new StyledString(text, getStyler());
65
				StyleRange[] styleRanges;
66
				styleRanges = styledString.getStyleRanges();
67
				cell.setStyleRanges(styleRanges);
68
			}
69
		}
70

    
71
		super.update(cell);
72
	}
73

    
74
	private String getTextForColumnIndex(PolytomousKeyNode node, int columnIndex) {
75
		switch (columnIndex) {
76
		case 0:
77
			return getItemNumber(node);
78
		case 1:
79
			return getItemQuestion(node);			
80
		case 2:
81
			return getItemEdgeNumber(node);
82
		case 3:
83
			return getItemStatement(node);
84
		case 4:
85
			return getItemLink(node);
86
		case 5:
87
            return getItemTaxon(node);
88
		}
89
		return EMPTY;
90
	}
91

    
92
	/**
93
	 * @return
94
	 */
95
	private Styler getStyler() {
96
		if (styler == null) {
97
			styler = new Styler() {
98
				@Override
99
				public void applyStyles(TextStyle textStyle) {
100
					textStyle.underline = true;
101
					textStyle.underlineColor = colorLink;
102
					textStyle.foreground = colorLink;
103
					textStyle.underlineStyle = SWT.UNDERLINE_SINGLE;
104
				}
105
			};
106
		}
107
		return styler;
108
	}
109

    
110
	private String getItemNumber(PolytomousKeyNode node) {
111
		if (isParentRoot(node)) {
112
			return "root"; //$NON-NLS-1$
113
		} else {
114
			
115
			String itemNumber = (node.getParent().getNodeNumber() != null) ? node.getParent().getNodeNumber().toString() : EMPTY;					
116
			return itemNumber;
117
		}
118
	}
119

    
120
	private String getItemEdgeNumber(PolytomousKeyNode node) {
121
		String itemEdgeNumber = node.getParent().getNodeNumber() != null ? node.getParent().getNodeNumber().toString() : EMPTY;		
122
		PolytomousKeyNode parent = getParent(node);
123
		int index = parent.getChildren().indexOf(node);
124

    
125
		char numberChar = 'a';
126
		//FIXME: Currently this numbering works only until 'z', after which all siblings will be named with 'z'
127
		for (int i = 0; i < index; i++) {
128
			if(index < 26) {
129
				numberChar++;
130
			} else {
131
				numberChar = 'z';
132
			}
133
			//itemEdgeNumber += INCREMENTOR_CHARACTER;
134
		}
135
		return itemEdgeNumber + numberChar;
136
	}
137
	
138
	private String getItemQuestion(PolytomousKeyNode node) {
139
		if (isParentRoot(node)) {
140
			return ""; //$NON-NLS-1$
141
		} else {
142
			KeyStatement question = getParent(node).getQuestion();					
143
			return question != null ? CdmUtils.Nz(question.getLabelText(CdmStore.getDefaultLanguage())) : EMPTY;
144
			
145
		}
146

    
147
	}
148

    
149
	private String getItemStatement(PolytomousKeyNode node) {
150
		KeyStatement statement = node.getStatement();
151
		return statement != null ? CdmUtils.Nz(statement.getLabelText(CdmStore
152
				.getDefaultLanguage())) : EMPTY;
153
	}
154

    
155
	private String getItemLink(PolytomousKeyNode node) {
156
		String linkString = node.getChildren().isEmpty() ? EMPTY : node.getNodeNumber().toString();
157

    
158
		return linkString;
159
	}
160
	
161
	private String getItemTaxon(PolytomousKeyNode node) {
162
	    String taxonString = node.getTaxon() != null ? node.getTaxon().getName().getTitleCache() : EMPTY;
163

    
164
	    return taxonString;
165
	}
166

    
167
	private PolytomousKeyNode getParent(PolytomousKeyNode node) {
168
		return node.getParent();
169
	}
170

    
171
	private boolean isParentRoot(PolytomousKeyNode node) {
172
		return getParent(node) == null;
173
	}
174

    
175
}
(8-8/9)