Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / derivedUnit / PreservedSpecimenCurrentDeterminationDetailSection.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
10 package eu.etaxonomy.taxeditor.ui.section.occurrence.derivedUnit;
11
12 import java.util.Arrays;
13 import java.util.Collection;
14 import java.util.Set;
15
16 import org.eclipse.jface.action.Action;
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.api.conversation.ConversationHolder;
24 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
25 import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
26 import eu.etaxonomy.taxeditor.model.ImageResources;
27 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
28 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
29
30 /**
31 * @author n.hoffmann
32 * @created May 11, 2011
33 * @version 1.0
34 */
35 public class PreservedSpecimenCurrentDeterminationDetailSection extends
36 PreservedSpecimenAbstractDeterminationEventDetailSection {
37
38 /**
39 * @param formFactory
40 * @param conversation
41 * @param parentElement
42 * @param style
43 */
44 public PreservedSpecimenCurrentDeterminationDetailSection(CdmFormFactory formFactory,
45 ConversationHolder conversation, ICdmFormElement parentElement,
46 int style) {
47 super(formFactory, conversation, parentElement, "Current Determination", style);
48 }
49
50 @Override
51 protected Control createToolbar() {
52 ToolBarManager toolBarManager = new ToolBarManager(SWT.FLAT);
53
54 Action addAction = new Action("add", Action.AS_PUSH_BUTTON){
55 /* (non-Javadoc)
56 * @see org.eclipse.jface.action.Action#run()
57 */
58 @Override
59 public void run() {
60 DeterminationEvent element = createNewElement();
61
62 // set as preferred determination
63 Set<DeterminationEvent> events = getEntity().getDeterminations();
64 for (DeterminationEvent event : events){
65 if (event.getPreferredFlag() == true){
66 event.setPreferredFlag(false);
67 }
68 }
69 element.setPreferredFlag(true);
70 events.add(element);
71
72 if(element != null){
73 if(! getSection().isExpanded()) {
74 getSection().setExpanded(true);
75 }
76 internalUpdateSection(true);
77 }
78 }
79 };
80 addAction.setImageDescriptor(new ImageDescriptor() {
81
82 @Override
83 public ImageData getImageData() {
84 return ImageResources.getImage(ImageResources.ADD_ICON).getImageData();
85 }
86 });
87 addAction.setToolTipText(getTooltipString());
88
89 toolBarManager.add(addAction);
90
91 return toolBarManager.createControl(this);
92 }
93
94 @Override
95 public void refresh() {
96 internalUpdateSection(false);
97 }
98
99 /* (non-Javadoc)
100 * @see eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection#getCollection(java.lang.Object)
101 */
102 @Override
103 public Collection<DeterminationEvent> getCollection(DerivedUnit entity) {
104 DeterminationEvent preferredDetermination = null;
105 Set<DeterminationEvent> events = entity.getDeterminations();
106 for (DeterminationEvent event : events){
107 if (event.getPreferredFlag() == true){
108 preferredDetermination = event;
109 }
110 }
111 return preferredDetermination != null ? Arrays.asList(new DeterminationEvent[]{preferredDetermination}) : null;
112 }
113
114 /* (non-Javadoc)
115 * @see eu.etaxonomy.taxeditor.ui.section.occurrence.AbstractDeterminationEventDetailSection#createNewElement()
116 */
117 @Override
118 public DeterminationEvent createNewElement() {
119 DeterminationEvent newElement = super.createNewElement();
120 newElement.setPreferredFlag(true);
121 return newElement;
122 }
123
124
125 /* (non-Javadoc)
126 * @see eu.etaxonomy.taxeditor.section.AbstractEntityCollectionSection#getTooltipString()
127 */
128 /** {@inheritDoc} */
129 @Override
130 protected String getTooltipString() {
131 return "Create new current determination event";
132 }
133
134 /**
135 * {@inheritDoc}
136 */
137 @Override
138 public DeterminationEvent addExisting() {
139 return null;
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 @Override
146 public boolean allowAddExisting() {
147 return false;
148 }
149
150 }