Merge branch 'hotfix/5.22.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / descriptive / e4 / handler / CreateDescriptionElementHandlerE4.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.editor.view.descriptive.e4.handler;
11
12 import java.util.UUID;
13
14 import javax.inject.Named;
15
16 import org.eclipse.e4.core.di.annotations.CanExecute;
17 import org.eclipse.e4.core.di.annotations.Execute;
18 import org.eclipse.e4.core.di.annotations.Optional;
19 import org.eclipse.e4.ui.di.UISynchronize;
20 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22 import org.eclipse.e4.ui.services.IServiceConstants;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24
25 import eu.etaxonomy.cdm.api.service.ITermService;
26 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27 import eu.etaxonomy.cdm.model.description.DescriptionBase;
28 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
29 import eu.etaxonomy.cdm.model.description.Feature;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
32 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
33 import eu.etaxonomy.taxeditor.model.AbstractUtility;
34 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38 import eu.etaxonomy.taxeditor.store.CdmStore;
39
40 /**
41 *
42 * @author pplitzner
43 * @date 15.08.2017
44 *
45 */
46 public class CreateDescriptionElementHandlerE4 {
47
48 @Execute
49 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
50 @Optional@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
51 MHandledMenuItem menuItem,
52 UISynchronize sync) {
53
54 String commandId = menuItem.getCommand().getElementId();
55 UUID uuid = (UUID) menuItem.getTransientData().get(commandId+".feature.uuid");
56 Feature feature = HibernateProxyHelper.deproxy(CdmStore.getService(ITermService.class).load(uuid), Feature.class);
57
58 FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
59
60 DescriptionBase description = null;
61 Object object = selection.getFirstElement();
62 if(object instanceof FeatureNodeContainer){
63 description = ((FeatureNodeContainer) object).getDescription();
64 }
65 else if(object instanceof DescriptionElementBase){
66 DescriptionElementBase elementBase = HibernateProxyHelper.deproxy(object, DescriptionElementBase.class);
67 description = elementBase.getInDescription();
68 }
69 else if (object instanceof DescriptionBase) {
70 description = HibernateProxyHelper.deproxy(object, DescriptionBase.class);
71 } else {
72 MessagingUtils.error(getClass(), new IllegalArgumentException("Could not determine the taxon description")); //$NON-NLS-1$
73 return;
74 }
75 AbstractPostOperation operation = operationCreationInstance(menuItem.getLocalizedLabel(), feature, description,
76 factualDataPart);
77 // TODO use undo context specific to editor
78 AbstractUtility.executeOperation(operation, sync);
79 }
80
81 @CanExecute
82 public boolean canExecute(MHandledMenuItem menuItem,
83 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection){
84 boolean canExecute = false;
85 canExecute = selection.size()==1
86 &&
87 (selection.getFirstElement() instanceof DescriptionBase
88 || selection.getFirstElement() instanceof DescriptionElementBase
89 || selection.getFirstElement() instanceof FeatureNodeContainer);
90 menuItem.setVisible(canExecute);
91 return canExecute;
92
93 }
94
95 /**
96 * Added to make the Class more generic and limit the amount of code changes required
97 * @param label
98 * @param event
99 * @param taxon
100 * @param description
101 * @param postOperationEnabled
102 * @return
103 */
104 protected AbstractPostOperation operationCreationInstance(String label, Feature feature,
105 DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
106 return new CreateDescriptionElementOperation(label, EditorUtil.getUndoContext(), description, feature,
107 postOperationEnabled);
108 }
109
110 }