c022ca4e2debf372e73797e8bb304967bc28da25
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / TaxonNodeLabelProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.navigation.navigator;
12
13 import org.eclipse.jface.viewers.ColumnLabelProvider;
14 import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider;
15 import org.eclipse.jface.viewers.ILabelProvider;
16 import org.eclipse.jface.viewers.StyledString;
17 import org.eclipse.jface.viewers.StyledString.Styler;
18 import org.eclipse.swt.graphics.Image;
19 import org.eclipse.swt.graphics.TextStyle;
20 import org.eclipse.ui.navigator.IDescriptionProvider;
21 import org.hibernate.LazyInitializationException;
22
23 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28 import eu.etaxonomy.taxeditor.model.MessagingUtils;
29 import eu.etaxonomy.taxeditor.preference.Resources;
30 import eu.etaxonomy.taxeditor.security.RequiredPermissions;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32 import eu.etaxonomy.taxeditor.store.StoreUtil;
33
34 /**
35 * <p>TaxonNodeLabelProvider class.</p>
36 *
37 * @author p.ciardelli
38 * @created 04.06.2009
39 * @version 1.0
40 */
41 public class TaxonNodeLabelProvider extends ColumnLabelProvider
42 implements ILabelProvider, IDescriptionProvider, IStyledLabelProvider {
43
44
45 private Styler notGrantedStyler = null;
46
47 /** {@inheritDoc} */
48 @Override
49 public Image getImage(Object element) {
50 return null;
51 }
52
53 /** {@inheritDoc} */
54 @Override
55 public String getText(Object element) {
56 if (element instanceof TaxonNode){
57 TaxonNode taxonNode = (TaxonNode) HibernateProxyHelper.deproxy(element);
58
59 try{
60 Taxon taxon = (Taxon) HibernateProxyHelper.deproxy(taxonNode.getTaxon());
61 if(taxon == null){
62 MessagingUtils.error(getClass(), "The taxon node does not have a taxon attached to it. Please check your import. TaxonNode: " + element, null);
63 // throw new RuntimeException("The data your are trying to view is corrupt. Please check your import.");
64 }else{
65 try{
66 return taxon.getName() != null ? ((IIdentifiableEntity) HibernateProxyHelper.deproxy(taxon.getName())).getTitleCache() : new String();
67 }catch(Exception e){
68 MessagingUtils.error(getClass(), e);
69 }
70 }
71 }catch (LazyInitializationException e){
72 MessagingUtils.error(getClass(), e);
73 }
74
75 }
76 return new String();
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.ui.navigator.IDescriptionProvider#getDescription(java.lang.Object)
81 */
82 /** {@inheritDoc} */
83 @Override
84 public String getDescription(Object anElement) {
85 if (anElement instanceof TaxonNode) {
86 Taxon data = ((TaxonNode) anElement).getTaxon();
87 return "Taxon: " + data != null ? data.getTitleCache() : ">NO TAXON<";
88 }
89 return null;
90 }
91
92 /* (non-Javadoc)
93 * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider#getStyledText(java.lang.Object)
94 */
95 /** {@inheritDoc} */
96 @Override
97 public StyledString getStyledText(Object element) {
98 // determine style base on user grants
99 Styler styler = null;
100 if(!CdmStore.currentAuthentiationHasPermission((CdmBase) element, RequiredPermissions.TAXONNODE_EDIT)){
101 styler = getNotGrantedStyler();
102 }
103 return new StyledString(getText(element), styler);
104
105 }
106
107 /**
108 * @return
109 */
110 private Styler getNotGrantedStyler() {
111 if (notGrantedStyler == null) {
112 notGrantedStyler = new Styler() {
113 @Override
114 public void applyStyles(TextStyle textStyle) {
115 textStyle.underline = false;
116 textStyle.foreground = StoreUtil.getColor(Resources.COLOR_TEXT_DISABLED);
117 }
118 };
119 }
120 return notGrantedStyler;
121 }
122 }