Revision e6aba306
Added by Patrick Plitzner over 6 years ago
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/media/handler/CreateMediaHandler.java | ||
---|---|---|
3 | 3 |
*/ |
4 | 4 |
package eu.etaxonomy.taxeditor.editor.view.media.handler; |
5 | 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.TreePath; |
|
12 |
import org.eclipse.jface.viewers.TreeSelection; |
|
13 |
import org.eclipse.ui.IEditorPart; |
|
14 |
import org.eclipse.ui.IWorkbenchPart; |
|
15 |
import org.eclipse.ui.forms.editor.FormEditor; |
|
16 |
import org.eclipse.ui.handlers.HandlerUtil; |
|
6 |
import javax.inject.Named; |
|
7 |
|
|
8 |
import org.eclipse.e4.core.di.annotations.Execute; |
|
9 |
import org.eclipse.e4.core.di.annotations.Optional; |
|
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; |
|
17 | 13 |
|
18 | 14 |
import eu.etaxonomy.cdm.model.description.DescriptionBase; |
19 | 15 |
import eu.etaxonomy.taxeditor.editor.EditorUtil; |
16 |
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4; |
|
20 | 17 |
import eu.etaxonomy.taxeditor.editor.view.media.operation.AddMediaToImageGalleryOperation; |
21 | 18 |
import eu.etaxonomy.taxeditor.model.AbstractUtility; |
22 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
|
23 | 19 |
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation; |
24 |
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled; |
|
25 | 20 |
|
26 | 21 |
/** |
27 |
* <p>CreateMediaHandler class.</p> |
|
28 | 22 |
* |
29 |
* @author p.ciardelli |
|
30 |
* @version $Id: $ |
|
23 |
* @author pplitzner |
|
24 |
* @since Aug 14, 2017 |
|
25 |
* |
|
31 | 26 |
*/ |
32 |
public class CreateMediaHandler extends AbstractHandler { |
|
33 |
|
|
34 |
/* (non-Javadoc) |
|
35 |
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent) |
|
36 |
*/ |
|
37 |
/** {@inheritDoc} */ |
|
38 |
@Override |
|
39 |
public Object execute(ExecutionEvent event) throws ExecutionException { |
|
40 |
IWorkbenchPart part = HandlerUtil.getActivePart(event); |
|
41 |
IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null; |
|
42 |
|
|
43 |
|
|
44 |
DescriptionBase description = null; |
|
27 |
public class CreateMediaHandler { |
|
45 | 28 |
|
46 |
IEditorPart editor = HandlerUtil.getActiveEditor(event); |
|
47 |
if (editor instanceof FormEditor) { |
|
48 |
editor = ((FormEditor) editor).getActiveEditor(); |
|
49 |
} |
|
50 |
|
|
51 |
ISelection selection = HandlerUtil.getCurrentSelection(event); |
|
52 |
if (selection instanceof TreeSelection) { |
|
53 |
TreePath[] paths = ((TreeSelection) selection).getPaths(); |
|
29 |
@Execute |
|
30 |
public Object execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart, |
|
31 |
@Optional@Named(IServiceConstants.ACTIVE_SELECTION) DescriptionBase description, MMenuItem menuItem) { |
|
54 | 32 |
|
55 |
int count = paths[0].getSegmentCount(); |
|
56 |
|
|
57 |
for (int i = 0; i < count; i++ ) { |
|
58 |
if (paths[0].getSegment(i) instanceof DescriptionBase) { |
|
59 |
description = (DescriptionBase) paths[0].getSegment(i); |
|
60 |
break; |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
33 |
MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject(); |
|
64 | 34 |
|
65 | 35 |
if(description != null){ |
66 |
executeOperation(event, postOperationEnabled, description); |
|
36 |
AbstractPostOperation operation = null; |
|
37 |
// TODO use undo context specific to editor |
|
38 |
operation = new AddMediaToImageGalleryOperation(menuItem.getLocalizedLabel(), |
|
39 |
EditorUtil.getUndoContext(), description, mediaView); |
|
40 |
AbstractUtility.executeOperation(operation); |
|
67 | 41 |
} |
68 | 42 |
|
69 | 43 |
return null; |
70 | 44 |
} |
71 | 45 |
|
72 |
/** |
|
73 |
* @param event |
|
74 |
* @param postOperationEnabled |
|
75 |
* @param description |
|
76 |
*/ |
|
77 |
protected void executeOperation(ExecutionEvent event, IPostOperationEnabled postOperationEnabled, |
|
78 |
DescriptionBase description) { |
|
79 |
AbstractPostOperation operation = null; |
|
80 |
try { |
|
81 |
// TODO use undo context specific to editor |
|
82 |
operation = new AddMediaToImageGalleryOperation(event.getCommand().getName(), |
|
83 |
EditorUtil.getUndoContext(), description, postOperationEnabled); |
|
84 |
AbstractUtility.executeOperation(operation); |
|
85 |
} catch (NotDefinedException e) { |
|
86 |
MessagingUtils.warn(getClass(), "Command name not set."); //$NON-NLS-1$ |
|
87 |
} |
|
88 |
} |
|
89 |
|
|
90 | 46 |
} |
Also available in: Unified diff
ref #6906 Migrate media view context menu