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
.jface
.viewers
.IStructuredSelection
;
21 import org
.eclipse
.jface
.viewers
.TreePath
;
22 import org
.eclipse
.jface
.viewers
.TreeSelection
;
23 import org
.eclipse
.ui
.IWorkbenchPart
;
24 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
26 import eu
.etaxonomy
.cdm
.api
.service
.config
.MediaDeletionConfigurator
;
27 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionBase
;
28 import eu
.etaxonomy
.cdm
.model
.description
.DescriptionElementBase
;
29 import eu
.etaxonomy
.cdm
.model
.description
.SpecimenDescription
;
30 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
31 import eu
.etaxonomy
.cdm
.model
.media
.Media
;
32 import eu
.etaxonomy
.taxeditor
.editor
.EditorUtil
;
33 import eu
.etaxonomy
.taxeditor
.editor
.l10n
.Messages
;
34 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteDescriptionElementOperation
;
35 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteSpecimenDescriptionOperation
;
36 import eu
.etaxonomy
.taxeditor
.editor
.view
.descriptive
.operation
.DeleteTaxonDescriptionOperation
;
37 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.operation
.DeleteMediaOperation
;
38 import eu
.etaxonomy
.taxeditor
.editor
.view
.media
.operation
.RemoveImageFromDescriptionElementOperation
;
39 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
40 import eu
.etaxonomy
.taxeditor
.model
.FeatureNodeContainer
;
41 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
42 import eu
.etaxonomy
.taxeditor
.operation
.AbstractPostOperation
;
43 import eu
.etaxonomy
.taxeditor
.operation
.IPostOperationEnabled
;
44 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionEnabled
;
45 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.configurator
.deleteConfigurator
.DeleteConfiguratorDialog
;
48 * <p>DeleteDescriptionHandler class.</p>
51 * @created Jun 22, 2010
54 public class DeleteHandler
extends AbstractHandler
{
57 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
61 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
62 IStructuredSelection selection
= (IStructuredSelection
) HandlerUtil
.getActiveSite(event
).getSelectionProvider().getSelection();
64 IWorkbenchPart part
= HandlerUtil
.getActivePart(event
);
65 IPostOperationEnabled postOperationEnabled
= (part
instanceof IPostOperationEnabled
) ?
(IPostOperationEnabled
) part
: null;
66 ICdmEntitySessionEnabled cdmEntitySessionEnabled
= (part
instanceof ICdmEntitySessionEnabled
) ?
(ICdmEntitySessionEnabled
) part
: null;
69 String label
= event
.getCommand().getName();
71 IUndoContext undoContext
= EditorUtil
.getUndoContext();
73 List
<AbstractPostOperation
<?
>> operations
= new ArrayList
<AbstractPostOperation
<?
>>();
75 for(Object object
: selection
.toArray()){
78 if(object
instanceof TaxonDescription
){
79 operations
.add(new DeleteTaxonDescriptionOperation(label
, undoContext
, (TaxonDescription
) object
, postOperationEnabled
, cdmEntitySessionEnabled
));
81 else if(object
instanceof SpecimenDescription
){
82 operations
.add(new DeleteSpecimenDescriptionOperation(label
, undoContext
, (SpecimenDescription
) object
, postOperationEnabled
, cdmEntitySessionEnabled
)) ;
84 // DescriptionElementBase
85 else if(object
instanceof DescriptionElementBase
){
86 operations
.add(new DeleteDescriptionElementOperation(label
, undoContext
, (DescriptionElementBase
) object
, postOperationEnabled
, cdmEntitySessionEnabled
));
88 else if(object
instanceof FeatureNodeContainer
){
89 List
<DescriptionElementBase
> descriptions
= ((FeatureNodeContainer
) object
).getDescriptionElementsForEntireBranch();
91 for(DescriptionElementBase description
: descriptions
){
92 operations
.add(new DeleteDescriptionElementOperation(label
, undoContext
, description
, postOperationEnabled
, cdmEntitySessionEnabled
));
96 else if(object
instanceof Media
){
97 TreeSelection treeSelection
= (TreeSelection
) selection
;
99 TreePath
[] path
= treeSelection
.getPathsFor(object
);
101 DescriptionBase
<?
> imageGallery
= (DescriptionBase
<?
>) path
[0].getFirstSegment();
103 // TODO use undo context specific to editor
104 MediaDeletionConfigurator config
= new MediaDeletionConfigurator();
105 config
.setDeleteIfUsedInTaxonDescription(true);
106 config
.setOnlyRemoveFromGallery(false);
108 if (!DeleteConfiguratorDialog
.openConfirmWithConfigurator(config
, HandlerUtil
.getActiveShell(event
), Messages
.DeleteHandler_CONFIRM_DELETION
, Messages
.DeleteHandler_CONFIRM_DELETION_MESSAGE
)){
114 if (config
.isOnlyRemoveFromGallery() || ((Media
)object
).getId() == 0){
115 operations
.add(new RemoveImageFromDescriptionElementOperation(label
, undoContext
, (Media
) object
, imageGallery
, postOperationEnabled
));
117 operations
.add(new DeleteMediaOperation(label
, undoContext
, imageGallery
, (Media
) object
, config
, postOperationEnabled
));
124 MessagingUtils
.error(getClass(), Messages
.DeleteHandler_INVALID_SELECTION
, null);
128 // execute all cumulated operations
129 for(AbstractPostOperation
<?
> operation
: operations
){
130 AbstractUtility
.executeOperation(operation
);
133 } catch (NotDefinedException e
) {
134 MessagingUtils
.warn(getClass(), "Command name not set."); //$NON-NLS-1$