Project

General

Profile

Download (9.44 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
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
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Set;
15
import java.util.UUID;
16

    
17
import javax.inject.Named;
18

    
19
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
20
import org.eclipse.e4.core.di.annotations.CanExecute;
21
import org.eclipse.e4.core.di.annotations.Execute;
22
import org.eclipse.e4.ui.di.UISynchronize;
23
import org.eclipse.e4.ui.model.application.MApplication;
24
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26
import org.eclipse.e4.ui.services.IServiceConstants;
27
import org.eclipse.e4.ui.workbench.modeling.EModelService;
28
import org.eclipse.e4.ui.workbench.modeling.EPartService;
29
import org.eclipse.jface.viewers.IStructuredSelection;
30
import org.eclipse.swt.widgets.Shell;
31

    
32
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
33
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
34
import eu.etaxonomy.cdm.model.reference.Reference;
35
import eu.etaxonomy.cdm.model.taxon.Synonym;
36
import eu.etaxonomy.cdm.model.taxon.Taxon;
37
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
38
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
39
import eu.etaxonomy.taxeditor.editor.EditorUtil;
40
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
41
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
43
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
44
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
45
import eu.etaxonomy.taxeditor.event.EventUtility;
46
import eu.etaxonomy.taxeditor.event.WorkbenchEventConstants;
47
import eu.etaxonomy.taxeditor.model.AbstractUtility;
48
import eu.etaxonomy.taxeditor.model.MessagingUtils;
49
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
51
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
52
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
53

    
54
/**
55
 * @author pplitzner
56
 * @since Aug 28, 2017
57
 */
58
public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
59

    
60
    private static final Logger logger = LogManager.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
61

    
62
	private TaxonNameEditorE4 editor;
63
    private EPartService partService;
64
    private MApplication application;
65
    private EModelService modelService;
66

    
67
    @Execute
68
    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
            UISynchronize sync) {
72

    
73
        this.modelService = modelService;
74
        this.application = application;
75
        this.partService = partService;
76

    
77
        editor = (TaxonNameEditorE4) activePart.getObject();
78

    
79
        TaxonEditorInputE4 input = editor.getEditorInput();
80

    
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
		Reference synSecRef = synonym.getSec();
89

    
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
		Taxon taxon = input.getTaxon();
97

    
98
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
99

    
100
		if(newParentNode != null){
101
		    Reference parentSecRef = newParentNode.getTaxon() != null? newParentNode.getTaxon().getSec(): null;
102

    
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
			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
			// FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
117

    
118
			//compare parentSec and synSec and ask for handling.
119
			SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
120
			UUID newSecUuid = null;
121
			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

    
135
//	                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
	                if (result == 2){
138
	                    newSecUuid = parentSecRef != null? parentSecRef.getUuid(): null;
139
	                }else if (result == 1){
140
	                    Reference sec = ReferenceSelectionDialog.select(shell, null);
141
                        newSecUuid = sec != null? sec.getUuid(): null;
142
	                }else if (result == 0){
143
	                    secHandling = SecReferenceHandlingEnum.KeepOrWarn;
144
	                }else{
145
	                    return ;
146
	                }
147

    
148
	            }else if (parentSecRef != synSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepOrWarn)){
149
	                MessagingUtils.warningDialog(Messages.SecundumReference,this, Messages.ChangeSynonymToAcceptedHandler_Different_Secundum_references);
150

    
151
	            }
152
			}
153
			if (newParentNode.getTaxon() != null &&
154
					synonym.isPublish() != newParentNode.getTaxon().isPublish()){
155
			    MessagingUtils.warningDialog("Publish flag",this, Messages.ChangeSynonymToAcceptedHandler_Different_Publish_Flag);
156
			}
157

    
158
			ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
159
					taxon, newParentNode, synonym,
160
					newSecUuid, secHandling,
161
					this, editor.getEditorInput()); //$NON-NLS-1$
162

    
163
			AbstractUtility.executeOperation(operation, sync);
164
		}
165
	}
166

    
167
	@Override
168
    public boolean postOperation(Object objectAffectedByOperation) {
169

    
170
		// Redraw existing editor
171
		((IPostOperationEnabled) editor).postOperation(null);
172

    
173
		editor.save(AbstractUtility.getMonitor());
174

    
175

    
176
		if (objectAffectedByOperation instanceof TaxonNode) {
177
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
178
			EventUtility.postEvent(WorkbenchEventConstants.REFRESH_NAVIGATOR, new TaxonNodeDto(newNode.getParent()));
179
		}
180
		return true;
181
	}
182

    
183
	@Override
184
    public boolean onComplete() {
185
		return false;
186
	}
187

    
188
    @CanExecute
189
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
190
            MHandledMenuItem menuItem){
191
        boolean canExecute = false;
192
        if(selection.size()==1){
193
            Object selectedElement = selection.getFirstElement();
194
            canExecute =
195
                    NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
196
                    && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
197
                    && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
198
                    && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement);
199
            menuItem.setVisible(canExecute);
200
        }
201
        return canExecute;
202
    }
203
}
(1-1/15)