Merge branch 'develop' into nameEditorE4
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / ChangeSynonymToAcceptedTaxonHandler.java
index b710737bcbfb40eff19c51dcf02f5d14e7ff0c80..09f2598d245152278ee0462d81d607e3cd440092 100644 (file)
@@ -1,16 +1,17 @@
-// $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.
 */
 
 package eu.etaxonomy.taxeditor.editor.name.handler;
 
+import java.util.ArrayList;
 import java.util.List;
+import java.util.Set;
 import java.util.UUID;
 
 import org.apache.log4j.Logger;
@@ -25,15 +26,20 @@ import org.eclipse.ui.handlers.HandlerUtil;
 
 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
 import eu.etaxonomy.cdm.model.common.CdmBase;
+import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
+import eu.etaxonomy.cdm.model.name.TaxonName;
 import eu.etaxonomy.cdm.model.taxon.Synonym;
 import eu.etaxonomy.cdm.model.taxon.Taxon;
 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
 import eu.etaxonomy.taxeditor.editor.EditorUtil;
 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
+import eu.etaxonomy.taxeditor.editor.l10n.Messages;
 import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
+import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
 import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
 
 /**
@@ -41,36 +47,33 @@ import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
  *
  * @author n.hoffmann
  * @created 21.04.2009
- * @version 1.0
  */
 public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
        private static final Logger logger = Logger
                        .getLogger(ChangeSynonymToAcceptedTaxonHandler.class);
        private MultiPageTaxonEditor editor;
-       
-       /* (non-Javadoc)
-        * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
-        */
+
        /** {@inheritDoc} */
-       public Object execute(ExecutionEvent event) throws ExecutionException {
+       @Override
+    public Object execute(ExecutionEvent event) throws ExecutionException {
                editor =  (MultiPageTaxonEditor) HandlerUtil.getActiveEditor(event);
                Shell shell = HandlerUtil.getActiveShell(event);
                IEditorInput input = editor.getEditorInput();
-               
+
                if (!(input instanceof TaxonEditorInput)) {
-                       logger.error("Editor input is not TaxonEditorInput");
+                       logger.error("Editor input is not TaxonEditorInput"); //$NON-NLS-1$
                        return null;
                }
 
                // Get synonym from selection
                StructuredSelection selection = (StructuredSelection) HandlerUtil.getCurrentSelection(event);
                if (!(selection.getFirstElement() instanceof Synonym)) {
-                       logger.error("Selection does not contain a Synonym");
+                       logger.error("Selection does not contain a Synonym"); //$NON-NLS-1$
                        return null;
                }
 
                Synonym synonym = (Synonym) selection.getFirstElement();
-               
+
                // Force user to save taxon - not really necessary though, is it?
                if (!EditorUtil.forceUserSave(editor, shell)) {
                        return null;
@@ -78,44 +81,46 @@ public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler impleme
 
                // Get taxon
                Taxon taxon = ((TaxonEditorInput) input).getTaxon();
-               
-               TaxonNode parentNode = (TaxonNode) HibernateProxyHelper.deproxy(((TaxonEditorInput) input).getTaxonNode().getParent());
-               
-               List<UUID> excludeTaxa = null;
-               
-               TaxonNode newParentNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), editor.getConversationHolder(), "Select parent", excludeTaxa, null, null);
-               
+
+               TaxonNode parentNode = HibernateProxyHelper.deproxy(((TaxonEditorInput) input).getTaxonNode().getParent());
+
+               List<UUID> excludeTaxa = new ArrayList<>();
+               //excludeTaxa.add(taxon.getUuid());//there are some cases where the accepted taxon should be the parent of the new created accepted taxon
+
+               TaxonNode newParentNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), editor.getConversationHolder(), Messages.ChangeSynonymToAcceptedTaxonHandler_SELECT_PARENT, null, null, ((TaxonEditorInput) input).getTaxonNode().getClassification());
+
+
                if(newParentNode != null){
-                       
+
                        // TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
                        // apply confirmation dialog
-                       
+                       HomotypicalGroup group = synonym.getHomotypicGroup();
+                       Set<TaxonName> namesInGroup = group.getTypifiedNames();
                        // FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
-                       AbstractPostOperation operation = new ChangeSynonymToAcceptedTaxonOperation("Change synonym to accepted taxon", EditorUtil.getUndoContext(), 
-                                       taxon, newParentNode, synonym, null, this, editor); //$NON-NLS-1$
+                       AbstractPostOperation operation = new ChangeSynonymToAcceptedTaxonOperation(Messages.ChangeSynonymToAcceptedTaxonHandler_CHANGE_SYN_TO_ACC_TAXON, EditorUtil.getUndoContext(),
+                                       taxon, newParentNode, synonym, namesInGroup, this, editor, (ICdmEntitySessionEnabled)editor.getEditorInput()); //$NON-NLS-1$
+
                        EditorUtil.executeOperation(operation);
                }
-               
+
                return null;
        }
 
-       /* (non-Javadoc)
-        * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
-        */
        /** {@inheritDoc} */
-       public boolean postOperation(CdmBase objectAffectedByOperation) {
-               
+       @Override
+    public boolean postOperation(CdmBase objectAffectedByOperation) {
+
                // Redraw existing editor
                ((IPostOperationEnabled) editor).postOperation(null);
-               
+
                editor.doSave(EditorUtil.getMonitor());
-               
+
                if (objectAffectedByOperation instanceof TaxonNode) {
-               
+
                        // Open new unsaved editor with existing taxon's parent as temporary parent
                        TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
 //                     TaxonNode newNode = parentNode.addChild(newTaxon);
-                                       
+
                        try {
                                // TODO
                                /*
@@ -123,13 +128,13 @@ public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler impleme
                                 *  CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
                                 *  doesn't work yet.
                                 */
-                               EditorUtil.openTaxonNode(newNode.getUuid());
-                               
+                               EditorUtil.openTaxonBaseE4(newNode.getUuid());
+
                        } catch (PartInitException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                        } catch (Exception e) {
-                               EditorUtil.warningDialog("Could not create Taxon", this, e.getMessage());
+                               MessagingUtils.warningDialog(Messages.ChangeSynonymToAcceptedTaxonHandler_CREATE_FAILURE, this, e.getMessage());
                        }
                }
                return true;
@@ -140,7 +145,8 @@ public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler impleme
         *
         * @return a boolean.
         */
-       public boolean onComplete() {
+       @Override
+    public boolean onComplete() {
                // TODO Auto-generated method stub
                return false;
        }