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
.handler
;
12 import java
.util
.ArrayList
;
13 import java
.util
.List
;
15 import org
.eclipse
.core
.commands
.AbstractHandler
;
16 import org
.eclipse
.core
.commands
.ExecutionEvent
;
17 import org
.eclipse
.core
.commands
.ExecutionException
;
18 import org
.eclipse
.core
.commands
.common
.NotDefinedException
;
19 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
23 import org
.eclipse
.jface
.viewers
.TreePath
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.ui
.IWorkbenchPart
;
26 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
28 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
29 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
30 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
31 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
32 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
33 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
34 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteDescriptionElementOperation
;
36 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteSpecimenDescriptionOperation
;
37 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteTaxonDescriptionOperation
;
38 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.operation
.DeleteMediaOperation
;
39 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.operation
.RemoveImageFromDescriptionElementOperation
;
40 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
41 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
44 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
45 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
46 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.deleteConfigurator
.DeleteConfiguratorDialog
;
49 * <p>DeleteDescriptionHandler class.</p>
52 * @created Jun 22, 2010
55 public class DeleteHandler
extends AbstractHandler
{
58 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
62 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
63 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
.getActiveSite(event
).getSelectionProvider().getSelection();
65 IWorkbenchPart part
= HandlerUtil
.getActivePart(event
);
66 IPostOperationEnabled postOperationEnabled
= (part
instanceof IPostOperationEnabled
) ?
(IPostOperationEnabled
) part
: null;
67 ICdmEntitySessionEnabled cdmEntitySessionEnabled
= (part
instanceof ICdmEntitySessionEnabled
) ?
(ICdmEntitySessionEnabled
) part
: null;
70 String label
= event
.getCommand().getName();
72 IUndoContext undoContext
= EditorUtil
.getUndoContext();
74 List
<AbstractPostOperation
<?
>> operations
= new ArrayList
<AbstractPostOperation
<?
>>();
76 for(Object object
: selection
.toArray()){
79 if(object
instanceof TaxonDescription
){
80 operations
.add(new DeleteTaxonDescriptionOperation(label
, undoContext
, (TaxonDescription
) object
, postOperationEnabled
, cdmEntitySessionEnabled
));
82 else if(object
instanceof SpecimenDescription
){
83 operations
.add(new DeleteSpecimenDescriptionOperation(label
, undoContext
, (SpecimenDescription
) object
, postOperationEnabled
, cdmEntitySessionEnabled
)) ;
85 // DescriptionElementBase
86 else if(object
instanceof DescriptionElementBase
){
87 operations
.add(new DeleteDescriptionElementOperation(label
, undoContext
, (DescriptionElementBase
) object
, postOperationEnabled
, cdmEntitySessionEnabled
));
89 else if(object
instanceof FeatureNodeContainer
){
90 List
<DescriptionElementBase
> descriptions
= ((FeatureNodeContainer
) object
).getDescriptionElementsForEntireBranch();
92 for(DescriptionElementBase description
: descriptions
){
93 operations
.add(new DeleteDescriptionElementOperation(label
, undoContext
, description
, postOperationEnabled
, cdmEntitySessionEnabled
));
97 else if(object
instanceof Media
){
98 TreeSelection treeSelection
= (TreeSelection
) selection
;
100 TreePath
[] path
= treeSelection
.getPathsFor(object
);
102 DescriptionBase
<?
> imageGallery
= (DescriptionBase
<?
>) path
[0].getFirstSegment();
104 // TODO use undo context specific to editor
105 MediaDeletionConfigurator config
= new MediaDeletionConfigurator();
107 DeleteConfiguratorDialog dialog
;
108 dialog
= new DeleteConfiguratorDialog(config
, HandlerUtil
.getActiveShell(event
), "Confirm Deletion", null, "Do you really want to delete the media?", MessageDialog
.WARNING
, new String
[] { "Delete", "Skip" }, 0);
109 int result_dialog
= dialog
.open();
110 if (result_dialog
!= Status
.OK
){
116 if (config
.isOnlyRemoveFromGallery()){
117 operations
.add(new RemoveImageFromDescriptionElementOperation(label
, undoContext
, (Media
) object
, imageGallery
, postOperationEnabled
));
119 operations
.add(new DeleteMediaOperation(label
, undoContext
, imageGallery
, (Media
) object
, config
, postOperationEnabled
));
126 MessagingUtils
.error(getClass(), "Selection is not valid for this delete handler", null);
130 // execute all cumulated operations
131 for(AbstractPostOperation
<?
> operation
: operations
){
132 AbstractUtility
.executeOperation(operation
);
135 } catch (NotDefinedException e
) {
136 MessagingUtils
.warn(getClass(), "Command name not set.");