ref #8774: show which terms are duplicates in dialog
[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 import eu.etaxonomy.taxeditor.store.StoreUtil;
41
42 /**
43 * @author k.luther
44 * @since Nov 10, 2020
45 */
46 public abstract class AbstractAddFeatureHandler {
47 @Execute
48 public void execute(@Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
49 @Named(IServiceConstants.ACTIVE_PART)MPart thisPart,
50 @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
51 UISynchronize sync) {
52 IFeatureTreeEditor editor = ((IFeatureTreeEditor) thisPart.getObject());
53 if (StoreUtil.promptCheckIsDirty(editor)) {
54 return;
55 }
56 TermNodeDto parent = getParent(selection);
57 TermType type = null;
58 if (parent.getTerm() == null){
59 type = parent.getTree().getTermType();
60 }else{
61 type = parent.getTerm().getTermType();
62 }
63 AvailableFeaturesWizard wizard = new AvailableFeaturesWizard(type);
64 WizardDialog dialog = new WizardDialog(shell, wizard);
65
66 if (dialog.open() == IStatus.OK) {
67 Collection<DefinedTermBase> additionalTerms = wizard.getAdditionalFeatures();
68 List<DefinedTermBase> duplicates = new ArrayList<>();
69 for (DefinedTermBase term : additionalTerms) {
70
71 boolean isDuplicate = false;
72 if (!parent.getTree().isAllowDuplicate()){
73 isDuplicate = editor.checkDuplicates(term.getUuid(), parent.getTree().getUuid());
74 if (isDuplicate){
75 duplicates.add(term);
76 }
77 }
78 if (isDuplicate){
79 continue;
80 }
81 AddFeatureOperation operation = new AddFeatureOperation(term.getUuid(), parent, editor, editor);
82 // AbstractUtility.executeOperation(operation, sync);
83 editor.addOperation(operation);
84 editor.setDirty();
85
86 if (editor instanceof ICharacterEditor){
87 CharacterNodeDto newDto = new CharacterNodeDto(CharacterDto.fromCharacter((Character)term), parent, 0, parent.getTree(), null, null, null);
88 }else{
89 TermNodeDto newDto = new TermNodeDto(TermDto.fromTerm(term), parent, 0, parent.getTree(), null, null, null);
90 }
91
92 // ((AbstractTermTreeEditor)editor).getViewer().refresh();
93 if (editor instanceof ICharacterEditor){
94 editor.refresh();
95 }else{
96 Object[] expandedElements = ((AbstractTermTreeEditor)editor).getViewer().getExpandedElements();
97 ((AbstractTermTreeEditor)editor).getViewer().setInput(((AbstractTermTreeEditor)editor).getTrees());
98 ((AbstractTermTreeEditor)editor).getViewer().setExpandedElements(expandedElements);
99 }
100 }
101 if (!duplicates.isEmpty()){
102 String termsTitles = "";
103 for (DefinedTermBase term: duplicates){
104 termsTitles = termsTitles + term.getTitleCache() +"\n";
105 }
106 MessagingUtils.informationDialog(Messages.AddFeatureHandler_Duplicates_not_allowed, Messages.AddFeatureHandler_Duplicates_not_allowed_message + "\n"+termsTitles);
107 }
108 }
109 }
110
111 /**
112 * @param selection
113 * @return
114 */
115 abstract protected TermNodeDto getParent(IStructuredSelection selection);
116 }