fix #3758
[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 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
27
28 /**
29 * @author l.morris
30 * @date 9 Dec 2011
31 *
32 */
33 public class TermLabelProvider extends StyledCellLabelProvider {
34
35 private static Color vocColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
36 private Styler vocStyler;
37
38 public TermLabelProvider() {
39 }
40
41 public TermLabelProvider(Styler vocStyler){
42 this.vocStyler = vocStyler;
43 }
44
45 /*
46 * (non-Javadoc)
47 *
48 * @see
49 * org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.
50 * jface.viewers.ViewerCell)
51 */
52 @Override
53 public void update(ViewerCell cell) {
54 Object element = cell.getElement();
55 int columnIndex = cell.getColumnIndex();
56
57 String text = getText(element);
58 cell.setText(text);
59
60 if (element instanceof TermVocabulary) {
61 StyledString styledString = new StyledString(text, getVocabularyStyler());
62 StyleRange[] styleRanges;
63 styleRanges = styledString.getStyleRanges();
64 cell.setStyleRanges(styleRanges);
65 }
66 super.update(cell);
67 }
68
69 public StyledString getStyledText(Object element) {
70
71 if (element instanceof TermVocabulary) {
72 new StyledString(getText(element), getVocabularyStyler());
73 }
74 return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
75 }
76
77 /*
78 * (non-Javadoc)
79 *
80 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
81 */
82
83 public String getText(Object element) {
84
85 if (element instanceof DefinedTermBase) {
86 DefinedTermBase<?> dtb = (DefinedTermBase<?>) element;
87 if (StringUtils.isNotBlank(dtb.getIdInVocabulary())) {
88 return dtb.getIdInVocabulary() + " : " + dtb.getLabel(PreferencesUtil.getGlobalLanguage());
89 } else {
90 return dtb.getLabel(PreferencesUtil.getGlobalLanguage());
91 }
92 } else if (element instanceof TermBase) {
93 return ((TermBase) element).getTitleCache();
94 }
95
96 // FIXME : must throw an exception here
97 return element.toString();
98 }
99
100 private Styler getVocabularyStyler() {
101 if (vocStyler == null) {
102 vocStyler = new Styler() {
103 @Override
104 public void applyStyles(TextStyle textStyle) {
105 textStyle.foreground = vocColor;
106 }
107 };
108 }
109 return vocStyler;
110 }
111
112 }