Project

General

Profile

Download (7.01 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.List;
14

    
15
import javax.inject.Named;
16

    
17
import org.eclipse.core.commands.operations.IUndoContext;
18
import org.eclipse.e4.core.di.annotations.CanExecute;
19
import org.eclipse.e4.core.di.annotations.Execute;
20
import org.eclipse.e4.core.di.annotations.Optional;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.TreePath;
27
import org.eclipse.jface.viewers.TreeSelection;
28
import org.eclipse.swt.widgets.Shell;
29

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

    
52
/**
53
 *
54
 * @author pplitzner
55
 * @date 15.08.2017
56
 *
57
 */
58
public class DeleteHandlerE4 {
59

    
60
	@Execute
61
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
62
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) Object selection,
63
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell, MMenuItem menuItem) {
64

    
65
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
66

    
67
        Object selectionProvidingPart = factualDataPart.getSelectionProvidingPart();
68
        ICdmEntitySessionEnabled cdmEntitySessionEnabled = null;
69
        if(selectionProvidingPart instanceof ICdmEntitySessionEnabled){
70
            cdmEntitySessionEnabled = (ICdmEntitySessionEnabled) selectionProvidingPart;
71
        }
72

    
73
		String label = menuItem.getLocalizedLabel();
74

    
75
        IUndoContext undoContext = EditorUtil.getUndoContext();
76

    
77
        List<AbstractPostOperation<?>> operations = new ArrayList<AbstractPostOperation<?>>();
78

    
79
//			for(Object object : selection.toArray()){
80

    
81
        // TaxonDescription
82
        if(selection instanceof TaxonDescription){
83
            operations.add(new DeleteTaxonDescriptionOperation(label, undoContext, (TaxonDescription) selection, factualDataPart, cdmEntitySessionEnabled));
84
        }
85
        else if(selection instanceof SpecimenDescription){
86
            operations.add(new DeleteSpecimenDescriptionOperation(label, undoContext, (SpecimenDescription) selection, factualDataPart, cdmEntitySessionEnabled)) ;
87
        }
88
        // DescriptionElementBase
89
        else if(selection instanceof DescriptionElementBase){
90
            operations.add(new DeleteDescriptionElementOperation(label, undoContext, (DescriptionElementBase) selection, factualDataPart, cdmEntitySessionEnabled));
91
        }
92
        else if(selection instanceof FeatureNodeContainer){
93
            List<DescriptionElementBase> descriptions = ((FeatureNodeContainer) selection).getDescriptionElementsForEntireBranch();
94

    
95
            for(DescriptionElementBase description : descriptions){
96
                operations.add(new DeleteDescriptionElementOperation(label, undoContext, description, factualDataPart, cdmEntitySessionEnabled));
97
            }
98
        }
99
        // Media
100
        else if(selection instanceof Media){
101

    
102
            TreeSelection treeSelection = (TreeSelection) factualDataPart.getViewer().getSelection();
103

    
104
            TreePath[] path = treeSelection.getPathsFor(selection);
105

    
106
            DescriptionBase<?> imageGallery = (DescriptionBase<?>) path[0].getFirstSegment();
107

    
108
            // TODO use undo context specific to editor
109
            MediaDeletionConfigurator config = new MediaDeletionConfigurator();
110
            config.setDeleteFromDescription(true);
111
            config.setOnlyRemoveFromGallery(false);
112

    
113
            if (imageGallery instanceof SpecimenDescription){
114
                config.setDeleteFrom(((SpecimenDescription)imageGallery).getDescribedSpecimenOrObservation());
115
            } else if (imageGallery instanceof TaxonDescription){
116
                config.setDeleteFrom(((TaxonDescription)imageGallery).getTaxon());
117
            }else if (imageGallery instanceof TaxonNameDescription){
118
                config.setDeleteFrom(((TaxonNameDescription)imageGallery).getTaxonName());
119
            }
120

    
121

    
122
            if (!DeleteConfiguratorDialog.openConfirmWithConfigurator(config, shell, Messages.DeleteHandler_CONFIRM_DELETION,  Messages.DeleteHandler_CONFIRM_DELETION_MESSAGE)){
123
                return;
124
            }
125

    
126

    
127

    
128
            if (config.isOnlyRemoveFromGallery() || ((Media)selection).getId() == 0){
129
                operations.add(new RemoveImageFromDescriptionElementOperation(label, undoContext,  (Media) selection, imageGallery, factualDataPart));
130
            }else{
131
                operations.add(new DeleteMediaOperation(label, undoContext, imageGallery, (Media) selection, config, factualDataPart));
132
            }
133

    
134

    
135

    
136
        }
137
        else{
138
            MessagingUtils.error(getClass(), Messages.DeleteHandler_INVALID_SELECTION, null);
139
        }
140
//			}
141

    
142
        // execute all cumulated operations
143
        for(AbstractPostOperation<?> operation : operations){
144
        	AbstractUtility.executeOperation(operation);
145
        }
146
	}
147

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