+// $Id$
+/**
+* Copyright (C) 2007 EDIT
+* European Distributed Institute of Taxonomy
+* http://www.e-taxonomy.eu
+*
+* The contents of this file are subject to the Mozilla Public License Version 1.1
+* See LICENSE.TXT at the top of this package for the full license terms.
+*/
+
+package eu.etaxonomy.taxeditor.ui.section.occurrence.dna;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+
+import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.action.ToolBarManager;
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.widgets.Control;
+
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
+import eu.etaxonomy.cdm.model.common.Identifier;
+import eu.etaxonomy.cdm.model.molecular.DnaSample;
+import eu.etaxonomy.taxeditor.model.ImageResources;
+import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
+import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
+import eu.etaxonomy.taxeditor.view.derivateSearch.DerivateLabelProvider;
+
+/**
+ *
+ * @author pplitzner
+ * @date Oct 16, 2014
+ *
+ */
+public class CurrentSampleDesignationDetailSection extends AbstractSampleDesignationDetailSection {
+
+ public CurrentSampleDesignationDetailSection(CdmFormFactory formFactory,
+ ConversationHolder conversation, ICdmFormElement parentElement,
+ int style) {
+ super(formFactory, conversation, parentElement, "Current Sample Designation", style);
+ }
+
+ @Override
+ protected Control createToolbar() {
+ ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
+
+ Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
+ /* (non-Javadoc)
+ * @see org.eclipse.jface.action.Action#run()
+ */
+ @Override
+ public void run() {
+ Identifier<DnaSample> element = createNewElement();
+
+ getEntity().addIdentifier(element);
+
+ if(element != null){
+ if(! getSection().isExpanded()) {
+ getSection().setExpanded(true);
+ }
+ internalUpdateSection(true);
+ }
+ }
+ };
+ addAction.setImageDescriptor(new ImageDescriptor() {
+
+ @Override
+ public ImageData getImageData() {
+ return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
+ }
+ });
+ addAction.setToolTipText(getTooltipString());
+
+ toolBarManager.add(addAction);
+
+ return toolBarManager.createControl(this);
+ }
+
+ @Override
+ public void refresh() {
+ internalUpdateSection(false);
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection#getCollection(java.lang.Object)
+ */
+ @Override
+ public Collection<Identifier<DnaSample>> getCollection(DnaSample entity) {
+ LinkedList<Identifier<DnaSample>> sampleDesignations = new LinkedList<Identifier<DnaSample>>();
+ for (Identifier<DnaSample> identifier : entity.getIdentifiers()) {
+ if(identifier.getType()!=null && identifier.getType().equals(DerivateLabelProvider.getSampleDesignationTerm())){
+ sampleDesignations.add(identifier);
+ }
+ }
+ Collections.reverse(sampleDesignations);
+ if(sampleDesignations.size()>0){
+ //last sample designation is the current
+ return Collections.singleton(sampleDesignations.getFirst());
+ }
+ else{
+ return Collections.emptyList();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#getTooltipString()
+ */
+ /** {@inheritDoc} */
+ @Override
+ protected String getTooltipString() {
+ return "Create new current sample designation";
+ }
+
+}