Project

General

Profile

Download (5.4 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(),
91
		        Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid());
92

    
93

    
94
		if(newParentNode != null){
95

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

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

    
107
	}
108

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

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

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

    
118
		if (objectAffectedByOperation instanceof TaxonNode) {
119

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

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

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

    
133

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

    
147
}
(1-1/12)