1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.view.media.handler;
|
5
|
|
6
|
import java.util.Iterator;
|
7
|
|
8
|
import javax.inject.Named;
|
9
|
|
10
|
import org.eclipse.core.runtime.IStatus;
|
11
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
12
|
import org.eclipse.e4.core.di.annotations.Execute;
|
13
|
import org.eclipse.e4.ui.di.UISynchronize;
|
14
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
15
|
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
|
16
|
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
|
17
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
18
|
import org.eclipse.jface.dialogs.MessageDialog;
|
19
|
import org.eclipse.jface.viewers.ISelection;
|
20
|
import org.eclipse.jface.viewers.TreePath;
|
21
|
import org.eclipse.jface.viewers.TreeSelection;
|
22
|
import org.eclipse.swt.widgets.Shell;
|
23
|
|
24
|
import eu.etaxonomy.cdm.api.service.config.MediaDeletionConfigurator;
|
25
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
26
|
import eu.etaxonomy.cdm.model.description.SpecimenDescription;
|
27
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
28
|
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
|
29
|
import eu.etaxonomy.cdm.model.media.Media;
|
30
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
31
|
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
|
32
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteSpecimenDescriptionOperation;
|
33
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.DeleteTaxonDescriptionOperation;
|
34
|
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
|
35
|
import eu.etaxonomy.taxeditor.editor.view.media.operation.DeleteMediaOperation;
|
36
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
37
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
38
|
import eu.etaxonomy.taxeditor.ui.dialog.configurator.deleteConfigurator.DeleteConfiguratorDialog;
|
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_SHELL)Shell shell,
|
51
|
MMenuItem menuItem,
|
52
|
UISynchronize sync) {
|
53
|
|
54
|
MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
|
55
|
TreeSelection selection = (TreeSelection) mediaView.getViewer().getSelection();
|
56
|
|
57
|
for(Iterator iter = selection.iterator();iter.hasNext();){
|
58
|
Object object = iter.next();
|
59
|
TreePath[] paths = selection.getPaths();
|
60
|
int count = paths[0].getSegmentCount();
|
61
|
DescriptionBase description = null;
|
62
|
for (int i = 0; i < count; i++ ) {
|
63
|
if (paths[0].getSegment(i) instanceof DescriptionBase) {
|
64
|
description = (DescriptionBase) paths[0].getSegment(i);
|
65
|
break;
|
66
|
}
|
67
|
}
|
68
|
// TODO use undo context specific to editor
|
69
|
MediaDeletionConfigurator config = new MediaDeletionConfigurator();
|
70
|
|
71
|
config.setDeleteFromDescription(true);
|
72
|
config.setOnlyRemoveFromGallery(false);
|
73
|
|
74
|
if (description instanceof SpecimenDescription){
|
75
|
config.setDeleteFrom(((SpecimenDescription)description).getDescribedSpecimenOrObservation());
|
76
|
} else if (description instanceof TaxonDescription){
|
77
|
config.setDeleteFrom(((TaxonDescription)description).getTaxon());
|
78
|
}else if (description instanceof TaxonNameDescription){
|
79
|
config.setDeleteFrom(((TaxonNameDescription)description).getTaxonName());
|
80
|
}
|
81
|
DeleteConfiguratorDialog dialog;
|
82
|
dialog = new DeleteConfiguratorDialog(config, shell, Messages.DeleteMediaHandler_CONFIRM, null, Messages.DeleteMediaHandler_CONFIRM_MESSAGE, MessageDialog.WARNING, new String[] { Messages.DeleteMediaHandler_DELETE, Messages.DeleteMediaHandler_SKIP }, 0);
|
83
|
int result_dialog= dialog.open();
|
84
|
if (result_dialog != IStatus.OK){
|
85
|
return;
|
86
|
}
|
87
|
if(object instanceof Media){
|
88
|
AbstractPostOperation<?> operation = new DeleteMediaOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), description, (Media) object, config, mediaView);
|
89
|
AbstractUtility.executeOperation(operation, sync);
|
90
|
}
|
91
|
if(object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery()){
|
92
|
if(object instanceof TaxonDescription){
|
93
|
DeleteTaxonDescriptionOperation deleteTaxonDescriptionOperation = new DeleteTaxonDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (TaxonDescription)object, mediaView, null);
|
94
|
AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
|
95
|
}
|
96
|
else if(object instanceof SpecimenDescription){
|
97
|
DeleteSpecimenDescriptionOperation deleteTaxonDescriptionOperation = new DeleteSpecimenDescriptionOperation(menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(), (SpecimenDescription)object, mediaView, null);
|
98
|
AbstractUtility.executeOperation(deleteTaxonDescriptionOperation, sync);
|
99
|
}
|
100
|
}
|
101
|
}
|
102
|
}
|
103
|
|
104
|
@CanExecute
|
105
|
public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
|
106
|
MHandledMenuItem menuItem){
|
107
|
MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
|
108
|
ISelection selection = mediaView.getViewer().getSelection();
|
109
|
|
110
|
boolean canExecute = selection instanceof TreeSelection && !selection.isEmpty();
|
111
|
for(Iterator iter = ((TreeSelection) selection).iterator();iter.hasNext();){
|
112
|
Object object = iter.next();
|
113
|
canExecute &= object instanceof Media
|
114
|
|| (object instanceof DescriptionBase && ((DescriptionBase) object).isImageGallery());
|
115
|
}
|
116
|
menuItem.setVisible(canExecute);
|
117
|
return canExecute;
|
118
|
}
|
119
|
|
120
|
|
121
|
|
122
|
}
|