a6cf8ad0baa81d783bcd18bd8fd6750a6d348fc8
[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 logger.error("Not yet implemented");
56
57 editor = HandlerUtil.getActiveEditor(event);
58 Shell shell = HandlerUtil.getActiveShell(event);
59 IEditorInput input = editor.getEditorInput();
60
61 if (!(input instanceof TaxonEditorInput)) {
62 logger.error("Editor input is not TaxonEditorInput");
63 return null;
64 }
65
66 // Get synonym from selection
67 StructuredSelection selection = (StructuredSelection) HandlerUtil.getActiveMenuSelection(event);
68 if (!(selection.getFirstElement() instanceof SynonymPropertySource)) {
69 logger.error("Expected selection containing SynonymPropertySource");
70 return null;
71 }
72
73 Synonym synonym = (Synonym) ((SynonymPropertySource) selection.getFirstElement()).getTaxonBase();
74
75 // Force user to save taxon - not really necessary though, is it?
76 if (!EditorUtil.forceUserSave(editor, shell)) {
77 return null;
78 }
79
80 // Get taxon
81 taxon = ((TaxonEditorInput) input).getTaxon();
82 parentNode = ((TaxonEditorInput) input).getTaxonNode().getParent();
83
84 // Create new taxon with no parent
85 IUndoableOperation operation = new ChangeSynonymToAcceptedTaxonOperation("Change synonym to accepted taxon", EditorUtil.getUndoContext(),
86 taxon, parentNode, synonym, this); //$NON-NLS-1$
87 EditorUtil.executeOperation(operation);
88
89 return null;
90 }
91
92 /* (non-Javadoc)
93 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
94 */
95 public boolean postOperation(CdmBase objectAffectedByOperation) {
96
97 // Redraw existing editor
98 ((IPostOperationEnabled) editor).postOperation(null);
99
100 editor.doSave(null);
101
102 if (objectAffectedByOperation instanceof TaxonNode) {
103
104 // Open new unsaved editor with existing taxon's parent as temporary parent
105 TaxonNode newNode = (TaxonNode) objectAffectedByOperation;
106 // TaxonNode newNode = parentNode.addChild(newTaxon);
107
108 try {
109 // TODO
110 /*
111 * This doesn't work b/c newNode hasn't been committed yet, and therefore
112 * CdmStore.getTaxonService().getTaxonNodeByUuid(taxonNodeUuid);
113 * doesn't work yet.
114 */
115 IEditorPart newEditor = EditorUtil.open(newNode.getUuid());
116
117 } catch (PartInitException e) {
118 // TODO Auto-generated catch block
119 e.printStackTrace();
120 }
121 }
122 return true;
123 }
124 }