Merge branch 'develop' into bulkEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / editor / definedterm / e4 / handler / CreateDefinedTermHandlerE4.java
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.core.di.annotations.Optional;
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
21 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
22 import eu.etaxonomy.cdm.model.common.Marker;
23 import eu.etaxonomy.cdm.model.common.MarkerType;
24 import eu.etaxonomy.cdm.model.common.TermBase;
25 import eu.etaxonomy.cdm.model.common.TermVocabulary;
26 import eu.etaxonomy.taxeditor.editor.definedterm.e4.DefinedTermEditorE4;
27 import eu.etaxonomy.taxeditor.editor.definedterm.operation.CreateDefinedTermOperation;
28 import eu.etaxonomy.taxeditor.model.AbstractUtility;
29 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
30 import eu.etaxonomy.taxeditor.store.StoreUtil;
31
32 /**
33 *
34 * @author pplitzner
35 * @since Aug 22, 2017
36 *
37 */
38 public class CreateDefinedTermHandlerE4 {
39
40 @Execute
41 public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
42 @Optional@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase, MHandledMenuItem menuItem) {
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,
56 termEditor.getDefinedTermEditorInput(),
57 termEditor, addTermAsKindOf);
58 AbstractUtility.executeOperation(operation);
59
60 }
61
62 @CanExecute
63 public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) TermBase termBase){
64 if (termBase == null){
65 return true;
66 }
67
68 TermVocabulary vocabulary = null;
69
70 if(termBase instanceof DefinedTermBase){
71 vocabulary = ((DefinedTermBase) termBase).getVocabulary();
72 }else if(termBase instanceof TermVocabulary){
73 vocabulary = (TermVocabulary) termBase;
74 }
75
76 if(vocabulary == null){
77 return true;
78 }
79
80 for(Marker vocabularyMarker : vocabulary.getMarkers()){
81 if(vocabularyMarker.getMarkerType().equals(MarkerType.MODIFIABLE())){
82 return vocabularyMarker.getValue();
83 }
84 }
85
86 return true;
87 }
88
89 }