4 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.handler
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Collection
;
8 import java
.util
.HashSet
;
11 import java
.util
.UUID
;
13 import javax
.inject
.Named
;
15 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
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
.MApplication
;
21 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
22 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MMenuItem
;
24 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
25 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EModelService
;
26 import org
.eclipse
.e4
.ui
.workbench
.modeling
.EPartService
;
27 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
28 import org
.eclipse
.jface
.viewers
.ISelection
;
29 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Shell
;
33 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
34 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
35 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonNode
;
38 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
39 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
40 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
41 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.FactualDataPartE4
;
42 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.MoveDescriptionToOtherTaxonOperation
;
43 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
44 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
45 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
46 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
47 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.selection
.TaxonNodeSelectionDialog
;
48 import eu
.etaxonomy
.taxeditor
.workbench
.WorkbenchUtility
;
49 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
57 public class MoveDescriptionToOtherTaxonHandlerE4
implements IPostOperationEnabled
{
59 private MoveDescriptionToOtherTaxonOperation operation
;
61 private UUID newAcceptedTaxonNodeUuid
;
62 private UUID oldAcceptedTaxonNodeUuid
;
63 private UUID newAcceptedTaxonUuid
;
64 private UUID oldAcceptedTaxonUuid
;
65 private TaxonNameEditorE4 editor
;
66 private MApplication application
;
67 private EModelService modelService
;
68 private EPartService partService
;
71 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
,
72 @Optional @Named(IServiceConstants
.ACTIVE_SELECTION
) IStructuredSelection selection
,
73 @Named(IServiceConstants
.ACTIVE_SHELL
) Shell shell
, MMenuItem menuItem
, EPartService partService
,
74 EModelService modelService
, MApplication application
, UISynchronize sync
) {
76 this.modelService
= modelService
;
77 this.application
= application
;
78 this.partService
= partService
;
80 FactualDataPartE4 factualDataPart
= (FactualDataPartE4
) activePart
.getObject();
81 Object e4WrappedPart
= WorkbenchUtility
.getE4WrappedPart(factualDataPart
.getSelectionProvidingPart());
82 if(e4WrappedPart
instanceof TaxonNameEditorE4
){
83 editor
= (TaxonNameEditorE4
) e4WrappedPart
;
87 List
<TaxonDescription
> descriptions
= new ArrayList
<TaxonDescription
>();
88 TaxonDescription description
= null;
89 Object selectedElement
= selection
.getFirstElement();
90 if (selectedElement
instanceof FeatureNodeContainer
){
91 FeatureNodeContainer container
= (FeatureNodeContainer
)selectedElement
;
92 description
= (TaxonDescription
) container
.getDescription();
93 }else if (selectedElement
instanceof TaxonDescription
){
94 description
= (TaxonDescription
)selectedElement
;
96 descriptions
.add(description
);
98 // Choose the target taxon
101 //get current taxon node
102 TaxonNode node
= null;
103 Classification classification
= null;
105 node
= editor
.getEditorInput().getTaxonNode();
106 oldAcceptedTaxonUuid
= editor
.getTaxon().getUuid();
107 oldAcceptedTaxonNodeUuid
= node
.getUuid();
108 classification
= node
.getClassification();
111 Set
<UUID
> excludeTaxa
= new HashSet
<>();
112 excludeTaxa
.add(oldAcceptedTaxonNodeUuid
);
113 TaxonNode newAcceptedTaxonNode
= TaxonNodeSelectionDialog
.select(shell
,
114 // editor.getConversationHolder(),
115 Messages
.MoveDescriptionToOtherTaxonHandler_CHOOSE_ACC_TAXON
,
118 classification
.getUuid());
120 if (newAcceptedTaxonNode
== null) {
124 newAcceptedTaxonNodeUuid
= newAcceptedTaxonNode
.getUuid();
125 newAcceptedTaxonUuid
= newAcceptedTaxonNode
.getTaxon().getUuid();
127 Collection
<IE4SavablePart
> targetEditors
= EditorUtil
.checkForTaxonChanges(newAcceptedTaxonUuid
, partService
);
129 if (!targetEditors
.isEmpty() || this.editor
.isDirty()){
130 if(MessageDialog
.openQuestion(null,Messages
.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES
, Messages
.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE
)) {
131 this.editor
.save(new NullProgressMonitor());
132 targetEditors
.forEach(editor
->editor
.save(new NullProgressMonitor()));
140 for(TaxonDescription taxonDescription
: descriptions
){
141 operation
= new MoveDescriptionToOtherTaxonOperation(menuItem
.getLocalizedLabel(),
142 editor
.getUndoContext(),
144 newAcceptedTaxonNode
,
147 editor
.getEditorInput());
148 AbstractUtility
.executeOperation(operation
, sync
);
156 public boolean postOperation(Object objectAffectedByOperation
) {
157 Display
.getDefault().asyncExec(new Runnable(){
162 // AbstractUtility.close(editor);
165 // MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
166 // if(possibleOpenEditor != null){
167 // AbstractUtility.close(possibleOpenEditor);
169 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, oldAcceptedTaxonNodeUuid);
170 // EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAME_EDITOR, newAcceptedTaxonNodeUuid);
171 EditorUtil
.openTaxonNodeE4(newAcceptedTaxonNodeUuid
, modelService
, partService
, application
);
172 EditorUtil
.openTaxonNodeE4(oldAcceptedTaxonNodeUuid
, modelService
, partService
, application
);
173 // } catch (PartInitException e) {
174 // MessagingUtils.error(this.getClass(), e);
175 // throw new RuntimeException(e);
176 } catch (Exception e
) {
177 MessagingUtils
.warningDialog(Messages
.MoveDescriptionToOtherTaxonHandler_CREATE_FAILED
, this, e
.getMessage());
188 public boolean onComplete() {
193 public boolean canExecute(@Optional @Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
,
194 MHandledMenuItem menuItem
){
195 boolean canExecute
= false;
196 FactualDataPartE4 factualDataPart
= (FactualDataPartE4
) activePart
.getObject();
198 Object e4WrappedPart
= WorkbenchUtility
.getE4WrappedPart(factualDataPart
.getSelectionProvidingPart());
199 if(e4WrappedPart
instanceof TaxonNameEditorE4
){
200 ISelection selection
= factualDataPart
.getViewer().getSelection();
201 if(selection
instanceof IStructuredSelection
){
202 Object firstElement
= ((IStructuredSelection
) selection
).getFirstElement();
203 canExecute
= ((IStructuredSelection
) selection
).size()==1
204 && firstElement
instanceof DescriptionBase
&& !(firstElement
instanceof TaxonNameDescription
);
205 menuItem
.setVisible(canExecute
);