Project

General

Profile

Download (4.76 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.Execute;
18
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.swt.widgets.Shell;
22
import org.eclipse.ui.IEditorInput;
23
import org.eclipse.ui.PartInitException;
24

    
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
27
import eu.etaxonomy.cdm.model.name.TaxonName;
28
import eu.etaxonomy.cdm.model.taxon.Synonym;
29
import eu.etaxonomy.cdm.model.taxon.Taxon;
30
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31
import eu.etaxonomy.taxeditor.editor.EditorUtil;
32
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
33
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
34
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
35
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
36
import eu.etaxonomy.taxeditor.model.AbstractUtility;
37
import eu.etaxonomy.taxeditor.model.MessagingUtils;
38
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
39
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
40

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

    
51
	private TaxonNameEditorE4 editor;
52

    
53
    @Execute
54
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
55
            @Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
56
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
57

    
58
        editor = (TaxonNameEditorE4) activePart.getObject();
59

    
60
		IEditorInput input = editor.getEditorInput();
61

    
62
		if (!(input instanceof TaxonEditorInput)) {
63
			logger.error("Editor input is not TaxonEditorInput"); //$NON-NLS-1$
64
			return;
65
		}
66

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

    
73
		Synonym synonym = (Synonym) selection.getFirstElement();
74

    
75
		// Force user to save taxon - not really necessary though, is it?
76
		if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
77
			return;
78
		}
79

    
80
		// Get taxon
81
		Taxon taxon = ((TaxonEditorInput) input).getTaxon();
82

    
83
		TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, editor.getConversationHolder(), Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, ((TaxonEditorInput) input).getTaxonNode().getClassification());
84

    
85

    
86
		if(newParentNode != null){
87

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

    
96
			AbstractUtility.executeOperation(operation);
97
		}
98

    
99
	}
100

    
101
	/** {@inheritDoc} */
102
	@Override
103
    public boolean postOperation(CdmBase objectAffectedByOperation) {
104

    
105
		// Redraw existing editor
106
		((IPostOperationEnabled) editor).postOperation(null);
107

    
108
		editor.doSave(AbstractUtility.getMonitor());
109

    
110
		if (objectAffectedByOperation instanceof TaxonNode) {
111

    
112
			// Open new unsaved editor with existing taxon's parent as temporary parent
113
			TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
114
//			TaxonNode newNode = parentNode.addChild(newTaxon);
115

    
116
			try {
117
				// TODO
118
				/*
119
				 * This doesn't work b/c newNode hasn't been committed yet, and therefore
120
				 *  CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
121
				 *  doesn't work yet.
122
				 */
123
				EditorUtil.openTaxonNode(newNode.getUuid());
124

    
125
			} catch (PartInitException e) {
126
				// TODO Auto-generated catch block
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
}
(1-1/12)