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