Project

General

Profile

Download (2.92 KB) Statistics
| Branch: | Tag: | Revision:
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.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;
17

    
18
import eu.etaxonomy.cdm.model.description.DescriptionBase;
19
import eu.etaxonomy.taxeditor.editor.EditorUtil;
20
import eu.etaxonomy.taxeditor.editor.view.media.operation.AddMediaToImageGalleryOperation;
21
import eu.etaxonomy.taxeditor.model.AbstractUtility;
22
import eu.etaxonomy.taxeditor.model.MessagingUtils;
23
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
24
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
25

    
26
/**
27
 * <p>CreateMediaHandler class.</p>
28
 *
29
 * @author p.ciardelli
30
 * @version $Id: $
31
 */
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;
45

    
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();
54

    
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
		}
64

    
65
		if(description != null){
66
			executeOperation(event, postOperationEnabled, description);
67
		}
68

    
69
		return null;
70
	}
71

    
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.");
87
        }
88
    }
89

    
90
}
(3-3/6)