Project

General

Profile

Download (6.95 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.Set;
13

    
14
import javax.inject.Named;
15

    
16
import org.apache.log4j.Logger;
17
import org.eclipse.e4.core.di.annotations.CanExecute;
18
import org.eclipse.e4.core.di.annotations.Execute;
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.services.IServiceConstants;
24
import org.eclipse.e4.ui.workbench.modeling.EModelService;
25
import org.eclipse.e4.ui.workbench.modeling.EPartService;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.swt.widgets.Shell;
28

    
29
import eu.etaxonomy.cdm.model.metadata.SecReferenceHandlingEnum;
30
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
31
import eu.etaxonomy.cdm.model.name.TaxonName;
32
import eu.etaxonomy.cdm.model.reference.Reference;
33
import eu.etaxonomy.cdm.model.taxon.Synonym;
34
import eu.etaxonomy.cdm.model.taxon.Taxon;
35
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
38
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
39
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
40
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
41
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
42
import eu.etaxonomy.taxeditor.model.AbstractUtility;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
45
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
46
import eu.etaxonomy.taxeditor.ui.dialog.selection.ReferenceSelectionDialog;
47
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
48

    
49
/**
50
 * @author pplitzner
51
 * @since Aug 28, 2017
52
 */
53
public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
54
	private static final Logger logger = Logger
55
			.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
56

    
57
	private TaxonNameEditorE4 editor;
58
    private EPartService partService;
59
    private MApplication application;
60
    private EModelService modelService;
61

    
62
    @Execute
63
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
64
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, EPartService partService,
65
            EModelService modelService, MApplication application, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
66
            UISynchronize sync) {
67

    
68
        this.modelService = modelService;
69
        this.application = application;
70
        this.partService = partService;
71

    
72
        editor = (TaxonNameEditorE4) activePart.getObject();
73

    
74
        TaxonEditorInputE4 input = editor.getEditorInput();
75

    
76
		// Get synonym from selection
77
		if (!(selection.getFirstElement() instanceof Synonym)) {
78
			logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
79
			return;
80
		}
81

    
82
		Synonym synonym = (Synonym) selection.getFirstElement();
83
		Reference synSecRef = synonym.getSec();
84

    
85
		// Force user to save taxon - not really necessary though, is it?
86
		if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
87
			return;
88
		}
89

    
90
		// Get taxon
91
		Taxon taxon = input.getTaxon();
92

    
93
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell,// editor.getConversationHolder(),
94
		        Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
95

    
96
		Reference parentSecRef = newParentNode.getTaxon() != null? newParentNode.getTaxon().getSec(): null;
97
		if(newParentNode != null){
98

    
99
			// TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
100
			// apply confirmation dialog
101
			HomotypicalGroup group = synonym.getHomotypicGroup();
102
			Set<TaxonName> namesInGroup = group.getTypifiedNames();
103
			// FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
104

    
105
			//compare parentSec and synSec and ask for handling.
106
			SecReferenceHandlingEnum secHandling = PreferencesUtil.getSecReferenceHandlingPreference();
107
			Reference newSec = null;
108
			if ((synSecRef != parentSecRef && secHandling.equals(SecReferenceHandlingEnum.KeepWhenSame) )|| secHandling.equals(SecReferenceHandlingEnum.WarningSelect)){
109
			    String message = null;
110
			    if (secHandling.equals(SecReferenceHandlingEnum.KeepWhenSame)){
111
			        message = "The secundum reference of the former synonym is not the same as the secundum of the new parent, therefore please select the secundum reference for the new accepted taxon.";
112
			    }else{
113
			        message = "Please select the secundum reference for the new accepted taxon.";
114
			    }
115

    
116
			    int result = MessagingUtils.confirmDialog("Select secundum reference for accepted taxon", message, new String[]{"OK", "Cancel"});
117
			    if (result == 0){
118
			        newSec = ReferenceSelectionDialog.select(shell, null);
119
			    }else{
120
			        return;
121
			    }
122

    
123
			}
124
			ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
125
					taxon, newParentNode, synonym, namesInGroup,
126
					newSec, secHandling,
127
					this, editor, editor.getEditorInput()); //$NON-NLS-1$
128

    
129
			AbstractUtility.executeOperation(operation, sync);
130
		}
131
	}
132

    
133
	@Override
134
    public boolean postOperation(Object objectAffectedByOperation) {
135

    
136
		// Redraw existing editor
137
		((IPostOperationEnabled) editor).postOperation(null);
138

    
139
		editor.save(AbstractUtility.getMonitor());
140

    
141
		if (objectAffectedByOperation instanceof TaxonNode) {
142

    
143
			// Open new unsaved editor with existing taxon's parent as temporary parent
144
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
145

    
146
			EditorUtil.openTaxonNodeE4(newNode.getUuid(), modelService, partService, application);
147
		}
148
		return true;
149
	}
150

    
151
	@Override
152
    public boolean onComplete() {
153
		return false;
154
	}
155

    
156
    @CanExecute
157
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
158
            MHandledMenuItem menuItem){
159
        boolean canExecute = false;
160
        if(selection.size()==1){
161
            Object selectedElement = selection.getFirstElement();
162
            canExecute =
163
                    NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
164
                    && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
165
                    && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
166
                    && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement);
167
            menuItem.setVisible(canExecute);
168
        }
169
        return canExecute;
170
    }
171
}
(1-1/15)