Merge branch 'release/5.44.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / CurrentDeterminationDetailSection.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.ui.section.occurrence;
10
11 import java.util.Arrays;
12 import java.util.Collection;
13 import java.util.Comparator;
14
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.ToolBarManager;
18 import org.eclipse.jface.resource.ImageDescriptor;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.graphics.ImageData;
21 import org.eclipse.swt.widgets.Control;
22
23 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
24 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
25 import eu.etaxonomy.taxeditor.model.ImageResources;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
28 import eu.etaxonomy.taxeditor.ui.section.DefaultCdmBaseComparator;
29
30 /**
31 * @author n.hoffmann
32 * @created May 11, 2011
33 */
34 public class CurrentDeterminationDetailSection extends
35 AbstractDeterminationEventDetailSection {
36
37 public CurrentDeterminationDetailSection(CdmFormFactory formFactory,
38 ICdmFormElement parentElement,
39 int style) {
40 super(formFactory, parentElement, "Current Determination", style);
41 }
42
43 @Override
44 protected Control createToolbar() {
45 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
46
47 Action addAction = new Action("add", IAction.AS_PUSH_BUTTON){
48 @Override
49 public void run() {
50 DeterminationEvent element = createNewElement();
51
52 // set as preferred determination
53 getEntity().setPreferredDetermination(element);
54
55 if(element != null){
56 if(! getSection().isExpanded()) {
57 getSection().setExpanded(true);
58 }
59 internalUpdateSection(true);
60 }
61 }
62 };
63 addAction.setImageDescriptor(new ImageDescriptor() {
64
65 @Override
66 public ImageData getImageData() {
67 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
68 }
69 });
70 addAction.setToolTipText(getTooltipString());
71
72 toolBarManager.add(addAction);
73
74 return toolBarManager.createControl(this);
75 }
76
77 @Override
78 public void refresh() {
79 internalUpdateSection(false);
80 }
81
82 @Override
83 public Collection<DeterminationEvent> getCollection(SpecimenOrObservationBase entity) {
84 DeterminationEvent preferredDetermination = entity.getPreferredDetermination();
85 return preferredDetermination != null ? Arrays.asList(new DeterminationEvent[]{preferredDetermination}) : null;
86 }
87
88 @Override
89 public Comparator<DeterminationEvent> getComparator() {
90 return new DefaultCdmBaseComparator<>();
91 }
92
93 @Override
94 public DeterminationEvent createNewElement() {
95 DeterminationEvent newElement = super.createNewElement();
96 newElement.setPreferredFlag(true);
97 return newElement;
98 }
99
100 @Override
101 protected String getTooltipString() {
102 return "Create new current determination event";
103 }
104
105 @Override
106 public DeterminationEvent addExisting() {
107 return null;
108 }
109
110 @Override
111 public boolean allowAddExisting() {
112 return false;
113 }
114 }