ref #5801 Fix several NPE when editing/creating concept relations from
authorPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 12 Sep 2016 12:22:35 +0000 (14:22 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Mon, 12 Sep 2016 12:22:35 +0000 (14:22 +0200)
active bulk editor

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/concept/handler/CreateConceptRelationHandler.java

index d850131..44c0e58 100644 (file)
@@ -1,9 +1,9 @@
 // $Id$
 /**
 * Copyright (C) 2007 EDIT
-* European Distributed Institute of Taxonomy 
+* European Distributed Institute of Taxonomy
 * http://www.e-taxonomy.eu
-* 
+*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * See LICENSE.TXT at the top of this package for the full license terms.
 */
@@ -14,16 +14,20 @@ import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.commands.common.NotDefinedException;
+import org.eclipse.core.commands.operations.IUndoContext;
 import org.eclipse.core.runtime.Status;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.jface.viewers.IStructuredSelection;
 import org.eclipse.swt.widgets.Event;
+import org.eclipse.ui.IEditorPart;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.handlers.HandlerUtil;
 
+import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
-import eu.etaxonomy.taxeditor.editor.EditorUtil;
-import eu.etaxonomy.taxeditor.editor.Page;
-import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
+import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
+import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
 import eu.etaxonomy.taxeditor.editor.view.concept.operation.CreateConceptRelationOperation;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.MessagingUtils;
@@ -33,52 +37,61 @@ import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonBaseSelectionDialog;
 
 /**
- * <p>CreateConceptRelationHandler class.</p>
- *
  * @author n.hoffmann
  * @created 16.04.2009
  * @version 1.0
  */
 public class CreateConceptRelationHandler extends AbstractHandler {
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
-        */
+
        /** {@inheritDoc} */
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
-               TaxonNameEditor editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(
-                               Page.NAME);
-               
-               Taxon taxonToBeFiltered = editor.getTaxon();
-               
-               /* we need to get the activePart before calling TaxonBaseSelectionDialog.selectTaxon(), otherwise the 
+           IUndoContext undoContext = null;
+           ConversationHolder conversationHolder = null;
+           Taxon taxon = null;
+               IEditorPart activeEditor = AbstractUtility.getActiveEditor();
+               if(activeEditor instanceof MultiPageTaxonEditor){
+                   MultiPageTaxonEditor taxonEditor = (MultiPageTaxonEditor)activeEditor;
+                   taxon = taxonEditor.getTaxon();
+                   conversationHolder = taxonEditor.getConversationHolder();
+                   undoContext = taxonEditor.getUndoContext();
+               }
+               else if(activeEditor instanceof BulkEditor){
+                   BulkEditor bulkEditor = (BulkEditor)activeEditor;
+            ISelection selection = bulkEditor.getSelectionProvider().getSelection();
+                   taxon = (Taxon) ((IStructuredSelection)selection).getFirstElement();
+                   conversationHolder = bulkEditor.getConversationHolder();
+                   undoContext = bulkEditor.getUndoContext();
+               }
+
+
+               /* we need to get the activePart before calling TaxonBaseSelectionDialog.selectTaxon(), otherwise the
                 * the variable of the parent applicationContext in event are overwritten during this method call
                 * this is Linux specific bugfix see: #2685 ([LINUX] Editing concept relationships does not work under linux)
                 */
                IWorkbenchPart activePart = HandlerUtil.getActivePart(event);
 
-               Taxon relatedConcept = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), editor.getConversationHolder(), taxonToBeFiltered);
-                                       
-               
+               Taxon relatedConcept = TaxonBaseSelectionDialog.selectTaxon(HandlerUtil.getActiveShell(event), conversationHolder, taxon);
+
+
                if(relatedConcept == null){
                        return Status.CANCEL_STATUS;
                }
                TaxonRelationshipType type = (TaxonRelationshipType) ((Event)event.getTrigger()).data;
                boolean isInverse = type.getInverseLabel().isEmpty()?false:true;
                TaxonRelationshipTypeInverseContainer typeInverseContainer =new TaxonRelationshipTypeInverseContainer(type, isInverse) ;
-               
+
 
                IPostOperationEnabled postOperationEnabled = (activePart instanceof IPostOperationEnabled) ? (IPostOperationEnabled) activePart : null;
-               
+
                try {
-                       AbstractPostOperation operation = new CreateConceptRelationOperation(event.getCommand().getName(), 
-                                       editor.getUndoContext(), editor.getTaxon(), relatedConcept, typeInverseContainer, postOperationEnabled);
+                       AbstractPostOperation operation = new CreateConceptRelationOperation(event.getCommand().getName(),
+                                       undoContext, taxon, relatedConcept, typeInverseContainer, postOperationEnabled);
                        AbstractUtility.executeOperation(operation);
                } catch (NotDefinedException e) {
                        MessagingUtils.warn(getClass(), "Command name not set");
                }
-               
+
                return null;
        }
 }