Project

General

Profile

Download (11.3 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.media.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Set;
17

    
18
import javax.inject.Named;
19

    
20
import org.eclipse.core.runtime.IStatus;
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.core.di.annotations.Execute;
23
import org.eclipse.e4.ui.di.UISynchronize;
24
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
25
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
26
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
27
import org.eclipse.e4.ui.services.IServiceConstants;
28
import org.eclipse.jface.dialogs.MessageDialog;
29
import org.eclipse.jface.viewers.ISelection;
30
import org.eclipse.jface.viewers.TreePath;
31
import org.eclipse.jface.viewers.TreeSelection;
32
import org.eclipse.swt.widgets.Shell;
33

    
34
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
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.media.Media;
41
import eu.etaxonomy.taxeditor.editor.EditorUtil;
42
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
43
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditor;
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.e4.MediaViewPartE4;
47
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
48
import eu.etaxonomy.taxeditor.model.AbstractUtility;
49
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
50
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
51

    
52
/**
53
 * @author pplitzner
54
 * @since Aug 15, 2017
55
 */
56
public class DeleteMediaHandler  {
57

    
58
    @Execute
59
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
60
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
61
            MMenuItem menuItem,
62
            UISynchronize sync) {
63

    
64
        MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
65
        TreeSelection selection = (TreeSelection) mediaView.getViewer().getSelection();
66

    
67
        for(Iterator<?> iter = selection.iterator();iter.hasNext();){
68
            Object object = iter.next();
69
            TreePath[] paths = selection.getPaths();
70
            int count = paths[0].getSegmentCount();
71
            DescriptionBase<?> description = null;
72
            for (int i = 0; i < count; i++ ) {
73
                if (paths[0].getSegment(i) instanceof DescriptionBase) {
74
                    description = (DescriptionBase<?>) paths[0].getSegment(i);
75
                    break;
76
                }
77
            }
78

    
79
            if(object instanceof Media){
80
                // TODO use undo context specific to editor
81
                if (!((Media)object).isPersited()){
82
                    Set<DescriptionElementBase> elements = description.getElements();
83
                    if (elements.size() == 1){
84
                          DescriptionElementBase element = elements.iterator().next();
85
                          Set<Media> toDelete = new HashSet<>();
86
                          element.removeMedia((Media)object);
87
                          ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).redraw();
88
                          return;
89

    
90
                    }
91
                }
92
                MediaDeletionConfigurator config = fillDeleteConfigurator(shell, description, Messages.DeleteMediaHandler_CONFIRM_MESSAGE_MEDIA);
93
                if (config == null){
94
                    return;
95
                }
96
                List<Media> mediaList = new ArrayList<>();
97
                mediaList.add((Media)object);
98
                createMediaDeleteOperation(menuItem.getLocalizedLabel(), sync, mediaView, mediaList, description, config);
99
            }
100

    
101
            if(object instanceof DescriptionBase && ((DescriptionBase<?>) object).isImageGallery()){
102
                if (!((DescriptionBase<?>)object).getElements().isEmpty()){
103
                    Set<DescriptionElementBase> elements = description.getElements();
104
                    boolean containsPersistedMedia = false;
105
                    DescriptionElementBase element = null;
106
                    List<Media> mediaList = new ArrayList<>();
107

    
108
                    if (elements.size() == 1){
109
                        element = elements.iterator().next();
110
//                        containsPersistedMedia = element.getMedia().isEmpty();
111
                        if (element.getMedia() != null && !element.getMedia().isEmpty()){
112
                            for (Media media: element.getMedia()){
113
                                containsPersistedMedia |= media.isPersited();
114
                                if (media.isPersited()){
115
                                    mediaList.add(media);
116
                                }
117
                            }
118
                        }
119
                    }
120
                    if (containsPersistedMedia){
121
                        MediaDeletionConfigurator config = fillDeleteConfigurator(shell, description, Messages.DeleteMediaHandler_CONFIRM_MESSAGE_DESCRIPTION);
122

    
123
                        if (config == null){
124
                            return;
125
                        }
126
                        if (element != null){
127
                            createMediaDeleteOperation(menuItem.getLocalizedLabel(), sync, mediaView, mediaList, description, config);
128
                        }
129
                    }
130

    
131
                }
132

    
133
                if(object instanceof TaxonDescription){
134

    
135
                    DeleteTaxonDescriptionOperation deleteTaxonDescriptionOperation = new DeleteTaxonDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (TaxonDescription)object, mediaView, null);
136
                    if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditor){
137
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).getTaxon().removeDescription((TaxonDescription)description);
138
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(deleteTaxonDescriptionOperation);
139
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).setDirty();
140
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).redraw();
141

    
142
                     }else{
143
                         AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
144
                     }
145
                }
146
                else if(object instanceof SpecimenDescription){
147
                    DeleteSpecimenDescriptionOperation deleteTaxonDescriptionOperation = new DeleteSpecimenDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (SpecimenDescription)object, mediaView, null);
148
                    if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditor){
149
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(deleteTaxonDescriptionOperation);
150
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).setDirty();
151
                        ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).redraw();
152
                     }else{
153
                         AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
154
                     }
155
                }
156
            }
157
        }
158
    }
159

    
160
    public void createMediaDeleteOperation(String menuLabel, UISynchronize sync, MediaViewPartE4 mediaView,
161
            List<Media> media, DescriptionBase<?> description, MediaDeletionConfigurator config) {
162
        AbstractPostOperation<?> operation = new DeleteMediaOperation(menuLabel, EditorUtil.getUndoContext(), description, media, config, mediaView);
163
        if (mediaView.getSelectionProvidingPart().getObject() instanceof TaxonNameEditor){
164
            if (!config.isOnlyRemoveFromGallery()){
165
                ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).getEditorInput().addOperation(operation);
166
            }
167

    
168
            if (description.isImageGallery()){
169
                Set<DescriptionElementBase> elements = description.getElements();
170
                if (elements.size() == 1){
171
                      DescriptionElementBase element = elements.iterator().next();
172
                      Set<Media> toDelete = new HashSet<>();
173
                      for (Media mediaElement:media){
174
                          toDelete.add(mediaElement);
175
                      }
176
                      for (Media mediaElement:toDelete){
177
                          element.removeMedia(mediaElement);
178
                      }
179
                }
180
            }
181

    
182
          ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).setDirty();
183
          ((TaxonNameEditor)mediaView.getSelectionProvidingPart().getObject()).redraw();
184
        }else{
185
            AbstractUtility.executeOperation(operation, sync);
186
        }
187
    }
188

    
189
    public MediaDeletionConfigurator fillDeleteConfigurator(Shell shell, DescriptionBase<?> description, String message) {
190
        MediaDeletionConfigurator config = new MediaDeletionConfigurator();
191

    
192
        config.setDeleteFromDescription(true);
193
        config.setOnlyRemoveFromGallery(false);
194

    
195
        if (description instanceof SpecimenDescription){
196
            config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
197
        } else if (description instanceof TaxonDescription){
198
            config.setDeleteFrom(((TaxonDescription)description).getTaxon());
199
        }else if (description instanceof TaxonNameDescription){
200
            config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
201
        }
202
        DeleteConfiguratorDialog dialog;
203
        dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  message, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
204
        int result_dialog= dialog.open();
205
        if (result_dialog != IStatus.OK){
206
            return null;
207
        }
208
        return config;
209
    }
210

    
211
    @CanExecute
212
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
213
            MHandledMenuItem menuItem){
214
        MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
215
        ISelection selection = mediaView.getViewer().getSelection();
216

    
217
        boolean canExecute = selection instanceof TreeSelection && !selection.isEmpty();
218
        for(Iterator<?> iter = ((TreeSelection) selection).iterator();iter.hasNext();){
219
            Object object = iter.next();
220
            canExecute &= object instanceof Media
221
                    || (object instanceof DescriptionBase && ((DescriptionBase<?>) object).isImageGallery());
222
        }
223
        menuItem.setVisible(canExecute);
224
        return canExecute;
225
    }
226

    
227

    
228

    
229
}
(5-5/7)