Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / TypeDesignationSection.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.name;
11
12 import java.util.Collection;
13 import java.util.Comparator;
14
15 import org.eclipse.core.commands.operations.IOperationHistory;
16 import org.eclipse.core.runtime.IStatus;
17 import org.eclipse.e4.ui.workbench.modeling.EPartService;
18 import org.eclipse.jface.action.Action;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.action.ToolBarManager;
21 import org.eclipse.jface.resource.ImageDescriptor;
22 import org.eclipse.jface.wizard.WizardDialog;
23 import org.eclipse.swt.graphics.ImageData;
24
25 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26 import eu.etaxonomy.cdm.api.service.INameService;
27 import eu.etaxonomy.cdm.api.service.UpdateResult;
28 import eu.etaxonomy.cdm.api.service.name.TypeDesignationComparator;
29 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
30 import eu.etaxonomy.cdm.model.name.NameTypeDesignation;
31 import eu.etaxonomy.cdm.model.name.Rank;
32 import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
33 import eu.etaxonomy.cdm.model.name.TaxonName;
34 import eu.etaxonomy.cdm.model.name.TextualTypeDesignation;
35 import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
36 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
37 import eu.etaxonomy.taxeditor.editor.ITaxonEditor;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.model.AbstractUtility;
40 import eu.etaxonomy.taxeditor.model.ImageResources;
41 import eu.etaxonomy.taxeditor.model.MessagingUtils;
42 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
43 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
44 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45 import eu.etaxonomy.taxeditor.store.CdmStore;
46 import eu.etaxonomy.taxeditor.store.StoreUtil;
47 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
48 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
49 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
50 import eu.etaxonomy.taxeditor.ui.section.ITaxonBaseDetailSection;
51 import eu.etaxonomy.taxeditor.ui.section.name.operation.DeleteTypeDesignationOperation;
52 import eu.etaxonomy.taxeditor.ui.section.name.type.CloneTypeWizard;
53 import eu.etaxonomy.taxeditor.view.e4.details.DetailsPartE4;
54 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
55
56 /**
57 * @author n.hoffmann
58 * @created May 17, 2010
59 */
60 public class TypeDesignationSection extends AbstractEntityCollectionSection<TaxonName, TypeDesignationBase> implements ITaxonBaseDetailSection {
61
62 private TaxonBase<?> taxonBase;
63
64 public TypeDesignationSection(CdmFormFactory formFactory, ConversationHolder conversation,
65 ICdmFormElement parentElement, int style) {
66 super(formFactory, conversation, parentElement, Messages.TypeDesignationSection_TYPE_DESIGNATIONS, style);
67 }
68
69 @Override
70 protected void addAction(ToolBarManager toolBarManager) {
71
72 // add textual type designation button
73 Action addTextualTypeDesignationAction = new Action("Add", IAction.AS_PUSH_BUTTON){
74 @Override
75 public void run() {
76 TextualTypeDesignation textualTypeDesignation = TextualTypeDesignation.NewInstance();
77 addElement(textualTypeDesignation);
78 if(! getSection().isExpanded()) {
79 getSection().setExpanded(true);
80 }
81 internalUpdateSection(true);
82 }
83 };
84 addTextualTypeDesignationAction.setImageDescriptor(new ImageDescriptor() {
85
86 @Override
87 public ImageData getImageData() {
88 return ImageResources.getImage(ImageResources.ADD_TEXT).getImageData();
89 }
90 });
91 addTextualTypeDesignationAction.setToolTipText("Add textual type designation");
92 toolBarManager.add(addTextualTypeDesignationAction);
93
94 // clone button
95 if(!isSpecimenType() ||
96 getEntity().getTypeDesignations().isEmpty()
97 || getEntity().getTypeDesignations().stream().noneMatch(designation->designation instanceof SpecimenTypeDesignation)){
98 return;
99 }
100 Action cloneAction = new Action(Messages.TypeDesignationSection_CREATE_DUPLICATE, IAction.AS_PUSH_BUTTON){
101 @Override
102 public void run() {
103 DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
104 if(detailsView!=null
105 && detailsView.getSelectionProvidingPart().getObject() instanceof IE4SavablePart
106 && StoreUtil.promptCheckIsDirty(((IE4SavablePart)detailsView.getSelectionProvidingPart().getObject()))){
107 return;
108 }
109 CloneTypeWizard wizard = new CloneTypeWizard(getEntity());
110 WizardDialog dialog = new WizardDialog(getShell(), wizard);
111
112 if (dialog.open() == IStatus.OK) {
113 SpecimenTypeDesignation baseTypeDesignation = HibernateProxyHelper.deproxy(wizard.getBaseTypeDesignation());
114 UpdateResult result = CdmStore.getService(INameService.class).cloneTypeDesignation(
115 wizard.getTaxonName().getUuid(),
116 baseTypeDesignation,
117 wizard.getAccessionNumber(),
118 wizard.getBarcode(),
119 wizard.getCatalogNumber(),
120 wizard.getCollection().getUuid(),
121 wizard.getTypeStatus());
122 if(!result.isOk()){
123 MessagingUtils.warningDialog(Messages.TypeDesignationSection_DUPLICATE_FAILED, this, result.getExceptions().toString());
124 }
125 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
126 internalUpdateSection(false);
127 }
128 }
129 };
130 cloneAction.setImageDescriptor(new ImageDescriptor() {
131
132 @Override
133 public ImageData getImageData() {
134 return ImageResources.getImage(ImageResources.ADD_DOUBLE).getImageData();
135 }
136 });
137 cloneAction.setToolTipText(Messages.TypeDesignationSection_CREATE_DUPLICATE);
138
139 toolBarManager.add(cloneAction);
140 }
141
142 @Override
143 public void addElement(TypeDesignationBase element) {
144 getEntity().addTypeDesignation(element, PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES));
145 updateToolbar();
146 }
147
148 @Override
149 public TypeDesignationBase createNewElement() {
150 if(isSpecimenType()){
151 return SpecimenTypeDesignation.NewInstance();
152 }else{
153 return NameTypeDesignation.NewInstance();
154 }
155 }
156
157 @Override
158 public Collection<TypeDesignationBase> getCollection(TaxonName entity) {
159 if (entity == null){
160 return null;
161 }
162 return entity.getTypeDesignations();
163 }
164
165 @Override
166 public Comparator<TypeDesignationBase> getComparator() {
167 return new TypeDesignationComparator();
168 }
169
170 @Override
171 public String getEmptyString() {
172 return Messages.TypeDesignationSection_NO_TYPES_YET;
173 }
174
175 @Override
176 protected String getTooltipString() {
177 return Messages.TypeDesignationSection_ADD_TYPE;
178 }
179
180 @Override
181 public void removeElement(TypeDesignationBase element) {
182 DetailsPartE4 detailsView = AbstractUtility.getDetailsView(getFormFactory().getContext().get((EPartService.class)));
183 if(detailsView!=null
184 && detailsView.getSelectionProvidingPart().getObject() instanceof IE4SavablePart
185 && StoreUtil.promptCheckIsDirty(((IE4SavablePart)detailsView.getSelectionProvidingPart().getObject()))){
186 return;
187 }
188 boolean removeTypeDesignationFromAllTypifiedNames = PreferencesUtil.getBooleanValue(IPreferenceKeys.ADD_TYPES_TO_ALL_NAMES);
189 TaxonName entity = removeTypeDesignationFromAllTypifiedNames ? null : getEntity();
190 if(!element.isPersited() && entity!=null){
191 entity.removeTypeDesignation(element);
192 }
193 else if(detailsView!=null
194 && detailsView.getSelectionProvidingPart().getObject() instanceof ITaxonEditor
195 && detailsView.getSelectionProvidingPart().getObject() instanceof IPostOperationEnabled) {
196 DeleteTypeDesignationOperation operation = new DeleteTypeDesignationOperation(
197 "Remove type designation",
198 IOperationHistory.GLOBAL_UNDO_CONTEXT,
199 entity,
200 element,
201 (IPostOperationEnabled) detailsView.getSelectionProvidingPart().getObject(),
202 null);
203 ((ITaxonEditor) detailsView.getSelectionProvidingPart().getObject()).addOperation(operation);
204 if(entity!=null){
205 entity.removeTypeDesignation(element);
206 }
207 }
208 updateToolbar();
209 }
210
211 @Override
212 public void setTaxonBase(TaxonBase entity) {
213 this.taxonBase = entity;
214 TaxonName name = HibernateProxyHelper.deproxy(entity.getName());
215 setEntity(name);
216 }
217
218 private boolean isSpecimenType(){
219 Rank rank = getEntity() == null ? null : getEntity().getRank();
220 if(rank == null){
221 return false;
222 }
223 return rank.isSpecies() || rank.isInfraSpecific();
224 }
225
226 @Override
227 public TaxonBase getTaxonBase() {
228 return taxonBase;
229 }
230
231 @Override
232 public TypeDesignationBase addExisting() {
233 return null;
234 }
235
236 @Override
237 public boolean allowAddExisting() {
238 return false;
239 }
240 }