Project

General

Profile

Download (5.58 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.name.HomotypicalGroup;
30
import eu.etaxonomy.cdm.model.name.TaxonName;
31
import eu.etaxonomy.cdm.model.taxon.Synonym;
32
import eu.etaxonomy.cdm.model.taxon.Taxon;
33
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34
import eu.etaxonomy.taxeditor.editor.EditorUtil;
35
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
36
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
37
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
38
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
39
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
40
import eu.etaxonomy.taxeditor.model.AbstractUtility;
41
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
42
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
43

    
44
/**
45
 * @author pplitzner
46
 * @since Aug 28, 2017
47
 */
48
public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
49
	private static final Logger logger = Logger
50
			.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
51

    
52
	private TaxonNameEditorE4 editor;
53
    private EPartService partService;
54
    private MApplication application;
55
    private EModelService modelService;
56

    
57
    @Execute
58
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
59
            @Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, EPartService partService,
60
            EModelService modelService, MApplication application, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell,
61
            UISynchronize sync) {
62

    
63
        this.modelService = modelService;
64
        this.application = application;
65
        this.partService = partService;
66

    
67
        editor = (TaxonNameEditorE4) activePart.getObject();
68

    
69
        TaxonEditorInputE4 input = editor.getEditorInput();
70

    
71
		// Get synonym from selection
72
		if (!(selection.getFirstElement() instanceof Synonym)) {
73
			logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
74
			return;
75
		}
76

    
77
		Synonym synonym = (Synonym) selection.getFirstElement();
78

    
79
		// Force user to save taxon - not really necessary though, is it?
80
		if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
81
			return;
82
		}
83

    
84
		// Get taxon
85
		Taxon taxon = input.getTaxon();
86

    
87
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell,// editor.getConversationHolder(),
88
		        Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
89

    
90

    
91
		if(newParentNode != null){
92

    
93
			// TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
94
			// apply confirmation dialog
95
			HomotypicalGroup group = synonym.getHomotypicGroup();
96
			Set<TaxonName> namesInGroup = group.getTypifiedNames();
97
			// FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
98
			ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
99
					taxon, newParentNode, synonym, namesInGroup, this, editor, editor.getEditorInput()); //$NON-NLS-1$
100

    
101
			AbstractUtility.executeOperation(operation, sync);
102
		}
103
	}
104

    
105
	@Override
106
    public boolean postOperation(Object objectAffectedByOperation) {
107

    
108
		// Redraw existing editor
109
		((IPostOperationEnabled) editor).postOperation(null);
110

    
111
		editor.save(AbstractUtility.getMonitor());
112

    
113
		if (objectAffectedByOperation instanceof TaxonNode) {
114

    
115
			// Open new unsaved editor with existing taxon's parent as temporary parent
116
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
117

    
118
			EditorUtil.openTaxonNodeE4(newNode.getUuid(), modelService, partService, application);
119
		}
120
		return true;
121
	}
122

    
123
	@Override
124
    public boolean onComplete() {
125
		return false;
126
	}
127

    
128
    @CanExecute
129
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
130
            MHandledMenuItem menuItem){
131
        boolean canExecute = false;
132
        if(selection.size()==1){
133
            Object selectedElement = selection.getFirstElement();
134
            canExecute =
135
                    NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
136
                    && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
137
                    && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
138
                    && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement)
139
                    && NameEditorMenuPropertyTester.isNotInvalidDesignation(selectedElement);
140
            menuItem.setVisible(canExecute);
141
        }
142
        return canExecute;
143
    }
144
}
(1-1/17)