Merge branch 'hotfix/5.18.2'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / featuretree / e4 / handler / AbstractAddFeatureHandler.java
1 /**
2 * Copyright (C) 2020 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.featuretree.e4.handler;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14
15 import javax.inject.Named;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.e4.core.di.annotations.Execute;
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.services.IServiceConstants;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.wizard.WizardDialog;
24 import org.eclipse.swt.widgets.Shell;
25
26 import eu.etaxonomy.cdm.model.description.Character;
27 import eu.etaxonomy.cdm.model.term.DefinedTermBase;
28 import eu.etaxonomy.cdm.model.term.TermType;
29 import eu.etaxonomy.cdm.persistence.dto.CharacterDto;
30 import eu.etaxonomy.cdm.persistence.dto.CharacterNodeDto;
31 import eu.etaxonomy.cdm.persistence.dto.TermDto;
32 import eu.etaxonomy.cdm.persistence.dto.TermNodeDto;
33 import eu.etaxonomy.taxeditor.featuretree.AvailableFeaturesWizard;
34 import eu.etaxonomy.taxeditor.featuretree.e4.AbstractTermTreeEditor;
35 import eu.etaxonomy.taxeditor.featuretree.e4.ICharacterEditor;
36 import eu.etaxonomy.taxeditor.featuretree.e4.IFeatureTreeEditor;
37 import eu.etaxonomy.taxeditor.featuretree.e4.operation.AddFeatureOperation;
38 import eu.etaxonomy.taxeditor.l10n.Messages;
39 import eu.etaxonomy.taxeditor.model.MessagingUtils;
40
41 /**
42 * @author k.luther
43 * @since Nov 10, 2020
44 */
45 public abstract class AbstractAddFeatureHandler {
46 @Execute
47 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
48 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
49 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
50 UISynchronize sync) {
51 IFeatureTreeEditor editor = ((IFeatureTreeEditor) thisPart.getObject());
52
53 TermNodeDto parent = getParent(selection);
54 TermType type = null;
55 if (parent.getTerm() == null){
56 type = parent.getTree().getTermType();
57 }else{
58 type = parent.getTerm().getTermType();
59 }
60 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(type);
61 WizardDialog dialog = new WizardDialog(shell, wizard);
62
63 if (dialog.open() == IStatus.OK) {
64 Collection<DefinedTermBase> additionalTerms = wizard.getAdditionalFeatures();
65 List<DefinedTermBase> duplicates = new ArrayList<>();
66 for (DefinedTermBase term : additionalTerms) {
67
68 boolean isDuplicate = false;
69 if (!parent.getTree().isAllowDuplicate()){
70 isDuplicate = editor.checkDuplicates(term.getUuid(), parent.getTree().getUuid());
71 if (isDuplicate){
72 duplicates.add(term);
73 }
74 }
75 if (isDuplicate){
76 continue;
77 }
78 AddFeatureOperation operation = new AddFeatureOperation(term.getUuid(), parent, editor, editor);
79 // AbstractUtility.executeOperation(operation, sync);
80 editor.addOperation(operation);
81 editor.setDirty();
82
83 if (editor instanceof ICharacterEditor){
84 CharacterNodeDto newDto = new CharacterNodeDto(CharacterDto.fromCharacter((Character)term), parent, 0, parent.getTree(), null, null, null);
85 }else{
86 TermNodeDto newDto = new TermNodeDto(TermDto.fromTerm(term), parent, 0, parent.getTree(), null, null, null);
87 }
88
89 // ((AbstractTermTreeEditor)editor).getViewer().refresh();
90 if (editor instanceof ICharacterEditor){
91 editor.refresh();
92 }else{
93 Object[] expandedElements = ((AbstractTermTreeEditor)editor).getViewer().getExpandedElements();
94 ((AbstractTermTreeEditor)editor).getViewer().setInput(((AbstractTermTreeEditor)editor).getTrees());
95 ((AbstractTermTreeEditor)editor).getViewer().setExpandedElements(expandedElements);
96 }
97 }
98 if (!duplicates.isEmpty()){
99 String termsTitles = "";
100 for (DefinedTermBase term: duplicates){
101 termsTitles = termsTitles + term.getTitleCache() +"\n";
102 }
103 MessagingUtils.informationDialog(Messages.AddFeatureHandler_Duplicates_not_allowed, Messages.AddFeatureHandler_Duplicates_not_allowed_message + "\n"+termsTitles);
104 }
105 }
106 }
107
108 /**
109 * @param selection
110 * @return
111 */
112 abstract protected TermNodeDto getParent(IStructuredSelection selection);
113 }