ref #8890: handle dynamic menu for ref objects view and taxonnode wizard correctly
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / handler / defaultHandler / OpenReferencingObjectsViewHandler.java
1 package eu.etaxonomy.taxeditor.handler.defaultHandler;
2
3 import java.util.UUID;
4
5 import javax.inject.Named;
6
7 import org.eclipse.e4.core.di.annotations.CanExecute;
8 import org.eclipse.e4.core.di.annotations.Execute;
9 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
10 import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
11 import org.eclipse.e4.ui.services.IServiceConstants;
12 import org.eclipse.e4.ui.workbench.modeling.EPartService;
13 import org.eclipse.e4.ui.workbench.modeling.EPartService.PartState;
14 import org.eclipse.jface.viewers.IStructuredSelection;
15
16 import eu.etaxonomy.cdm.model.common.CdmBase;
17 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
18 import eu.etaxonomy.taxeditor.bulkeditor.AppModelId;
19 import eu.etaxonomy.taxeditor.editor.IReferencingObjectsView;
20
21 public class OpenReferencingObjectsViewHandler {
22
23 Object selectedObject;
24 @Execute
25 public void execute(EPartService partService) {
26 MPart part = partService.createPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_BULKEDITOR_REFERENCINGOBJECTS_E4_REFERENCINGOBJECTSVIEWE4);
27 part = partService.showPart(part, PartState.ACTIVATE);
28 IReferencingObjectsView view = (IReferencingObjectsView)part.getObject();
29 UUID uuid = null;
30 Class clazz = null;
31
32 if (selectedObject instanceof IStructuredSelection){
33 selectedObject = ((IStructuredSelection)selectedObject).getFirstElement();
34 }
35 if (selectedObject instanceof CdmBase){
36 uuid = ((CdmBase)selectedObject).getUuid();
37 clazz = selectedObject.getClass();
38 } else if (selectedObject instanceof UuidAndTitleCache){
39 uuid = ((UuidAndTitleCache)selectedObject).getUuid();
40 clazz = ((UuidAndTitleCache)selectedObject).getType();
41 }
42 if (uuid != null && clazz != null){
43 view.updateReferencingObjects(uuid, clazz);
44 }
45
46
47
48 }
49
50 @CanExecute
51 public boolean canExecute(EPartService partService,
52 @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
53 MHandledMenuItem menuItem) {
54 boolean canExecute = false;
55 MPart part = partService.findPart(AppModelId.PARTDESCRIPTOR_EU_ETAXONOMY_TAXEDITOR_BULKEDITOR_REFERENCINGOBJECTS_E4_REFERENCINGOBJECTSVIEWE4);
56 canExecute = !part.equals(activePart);
57
58
59 if (canExecute){
60 String commandId = menuItem.getCommand().getElementId();
61 selectedObject = menuItem.getTransientData().get(commandId+".uuid");
62 if (selectedObject instanceof IStructuredSelection){
63 canExecute = ((IStructuredSelection)selectedObject).size() == 1;
64 }
65 }
66 menuItem.setVisible(canExecute);
67 return canExecute;
68 }
69
70 }