Project

General

Profile

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

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

    
55
	private TaxonNameEditorE4 editor;
56
    private EPartService partService;
57
    private MApplication application;
58
    private EModelService modelService;
59

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

    
66
        this.modelService = modelService;
67
        this.application = application;
68
        this.partService = partService;
69

    
70
        editor = (TaxonNameEditorE4) activePart.getObject();
71

    
72
        TaxonEditorInputE4 input = editor.getEditorInput();
73

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

    
80
		Synonym synonym = (Synonym) selection.getFirstElement();
81

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

    
87
		// Get taxon
88
		Taxon taxon = input.getTaxon();
89

    
90
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, editor.getConversationHolder(), Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification());
91

    
92

    
93
		if(newParentNode != null){
94

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

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

    
106
	}
107

    
108
	/** {@inheritDoc} */
109
	@Override
110
    public boolean postOperation(CdmBase objectAffectedByOperation) {
111

    
112
		// Redraw existing editor
113
		((IPostOperationEnabled) editor).postOperation(null);
114

    
115
		editor.save(AbstractUtility.getMonitor());
116

    
117
		if (objectAffectedByOperation instanceof TaxonNode) {
118

    
119
			// Open new unsaved editor with existing taxon's parent as temporary parent
120
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
121

    
122
			EditorUtil.openTaxonNodeE4(newNode.getUuid(), modelService, partService, application);
123
		}
124
		return true;
125
	}
126

    
127
	@Override
128
    public boolean onComplete() {
129
		return false;
130
	}
131

    
132

    
133
    @CanExecute
134
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
135
            MHandledMenuItem menuItem){
136
        boolean canExecute = false;
137
        Object selectedElement = selection.getFirstElement();
138
        canExecute =
139
                NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
140
                && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
141
                && !NameEditorMenuPropertyTester.isMisapplication(selectedElement);
142
        menuItem.setVisible(canExecute);
143
        return canExecute;
144
    }
145

    
146
}
(1-1/12)