ref #6909 migrate dnd for navigator
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / TaxonNavigatorLabelProviderE4.java
1 // $Id$
2 /**
3 * Copyright (C) 2017 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.navigation.navigator.e4;
11
12 import org.eclipse.jface.viewers.ColumnLabelProvider;
13 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
14 import org.eclipse.jface.viewers.StyledString;
15 import org.eclipse.jface.viewers.StyledString.Styler;
16 import org.eclipse.swt.graphics.TextStyle;
17 import org.eclipse.ui.navigator.IDescriptionProvider;
18 import org.hibernate.LazyInitializationException;
19
20 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
21 import eu.etaxonomy.cdm.model.common.CdmBase;
22 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
23 import eu.etaxonomy.cdm.model.taxon.Classification;
24 import eu.etaxonomy.cdm.model.taxon.Taxon;
25 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
26 import eu.etaxonomy.taxeditor.model.MessagingUtils;
27 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
28 import eu.etaxonomy.taxeditor.preference.Resources;
29 import eu.etaxonomy.taxeditor.security.RequiredPermissions;
30 import eu.etaxonomy.taxeditor.store.CdmStore;
31 import eu.etaxonomy.taxeditor.store.StoreUtil;
32
33 /**
34 * @author pplitzner
35 * @date 05.09.2017
36 *
37 */
38 public class TaxonNavigatorLabelProviderE4 extends ColumnLabelProvider
39 implements IDescriptionProvider, IStyledLabelProvider {
40
41 private Styler notGrantedStyler = null;
42
43 /**
44 * {@inheritDoc}
45 */
46 @Override
47 public String getText(Object element) {
48 //classification
49 if(element instanceof Classification){
50 String text = ((Classification) element).getName().getText();
51 return text != null ? text : Messages.ClassificationLabelProvider_UNNAMED_TREE;
52 }
53 //FIXME E4 show synonym in navigator?
54 //synonym
55 // else if (element instanceof Synonym && ((Synonym) element).getName()!=null) {
56 // return "= " + ((Synonym) element).getName().getTitleCache(); //$NON-NLS-1$
57 // }
58 //taxon node
59 else if (element instanceof TaxonNode){
60 TaxonNode taxonNode = (TaxonNode) HibernateProxyHelper.deproxy(element);
61
62 try{
63 Taxon taxon = HibernateProxyHelper.deproxy(taxonNode.getTaxon());
64 if(taxon == null){
65 String text = taxonNode.getClassification().getName().getText();
66 if(text==null){
67 text = taxonNode.getClassification().getTitleCache();
68 }
69 return text;
70 }else{
71 try{
72 return taxon.getName() != null ? ((IIdentifiableEntity) HibernateProxyHelper.deproxy(taxon.getName())).getTitleCache() : new String();
73 }catch(Exception e){
74 MessagingUtils.error(getClass(), e);
75 }
76 }
77 }catch (LazyInitializationException e){
78 MessagingUtils.error(getClass(), e);
79 }
80 }
81 return new String();
82 }
83
84 /**
85 * {@inheritDoc}
86 */
87 @Override
88 public String getDescription(Object anElement) {
89 //classification
90 if (anElement instanceof Classification) {
91 return "Taxonomic Tree: " + ((Classification) anElement).getTitleCache(); //$NON-NLS-1$
92 }
93 //FIXME E4 show synonym in navigator?
94 //synonym
95 // else if (anElement instanceof Synonym) {
96 // Synonym data = (Synonym) anElement;
97 // return "Synonym: " + data.getTitleCache(); //$NON-NLS-1$
98 // }
99 //taxon node
100 else if (anElement instanceof TaxonNode) {
101 Taxon data = ((TaxonNode) anElement).getTaxon();
102 String text = (data != null ? Messages.TaxonNodeLabelProvider_TAXON + data.getTitleCache() : Messages.TaxonNodeLabelProvider_CLASSIFICATION + ((TaxonNode)anElement).getClassification().getTitleCache());
103 return text;
104 }
105 return null;
106 }
107
108 /**
109 * {@inheritDoc}
110 */
111 @Override
112 public StyledString getStyledText(Object element) {
113 //classification
114 if(element instanceof Classification){
115 return new StyledString(getText(element), StyledString.DECORATIONS_STYLER);
116 }
117 //FIXME E4 show synonym in navigator?
118 //synonym
119 // else if(element instanceof Synonym){
120 // return new StyledString(getText(element), StyledString.QUALIFIER_STYLER);
121 // }
122 //taxon node
123 else if(element instanceof TaxonNode){
124 // determine style base on user grants
125 Styler styler = null;
126 if(!CdmStore.currentAuthentiationHasPermission((CdmBase) element, RequiredPermissions.TAXONNODE_EDIT)){
127 styler = getNotGrantedStyler();
128 }
129 return new StyledString(getText(element), styler);
130 }
131 return new StyledString(getText(element));
132 }
133
134 private Styler getNotGrantedStyler() {
135 if (notGrantedStyler == null) {
136 notGrantedStyler = new Styler() {
137 @Override
138 public void applyStyles(TextStyle textStyle) {
139 textStyle.underline = false;
140 textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
141 }
142 };
143 }
144 return notGrantedStyler;
145 }
146 }