1
|
/**
|
2
|
* Copyright (C) 2009 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
|
package eu.etaxonomy.taxeditor.editor.definedterm.e4.handler;
|
10
|
|
11
|
import javax.inject.Named;
|
12
|
|
13
|
import org.eclipse.core.commands.operations.IUndoContext;
|
14
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
15
|
import org.eclipse.e4.core.di.annotations.Execute;
|
16
|
import org.eclipse.e4.ui.di.UISynchronize;
|
17
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
18
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
19
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
20
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
21
|
|
22
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
23
|
import eu.etaxonomy.cdm.model.common.TermBase;
|
24
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
25
|
import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
|
26
|
import eu.etaxonomy.taxeditor.editor.definedterm.operation.CreateDefinedTermOperation;
|
27
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
28
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
29
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
30
|
|
31
|
/**
|
32
|
*
|
33
|
* @author pplitzner
|
34
|
* @since Aug 22, 2017
|
35
|
*
|
36
|
*/
|
37
|
public class CreateDefinedTermHandlerE4 {
|
38
|
|
39
|
@Execute
|
40
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
41
|
@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection, MHandledMenuItem menuItem,
|
42
|
UISynchronize sync) {
|
43
|
|
44
|
DefinedTermEditorE4 termEditor = (DefinedTermEditorE4) activePart.getObject();
|
45
|
|
46
|
boolean addTermAsKindOf =
|
47
|
menuItem.getCommand().getElementId().equals("eu.etaxonomy.taxeditor.editor.definedTerms.newKindOfTerm")?true:false;
|
48
|
|
49
|
String label = menuItem.getLocalizedLabel();
|
50
|
IUndoContext undoContext = StoreUtil.getUndoContext();
|
51
|
|
52
|
AbstractPostOperation operation =
|
53
|
new CreateDefinedTermOperation(label,
|
54
|
undoContext,
|
55
|
(TermBase) selection.getFirstElement(),
|
56
|
termEditor.getDefinedTermEditorInput(),
|
57
|
termEditor, addTermAsKindOf);
|
58
|
AbstractUtility.executeOperation(operation, sync);
|
59
|
|
60
|
}
|
61
|
|
62
|
@CanExecute
|
63
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) IStructuredSelection selection,
|
64
|
MHandledMenuItem menuItem){
|
65
|
boolean canExecute = false;
|
66
|
Object firstElement = selection.getFirstElement();
|
67
|
canExecute = selection.size()==1
|
68
|
&&
|
69
|
(firstElement instanceof DefinedTermBase
|
70
|
|| firstElement instanceof TermVocabulary);
|
71
|
TermVocabulary vocabulary = null;
|
72
|
|
73
|
if(firstElement instanceof DefinedTermBase){
|
74
|
vocabulary = ((DefinedTermBase) firstElement).getVocabulary();
|
75
|
}else if(firstElement instanceof TermVocabulary){
|
76
|
vocabulary = (TermVocabulary) firstElement;
|
77
|
}
|
78
|
canExecute &= vocabulary!=null;
|
79
|
menuItem.setVisible(canExecute);
|
80
|
return canExecute;
|
81
|
}
|
82
|
|
83
|
}
|