Project

General

Profile

Download (4.16 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.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 = "";
40
	// TODO make this configurable via preferences
41
	private static final String INCREMENTOR_CHARACTER = "'";
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
			cell.setText(text);
60

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

    
69
		super.update(cell);
70
	}
71

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

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

    
106
	private String getItemNumber(PolytomousKeyNode node) {
107
		if (isParentRoot(node)) {
108
			return "root";
109
		} else {
110
			PolytomousKeyNode parent = getParent(node);
111
			String itemNumber = parent.getNodeNumber() != null ? parent
112
					.getNodeNumber().toString() : EMPTY;
113

    
114
			int index = parent.getChildren().indexOf(node);
115

    
116
			for (int i = 0; i < index; i++) {
117
				itemNumber += INCREMENTOR_CHARACTER;
118
			}
119

    
120
			return itemNumber;
121
		}
122
	}
123

    
124
	private String getItemQuestion(PolytomousKeyNode node) {
125
		if (isParentRoot(node)) {
126
			return "";
127
		} else {
128
			KeyStatement question = getParent(node).getQuestion();
129
			return question != null ? question.getLabelText(CdmStore
130
					.getDefaultLanguage()) : EMPTY;
131
		}
132

    
133
	}
134

    
135
	private String getItemStatement(PolytomousKeyNode node) {
136
		KeyStatement statement = node.getStatement();
137
		return statement != null ? CdmUtils.Nz(statement.getLabelText(CdmStore
138
				.getDefaultLanguage())) : EMPTY;
139
	}
140

    
141
	private String getItemLink(PolytomousKeyNode node) {
142
		String linkString = node.getChildren().isEmpty() ? EMPTY : node.getNodeNumber().toString();
143

    
144
		return linkString;
145
	}
146
	
147
	private String getItemTaxon(PolytomousKeyNode node) {
148
	    String taxonString = node.getTaxon() != null ? node.getTaxon().getName().getTitleCache() : EMPTY;
149

    
150
	    return taxonString;
151
	}
152

    
153
	private PolytomousKeyNode getParent(PolytomousKeyNode node) {
154
		return node.getParent();
155
	}
156

    
157
	private boolean isParentRoot(PolytomousKeyNode node) {
158
		return getParent(node) == null;
159
	}
160

    
161
}
(9-9/9)