2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.handler
;
12 import java
.util
.UUID
;
14 import javax
.inject
.Named
;
16 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
17 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
18 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
19 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
20 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
21 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
22 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
23 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
25 import eu
.etaxonomy
.cdm
.api
.service
.ITermService
;
26 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
30 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
31 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.FactualDataPartE4
;
32 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.CreateDescriptionElementOperation
;
33 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
34 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
35 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
36 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
37 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
38 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
46 public class CreateDescriptionElementHandlerE4
{
49 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
,
50 @Optional@Named(IServiceConstants
.ACTIVE_SELECTION
) IStructuredSelection selection
,
51 MHandledMenuItem menuItem
,
54 String commandId
= menuItem
.getCommand().getElementId();
55 UUID uuid
= (UUID
) menuItem
.getTransientData().get(commandId
+".feature.uuid");
56 Feature feature
= HibernateProxyHelper
.deproxy(CdmStore
.getService(ITermService
.class).load(uuid
), Feature
.class);
58 FactualDataPartE4 factualDataPart
= (FactualDataPartE4
) activePart
.getObject();
60 DescriptionBase description
= null;
61 Object object
= selection
.getFirstElement();
62 if(object
instanceof FeatureNodeContainer
){
63 description
= ((FeatureNodeContainer
) object
).getDescription();
65 else if(object
instanceof DescriptionElementBase
){
66 DescriptionElementBase elementBase
= HibernateProxyHelper
.deproxy(object
, DescriptionElementBase
.class);
67 description
= elementBase
.getInDescription();
69 else if (object
instanceof DescriptionBase
) {
70 description
= HibernateProxyHelper
.deproxy(object
, DescriptionBase
.class);
72 MessagingUtils
.error(getClass(), new IllegalArgumentException("Could not determine the taxon description")); //$NON-NLS-1$
75 AbstractPostOperation operation
= operationCreationInstance(menuItem
.getLocalizedLabel(), feature
, description
,
77 // TODO use undo context specific to editor
78 AbstractUtility
.executeOperation(operation
, sync
);
82 public boolean canExecute(MHandledMenuItem menuItem
,
83 @Named(IServiceConstants
.ACTIVE_SELECTION
)IStructuredSelection selection
){
84 boolean canExecute
= false;
85 canExecute
= selection
.size()==1
87 (selection
.getFirstElement() instanceof DescriptionBase
88 || selection
.getFirstElement() instanceof DescriptionElementBase
89 || selection
.getFirstElement() instanceof FeatureNodeContainer
);
90 menuItem
.setVisible(canExecute
);
96 * Added to make the Class more generic and limit the amount of code changes required
101 * @param postOperationEnabled
104 protected AbstractPostOperation
operationCreationInstance(String label
, Feature feature
,
105 DescriptionBase
<?
> description
, IPostOperationEnabled postOperationEnabled
) {
106 return new CreateDescriptionElementOperation(label
, EditorUtil
.getUndoContext(), description
, feature
,
107 postOperationEnabled
);