Project

General

Profile

Download (3.07 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.editor.view.uses.handler;
2

    
3
import org.eclipse.core.commands.AbstractHandler;
4
import org.eclipse.core.commands.ExecutionEvent;
5
import org.eclipse.core.commands.ExecutionException;
6
import org.eclipse.core.commands.common.NotDefinedException;
7
import org.eclipse.jface.viewers.ISelection;
8
import org.eclipse.jface.viewers.IStructuredSelection;
9
import org.eclipse.jface.viewers.ITreeSelection;
10
import org.eclipse.jface.viewers.TreePath;
11
import org.eclipse.swt.widgets.Event;
12
import org.eclipse.ui.IEditorInput;
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.Feature;
19
import eu.etaxonomy.cdm.model.description.TaxonDescription;
20
import eu.etaxonomy.cdm.model.taxon.Taxon;
21
import eu.etaxonomy.taxeditor.editor.EditorUtil;
22
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
23
import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseRecordOperation;
24
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
25
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
26

    
27
public class CreateUseRecordHandler extends AbstractHandler {
28
	public Object execute(ExecutionEvent event) throws ExecutionException {
29
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
30
		IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
31
				: null;
32

    
33
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
34
		if (editor instanceof FormEditor) {
35
			editor = ((FormEditor) editor).getActiveEditor();
36
		}
37
		IEditorInput input = editor.getEditorInput();
38
		if (input instanceof TaxonEditorInput) {
39
			Taxon taxon = ((TaxonEditorInput) input).getTaxon();
40

    
41
			TaxonDescription description = null;
42

    
43
			ISelection selection = HandlerUtil.getActiveMenuSelection(event);
44
			if (selection instanceof ITreeSelection) {
45
				TreePath[] paths = ((ITreeSelection) selection).getPaths();
46
				Object firstSegment = paths[0].getFirstSegment();
47
				if (firstSegment instanceof TaxonDescription) {
48
					description = (TaxonDescription) firstSegment;
49
				}
50
			}else if (selection instanceof IStructuredSelection) {
51
				Object selectedElement = ((IStructuredSelection) selection)
52
						.getFirstElement();
53
				if (selectedElement instanceof TaxonDescription){
54
					description = (TaxonDescription) selectedElement;
55
				}
56
			} 
57

    
58
			if (description != null) {
59

    
60
				Feature feature = (Feature) ((Event) event.getTrigger()).data;
61

    
62
				AbstractPostOperation operation = null;
63
				try {
64
					// TODO use undo context specific to editor
65
					operation = new CreateUseRecordOperation(event
66
							.getCommand().getName(),
67
							EditorUtil.getUndoContext(), taxon,
68
							description, element, postOperationEnabled);
69
					EditorUtil.executeOperation(operation);
70
				} catch (NotDefinedException e) {
71
					EditorUtil.warn(getClass(), "Command name not set");
72
				}
73
			} else {
74
				EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
75
				return null;
76
			}
77
		}
78
		return null;
79

    
80
	}
81
}
(2-2/4)