Project

General

Profile

Download (7.02 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.editor.view.media.handler;
5

    
6
import javax.inject.Named;
7

    
8
import org.eclipse.core.runtime.IStatus;
9
import org.eclipse.e4.core.di.annotations.Execute;
10
import org.eclipse.e4.ui.di.UISynchronize;
11
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
12
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
13
import org.eclipse.e4.ui.services.IServiceConstants;
14
import org.eclipse.jface.dialogs.MessageDialog;
15
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.TreePath;
17
import org.eclipse.jface.viewers.TreeSelection;
18
import org.eclipse.swt.widgets.Shell;
19
import org.eclipse.ui.IEditorInput;
20
import org.eclipse.ui.IEditorPart;
21
import org.eclipse.ui.forms.editor.FormEditor;
22

    
23
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
24
import eu.etaxonomy.cdm.model.description.DescriptionBase;
25
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
26
import eu.etaxonomy.cdm.model.description.TaxonDescription;
27
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
28
import eu.etaxonomy.cdm.model.media.Media;
29
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
30
import eu.etaxonomy.taxeditor.editor.EditorUtil;
31
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
33
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
34
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
35
import eu.etaxonomy.taxeditor.model.AbstractUtility;
36
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
37
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
38
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
39

    
40
/**
41
 *
42
 * @author pplitzner
43
 * @since Aug 15, 2017
44
 *
45
 */
46
public class DeleteMediaHandler  {
47

    
48
    @Execute
49
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
50
            @Named(IServiceConstants.ACTIVE_SELECTION) Media media,
51
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
52
            MMenuItem menuItem,
53
            UISynchronize sync) {
54

    
55
        MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
56

    
57

    
58
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
59
        if (e4WrappedPart instanceof FormEditor) {
60
        	IEditorPart editor = null;
61
            editor = (FormEditor) e4WrappedPart;
62
            IEditorInput input = editor.getEditorInput();
63
            if (input instanceof CdmEntitySessionInput) {
64
                ISelection selection = mediaView.getViewer().getSelection();
65
                if (selection instanceof TreeSelection) {
66
                    TreePath[] paths = ((TreeSelection) selection).getPaths();
67
                    int count = paths[0].getSegmentCount();
68
                    DescriptionBase description = null;
69
                    for (int i = 0; i < count; i++ ) {
70
                        if (paths[0].getSegment(i) instanceof DescriptionBase) {
71
                            description = (DescriptionBase) paths[0].getSegment(i);
72
                            break;
73
                        }
74
                    }
75
                    // TODO use undo context specific to editor
76
                    MediaDeletionConfigurator config = new MediaDeletionConfigurator();
77

    
78
                    config.setDeleteFromDescription(true);
79
                    config.setOnlyRemoveFromGallery(false);
80

    
81
                    if (description instanceof SpecimenDescription){
82
                    	config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
83
                    } else if (description instanceof TaxonDescription){
84
                    	config.setDeleteFrom(((TaxonDescription)description).getTaxon());
85
                    }else if (description instanceof TaxonNameDescription){
86
                    	config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
87
                    }
88

    
89

    
90
                    DeleteConfiguratorDialog dialog;
91
                    dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
92
                    int result_dialog= dialog.open();
93
                    if (result_dialog != IStatus.OK){
94
                        return;
95
                    }
96
                    AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
97
                    AbstractUtility.executeOperation(operation, sync);
98

    
99
                }
100
            }
101
        } else if (e4WrappedPart instanceof DerivateView){
102
        	 DerivateView view = (DerivateView) e4WrappedPart;
103

    
104
                 ISelection selection = mediaView.getViewer().getSelection();
105
                 if (selection instanceof TreeSelection) {
106
                     TreePath[] paths = ((TreeSelection) selection).getPaths();
107
                     int count = paths[0].getSegmentCount();
108
                     DescriptionBase description = null;
109
                     for (int i = 0; i < count; i++ ) {
110
                         if (paths[0].getSegment(i) instanceof DescriptionBase) {
111
                             description = (DescriptionBase) paths[0].getSegment(i);
112
                             break;
113
                         }
114
                     }
115
                     // TODO use undo context specific to editor
116
                     MediaDeletionConfigurator config = new MediaDeletionConfigurator();
117

    
118
                     config.setDeleteFromDescription(true);
119
                     config.setOnlyRemoveFromGallery(false);
120

    
121
                     if (description instanceof SpecimenDescription){
122
                     	config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
123
                     } else if (description instanceof TaxonDescription){
124
                     	config.setDeleteFrom(((TaxonDescription)description).getTaxon());
125
                     }else if (description instanceof TaxonNameDescription){
126
                     	config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
127
                     }
128

    
129

    
130
                     DeleteConfiguratorDialog dialog;
131
                     dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM,  null,  Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
132
                     int result_dialog= dialog.open();
133
                     if (result_dialog != IStatus.OK){
134
                         return;
135
                     }
136
                     AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
137
                     AbstractUtility.executeOperation(operation, sync);
138

    
139
                 }
140
             }
141
        }
142

    
143

    
144
}
(4-4/6)