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.model.application.ui.basic.MPart;
|
11
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
|
12
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
13
|
import org.eclipse.jface.dialogs.MessageDialog;
|
14
|
import org.eclipse.jface.viewers.ISelection;
|
15
|
import org.eclipse.jface.viewers.TreePath;
|
16
|
import org.eclipse.jface.viewers.TreeSelection;
|
17
|
import org.eclipse.swt.widgets.Shell;
|
18
|
import org.eclipse.ui.IEditorInput;
|
19
|
import org.eclipse.ui.IEditorPart;
|
20
|
import org.eclipse.ui.forms.editor.FormEditor;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
24
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
25
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
26
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
27
|
import eu.etaxonomy.cdm.model.media.Media;
|
28
|
import eu.etaxonomy.taxeditor.editor.CdmEntitySessionInput;
|
29
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
30
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
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
|
|
54
|
MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
|
55
|
|
56
|
|
57
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
|
58
|
if (e4WrappedPart instanceof FormEditor) {
|
59
|
IEditorPart editor = null;
|
60
|
editor = (FormEditor) e4WrappedPart;
|
61
|
IEditorInput input = editor.getEditorInput();
|
62
|
if (input instanceof CdmEntitySessionInput) {
|
63
|
ISelection selection = mediaView.getViewer().getSelection();
|
64
|
if (selection instanceof TreeSelection) {
|
65
|
TreePath[] paths = ((TreeSelection) selection).getPaths();
|
66
|
int count = paths[0].getSegmentCount();
|
67
|
DescriptionBase description = null;
|
68
|
for (int i = 0; i < count; i++ ) {
|
69
|
if (paths[0].getSegment(i) instanceof DescriptionBase) {
|
70
|
description = (DescriptionBase) paths[0].getSegment(i);
|
71
|
break;
|
72
|
}
|
73
|
}
|
74
|
// TODO use undo context specific to editor
|
75
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
76
|
|
77
|
config.setDeleteFromDescription(true);
|
78
|
config.setOnlyRemoveFromGallery(false);
|
79
|
|
80
|
if (description instanceof SpecimenDescription){
|
81
|
config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
|
82
|
} else if (description instanceof TaxonDescription){
|
83
|
config.setDeleteFrom(((TaxonDescription)description).getTaxon());
|
84
|
}else if (description instanceof TaxonNameDescription){
|
85
|
config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
|
86
|
}
|
87
|
|
88
|
|
89
|
DeleteConfiguratorDialog dialog;
|
90
|
dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM, null, Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
|
91
|
int result_dialog= dialog.open();
|
92
|
if (result_dialog != IStatus.OK){
|
93
|
return;
|
94
|
}
|
95
|
AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
|
96
|
AbstractUtility.executeOperation(operation);
|
97
|
|
98
|
}
|
99
|
}
|
100
|
} else if (e4WrappedPart instanceof DerivateView){
|
101
|
DerivateView view = (DerivateView) e4WrappedPart;
|
102
|
|
103
|
ISelection selection = mediaView.getViewer().getSelection();
|
104
|
if (selection instanceof TreeSelection) {
|
105
|
TreePath[] paths = ((TreeSelection) selection).getPaths();
|
106
|
int count = paths[0].getSegmentCount();
|
107
|
DescriptionBase description = null;
|
108
|
for (int i = 0; i < count; i++ ) {
|
109
|
if (paths[0].getSegment(i) instanceof DescriptionBase) {
|
110
|
description = (DescriptionBase) paths[0].getSegment(i);
|
111
|
break;
|
112
|
}
|
113
|
}
|
114
|
// TODO use undo context specific to editor
|
115
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
116
|
|
117
|
config.setDeleteFromDescription(true);
|
118
|
config.setOnlyRemoveFromGallery(false);
|
119
|
|
120
|
if (description instanceof SpecimenDescription){
|
121
|
config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
|
122
|
} else if (description instanceof TaxonDescription){
|
123
|
config.setDeleteFrom(((TaxonDescription)description).getTaxon());
|
124
|
}else if (description instanceof TaxonNameDescription){
|
125
|
config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
|
126
|
}
|
127
|
|
128
|
|
129
|
DeleteConfiguratorDialog dialog;
|
130
|
dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM, null, Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
|
131
|
int result_dialog= dialog.open();
|
132
|
if (result_dialog != IStatus.OK){
|
133
|
return;
|
134
|
}
|
135
|
AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, media, config, mediaView);
|
136
|
AbstractUtility.executeOperation(operation);
|
137
|
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
|
142
|
|
143
|
}
|