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