1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2011 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
|
11
|
|
12
|
import org.eclipse.core.commands.AbstractHandler;
|
13
|
import org.eclipse.core.commands.ExecutionEvent;
|
14
|
import org.eclipse.core.commands.ExecutionException;
|
15
|
import org.eclipse.core.commands.common.NotDefinedException;
|
16
|
import org.eclipse.core.expressions.EvaluationContext;
|
17
|
import org.eclipse.ui.IEditorInput;
|
18
|
import org.eclipse.ui.IEditorPart;
|
19
|
import org.eclipse.ui.IWorkbenchPart;
|
20
|
import org.eclipse.ui.forms.editor.FormEditor;
|
21
|
import org.eclipse.ui.handlers.HandlerUtil;
|
22
|
|
23
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
24
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
25
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
26
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
27
|
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
|
28
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.DescriptiveViewPart;
|
29
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateSpecimenDescriptionOperation;
|
30
|
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateTaxonDescriptionOperation;
|
31
|
import eu.etaxonomy.taxeditor.model.AbstractUtility;
|
32
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
33
|
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
|
34
|
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
|
35
|
|
36
|
/**
|
37
|
* <p>AddDescriptionHandler class.</p>
|
38
|
*
|
39
|
* @author p.ciardelli
|
40
|
* @created 11.08.2009
|
41
|
* @version 1.0
|
42
|
*/
|
43
|
public class CreateDescriptionHandler extends AbstractHandler {
|
44
|
|
45
|
/* (non-Javadoc)
|
46
|
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
47
|
*/
|
48
|
/** {@inheritDoc} */
|
49
|
@Override
|
50
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
51
|
IWorkbenchPart part = HandlerUtil.getActivePart(event);
|
52
|
IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part : null;
|
53
|
|
54
|
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
55
|
if (editor instanceof FormEditor) {
|
56
|
editor = ((FormEditor) editor).getActiveEditor();
|
57
|
}
|
58
|
IEditorInput input = editor.getEditorInput();
|
59
|
AbstractPostOperation<?> operation;
|
60
|
|
61
|
// taxon description
|
62
|
if (input instanceof TaxonEditorInput) {
|
63
|
Taxon taxon = ((TaxonEditorInput) input).getTaxon();
|
64
|
try {
|
65
|
operation = createTaxonOperation(event.getCommand().getName(), taxon, postOperationEnabled);
|
66
|
AbstractUtility.executeOperation(operation);
|
67
|
} catch (NotDefinedException e) {
|
68
|
MessagingUtils.warn(getClass(), "Command name not set.");
|
69
|
}
|
70
|
}
|
71
|
// specimen description
|
72
|
else if(part instanceof DescriptiveViewPart){
|
73
|
Object viewerInput = ((DescriptiveViewPart)part).getViewer().getInput();
|
74
|
if(viewerInput instanceof SpecimenOrObservationBase<?>){
|
75
|
try {
|
76
|
operation = new CreateSpecimenDescriptionOperation(event.getCommand().getName(), EditorUtil.getUndoContext(), (SpecimenOrObservationBase<?>) viewerInput, postOperationEnabled);
|
77
|
AbstractUtility.executeOperation(operation);
|
78
|
} catch (NotDefinedException e) {
|
79
|
MessagingUtils.warn(getClass(), "Command name not set.");
|
80
|
}
|
81
|
}
|
82
|
if(viewerInput instanceof TaxonBase){
|
83
|
try{
|
84
|
operation = createTaxonOperation(event.getCommand().getName(), (Taxon) viewerInput, postOperationEnabled);
|
85
|
AbstractUtility.executeOperation(operation);
|
86
|
} catch (NotDefinedException e) {
|
87
|
MessagingUtils.warn(getClass(), "Command name not set.");
|
88
|
}
|
89
|
|
90
|
}
|
91
|
}
|
92
|
return null;
|
93
|
}
|
94
|
|
95
|
/** {@inheritDoc} */
|
96
|
protected CreateTaxonDescriptionOperation createTaxonOperation(String eventLabel, Taxon taxon, IPostOperationEnabled postOperationEnabled) {
|
97
|
return new CreateTaxonDescriptionOperation(eventLabel, EditorUtil.getUndoContext(), taxon, postOperationEnabled);
|
98
|
}
|
99
|
|
100
|
}
|