Project

General

Profile

Download (5.61 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
 *
46
 * @author pplitzner
47
 * @since Aug 28, 2017
48
 *
49
 */
50
public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
51
	private static final Logger logger = Logger
52
			.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
53

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

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

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

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

    
71
        TaxonEditorInputE4 input = editor.getEditorInput();
72

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

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

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

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

    
89
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell,// editor.getConversationHolder(),
90
		        Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, input.getTaxonNode().getClassification().getUuid(), true);
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(Object 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
        if(selection.size()==1){
138
            Object selectedElement = selection.getFirstElement();
139
            canExecute =
140
                    NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
141
                    && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
142
                    && NameEditorMenuPropertyTester.isNotMisapplication(selectedElement)
143
                    && NameEditorMenuPropertyTester.isNotProparteSynonym(selectedElement)
144
                    && NameEditorMenuPropertyTester.isNotInvalidDesignation(selectedElement);
145
            menuItem.setVisible(canExecute);
146
        }
147
        return canExecute;
148
    }
149

    
150
}
(1-1/16)