Merge branch 'develop' into unify_derivative_views
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / media / handler / AddImageGalleryHandler.java
1 /**
2 *
3 */
4 package eu.etaxonomy.taxeditor.editor.view.media.handler;
5
6 import org.eclipse.core.commands.AbstractHandler;
7 import org.eclipse.core.commands.ExecutionEvent;
8 import org.eclipse.core.commands.ExecutionException;
9 import org.eclipse.core.commands.common.NotDefinedException;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.jface.viewers.IStructuredSelection;
12 import org.eclipse.jface.viewers.TreeNode;
13 import org.eclipse.ui.IEditorInput;
14 import org.eclipse.ui.IEditorPart;
15 import org.eclipse.ui.IWorkbenchPart;
16 import org.eclipse.ui.handlers.HandlerUtil;
17
18 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
20 import eu.etaxonomy.cdm.model.taxon.Taxon;
21 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
22 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
23 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditorUtil;
24 import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
25 import eu.etaxonomy.taxeditor.editor.EditorUtil;
26 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
27 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateView;
28 import eu.etaxonomy.taxeditor.editor.view.derivate.DerivateViewEditorInput;
29 import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
30 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.AddDerivedUnitFacadeMediaOperation;
31 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateSpecimenDescriptionOperation;
32 import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
33 import eu.etaxonomy.taxeditor.editor.view.media.MediaViewPart;
34 import eu.etaxonomy.taxeditor.model.AbstractUtility;
35 import eu.etaxonomy.taxeditor.model.MessagingUtils;
36 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
37 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
38
39 /**
40 * <p>AddImageGalleryHandler class.</p>
41 *
42 * @author p.ciardelli
43 * @version $Id: $
44 */
45 public class AddImageGalleryHandler extends AbstractHandler {
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51 @Override
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53
54 IWorkbenchPart part = HandlerUtil.getActivePart(event);
55 IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
56
57
58 IEditorPart editor = HandlerUtil.getActiveEditor(event);
59 IEditorInput input = editor.getEditorInput();
60
61 if (input instanceof TaxonEditorInput) {
62 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
63 AbstractPostOperation<?> operation;
64 try {
65 // TODO use undo context specific to editor
66 operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
67 EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
68 AbstractUtility.executeOperation(operation);
69 } catch (NotDefinedException e) {
70 MessagingUtils.warn(getClass(), "Command name not set.");
71 }
72 }
73 else if(input instanceof OccurrenceEditorInput){
74 BulkEditor bulkEditor = (BulkEditor) editor;
75 ISelection selection = bulkEditor.getSelectionProvider().getSelection();
76 invokeOperation(event, postOperationEnabled, selection);
77 }
78 else if(input instanceof DerivateViewEditorInput){
79 ISelection selection = ((DerivateView)editor).getSelection();
80 invokeOperation(event, postOperationEnabled, selection);
81 } else if(part instanceof MediaViewPart){
82 Object viewerInput = ((MediaViewPart)part).getViewer().getInput();
83
84 if(viewerInput instanceof Taxon){
85 Taxon taxon = HibernateProxyHelper.deproxy(viewerInput, Taxon.class);
86 AbstractPostOperation<?> operation;
87 try {
88 // TODO use undo context specific to editor
89 operation = new CreateTaxonDescriptionOperation(event.getCommand().getName(),
90 EditorUtil.getUndoContext(), taxon, postOperationEnabled, true);
91 AbstractUtility.executeOperation(operation);
92 } catch (NotDefinedException e) {
93 MessagingUtils.warn(getClass(), "Command name not set.");
94 }
95
96 }
97 }
98
99 return null;
100 }
101
102 /**
103 * @param event
104 * @param postOperationEnabled
105 * @param selection
106 */
107 private void invokeOperation(ExecutionEvent event, IPostOperationEnabled postOperationEnabled, ISelection selection) {
108 if(selection instanceof IStructuredSelection){
109 Object element = ((IStructuredSelection) selection).getFirstElement();
110 if(element instanceof TreeNode){
111 element = ((TreeNode) element).getValue();
112 }
113 if(element instanceof SpecimenOrObservationBase<?>){
114 try {
115 AbstractPostOperation<?> operation = new AddDerivedUnitFacadeMediaOperation(event.getCommand().getName(),
116 BulkEditorUtil.getUndoContext(), (SpecimenOrObservationBase<?>)element, postOperationEnabled);
117 AbstractUtility.executeOperation(operation);
118 } catch (NotDefinedException e) {
119 MessagingUtils.warn(getClass(), "Command name not set.");
120 }
121 }
122 }
123 }
124
125 }