Merge branch 'develop' into LibrAlign
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / view / concept / handler / CreateConceptRelationHandler.java
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.concept.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.commands.operations.IUndoContext;
17 import org.eclipse.core.runtime.Status;
18 import org.eclipse.jface.viewers.ISelection;
19 import org.eclipse.jface.viewers.IStructuredSelection;
20 import org.eclipse.swt.widgets.Event;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.IWorkbenchPart;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
28 import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
29 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
30 import eu.etaxonomy.taxeditor.editor.view.concept.operation.CreateConceptRelationOperation;
31 import eu.etaxonomy.taxeditor.model.AbstractUtility;
32 import eu.etaxonomy.taxeditor.model.MessagingUtils;
33 import eu.etaxonomy.taxeditor.model.TaxonRelationshipTypeInverseContainer;
34 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
35 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
36 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonBaseSelectionDialog;
37
38 /**
39 * @author n.hoffmann
40 * @created 16.04.2009
41 * @version 1.0
42 */
43 public class CreateConceptRelationHandler extends AbstractHandler {
44
45 /** {@inheritDoc} */
46 @Override
47 public Object execute(ExecutionEvent event) throws ExecutionException {
48 IUndoContext undoContext = null;
49 ConversationHolder conversationHolder = null;
50 Taxon taxon = null;
51 IEditorPart activeEditor = AbstractUtility.getActiveEditor();
52 if(activeEditor instanceof MultiPageTaxonEditor){
53 MultiPageTaxonEditor taxonEditor = (MultiPageTaxonEditor)activeEditor;
54 taxon = taxonEditor.getTaxon();
55 conversationHolder = taxonEditor.getConversationHolder();
56 undoContext = taxonEditor.getUndoContext();
57 }
58 else if(activeEditor instanceof BulkEditor){
59 BulkEditor bulkEditor = (BulkEditor)activeEditor;
60 ISelection selection = bulkEditor.getSelectionProvider().getSelection();
61 taxon = (Taxon) ((IStructuredSelection)selection).getFirstElement();
62 conversationHolder = bulkEditor.getConversationHolder();
63 undoContext = bulkEditor.getUndoContext();
64 }
65
66
67 /* we need to get the activePart before calling TaxonBaseSelectionDialog.selectTaxon(), otherwise the
68 * the variable of the parent applicationContext in event are overwritten during this method call
69 * this is Linux specific bugfix see: #2685 ([LINUX] Editing concept relationships does not work under linux)
70 */
71 IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
72
73 Taxon relatedConcept = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), conversationHolder, taxon);
74
75
76 if(relatedConcept == null){
77 return Status.CANCEL_STATUS;
78 }
79 TaxonRelationshipType type = (TaxonRelationshipType) ((Event)event.getTrigger()).data;
80 boolean isInverse = type.getInverseLabel().isEmpty()?false:true;
81 TaxonRelationshipTypeInverseContainer typeInverseContainer =new TaxonRelationshipTypeInverseContainer(type, isInverse) ;
82
83
84 IPostOperationEnabled postOperationEnabled = (activePart instanceof IPostOperationEnabled) ? (IPostOperationEnabled) activePart : null;
85
86 try {
87 AbstractPostOperation operation = new CreateConceptRelationOperation(event.getCommand().getName(),
88 undoContext, taxon, relatedConcept, typeInverseContainer, postOperationEnabled);
89 AbstractUtility.executeOperation(operation);
90 } catch (NotDefinedException e) {
91 MessagingUtils.warn(getClass(), "Command name not set");
92 }
93
94 return null;
95 }
96 }