Revision 9c1235e7
Added by Patrick Plitzner over 7 years ago
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 |
} |
|
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.eclipse.jface.viewers.StyledCellLabelProvider; |
|
15 |
import org.eclipse.jface.viewers.StyledString; |
|
16 |
import org.eclipse.jface.viewers.StyledString.Styler; |
|
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.common.CdmUtils; |
|
25 |
import eu.etaxonomy.cdm.model.common.DefinedTermBase; |
|
26 |
import eu.etaxonomy.cdm.model.common.Language; |
|
27 |
import eu.etaxonomy.cdm.model.common.Representation; |
|
28 |
import eu.etaxonomy.cdm.model.common.TermBase; |
|
29 |
import eu.etaxonomy.cdm.model.common.TermVocabulary; |
|
30 |
import eu.etaxonomy.taxeditor.preference.PreferencesUtil; |
|
31 |
|
|
32 |
/** |
|
33 |
* @author l.morris |
|
34 |
* @date 9 Dec 2011 |
|
35 |
* |
|
36 |
*/ |
|
37 |
public class TermLabelProvider extends StyledCellLabelProvider { |
|
38 |
|
|
39 |
private static Color vocColor = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE); |
|
40 |
private Styler vocStyler; |
|
41 |
|
|
42 |
public TermLabelProvider() { |
|
43 |
} |
|
44 |
|
|
45 |
public TermLabelProvider(Styler vocStyler){ |
|
46 |
this.vocStyler = vocStyler; |
|
47 |
} |
|
48 |
|
|
49 |
@Override |
|
50 |
public void update(ViewerCell cell) { |
|
51 |
Object element = cell.getElement(); |
|
52 |
int columnIndex = cell.getColumnIndex(); |
|
53 |
|
|
54 |
String text = getText(element); |
|
55 |
cell.setText(text); |
|
56 |
|
|
57 |
if (element instanceof TermVocabulary) { |
|
58 |
StyledString styledString = new StyledString(text, getVocabularyStyler()); |
|
59 |
StyleRange[] styleRanges; |
|
60 |
styleRanges = styledString.getStyleRanges(); |
|
61 |
cell.setStyleRanges(styleRanges); |
|
62 |
} |
|
63 |
super.update(cell); |
|
64 |
} |
|
65 |
|
|
66 |
public StyledString getStyledText(Object element) { |
|
67 |
|
|
68 |
if (element instanceof TermVocabulary) { |
|
69 |
new StyledString(getText(element), getVocabularyStyler()); |
|
70 |
} |
|
71 |
return new StyledString(getText(element), StyledString.QUALIFIER_STYLER); |
|
72 |
} |
|
73 |
|
|
74 |
public String getText(Object element) { |
|
75 |
|
|
76 |
if (element instanceof DefinedTermBase) { |
|
77 |
DefinedTermBase<?> dtb = (DefinedTermBase<?>) element; |
|
78 |
|
|
79 |
Representation rep = dtb.getRepresentation(PreferencesUtil.getGlobalLanguage()); |
|
80 |
if ( rep == null){ |
|
81 |
rep = dtb.getPreferredRepresentation(new ArrayList<Language>()); |
|
82 |
} |
|
83 |
String label = rep != null? rep.getLabel() : dtb.getTitleCache(); |
|
84 |
return CdmUtils.concat(" : ", dtb.getIdInVocabulary(), label); |
|
85 |
|
|
86 |
} else if (element instanceof TermBase) { |
|
87 |
return ((TermBase) element).getTitleCache(); |
|
88 |
} |
|
89 |
|
|
90 |
// FIXME : must throw an exception here |
|
91 |
return element.toString(); |
|
92 |
} |
|
93 |
|
|
94 |
private Styler getVocabularyStyler() { |
|
95 |
if (vocStyler == null) { |
|
96 |
vocStyler = new Styler() { |
|
97 |
@Override |
|
98 |
public void applyStyles(TextStyle textStyle) { |
|
99 |
textStyle.foreground = vocColor; |
|
100 |
} |
|
101 |
}; |
|
102 |
} |
|
103 |
return vocStyler; |
|
104 |
} |
|
105 |
|
|
106 |
} |
Also available in: Unified diff
Trim code