2 * Copyright (C) 2020 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.featuretree
.e4
.handler
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Collection
;
13 import java
.util
.List
;
15 import javax
.inject
.Named
;
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
;
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
;
46 public abstract class AbstractAddFeatureHandler
{
48 public void execute(@Named(IServiceConstants
.ACTIVE_SHELL
) Shell shell
,
49 @Named(IServiceConstants
.ACTIVE_PART
)MPart thisPart
,
50 @Named(IServiceConstants
.ACTIVE_SELECTION
)IStructuredSelection selection
,
52 IFeatureTreeEditor editor
= ((IFeatureTreeEditor
) thisPart
.getObject());
53 if (StoreUtil
.promptCheckIsDirty(editor
)) {
56 TermNodeDto parent
= getParent(selection
);
58 if (parent
.getTerm() == null){
59 type
= parent
.getTree().getTermType();
61 type
= parent
.getTerm().getTermType();
63 AvailableFeaturesWizard wizard
= new AvailableFeaturesWizard(type
);
64 WizardDialog dialog
= new WizardDialog(shell
, wizard
);
66 if (dialog
.open() == IStatus
.OK
) {
67 Collection
<DefinedTermBase
> additionalTerms
= wizard
.getAdditionalFeatures();
68 List
<DefinedTermBase
> duplicates
= new ArrayList
<>();
69 for (DefinedTermBase term
: additionalTerms
) {
71 boolean isDuplicate
= false;
72 if (!parent
.getTree().isAllowDuplicate()){
73 isDuplicate
= editor
.checkDuplicates(term
.getUuid(), parent
.getTree().getUuid());
81 AddFeatureOperation operation
= new AddFeatureOperation(term
.getUuid(), parent
, editor
, editor
);
82 // AbstractUtility.executeOperation(operation, sync);
83 editor
.addOperation(operation
);
86 if (editor
instanceof ICharacterEditor
){
87 CharacterNodeDto newDto
= new CharacterNodeDto(CharacterDto
.fromCharacter((Character
)term
), parent
, 0, parent
.getTree(), null, null, null);
89 TermNodeDto newDto
= new TermNodeDto(TermDto
.fromTerm(term
), parent
, 0, parent
.getTree(), null, null, null);
92 // ((AbstractTermTreeEditor)editor).getViewer().refresh();
93 if (editor
instanceof ICharacterEditor
){
96 Object
[] expandedElements
= ((AbstractTermTreeEditor
)editor
).getViewer().getExpandedElements();
97 ((AbstractTermTreeEditor
)editor
).getViewer().setInput(((AbstractTermTreeEditor
)editor
).getTrees());
98 ((AbstractTermTreeEditor
)editor
).getViewer().setExpandedElements(expandedElements
);
101 if (!duplicates
.isEmpty()){
102 String termsTitles
= "";
103 for (DefinedTermBase term
: duplicates
){
104 termsTitles
= termsTitles
+ term
.getTitleCache() +"\n";
106 MessagingUtils
.informationDialog(Messages
.AddFeatureHandler_Duplicates_not_allowed
, Messages
.AddFeatureHandler_Duplicates_not_allowed_message
+ "\n"+termsTitles
);
115 abstract protected TermNodeDto
getParent(IStructuredSelection selection
);