ref #8930: come back to unicode in navigator
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / util / TaxonTreeNodeLabelProvider.java
1 /**
2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.util;
10
11 import org.eclipse.jface.viewers.ColumnLabelProvider;
12 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
13 import org.eclipse.jface.viewers.StyledString;
14 import org.eclipse.jface.viewers.StyledString.Styler;
15 import org.eclipse.swt.graphics.TextStyle;
16 import org.hibernate.LazyInitializationException;
17
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.common.CdmBase;
20 import eu.etaxonomy.cdm.model.taxon.Classification;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
23 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
24 import eu.etaxonomy.taxeditor.model.MessagingUtils;
25 import eu.etaxonomy.taxeditor.preference.Resources;
26 import eu.etaxonomy.taxeditor.security.RequiredPermissions;
27 import eu.etaxonomy.taxeditor.store.CdmStore;
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
29
30 /**
31 * @author pplitzner
32 * @date 05.09.2017
33 */
34 public class TaxonTreeNodeLabelProvider
35 extends ColumnLabelProvider
36 implements IStyledLabelProvider {
37
38 protected Styler notGrantedStyler = null;
39
40 @Override
41 public String getText(Object element) {
42 //classification
43 if(element instanceof Classification){
44 String text = ((Classification) element).getName().getText();
45 return text != null ? text : "Unnamed Classification";
46 }
47 //taxon node
48 else if (element instanceof TaxonNode){
49 TaxonNode taxonNode = (TaxonNode) HibernateProxyHelper.deproxy(element);
50 try{
51 Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon());
52 if(taxon == null){
53 String text = taxonNode.getClassification().getName() == null ? null : taxonNode.getClassification().getName().getText();
54 if(text==null){
55 text = taxonNode.getClassification().getTitleCache();
56 }
57 return text;
58 }else{
59 try{
60 String text = "";
61 if (taxonNode.getStatus() != null){
62 text = taxonNode.getStatus().getSymbol() + " ";
63 }
64 if (taxonNode.getTaxon() != null && !taxonNode.getTaxon().isPublish()) {
65 text = text + "\\u20ac";
66 }
67 text += taxon.getName() != null ? taxon.getName().getTitleCache() : "";
68 return text;
69 }catch(Exception e){
70 MessagingUtils.error(getClass(), e);
71 }
72 }
73 }catch (LazyInitializationException e){
74 MessagingUtils.error(getClass(), e);
75 }
76 }
77 else if (element instanceof TaxonNodeDto){
78 TaxonNodeDto taxonNode = (TaxonNodeDto) element;
79 String text= "";
80 if (taxonNode.getStatus() != null){
81 text = taxonNode.getStatus().getSymbol() + " ";
82 }
83 if (!taxonNode.isPublish() ) {
84 text = text + "\u26D4 ";
85 }
86 //TODO if symbols contain non-ASCII this may create problems
87 // if so we may use ASCII like text +=Character.toString((char)248) + " ";
88
89 text += taxonNode.getTitleCache();
90 return text;
91 }
92 return new String();
93 }
94
95 @Override
96 public StyledString getStyledText(Object element) {
97 //classification
98 if(element instanceof Classification){
99 return new StyledString(getText(element), StyledString.DECORATIONS_STYLER);
100 }
101 //taxon node
102 else if(element instanceof TaxonNode){
103 // determine style base on user grants
104 Styler styler = null;
105 if(!CdmStore.currentAuthentiationHasPermission((CdmBase) element, RequiredPermissions.TAXONNODE_EDIT)){
106 styler = getNotGrantedStyler();
107 }
108 return new StyledString(getText(element), styler);
109 }
110 return new StyledString(getText(element));
111 }
112
113 private Styler getNotGrantedStyler() {
114 if (notGrantedStyler == null) {
115 notGrantedStyler = new Styler() {
116 @Override
117 public void applyStyles(TextStyle textStyle) {
118 textStyle.underline = false;
119 textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
120 }
121 };
122 }
123 return notGrantedStyler;
124 }
125
126 }