Project

General

Profile

Download (2.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * 
3
 */
4
package eu.etaxonomy.taxeditor.editor.view.media.handler;
5

    
6
import org.apache.log4j.Logger;
7
import org.eclipse.core.commands.AbstractHandler;
8
import org.eclipse.core.commands.ExecutionEvent;
9
import org.eclipse.core.commands.ExecutionException;
10
import org.eclipse.core.commands.common.NotDefinedException;
11
import org.eclipse.jface.viewers.ISelection;
12
import org.eclipse.jface.viewers.TreePath;
13
import org.eclipse.jface.viewers.TreeSelection;
14
import org.eclipse.ui.IEditorInput;
15
import org.eclipse.ui.IEditorPart;
16
import org.eclipse.ui.IWorkbenchPart;
17
import org.eclipse.ui.forms.editor.FormEditor;
18
import org.eclipse.ui.handlers.HandlerUtil;
19

    
20
import eu.etaxonomy.cdm.model.description.DescriptionBase;
21
import eu.etaxonomy.cdm.model.taxon.Taxon;
22
import eu.etaxonomy.taxeditor.editor.EditorUtil;
23
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
24
import eu.etaxonomy.taxeditor.operations.AbstractPostOperation;
25
import eu.etaxonomy.taxeditor.operations.AddMediaToImageGalleryOperation;
26
import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
27

    
28
/**
29
 * <p>CreateMediaHandler class.</p>
30
 *
31
 * @author p.ciardelli
32
 * @version $Id: $
33
 */
34
public class CreateMediaHandler extends AbstractHandler {
35
	private static final Logger logger = Logger
36
			.getLogger(CreateMediaHandler.class);
37

    
38
	/* (non-Javadoc)
39
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
40
	 */
41
	/** {@inheritDoc} */
42
	public Object execute(ExecutionEvent event) throws ExecutionException {
43
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
44
		IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
45
		
46
		
47
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
48
		if (editor instanceof FormEditor) {
49
			editor = ((FormEditor) editor).getActiveEditor();
50
		}
51
		IEditorInput input = editor.getEditorInput();
52
		if (input instanceof TaxonEditorInput) {
53
			Taxon taxon = ((TaxonEditorInput) input).getTaxon();
54
			
55
			ISelection selection = HandlerUtil.getCurrentSelection(event);
56
			if (selection instanceof TreeSelection) {
57
				TreePath[] paths = ((TreeSelection) selection).getPaths();
58
				
59
				int count = paths[0].getSegmentCount();
60
				DescriptionBase description = null;
61
				for (int i = 0; i < count; i++ ) {
62
					if (paths[0].getSegment(i) instanceof DescriptionBase) {
63
						description = (DescriptionBase) paths[0].getSegment(i);
64
						break;
65
					}
66
				}
67
				
68
				AbstractPostOperation operation = null;
69
				try {
70
					// TODO use undo context specific to editor
71
					operation = new AddMediaToImageGalleryOperation(event.getCommand().getName(), 
72
							EditorUtil.getUndoContext(), taxon, description, postOperationEnabled);
73
					EditorUtil.executeOperation(operation);
74
				} catch (NotDefinedException e) {
75
					logger.warn("Command name not set");
76
				}
77
			}
78
		}
79
		return null;
80
	}
81

    
82
}
(2-2/5)