Project

General

Profile

Download (9.41 KB) Statistics
| Branch: | Tag: | Revision:
1 a7d1d751 Patrick Plitzner
/**
2
* Copyright (C) 2007 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
10
package eu.etaxonomy.taxeditor.editor.name.e4.handler;
11
12 184a2a6b Katja Luther
import java.util.HashSet;
13
import java.util.List;
14 a7d1d751 Patrick Plitzner
import java.util.Set;
15 44000ac1 Katja Luther
import java.util.UUID;
16 a7d1d751 Patrick Plitzner
17
import javax.inject.Named;
18
19 f274f083 Andreas Müller
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20 90cf660a Patrick Plitzner
import org.eclipse.e4.core.di.annotations.CanExecute;
21 a7d1d751 Patrick Plitzner
import org.eclipse.e4.core.di.annotations.Execute;
22 b5b429af Patrick Plitzner
import org.eclipse.e4.ui.di.UISynchronize;
23 41c668f3 Patrick Plitzner
import org.eclipse.e4.ui.model.application.MApplication;
24 a7d1d751 Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25 90cf660a Patrick Plitzner
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26 a7d1d751 Patrick Plitzner
import org.eclipse.e4.ui.services.IServiceConstants;
27 41c668f3 Patrick Plitzner
import org.eclipse.e4.ui.workbench.modeling.EModelService;
28
import org.eclipse.e4.ui.workbench.modeling.EPartService;
29 a7d1d751 Patrick Plitzner
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.swt.widgets.Shell;
31
32 299adb56 Katja Luther
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
33 a7d1d751 Patrick Plitzner
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
34 299adb56 Katja Luther
import eu.etaxonomy.cdm.model.reference.Reference;
35 a7d1d751 Patrick Plitzner
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38 44000ac1 Katja Luther
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
39 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.EditorUtil;
40 9b135851 Katja Luther
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInput;
41 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42 bd122e26 Katja Luther
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonEditor;
43 90cf660a Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
44 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
45 44000ac1 Katja Luther
import eu.etaxonomy.taxeditor.event.EventUtility;
46
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
47 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.model.AbstractUtility;
48 299adb56 Katja Luther
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50 299adb56 Katja Luther
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
52 a7d1d751 Patrick Plitzner
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
53
54
/**
55
 * @author pplitzner
56
 * @since Aug 28, 2017
57
 */
58 9b135851 Katja Luther
public class ChangeSynonymToAcceptedTaxonHandler implements IPostOperationEnabled {
59 fbb8b34c Andreas Müller
60 9b135851 Katja Luther
    private static final Logger logger = LogManager.getLogger(ChangeSynonymToAcceptedTaxonHandler.class);
61 a7d1d751 Patrick Plitzner
62 bd122e26 Katja Luther
	private TaxonEditor editor;
63 41c668f3 Patrick Plitzner
    private EPartService partService;
64
    private MApplication application;
65
    private EModelService modelService;
66 a7d1d751 Patrick Plitzner
67
    @Execute
68 41c668f3 Patrick Plitzner
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
69
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, EPartService partService,
70
            EModelService modelService, MApplication application, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
71 b5b429af Patrick Plitzner
            UISynchronize sync) {
72 a7d1d751 Patrick Plitzner
73 41c668f3 Patrick Plitzner
        this.modelService = modelService;
74
        this.application = application;
75
        this.partService = partService;
76
77 bd122e26 Katja Luther
        editor = (TaxonEditor) activePart.getObject();
78 a7d1d751 Patrick Plitzner
79 9b135851 Katja Luther
        TaxonEditorInput input = editor.getEditorInput();
80 a7d1d751 Patrick Plitzner
81
		// Get synonym from selection
82
		if (!(selection.getFirstElement() instanceof Synonym)) {
83
			logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
84
			return;
85
		}
86
87
		Synonym synonym = (Synonym) selection.getFirstElement();
88 299adb56 Katja Luther
		Reference synSecRef = synonym.getSec();
89 a7d1d751 Patrick Plitzner
90
		// Force user to save taxon - not really necessary though, is it?
91
		if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
92
			return;
93
		}
94
95
		// Get taxon
96 4d745c9e Patrick Plitzner
		Taxon taxon = input.getTaxon();
97 a7d1d751 Patrick Plitzner
98 49d3c2dd Katja Luther
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
99 a7d1d751 Patrick Plitzner
100
		if(newParentNode != null){
101 3338ac1f Andreas Müller
		    Reference parentSecRef = newParentNode.getTaxon() != null? newParentNode.getTaxon().getSec(): null;
102 a7d1d751 Patrick Plitzner
103
			// TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
104
			// apply confirmation dialog
105
			HomotypicalGroup group = synonym.getHomotypicGroup();
106 184a2a6b Katja Luther
			List<Synonym> homotypicSynonyms = taxon.getSynonymsInGroup(synonym.getHomotypicGroup());
107
			Set<Reference> secRefs = new HashSet<>();
108
			boolean nullExist = false;
109
			for (Synonym synInHomtypicGroup: homotypicSynonyms){
110
			    if (synInHomtypicGroup.getSec() == null && synonym.getSec() != null){
111
			        nullExist = true;
112
			    }
113
			    secRefs.add(synInHomtypicGroup.getSec());
114
			}
115
116 a7d1d751 Patrick Plitzner
			// FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
117 299adb56 Katja Luther
118
			//compare parentSec and synSec and ask for handling.
119
			SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
120 44000ac1 Katja Luther
			UUID newSecUuid = null;
121 f13ee832 Katja Luther
			if (((synSecRef != parentSecRef || secRefs.size()>1)  && (secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn)))|| secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect)){
122
123
	            if (secHandling.equals(SecReferenceHandlingEnum.KeepOrSelect) || secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect)){
124
	                String message = null;
125
	                String[] options = null;
126
	                if (secHandling.equals(SecReferenceHandlingEnum.AlwaysSelect) && synSecRef == parentSecRef){
127
	                    message = Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message_always_select;
128
	                    options = new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select};
129
	                }else {
130
	                    message = Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message;
131
	                    options = new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep,  Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, };
132
	                }
133
	                int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, message, options);
134 44000ac1 Katja Luther
135 f13ee832 Katja Luther
//	                int result = MessagingUtils.confirmDialog(Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_title, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Handling_message,
136
//	                        new String[]{Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Keep, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Parent, Messages.ChangeSynonymToAcceptedHandler_Select_Sec_Reference_Select});
137 44000ac1 Katja Luther
	                if (result == 2){
138
	                    newSecUuid = parentSecRef != null? parentSecRef.getUuid(): null;
139 f13ee832 Katja Luther
	                }else if (result == 1){
140
	                    Reference sec = ReferenceSelectionDialog.select(shell, null);
141
                        newSecUuid = sec != null? sec.getUuid(): null;
142 44000ac1 Katja Luther
	                }else if (result == 0){
143 f13ee832 Katja Luther
	                    secHandling = SecReferenceHandlingEnum.KeepOrWarn;
144 44000ac1 Katja Luther
	                }else{
145
	                    return ;
146
	                }
147
148 f13ee832 Katja Luther
	            }else if (parentSecRef != synSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn)){
149
	                MessagingUtils.warningDialog(Messages.SecundumReference,this, Messages.ChangeSynonymToAcceptedHandler_Different_Secundum_references);
150 299adb56 Katja Luther
151 f13ee832 Katja Luther
	            }
152 299adb56 Katja Luther
			}
153 888a0d35 Katja Luther
			if (newParentNode.getTaxon() != null &&
154
					synonym.isPublish() != newParentNode.getTaxon().isPublish()){
155 fef9b989 Katja Luther
			    MessagingUtils.warningDialog("Publish flag",this, Messages.ChangeSynonymToAcceptedHandler_Different_Publish_Flag);
156 373576f7 Katja Luther
			}
157
158 a7d1d751 Patrick Plitzner
			ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
159 184a2a6b Katja Luther
					taxon, newParentNode, synonym,
160 44000ac1 Katja Luther
					newSecUuid, secHandling,
161 49d3c2dd Katja Luther
					this, editor.getEditorInput()); //$NON-NLS-1$
162 a7d1d751 Patrick Plitzner
163 b5b429af Patrick Plitzner
			AbstractUtility.executeOperation(operation, sync);
164 a7d1d751 Patrick Plitzner
		}
165
	}
166
167
	@Override
168 a3c56b93 Patrick Plitzner
    public boolean postOperation(Object objectAffectedByOperation) {
169 a7d1d751 Patrick Plitzner
170
		// Redraw existing editor
171
		((IPostOperationEnabled) editor).postOperation(null);
172
173 c8cdc672 Patrick Plitzner
		editor.save(AbstractUtility.getMonitor());
174 a7d1d751 Patrick Plitzner
175
176 44000ac1 Katja Luther
		if (objectAffectedByOperation instanceof TaxonNode) {
177 a7d1d751 Patrick Plitzner
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
178 373576f7 Katja Luther
			EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(newNode.getParent()));
179 a7d1d751 Patrick Plitzner
		}
180
		return true;
181
	}
182
183
	@Override
184
    public boolean onComplete() {
185
		return false;
186
	}
187 90cf660a Patrick Plitzner
188
    @CanExecute
189
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
190
            MHandledMenuItem menuItem){
191
        boolean canExecute = false;
192 38b32a1d Patrick Plitzner
        if(selection.size()==1){
193
            Object selectedElement = selection.getFirstElement();
194
            canExecute =
195
                    NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
196
                    && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
197 958e78a4 Katja Luther
                    && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
198 7f5fe60c Andreas Müller
                    && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement);
199 38b32a1d Patrick Plitzner
            menuItem.setVisible(canExecute);
200
        }
201 90cf660a Patrick Plitzner
        return canExecute;
202
    }
203 f609e74d Andreas Müller
}