removed a very obsolete log message
[taxeditor.git] / 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 org.apache.log4j.Logger;
14 import org.eclipse.core.commands.AbstractHandler;
15 import org.eclipse.core.commands.ExecutionEvent;
16 import org.eclipse.core.commands.ExecutionException;
17 import org.eclipse.core.commands.operations.IUndoableOperation;
18 import org.eclipse.jface.viewers.StructuredSelection;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IEditorInput;
21 import org.eclipse.ui.IEditorPart;
22 import org.eclipse.ui.PartInitException;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.taxon.ITreeNode;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
30 import eu.etaxonomy.taxeditor.editor.EditorUtil;
31 import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
32 import eu.etaxonomy.taxeditor.operations.ChangeSynonymToAcceptedTaxonOperation;
33 import eu.etaxonomy.taxeditor.operations.IPostOperationEnabled;
34 import eu.etaxonomy.taxeditor.propertysheet.name.SynonymPropertySource;
35
36 /**
37 * @author n.hoffmann
38 * @created 21.04.2009
39 * @version 1.0
40 */
41 public class ChangeSynonymToAcceptedTaxonHandler extends AbstractHandler implements IPostOperationEnabled {
42 private static final Logger logger = Logger
43 .getLogger(ChangeSynonymToAcceptedTaxonHandler.class);
44
45 private IEditorPart editor;
46
47 private Taxon taxon;
48
49 private ITreeNode parentNode;
50
51 /* (non-Javadoc)
52 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
53 */
54 public Object execute(ExecutionEvent event) throws ExecutionException {
55 editor = HandlerUtil.getActiveEditor(event);
56 Shell shell = HandlerUtil.getActiveShell(event);
57 IEditorInput input = editor.getEditorInput();
58
59 if (!(input instanceof TaxonEditorInput)) {
60 logger.error("Editor input is not TaxonEditorInput");
61 return null;
62 }
63
64 // Get synonym from selection
65 StructuredSelection selection = (StructuredSelection) HandlerUtil.getActiveMenuSelection(event);
66 if (!(selection.getFirstElement() instanceof SynonymPropertySource)) {
67 logger.error("Expected selection containing SynonymPropertySource");
68 return null;
69 }
70
71 Synonym synonym = (Synonym) ((SynonymPropertySource) selection.getFirstElement()).getTaxonBase();
72
73 // Force user to save taxon - not really necessary though, is it?
74 if (!EditorUtil.forceUserSave(editor, shell)) {
75 return null;
76 }
77
78 // Get taxon
79 taxon = ((TaxonEditorInput) input).getTaxon();
80 parentNode = ((TaxonEditorInput) input).getTaxonNode().getParent();
81
82 // Create new taxon with no parent
83 IUndoableOperation operation = new ChangeSynonymToAcceptedTaxonOperation("Change synonym to accepted taxon", EditorUtil.getUndoContext(),
84 taxon, parentNode, synonym, this); //$NON-NLS-1$
85 EditorUtil.executeOperation(operation);
86
87 return null;
88 }
89
90 /* (non-Javadoc)
91 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
92 */
93 public boolean postOperation(CdmBase objectAffectedByOperation) {
94
95 // Redraw existing editor
96 ((IPostOperationEnabled) editor).postOperation(null);
97
98 editor.doSave(null);
99
100 if (objectAffectedByOperation instanceof TaxonNode) {
101
102 // Open new unsaved editor with existing taxon's parent as temporary parent
103 TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
104 // TaxonNode newNode = parentNode.addChild(newTaxon);
105
106 try {
107 // TODO
108 /*
109 * This doesn't work b/c newNode hasn't been committed yet, and therefore
110 * CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
111 * doesn't work yet.
112 */
113 IEditorPart newEditor = EditorUtil.open(newNode.getUuid());
114
115 } catch (PartInitException e) {
116 // TODO Auto-generated catch block
117 e.printStackTrace();
118 }
119 }
120 return true;
121 }
122 }