ref #7865 Change method signature for checking for dirty taxa editors
[taxeditor.git] / eu.etaxonomy.taxeditor.navigation / src / main / java / eu / etaxonomy / taxeditor / navigation / navigator / e4 / handler / SetSecReferenceForSubtreeHandlerE4.java
1 /**
2 * Copyright (C) 2017 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.navigation.navigator.e4.handler;
10
11 import java.util.Collection;
12 import java.util.Iterator;
13
14 import javax.inject.Named;
15
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;
30
31 import eu.etaxonomy.cdm.api.service.config.SecundumForSubtreeConfigurator;
32 import eu.etaxonomy.cdm.model.taxon.Classification;
33 import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
34 import eu.etaxonomy.taxeditor.editor.EditorUtil;
35 import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
36 import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
37 import eu.etaxonomy.taxeditor.navigation.navigator.operation.SetSecundumForSubtreeOperation;
38 import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
39 import eu.etaxonomy.taxeditor.ui.dialog.configurator.SetSecundumForSubtreeConfigurationWizard;
40 import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
41
42 /**
43 * @author k.luther
44 * @author pplitzner
45 * @date 10.02.2017
46 *
47 */
48 public class SetSecReferenceForSubtreeHandlerE4 extends RemotingCdmHandlerE4 {
49
50 private TaxonNodeDto taxonNode;
51 private SecundumForSubtreeConfigurator configurator;
52
53 public SetSecReferenceForSubtreeHandlerE4() {
54 super(TaxonNavigatorLabels.CHANGE_SECUNDUM_FOR_SUBTREE);
55 }
56
57 @Override
58 public IStatus allowOperations(IStructuredSelection selection,
59 Shell shell,
60 MPart activePart,
61 MHandledMenuItem menuItem) {
62 // check that only a single taxon tree node has been selected
63 if(selection.size() > 1) { }
64
65 // check for no taxon tree node selected
66 if(selection.size() == 0) {
67 return new Status(IStatus.ERROR,
68 "unknown", //$NON-NLS-1$
69 TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
70 }
71
72 // check that selected object is a taxon node
73 Object obj = selection.iterator().next();
74 if(obj instanceof TaxonNodeDto) {
75 if(obj instanceof Classification){
76 taxonNode = new TaxonNodeDto(((Classification)obj).getRootNode());
77 }else{
78 taxonNode = (TaxonNodeDto)obj;
79 }
80 } else{
81 return new Status(IStatus.ERROR,
82 "unknown", //$NON-NLS-1$
83 TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
84 }
85 Collection<IE4SavablePart> dirtyParts = EditorUtil.checkForTaxonChanges(null, partService);
86
87 if (dirtyParts != null && !dirtyParts.isEmpty()){
88 String[] buttonLables = {Messages.YES, Messages.NO,Messages.TreeNodeDropAdapter_CANCEL};
89 MessageDialog dialog = new MessageDialog(null, Messages.SetPublishFlagForSubtreeHandlerE4_UnsavedChanges, null, Messages.SetPublishFlagForSubtreeHandlerE4_UnsavedChangesQuestion, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
90 dialog.open();
91 int returnCode = dialog.getReturnCode();
92 boolean proceed = false;
93 if (returnCode == 0){
94 proceed = true;
95 }else if (returnCode == 2){
96 return Status.CANCEL_STATUS;
97 }
98
99 Iterator<IE4SavablePart> partIterator = dirtyParts.iterator();
100 while( partIterator.hasNext() ){
101 IE4SavablePart part = partIterator.next();
102 if (proceed) {
103 if (part != null){
104 part.save(new NullProgressMonitor());
105 }
106 }
107 }
108 }
109
110 configurator = new SecundumForSubtreeConfigurator(taxonNode.getUuid());
111 SetSecundumForSubtreeConfigurationWizard wizard = new SetSecundumForSubtreeConfigurationWizard(configurator);
112
113 WizardDialog dialog = new WizardDialog(shell, wizard);
114
115 if (dialog.open() == Window.OK) {
116 return Status.OK_STATUS;
117 }else{
118 return Status.CANCEL_STATUS;
119 }
120 }
121
122 @CanExecute
123 private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
124 boolean canExecute = false;
125 canExecute = selection.size()==1
126 && selection.getFirstElement() instanceof TaxonNodeDto;
127 menuItem.setVisible(canExecute);
128 return canExecute;
129 }
130
131 @Override
132 public AbstractOperation prepareOperation(IStructuredSelection selection,
133 Shell shell,
134 MPart activePart,
135 MHandledMenuItem menuItem) {
136 SetSecundumForSubtreeOperation operation =
137 new SetSecundumForSubtreeOperation(getTrigger(),
138 true,
139 taxonNode.getUuid(),
140 partService, activePart, application, modelService,
141 configurator);
142
143 return operation;
144 }
145
146 @Override
147 public void onComplete() {
148 }
149
150 /**
151 * {@inheritDoc}
152 */
153 @Override
154 protected Object getTrigger() {
155 return this;
156 }
157
158 }