915d6ef6b10340777cdbb34666c52fa8a94d69da
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / ChangeSynonymToAcceptedTaxonHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 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
11 package eu.etaxonomy.taxeditor.editor.name.handler;
12
13 import java.util.List;
14 import java.util.UUID;
15
16 import org.apache.log4j.Logger;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.jface.viewers.StructuredSelection;
21 import org.eclipse.swt.widgets.Shell;
22 import org.eclipse.ui.IEditorInput;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.handlers.HandlerUtil;
25
26 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.taxon.Synonym;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
31 import eu.etaxonomy.taxeditor.editor.EditorUtil;
32 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
33 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
34 import eu.etaxonomy.taxeditor.editor.name.operation.ChangeSynonymToAcceptedTaxonOperation;
35 import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
36 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37 import eu.etaxonomy.taxeditor.ui.dialogs.filteredSelection.TaxonNodeSelectionDialog;
38
39 /**
40 * <p>ChangeSynonymToAcceptedTaxonHandler class.</p>
41 *
42 * @author n.hoffmann
43 * @created 21.04.2009
44 * @version 1.0
45 */
46 public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
47 private static final Logger logger = Logger
48 .getLogger(ChangeSynonymToAcceptedTaxonHandler.class);
49 private MultiPageTaxonEditor editor;
50
51 /* (non-Javadoc)
52 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
53 */
54 /** {@inheritDoc} */
55 public Object execute(ExecutionEvent event) throws ExecutionException {
56 editor = (MultiPageTaxonEditor) HandlerUtil.getActiveEditor(event);
57 Shell shell = HandlerUtil.getActiveShell(event);
58 IEditorInput input = editor.getEditorInput();
59
60 if (!(input instanceof TaxonEditorInput)) {
61 logger.error("Editor input is not TaxonEditorInput");
62 return null;
63 }
64
65 // Get synonym from selection
66 StructuredSelection selection = (StructuredSelection) HandlerUtil.getActiveMenuSelection(event);
67 if (!(selection.getFirstElement() instanceof Synonym)) {
68 logger.error("Selection does not contain a Synonym");
69 return null;
70 }
71
72 Synonym synonym = (Synonym) selection.getFirstElement();
73
74 // Force user to save taxon - not really necessary though, is it?
75 if (!EditorUtil.forceUserSave(editor, shell)) {
76 return null;
77 }
78
79 // Get taxon
80 Taxon taxon = ((TaxonEditorInput) input).getTaxon();
81
82 TaxonNode parentNode = (TaxonNode) HibernateProxyHelper.deproxy(((TaxonEditorInput) input).getTaxonNode().getParent());
83
84 List<UUID> excludeTaxa = null;
85
86 TaxonNode newParentNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event), editor.getConversationHolder(), "Select parent", excludeTaxa, null);
87
88 if(newParentNode != null){
89
90 // TODO get synonyms from homotypical group and add them as homotypic synonyms to new accepted taxon
91 // apply confirmation dialog
92
93 // FIXME with this implementation we can not create a taxonNode that is a direct child of the classification node
94 AbstractPostOperation operation = new ChangeSynonymToAcceptedTaxonOperation("Change synonym to accepted taxon", EditorUtil.getUndoContext(),
95 taxon, newParentNode, synonym, null, this, editor); //$NON-NLS-1$
96 EditorUtil.executeOperation(operation);
97 }
98
99 return null;
100 }
101
102 /* (non-Javadoc)
103 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
104 */
105 /** {@inheritDoc} */
106 public boolean postOperation(CdmBase objectAffectedByOperation) {
107
108 // Redraw existing editor
109 ((IPostOperationEnabled) editor).postOperation(null);
110
111 editor.doSave(EditorUtil.getMonitor());
112
113 if (objectAffectedByOperation instanceof TaxonNode) {
114
115 // Open new unsaved editor with existing taxon's parent as temporary parent
116 TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
117 // TaxonNode newNode = parentNode.addChild(newTaxon);
118
119 try {
120 // TODO
121 /*
122 * This doesn't work b/c newNode hasn't been committed yet, and therefore
123 * CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
124 * doesn't work yet.
125 */
126 EditorUtil.openTaxonNode(newNode.getUuid());
127
128 } catch (PartInitException e) {
129 // TODO Auto-generated catch block
130 e.printStackTrace();
131 } catch (Exception e) {
132 EditorUtil.warningDialog("Could not create Taxon", this, e.getMessage());
133 }
134 }
135 return true;
136 }
137
138 /**
139 * <p>onComplete</p>
140 *
141 * @return a boolean.
142 */
143 public boolean onComplete() {
144 // TODO Auto-generated method stub
145 return false;
146 }
147 }