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