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.model.application.ui.basic.MPart;
|
20
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
21
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
22
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
23
|
import org.eclipse.swt.widgets.Shell;
|
24
|
import org.eclipse.ui.IEditorInput;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
28
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
29
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
30
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
31
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
32
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
33
|
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInputE4;
|
34
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
35
|
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
|
36
|
import eu.etaxonomy.taxeditor.editor.name.handler.NameEditorMenuPropertyTester;
|
37
|
import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
|
38
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
39
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
40
|
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
|
41
|
|
42
|
/**
|
43
|
*
|
44
|
* @author pplitzner
|
45
|
* @since Aug 28, 2017
|
46
|
*
|
47
|
*/
|
48
|
public class ChangeSynonymToAcceptedTaxonHandlerE4 implements IPostOperationEnabled {
|
49
|
private static final Logger logger = Logger
|
50
|
.getLogger(ChangeSynonymToAcceptedTaxonHandlerE4.class);
|
51
|
|
52
|
private TaxonNameEditorE4 editor;
|
53
|
|
54
|
@Execute
|
55
|
public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
|
56
|
@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
|
57
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell) {
|
58
|
|
59
|
editor = (TaxonNameEditorE4) activePart.getObject();
|
60
|
|
61
|
IEditorInput input = editor.getEditorInput();
|
62
|
|
63
|
if (!(input instanceof TaxonEditorInputE4)) {
|
64
|
logger.error("Editor input is not TaxonEditorInput"); //$NON-NLS-1$
|
65
|
return;
|
66
|
}
|
67
|
|
68
|
// Get synonym from selection
|
69
|
if (!(selection.getFirstElement() instanceof Synonym)) {
|
70
|
logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
|
71
|
return;
|
72
|
}
|
73
|
|
74
|
Synonym synonym = (Synonym) selection.getFirstElement();
|
75
|
|
76
|
// Force user to save taxon - not really necessary though, is it?
|
77
|
if (!EditorUtil.forceUserSaveE4Editor(editor, shell)) {
|
78
|
return;
|
79
|
}
|
80
|
|
81
|
// Get taxon
|
82
|
Taxon taxon = ((TaxonEditorInputE4) input).getTaxon();
|
83
|
|
84
|
TaxonNode newParentNode = TaxonNodeSelectionDialog.select(shell, editor.getConversationHolder(), Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, ((TaxonEditorInputE4) input).getTaxonNode().getClassification());
|
85
|
|
86
|
|
87
|
if(newParentNode != null){
|
88
|
|
89
|
// TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
|
90
|
// apply confirmation dialog
|
91
|
HomotypicalGroup group = synonym.getHomotypicGroup();
|
92
|
Set<TaxonName> namesInGroup = group.getTypifiedNames();
|
93
|
// FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
|
94
|
ChangeSynonymToAcceptedTaxonOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
|
95
|
taxon, newParentNode, synonym, namesInGroup, this, editor, editor.getEditorInput()); //$NON-NLS-1$
|
96
|
|
97
|
AbstractUtility.executeOperation(operation);
|
98
|
}
|
99
|
|
100
|
}
|
101
|
|
102
|
/** {@inheritDoc} */
|
103
|
@Override
|
104
|
public boolean postOperation(CdmBase objectAffectedByOperation) {
|
105
|
|
106
|
// Redraw existing editor
|
107
|
((IPostOperationEnabled) editor).postOperation(null);
|
108
|
|
109
|
editor.save(AbstractUtility.getMonitor());
|
110
|
|
111
|
if (objectAffectedByOperation instanceof TaxonNode) {
|
112
|
|
113
|
// Open new unsaved editor with existing taxon's parent as temporary parent
|
114
|
TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
|
115
|
// TaxonNode newNode = parentNode.addChild(newTaxon);
|
116
|
|
117
|
// try {
|
118
|
// FIXME E4 this throws an exception
|
119
|
// EditorUtil.openTaxonNodeE4(newNode.getUuid());
|
120
|
//
|
121
|
// } catch (PartInitException e) {
|
122
|
// e.printStackTrace();
|
123
|
// } catch (Exception e) {
|
124
|
// MessagingUtils.warningDialog(Messages.ChangeSynonymToAcceptedTaxonHandler_CREATE_FAILURE, this, e.getMessage());
|
125
|
// }
|
126
|
}
|
127
|
return true;
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public boolean onComplete() {
|
132
|
return false;
|
133
|
}
|
134
|
|
135
|
|
136
|
@CanExecute
|
137
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)IStructuredSelection selection,
|
138
|
MHandledMenuItem menuItem){
|
139
|
boolean canExecute = false;
|
140
|
Object selectedElement = selection.getFirstElement();
|
141
|
canExecute =
|
142
|
NameEditorMenuPropertyTester.isNotHomotypicSynonymOfAcceptedTaxon(selectedElement)
|
143
|
&& !NameEditorMenuPropertyTester.isAccepted(selectedElement)
|
144
|
&& !NameEditorMenuPropertyTester.isMisapplication(selectedElement);
|
145
|
// menuItem.setVisible(canExecute);
|
146
|
return canExecute;
|
147
|
}
|
148
|
|
149
|
}
|