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