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
.ArrayList
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
16 import javax
.inject
.Named
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
19 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
20 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
21 import org
.eclipse
.e4
.core
.di
.annotations
.Optional
;
22 import org
.eclipse
.e4
.ui
.di
.UISynchronize
;
23 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
24 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
25 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MMenuItem
;
26 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
27 import org
.eclipse
.jface
.viewers
.ISelection
;
28 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeViewer
;
30 import org
.eclipse
.swt
.widgets
.Shell
;
32 import eu
.etaxonomy
.cdm
.api
.service
.DeleteResult
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IDescriptionService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
.Status
;
35 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
36 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
37 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
38 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
39 import eu
.etaxonomy
.cdm
.model
.description
.TaxonNameDescription
;
40 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
41 import eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
.BulkEditorE4
;
42 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
43 import eu
.etaxonomy
.taxeditor
.editor
.name
.e4
.TaxonNameEditorE4
;
44 import eu
.etaxonomy
.taxeditor
.editor
.view
.derivate
.DerivateView
;
45 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.e4
.FactualDataPartE4
;
46 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteDescriptionElementOperation
;
47 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteSpecimenDescriptionOperation
;
48 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteTaxonDescriptionOperation
;
49 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteTaxonNameDescriptionOperation
;
50 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
51 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
52 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
53 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
54 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
62 public class DeleteHandlerE4
{
65 public void execute(@Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
,
66 @Named(IServiceConstants
.ACTIVE_SHELL
)Shell shell
, MMenuItem menuItem
,
69 FactualDataPartE4 factualDataPart
= (FactualDataPartE4
) activePart
.getObject();
70 IStructuredSelection selection
= (IStructuredSelection
) factualDataPart
.getViewer().getSelection();
72 Object selectionProvidingPart
= factualDataPart
.getSelectionProvidingPart();
73 ICdmEntitySessionEnabled cdmEntitySessionEnabled
= null;
74 if(selectionProvidingPart
instanceof ICdmEntitySessionEnabled
){
75 cdmEntitySessionEnabled
= (ICdmEntitySessionEnabled
) selectionProvidingPart
;
77 TreeViewer viewer
= (TreeViewer
)factualDataPart
.getViewer();
78 Object
[] expandedElements
= viewer
.getExpandedElements();
80 String label
= menuItem
.getLocalizedLabel();
82 IUndoContext undoContext
= EditorUtil
.getUndoContext();
84 List
<AbstractPostOperation
> operations
= new ArrayList();
85 AbstractPostOperation operation
= null;
86 for(Iterator iter
= selection
.iterator();iter
.hasNext();){
87 Object object
= iter
.next();
91 DescriptionBase description
= null;
92 SpecimenOrObservationBase specimen
= null;
93 DeleteResult result
= new DeleteResult();
94 if (object
instanceof DescriptionBase
){
95 result
= CdmStore
.getService(IDescriptionService
.class).isDeletable(((DescriptionBase
) object
).getUuid());
96 description
= (DescriptionBase
) object
;
97 } else if (object
instanceof DescriptionElementBase
){
98 description
= ((DescriptionElementBase
)object
).getInDescription();
100 if (result
.getStatus().equals(Status
.OK
)){
101 List
<Object
> expandedElementsList
= new ArrayList
<>();
103 for (Object o
: expandedElements
){
104 if(!o
.equals(object
)){
105 expandedElementsList
.add(o
);
108 expandedElements
= expandedElementsList
.toArray();
110 if(object
instanceof TaxonDescription
){
112 operation
= new DeleteTaxonDescriptionOperation(label
, undoContext
, (TaxonDescription
) object
, factualDataPart
, cdmEntitySessionEnabled
);
114 ((TaxonDescription
) object
).getTaxon().removeDescription((TaxonDescription
) object
);
117 } else if(object
instanceof TaxonNameDescription
){
118 operation
= new DeleteTaxonNameDescriptionOperation(label
, undoContext
, (TaxonNameDescription
) object
, factualDataPart
, cdmEntitySessionEnabled
);
120 ((TaxonNameDescription
) object
).getTaxonName().removeDescription((TaxonNameDescription
) object
);
122 } else if(object
instanceof SpecimenDescription
){
123 operation
= new DeleteSpecimenDescriptionOperation(label
, undoContext
, (SpecimenDescription
) object
, factualDataPart
, cdmEntitySessionEnabled
);
124 specimen
= ((SpecimenDescription
) object
).getDescribedSpecimenOrObservation();
125 specimen
.removeDescription((SpecimenDescription
) object
);
127 } else if(object
instanceof DescriptionElementBase
){
128 // DescriptionElementBase
129 operation
= new DeleteDescriptionElementOperation(label
, undoContext
, (DescriptionElementBase
) object
, factualDataPart
, cdmEntitySessionEnabled
);
130 ((DescriptionElementBase
) object
).getInDescription().removeElement((DescriptionElementBase
) object
);
131 } else if(object
instanceof FeatureNodeContainer
){
132 List
<DescriptionElementBase
> descriptions
= ((FeatureNodeContainer
) object
).getDescriptionElementsForEntireBranch();
134 for(DescriptionElementBase descriptionElement
: descriptions
){
135 operation
= new DeleteDescriptionElementOperation(label
, undoContext
, descriptionElement
, factualDataPart
, cdmEntitySessionEnabled
);
137 ((FeatureNodeContainer
)object
).getContainerTree().removeContainer((FeatureNodeContainer
)object
);
143 if (factualDataPart
.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4
){
144 ((TaxonNameEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(operation
);
145 ((TaxonNameEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).setDirty();
146 ((TaxonNameEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).redraw();
148 if (factualDataPart
.getSelectionProvidingPart().getObject() instanceof DerivateView
){
149 ((DerivateView
)factualDataPart
.getSelectionProvidingPart().getObject()).addOperation(operation
);
150 ((DerivateView
)factualDataPart
.getSelectionProvidingPart().getObject()).setDirty(true);
152 if (object
instanceof DescriptionBase
){
153 factualDataPart
.getViewer().refresh();
154 factualDataPart
.collapse();
155 } else if (object
instanceof DescriptionElementBase
){
156 if (description
!= null){
157 factualDataPart
.changed(description
);
161 if (factualDataPart
.getSelectionProvidingPart().getObject() instanceof BulkEditorE4
){
162 ((BulkEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).addOperation(operation
);
163 ((BulkEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).setDirty(true);
164 if (object
instanceof SpecimenDescription
){
165 ((BulkEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).changed(((SpecimenDescription
)object
).getDescribedSpecimenOrObservation());
166 }else if (object
instanceof TaxonDescription
){
167 ((BulkEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).changed(((TaxonDescription
)object
).getTaxon());
168 }else if (object
instanceof TaxonNameDescription
){
169 ((BulkEditorE4
)factualDataPart
.getSelectionProvidingPart().getObject()).changed(((TaxonNameDescription
)object
).getTaxonName());
174 if (expandedElements
.length
>0){
175 for (Object o
: expandedElements
){
176 if (o
instanceof FeatureNodeContainer
&& object
instanceof DescriptionElementBase
){
177 FeatureNodeContainer container
= (FeatureNodeContainer
)o
;
178 if (container
.getDescriptionElements().contains(object
)){
179 container
.getDescriptionElements().remove(object
);
185 viewer
.setExpandedElements(expandedElements
);
189 Exception e
= result
.getExceptions() != null && !result
.getExceptions().isEmpty()? result
.getExceptions().iterator().next() : null;
191 MessagingUtils
.informationDialog("Delete not possible", e
.getMessage());
204 public boolean canExecute(@Optional @Named(IServiceConstants
.ACTIVE_PART
) MPart activePart
,
205 MHandledMenuItem menuItem
){
206 FactualDataPartE4 factualDataPart
= (FactualDataPartE4
) activePart
.getObject();
207 ISelection selection
= factualDataPart
.getViewer().getSelection();
208 boolean canExecute
= true;
209 if (selection
instanceof IStructuredSelection
&& !selection
.isEmpty()){
210 IStructuredSelection sel
= (IStructuredSelection
) selection
;
211 if (sel
.getFirstElement() instanceof FeatureNodeContainer
){
216 canExecute
&= selection
instanceof IStructuredSelection
&& !selection
.isEmpty() && !(((IStructuredSelection
)selection
).getFirstElement() instanceof FeatureNodeContainer
);
218 menuItem
.setVisible(canExecute
);