Project

General

Profile

Download (7.35 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
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.
8
*/
9

    
10
package eu.etaxonomy.taxeditor.editor.view.descriptive.e4.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.Iterator;
14
import java.util.List;
15

    
16
import javax.inject.Named;
17

    
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.TreePath;
30
import org.eclipse.jface.viewers.TreeSelection;
31
import org.eclipse.swt.widgets.Shell;
32

    
33
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
34
import eu.etaxonomy.cdm.model.description.DescriptionBase;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
36
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
37
import eu.etaxonomy.cdm.model.description.TaxonDescription;
38
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
39
import eu.etaxonomy.cdm.model.media.Media;
40
import eu.etaxonomy.taxeditor.editor.EditorUtil;
41
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
42
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
43
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteDescriptionElementOperation;
44
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
45
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
46
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
47
import eu.etaxonomy.taxeditor.editor.view.media.operation.RemoveImageFromDescriptionElementOperation;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
50
import eu.etaxonomy.taxeditor.model.MessagingUtils;
51
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
52
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
53
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
54

    
55
/**
56
 *
57
 * @author pplitzner
58
 * @date 15.08.2017
59
 *
60
 */
61
public class DeleteHandlerE4 {
62

    
63
	@Execute
64
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
65
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell, MMenuItem menuItem,
66
            UISynchronize sync) {
67

    
68
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
69
        IStructuredSelection selection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
70

    
71
        Object selectionProvidingPart = factualDataPart.getSelectionProvidingPart();
72
        ICdmEntitySessionEnabled cdmEntitySessionEnabled = null;
73
        if(selectionProvidingPart instanceof ICdmEntitySessionEnabled){
74
            cdmEntitySessionEnabled = (ICdmEntitySessionEnabled) selectionProvidingPart;
75
        }
76

    
77
		String label = menuItem.getLocalizedLabel();
78

    
79
        IUndoContext undoContext = EditorUtil.getUndoContext();
80

    
81
        List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
82

    
83
        for(Iterator iter = selection.iterator();iter.hasNext();){
84
            Object object = iter.next();
85

    
86
            // TaxonDescription
87
            if(object instanceof TaxonDescription){
88
                operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) object, factualDataPart, cdmEntitySessionEnabled));
89
            }
90
            else if(object instanceof SpecimenDescription){
91
                operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) object, factualDataPart, cdmEntitySessionEnabled)) ;
92
            }
93
            // DescriptionElementBase
94
            else if(object instanceof DescriptionElementBase){
95
                operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) object, factualDataPart, cdmEntitySessionEnabled));
96
            }
97
            else if(object instanceof FeatureNodeContainer){
98
                List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) object).getDescriptionElementsForEntireBranch();
99

    
100
                for(DescriptionElementBase description : descriptions){
101
                    operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, factualDataPart, cdmEntitySessionEnabled));
102
                }
103
            }
104
            // Media
105
            else if(object instanceof Media){
106

    
107
                TreeSelection treeSelection = (TreeSelection) factualDataPart.getViewer().getSelection();
108

    
109
                TreePath[] path = treeSelection.getPathsFor(object);
110

    
111
                DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
112

    
113
                MediaDeletionConfigurator config = new MediaDeletionConfigurator();
114
                config.setDeleteFromDescription(true);
115
                config.setOnlyRemoveFromGallery(false);
116

    
117
                if (imageGallery instanceof SpecimenDescription){
118
                    config.setDeleteFrom(((SpecimenDescription)imageGallery).getDescribedSpecimenOrObservation());
119
                } else if (imageGallery instanceof TaxonDescription){
120
                    config.setDeleteFrom(((TaxonDescription)imageGallery).getTaxon());
121
                }else if (imageGallery instanceof TaxonNameDescription){
122
                    config.setDeleteFrom(((TaxonNameDescription)imageGallery).getTaxonName());
123
                }
124
                if (!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, Messages.DeleteHandler_CONFIRM_DELETION,  Messages.DeleteHandler_CONFIRM_DELETION_MESSAGE)){
125
                    return;
126
                }
127
                if (config.isOnlyRemoveFromGallery() || ((Media)object).getId() == 0){
128
                    operations.add(new RemoveImageFromDescriptionElementOperation(label, undoContext,  (Media) object, imageGallery, factualDataPart));
129
                }else{
130
                    operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) object, config, factualDataPart));
131
                }
132
            }
133
            else{
134
                MessagingUtils.error(getClass(), Messages.DeleteHandler_INVALID_SELECTION, null);
135
            }
136
        }
137

    
138
        // execute all cumulated operations
139
        for(AbstractPostOperation<?> operation : operations){
140
        	AbstractUtility.executeOperation(operation, sync);
141
        }
142
	}
143

    
144

    
145
    @CanExecute
146
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
147
            MHandledMenuItem menuItem){
148
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
149
        ISelection selection = factualDataPart.getViewer().getSelection();
150
        boolean canExecute = selection instanceof IStructuredSelection && !selection.isEmpty();
151
        menuItem.setVisible(canExecute);
152
        return canExecute;
153
    }
154
}
(3-3/8)