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.CanExecute;
|
10
|
import org.eclipse.e4.core.di.annotations.Execute;
|
11
|
import org.eclipse.e4.ui.di.UISynchronize;
|
12
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
13
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
14
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
|
15
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
16
|
import org.eclipse.jface.dialogs.MessageDialog;
|
17
|
import org.eclipse.jface.viewers.ISelection;
|
18
|
import org.eclipse.jface.viewers.TreePath;
|
19
|
import org.eclipse.jface.viewers.TreeSelection;
|
20
|
import org.eclipse.swt.widgets.Shell;
|
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.EditorUtil;
|
29
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
30
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
|
31
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
|
32
|
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
|
33
|
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
|
34
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
35
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
36
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
37
|
|
38
|
/**
|
39
|
*
|
40
|
* @author pplitzner
|
41
|
* @since Aug 15, 2017
|
42
|
*
|
43
|
*/
|
44
|
public class DeleteMediaHandler {
|
45
|
|
46
|
@Execute
|
47
|
public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
48
|
@Named(IServiceConstants.ACTIVE_SELECTION) Object object,
|
49
|
@Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
|
50
|
MMenuItem menuItem,
|
51
|
UISynchronize sync) {
|
52
|
|
53
|
MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
|
54
|
|
55
|
ISelection selection = mediaView.getViewer().getSelection();
|
56
|
if (selection instanceof TreeSelection) {
|
57
|
TreePath[] paths = ((TreeSelection) selection).getPaths();
|
58
|
int count = paths[0].getSegmentCount();
|
59
|
DescriptionBase description = null;
|
60
|
for (int i = 0; i < count; i++ ) {
|
61
|
if (paths[0].getSegment(i) instanceof DescriptionBase) {
|
62
|
description = (DescriptionBase) paths[0].getSegment(i);
|
63
|
break;
|
64
|
}
|
65
|
}
|
66
|
// TODO use undo context specific to editor
|
67
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
68
|
|
69
|
config.setDeleteFromDescription(true);
|
70
|
config.setOnlyRemoveFromGallery(false);
|
71
|
|
72
|
if (description instanceof SpecimenDescription){
|
73
|
config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
|
74
|
} else if (description instanceof TaxonDescription){
|
75
|
config.setDeleteFrom(((TaxonDescription)description).getTaxon());
|
76
|
}else if (description instanceof TaxonNameDescription){
|
77
|
config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
|
78
|
}
|
79
|
DeleteConfiguratorDialog dialog;
|
80
|
dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM, null, Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
|
81
|
int result_dialog= dialog.open();
|
82
|
if (result_dialog != IStatus.OK){
|
83
|
return;
|
84
|
}
|
85
|
if(object instanceof Media){
|
86
|
AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, (Media) object, config, mediaView);
|
87
|
AbstractUtility.executeOperation(operation, sync);
|
88
|
}
|
89
|
if(object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery()){
|
90
|
if(object instanceof TaxonDescription){
|
91
|
DeleteTaxonDescriptionOperation deleteTaxonDescriptionOperation = new DeleteTaxonDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (TaxonDescription)object, mediaView, null);
|
92
|
AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
|
93
|
}
|
94
|
else if(object instanceof SpecimenDescription){
|
95
|
DeleteSpecimenDescriptionOperation deleteTaxonDescriptionOperation = new DeleteSpecimenDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (SpecimenDescription)object, mediaView, null);
|
96
|
AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
|
97
|
|
98
|
}
|
99
|
}
|
100
|
|
101
|
}
|
102
|
}
|
103
|
|
104
|
@CanExecute
|
105
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) Object object,
|
106
|
MHandledMenuItem menuItem){
|
107
|
boolean canExecute = false;
|
108
|
canExecute = object instanceof Media
|
109
|
|| (object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery());
|
110
|
menuItem.setVisible(canExecute);
|
111
|
return canExecute;
|
112
|
}
|
113
|
|
114
|
|
115
|
|
116
|
}
|