Project

General

Profile

Download (4.72 KB) Statistics
| Branch: | Tag: | Revision:
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.editor.key.polytomous;
12

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

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

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

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

    
40
	private static final String EMPTY = "";
41
	// TODO make this configurable via preferences
42
	private static final String INCREMENTOR_CHARACTER = "'";
43

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

    
56
		if (element instanceof PolytomousKeyNode) {
57

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

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

    
72
		super.update(cell);
73
	}
74

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

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

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

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

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

    
148
	}
149

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

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

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

    
165
	    return taxonString;
166
	}
167

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

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

    
176
}
(10-10/11)