merge-update from 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.eclipse.jface.viewers.StyledCellLabelProvider;
13 import org.eclipse.jface.viewers.StyledString;
14 import org.eclipse.jface.viewers.StyledString.Styler;
15 import org.eclipse.jface.viewers.ViewerCell;
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.custom.StyleRange;
18 import org.eclipse.swt.graphics.Color;
19 import org.eclipse.swt.graphics.TextStyle;
20 import org.eclipse.swt.widgets.Display;
21
22 import eu.etaxonomy.cdm.model.common.TermBase;
23 import eu.etaxonomy.cdm.model.common.TermVocabulary;
24
25 /**
26 * @author l.morris
27 * @date 9 Dec 2011
28 *
29 */
30 public class TermLabelProvider extends StyledCellLabelProvider {
31
32 private static Color vocColor = Display.getCurrent().getSystemColor(
33 SWT.COLOR_BLUE);
34 private Styler vocStyler;
35 /* (non-Javadoc)
36 * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider#getStyledText(java.lang.Object)
37 */
38
39 /*
40 * (non-Javadoc)
41 *
42 * @see
43 * org.eclipse.jface.viewers.StyledCellLabelProvider#update(org.eclipse.
44 * jface.viewers.ViewerCell)
45 */
46 @Override
47 public void update(ViewerCell cell) {
48 Object element = cell.getElement();
49 int columnIndex = cell.getColumnIndex();
50
51 String text = getText(element);
52 cell.setText(text);
53
54 if (element instanceof TermVocabulary) {
55 StyledString styledString = new StyledString(text, getVocabularyStyler());
56 StyleRange[] styleRanges;
57 styleRanges = styledString.getStyleRanges();
58 cell.setStyleRanges(styleRanges);
59 }
60 super.update(cell);
61 }
62
63 public StyledString getStyledText(Object element) {
64
65 if(element instanceof TermVocabulary){
66 new StyledString(getText(element), getVocabularyStyler());
67 }
68 return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
69 }
70
71 /* (non-Javadoc)
72 * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
73 */
74
75 public String getText(Object element) {
76
77 if (element instanceof TermBase){
78 return ((TermBase)element).getTitleCache();
79 }
80 //FIXME : must throw an exception here
81 return element.toString();
82 }
83
84 private Styler getVocabularyStyler() {
85 if (vocStyler == null) {
86 vocStyler = new Styler() {
87 @Override
88 public void applyStyles(TextStyle textStyle) {
89 textStyle.foreground = vocColor;
90 }
91 };
92 }
93 return vocStyler;
94 }
95
96 }