2 * Copyright (C) 2019 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
.navigation
.navigator
.e4
.handler
;
11 import java
.util
.Collection
;
12 import java
.util
.Iterator
;
14 import javax
.inject
.Named
;
16 import org
.eclipse
.core
.commands
.operations
.AbstractOperation
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
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
.services
.IServiceConstants
;
24 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
25 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
26 import org
.eclipse
.jface
.viewers
.TreeSelection
;
27 import org
.eclipse
.jface
.window
.Window
;
28 import org
.eclipse
.jface
.wizard
.WizardDialog
;
29 import org
.eclipse
.swt
.widgets
.Shell
;
31 import eu
.etaxonomy
.cdm
.api
.service
.description
.AggregationMode
;
32 import eu
.etaxonomy
.cdm
.api
.service
.description
.DistributionAggregationConfiguration
;
33 import eu
.etaxonomy
.cdm
.filter
.TaxonNodeFilter
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
35 import eu
.etaxonomy
.cdm
.persistence
.dto
.TaxonNodeDto
;
36 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
37 import eu
.etaxonomy
.taxeditor
.navigation
.l10n
.Messages
;
38 import eu
.etaxonomy
.taxeditor
.navigation
.navigator
.TaxonNavigatorLabels
;
39 import eu
.etaxonomy
.taxeditor
.operation
.e4
.RemotingCdmHandlerE4
;
40 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.DistributionAggregationConfiguratorWizard
;
41 import eu
.etaxonomy
.taxeditor
.workbench
.part
.IE4SavablePart
;
47 public class AggregateDistributionForSubTreeHandler
extends RemotingCdmHandlerE4
{
51 private TaxonNodeDto taxonNode
;
52 private DistributionAggregationConfiguration configurator
;
53 private static final String NO
= Messages
.NO
;
54 private static final String CANCEL
= Messages
.RemotingMoveTaxonNodeHandler_CANCEL
;
55 private static final String YES
= Messages
.YES
;
60 public AggregateDistributionForSubTreeHandler() {
67 public IStatus
allowOperations(IStructuredSelection selection
, Shell shell
, MPart activePart
,
68 MHandledMenuItem menuItem
) {
69 // check that only a single taxon tree node has been selected
70 if(selection
.size() > 1) { }
72 // check for no taxon tree node selected
73 if(selection
.size() == 0) {
74 return new Status(IStatus
.ERROR
,
75 "unknown", //$NON-NLS-1$
76 TaxonNavigatorLabels
.NO_TAXON_SELECTION_MESSAGE
);
79 // check that selected object is a taxon node
80 Object obj
= selection
.iterator().next();
81 if(obj
instanceof TaxonNodeDto
) {
82 taxonNode
= (TaxonNodeDto
)obj
;
83 } else if(obj
instanceof Classification
){
84 taxonNode
= new TaxonNodeDto(((Classification
)obj
).getRootNode());
86 return new Status(IStatus
.ERROR
,
87 "unknown", //$NON-NLS-1$
88 TaxonNavigatorLabels
.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE
);
90 Collection
<IE4SavablePart
> dirtyParts
= EditorUtil
.checkForTaxonChanges(null, partService
);
92 if (dirtyParts
!= null && !dirtyParts
.isEmpty()){
93 String
[] buttonLables
= {YES
, NO
,CANCEL
};
94 MessageDialog dialog
= new MessageDialog(null, Messages
.SetPublishFlagForSubtreeHandlerE4_UnsavedChanges
, null, Messages
.SetPublishFlagForSubtreeHandlerE4_UnsavedChangesQuestion
, MessageDialog
.QUESTION_WITH_CANCEL
, buttonLables
, 0);
96 int returnCode
= dialog
.getReturnCode();
97 boolean proceed
= false;
100 }else if (returnCode
== 2){
101 return Status
.CANCEL_STATUS
;
104 Iterator
<IE4SavablePart
> partIterator
= dirtyParts
.iterator();
105 while( partIterator
.hasNext() ){
106 IE4SavablePart part
= partIterator
.next();
109 part
.save(new NullProgressMonitor());
116 configurator
= DistributionAggregationConfiguration
.NewInstance(AggregationMode
.byAreasAndRanks(), null, TaxonNodeFilter
.NewSubtreeInstance(taxonNode
.getUuid()), null);
117 DistributionAggregationConfiguratorWizard wizard
= new DistributionAggregationConfiguratorWizard(configurator
);
119 WizardDialog dialog
= new WizardDialog(shell
, wizard
);
121 if (dialog
.open() == Window
.OK
) {
122 return Status
.OK_STATUS
;
124 return Status
.CANCEL_STATUS
;
129 public AbstractOperation
prepareOperation(IStructuredSelection selection
, Shell shell
, MPart activePart
,
130 MHandledMenuItem menuItem
) {
131 // TODO Auto-generated method stub
136 public void onComplete() {
137 // TODO Auto-generated method stub
142 protected Object
getTrigger() {
143 // TODO Auto-generated method stub
148 private boolean canExecute(@Named(IServiceConstants
.ACTIVE_SELECTION
)TreeSelection selection
, MHandledMenuItem menuItem
){
149 boolean canExecute
= false;
150 canExecute
= selection
.size()==1
151 && selection
.getFirstElement() instanceof TaxonNodeDto
;
152 menuItem
.setEnabled(canExecute
);