reintegrated redlist branch into trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / TermLabelProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2009 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 package eu.etaxonomy.taxeditor.editor.definedterm;
11
12 import org.apache.commons.lang.StringUtils;
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.model.common.DefinedTermBase;
24 import eu.etaxonomy.cdm.model.common.TermBase;
25 import eu.etaxonomy.cdm.model.common.TermVocabulary;
26
27 /**
28 * @author l.morris
29 * @date 9 Dec 2011
30 *
31 */
32 public class TermLabelProvider extends StyledCellLabelProvider {
33
34 private static Color vocColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
35 private Styler vocStyler;
36
37 public TermLabelProvider() {
38 }
39
40 public TermLabelProvider(Styler vocStyler){
41 this.vocStyler = vocStyler;
42 }
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 String text = getText(element);
57 cell.setText(text);
58
59 if (element instanceof TermVocabulary) {
60 StyledString styledString = new StyledString(text, getVocabularyStyler());
61 StyleRange[] styleRanges;
62 styleRanges = styledString.getStyleRanges();
63 cell.setStyleRanges(styleRanges);
64 }
65 super.update(cell);
66 }
67
68 public StyledString getStyledText(Object element) {
69
70 if (element instanceof TermVocabulary) {
71 new StyledString(getText(element), getVocabularyStyler());
72 }
73 return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
74 }
75
76 /*
77 * (non-Javadoc)
78 *
79 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
80 */
81
82 public String getText(Object element) {
83
84 if (element instanceof DefinedTermBase) {
85 DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
86 if (StringUtils.isNotBlank(dtb.getIdInVocabulary())) {
87 return dtb.getIdInVocabulary() + " : " + dtb.getTitleCache();
88 } else {
89 return dtb.getTitleCache();
90 }
91 } else if (element instanceof TermBase) {
92 return ((TermBase) element).getTitleCache();
93 }
94
95 // FIXME : must throw an exception here
96 return element.toString();
97 }
98
99 private Styler getVocabularyStyler() {
100 if (vocStyler == null) {
101 vocStyler = new Styler() {
102 @Override
103 public void applyStyles(TextStyle textStyle) {
104 textStyle.foreground = vocColor;
105 }
106 };
107 }
108 return vocStyler;
109 }
110
111 }