Project

General

Profile

Download (3.83 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
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.jface.viewers.ISelection;
17
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.jface.viewers.ITreeSelection;
19
import org.eclipse.jface.viewers.TreePath;
20
import org.eclipse.swt.widgets.Event;
21
import org.eclipse.ui.IWorkbenchPart;
22
import org.eclipse.ui.handlers.HandlerUtil;
23

    
24
import eu.etaxonomy.cdm.model.description.DescriptionBase;
25
import eu.etaxonomy.cdm.model.description.Feature;
26
import eu.etaxonomy.taxeditor.editor.EditorUtil;
27
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.CreateDescriptionElementOperation;
28
import eu.etaxonomy.taxeditor.model.AbstractUtility;
29
import eu.etaxonomy.taxeditor.model.MessagingUtils;
30
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
31
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
32

    
33
/**
34
 * <p>CreateDescriptionElementHandler class.</p>
35
 *
36
 * @author n.hoffmann
37
 * @created 16.04.2009
38
 * @version 1.0
39
 */
40
public class CreateDescriptionElementHandler extends AbstractHandler {
41

    
42
	/*
43
	 * (non-Javadoc)
44
	 *
45
	 * @see
46
	 * org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.
47
	 * ExecutionEvent)
48
	 */
49
    /** {@inheritDoc} */
50
    @Override
51
    public Object execute(ExecutionEvent event) throws ExecutionException {
52
        IWorkbenchPart part = HandlerUtil.getActivePart(event);
53
        IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
54
                : null;
55

    
56

    
57
        DescriptionBase<?> description = null;
58

    
59
        ISelection selection = HandlerUtil.getCurrentSelection(event);
60
        Object selectedElement = null;
61
        if (selection instanceof ITreeSelection) {
62
            TreePath[] paths = ((ITreeSelection) selection).getPaths();
63
            selectedElement = paths[0].getFirstSegment();
64
        }
65
        else if (selection instanceof IStructuredSelection) {
66
            selectedElement = ((IStructuredSelection) selection).getFirstElement();
67
        }
68
        if (selectedElement instanceof DescriptionBase<?>) {
69
            description = (DescriptionBase<?>) selectedElement;
70
        }
71

    
72
        if (description != null) {
73
            AbstractPostOperation operation = null;
74
            try {
75
                // TODO use undo context specific to editor
76
                operation = operationCreationInstance(event.getCommand().getName(), event, description, postOperationEnabled);
77
                AbstractUtility.executeOperation(operation);
78
            } catch (NotDefinedException e) {
79
                MessagingUtils.warn(getClass(), "Command name not set");
80
            }
81
        } else {
82
            MessagingUtils.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
83
            return null;
84
        }
85
        return null;
86

    
87
    }
88

    
89
    /**
90
	 * Added to make the Class more generic and limit the amount of code changes required
91
	 * @param label
92
	 * @param event
93
	 * @param taxon
94
	 * @param description
95
	 * @param postOperationEnabled
96
	 * @return
97
	 */
98
	protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, DescriptionBase<?> description, IPostOperationEnabled postOperationEnabled) {
99
		Feature feature = (Feature) ((Event) event.getTrigger()).data;
100
		return new CreateDescriptionElementOperation(label, EditorUtil.getUndoContext(), description, feature, postOperationEnabled);
101
	}
102

    
103
}
(1-1/8)