Project

General

Profile

Download (5.69 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
import org.eclipse.ui.PartInitException;
29

    
30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
32
import eu.etaxonomy.cdm.model.name.TaxonName;
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.ui.dialog.selection.TaxonNodeSelectionDialog;
46

    
47
/**
48
 *
49
 * @author pplitzner
50
 * @since Aug 28, 2017
51
 *
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

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

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

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

    
94

    
95
		if(newParentNode != null){
96

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

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

    
108
	}
109

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

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

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

    
119
		if (objectAffectedByOperation instanceof TaxonNode) {
120

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

    
124
			try {
125
				EditorUtil.openTaxonNodeE4(newNode.getUuid(), modelService, partService, application);
126
			} catch (PartInitException e) {
127
				e.printStackTrace();
128
			} catch (Exception e) {
129
				MessagingUtils.warningDialog(Messages.ChangeSynonymToAcceptedTaxonHandler_CREATE_FAILURE, this, e.getMessage());
130
			}
131
		}
132
		return true;
133
	}
134

    
135
	@Override
136
    public boolean onComplete() {
137
		return false;
138
	}
139

    
140

    
141
    @CanExecute
142
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
143
            MHandledMenuItem menuItem){
144
        boolean canExecute = false;
145
        Object selectedElement = selection.getFirstElement();
146
        canExecute =
147
                NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
148
                && !NameEditorMenuPropertyTester.isAccepted(selectedElement)
149
                && !NameEditorMenuPropertyTester.isMisapplication(selectedElement);
150
//        menuItem.setVisible(canExecute);
151
        return canExecute;
152
    }
153

    
154
}
(1-1/12)