merge-update from trunk
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / dna / CurrentSampleDesignationDetailSection.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.ui.section.occurrence.dna;
12
13 import java.util.Collection;
14 import java.util.Collections;
15 import java.util.LinkedList;
16
17 import org.eclipse.jface.action.Action;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.jface.resource.ImageDescriptor;
21 import org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.ImageData;
23 import org.eclipse.swt.widgets.Control;
24
25 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26 import eu.etaxonomy.cdm.model.common.Identifier;
27 import eu.etaxonomy.cdm.model.molecular.DnaSample;
28 import eu.etaxonomy.taxeditor.model.ImageResources;
29 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
30 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
31 import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
32
33 /**
34 *
35 * @author pplitzner
36 * @date Oct 16, 2014
37 *
38 */
39 public class CurrentSampleDesignationDetailSection extends AbstractSampleDesignationDetailSection {
40
41 public CurrentSampleDesignationDetailSection(CdmFormFactory formFactory,
42 ConversationHolder conversation, ICdmFormElement parentElement,
43 int style) {
44 super(formFactory, conversation, parentElement, "Current Sample Designation", style);
45 }
46
47 @Override
48 protected Control createToolbar() {
49 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
50
51 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
52 /* (non-Javadoc)
53 * @see org.eclipse.jface.action.Action#run()
54 */
55 @Override
56 public void run() {
57 Identifier<DnaSample> element = createNewElement();
58
59 getEntity().addIdentifier(element);
60
61 if(element != null){
62 if(! getSection().isExpanded()) {
63 getSection().setExpanded(true);
64 }
65 internalUpdateSection(true);
66 }
67 }
68 };
69 addAction.setImageDescriptor(new ImageDescriptor() {
70
71 @Override
72 public ImageData getImageData() {
73 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
74 }
75 });
76 addAction.setToolTipText(getTooltipString());
77
78 toolBarManager.add(addAction);
79
80 return toolBarManager.createControl(this);
81 }
82
83 @Override
84 public void refresh() {
85 internalUpdateSection(false);
86 }
87
88 /* (non-Javadoc)
89 * @see eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection#getCollection(java.lang.Object)
90 */
91 @Override
92 public Collection<Identifier<DnaSample>> getCollection(DnaSample entity) {
93 LinkedList<Identifier<DnaSample>> sampleDesignations = new LinkedList<Identifier<DnaSample>>();
94 for (Identifier<DnaSample> identifier : entity.getIdentifiers()) {
95 if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
96 sampleDesignations.add(identifier);
97 }
98 }
99 Collections.reverse(sampleDesignations);
100 if(sampleDesignations.size()>0){
101 //last sample designation is the current
102 return Collections.singleton(sampleDesignations.getFirst());
103 }
104 else{
105 return Collections.emptyList();
106 }
107 }
108
109 /* (non-Javadoc)
110 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#getTooltipString()
111 */
112 /** {@inheritDoc} */
113 @Override
114 protected String getTooltipString() {
115 return "Create new current sample designation";
116 }
117
118 }