ba8fa96b552751ec7fba20ececd391dfbbf36a49
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / derivateSearch / DerivateLabelProvider.java
1 // $Id$
2 /**
3 * Copyright (C) 2013 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.view.derivateSearch;
11
12 import java.util.List;
13 import java.util.Set;
14 import java.util.UUID;
15
16 import org.eclipse.jface.viewers.ColumnLabelProvider;
17 import org.eclipse.jface.viewers.TreeNode;
18 import org.eclipse.swt.graphics.Image;
19 import org.hibernate.LazyInitializationException;
20
21 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
23 import eu.etaxonomy.cdm.model.common.DefinedTerm;
24 import eu.etaxonomy.cdm.model.molecular.DnaSample;
25 import eu.etaxonomy.cdm.model.molecular.Sequence;
26 import eu.etaxonomy.cdm.model.molecular.SingleRead;
27 import eu.etaxonomy.cdm.model.occurrence.Collection;
28 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
29 import eu.etaxonomy.cdm.model.occurrence.FieldUnit;
30 import eu.etaxonomy.cdm.model.occurrence.GatheringEvent;
31 import eu.etaxonomy.cdm.model.occurrence.MediaSpecimen;
32 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
33 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
34 import eu.etaxonomy.taxeditor.model.ImageResources;
35 import eu.etaxonomy.taxeditor.store.CdmStore;
36
37 /**
38 * Label provider for the views to show {@link SpecimenOrObservationBase}s.<br>
39 * <br>
40 * <b>Note:</b> If you use this label provider you need to assure that you
41 * created a {@link ConversationHolder} resp. have an open session because
42 * the labels are generated from various fields of the derivate hierarchy which
43 * are lazy loaded and could therefore throw a {@link LazyInitializationException}.
44 */
45 public class DerivateLabelProvider extends ColumnLabelProvider {
46
47 private Set<SingleRead> multiLinkSingleReads;
48
49 private static DefinedTerm photoTerm = null;
50 private static DefinedTerm drawingTerm = null;
51 private static DefinedTerm specimenScanTerm = null;
52
53 private static void initializeTerms() {
54 List<DefinedTerm> preferredTerms = CdmStore.getTermManager().getPreferredTerms(DefinedTerm.class);
55 for (DefinedTerm definedTerm : preferredTerms) {
56 if(definedTerm.getUuid().equals(UUID.fromString("04673952-dab1-42ff-b47a-5e847c350eaf"))){//photo
57 photoTerm = definedTerm;
58 }
59 else if(definedTerm.getUuid().equals(UUID.fromString("76204a15-098f-4afe-9e8f-fa344ee3fdbb"))){//drawing
60 drawingTerm = definedTerm;
61 }
62 else if(definedTerm.getUuid().equals(UUID.fromString("c0b127d6-c849-48f9-bbc3-29e3337c1d03"))){//cover TODO temporary until new term is created
63 specimenScanTerm = definedTerm;
64 }
65 }
66 }
67
68 public static DefinedTerm getLivingPlantPhotoTerm(){
69 if(photoTerm==null){
70 initializeTerms();
71 }
72 return photoTerm;
73 }
74
75 public static DefinedTerm getArtworkTerm(){
76 if(drawingTerm==null){
77 initializeTerms();
78 }
79 return drawingTerm;
80 }
81
82 public static DefinedTerm getSpecimenScanTerm(){
83 if(specimenScanTerm==null){
84 initializeTerms();
85 }
86 return specimenScanTerm;
87 }
88
89
90 /** {@inheritDoc} */
91 @Override
92 public String getText(Object element) {
93 return getDerivateText(element);
94 }
95
96 /** {@inheritDoc} */
97 @Override
98 public String getToolTipText(Object element) {
99 return getDerivateText(element);
100 }
101
102 /**
103 *
104 * @param element
105 * @return
106 */
107 public static String getDerivateText(Object element){
108 if(element instanceof TreeNode){
109 //unwrap specimen from TreeNode
110 element = ((TreeNode) element).getValue();
111 }
112
113 String label = "";
114 if(element instanceof FieldUnit){
115 FieldUnit fieldUnit = (FieldUnit)element;
116 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
117 if(gatheringEvent!=null){
118 label += gatheringEvent.getCountry()!=null?gatheringEvent.getCountry().getLabel()+", ":"";
119 label += gatheringEvent.getLocality()!=null?gatheringEvent.getLocality().getText()+", ":"";
120 label += gatheringEvent.getGatheringDate()!=null?gatheringEvent.getGatheringDate()+", ":"";
121 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+", ":"";
122 }
123 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber():"";
124 }
125 else if(element instanceof MediaSpecimen){
126 MediaSpecimen mediaSpecimen = (MediaSpecimen)element;
127 label += mediaSpecimen.getMediaSpecimen().getTitle()!=null?mediaSpecimen.getMediaSpecimen().getTitle().getText()+", ":"[no motif]";
128 label += mediaSpecimen.getMediaSpecimen().getArtist()!=null?mediaSpecimen.getMediaSpecimen().getArtist()+", ":"";
129 label += mediaSpecimen.getAccessionNumber()!=null?mediaSpecimen.getAccessionNumber()+", ":"";
130 Collection collection = mediaSpecimen.getCollection();
131 if(collection!=null){
132 label += collection.getName()!=null?collection.getName()+" ":"";
133 }
134 }
135 else if(element instanceof DnaSample
136 || (element instanceof DerivedUnit
137 && ((SpecimenOrObservationBase<?>) element).getRecordBasis()==SpecimenOrObservationType.DnaSample)){
138 //TODO implement
139 label += element.toString();
140
141 }
142 else if(element instanceof DerivedUnit){
143 DerivedUnit derivedUnit = (DerivedUnit)element;
144 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
145 java.util.Collection<FieldUnit> fieldUnits = CdmStore.getService(IOccurrenceService.class).getFieldUnits(derivedUnit.getUuid());
146 if(fieldUnits!=null && !fieldUnits.isEmpty()){
147 FieldUnit fieldUnit = fieldUnits.iterator().next();
148 GatheringEvent gatheringEvent = fieldUnit.getGatheringEvent();
149 if(gatheringEvent!=null){
150 label += gatheringEvent.getCollector()!=null?gatheringEvent.getCollector()+", ":"";
151 }
152 label += fieldUnit.getFieldNumber()!=null?fieldUnit.getFieldNumber()+", ":"";
153 }
154 Collection collection = derivedUnit.getCollection();
155 if(collection!=null){
156 label += collection.getName()!=null?collection.getName()+" ":"";
157 label += collection.getCode()!=null?"("+collection.getCode()+"), ":"";
158 }
159 label += derivedUnit.getAccessionNumber()!=null?derivedUnit.getAccessionNumber()+", ":"";
160 label += derivedUnit.getBarcode()!=null?derivedUnit.getBarcode():"";
161 }
162 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
163 //TODO: implement
164 // kindofTissue + SampleDesignation
165 }
166 }
167 else if(element instanceof Sequence){
168 Sequence sequence = (Sequence)element;
169 label += "SampleDesignation?"+", ";
170 label += sequence.getDnaMarker()!=null?sequence.getDnaMarker():"";
171 }
172 else if(element instanceof SingleRead){
173 SingleRead singleRead = (SingleRead)element;
174 label += singleRead.getPrimer()!=null?singleRead.getPrimer().getLabel()+", ":"";
175 label += "Marker?";
176 }
177 else if(element instanceof SpecimenOrObservationBase){
178 SpecimenOrObservationBase<?> derivate = (SpecimenOrObservationBase<?>) element;
179 SpecimenOrObservationType type = derivate.getRecordBasis();
180 return derivate.getTitleCache() + (type!=null?" ["+type.toString()+"]":"");
181 }
182 if(label.isEmpty()){
183 label = element.toString();
184 }
185 //remove last comma
186 else if(label.endsWith(", ")){
187 label = label.substring(0, label.length()-2);
188 }
189 return label;
190 }
191
192 /* (non-Javadoc)
193 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getImage(java.lang.Object)
194 */
195 @Override
196 public Image getImage(Object element) {
197 if(element instanceof TreeNode){
198 element = ((TreeNode) element).getValue();
199 }
200 if(element instanceof FieldUnit){
201 return ImageResources.getImage(ImageResources.FIELD_UNIT);
202 }
203 else if(element instanceof DerivedUnit){
204 DerivedUnit derivedUnit = (DerivedUnit)element;
205 if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.FieldUnit){
206 return ImageResources.getImage(ImageResources.FIELD_UNIT);
207 }
208 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.DnaSample){
209 return ImageResources.getImage(ImageResources.DNA_SAMPLE_DERIVATE);
210 }
211 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.TissueSample){
212 return ImageResources.getImage(ImageResources.TISSUE_SAMPLE_DERIVATE);
213 }
214 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.PreservedSpecimen){
215 return ImageResources.getImage(ImageResources.SPECIMEN_DERIVATE);
216 }
217 else if(derivedUnit.getRecordBasis()==SpecimenOrObservationType.StillImage){
218 if(derivedUnit.getKindOfUnit()!=null){
219 if(derivedUnit.getKindOfUnit().equals(getArtworkTerm())){
220 return ImageResources.getImage(ImageResources.ARTWORK_DERIVATE);
221 }
222 else if(derivedUnit.getKindOfUnit().equals(getLivingPlantPhotoTerm())){
223 return ImageResources.getImage(ImageResources.LIVING_PLANT_PHOTO_DERIVATE);
224 }
225 else if(derivedUnit.getKindOfUnit().equals(getSpecimenScanTerm())){
226 return ImageResources.getImage(ImageResources.SPECIMEN_SCAN_DERIVATE);
227 }
228 }
229 }
230 }
231 else if(element instanceof Sequence){
232 return ImageResources.getImage(ImageResources.SEQUENCE_DERIVATE);
233 }
234
235 else if(element instanceof SingleRead){
236 if(multiLinkSingleReads.contains(element)){
237 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE_MULTILINK);
238 }
239 else{
240 return ImageResources.getImage(ImageResources.SINGLE_READ_DERIVATE);
241 }
242 }
243 return super.getImage(element);
244 }
245
246 /**
247 * @param multiLinkSingleReads2
248 */
249 public void setMultiLinkSingleReads(Set<SingleRead> multiLinkSingleReads) {
250 this.multiLinkSingleReads = multiLinkSingleReads;
251 }
252
253 }