Fixes #2235
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / handler / SwapSynonymAndAcceptedHandler.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.IHandler;
18 import org.eclipse.core.commands.common.NotDefinedException;
19 import org.eclipse.swt.widgets.Display;
20 import org.eclipse.swt.widgets.Shell;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.forms.editor.FormEditor;
23 import org.eclipse.ui.handlers.HandlerUtil;
24
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.taxon.Synonym;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
29 import eu.etaxonomy.taxeditor.editor.EditorUtil;
30 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
31 import eu.etaxonomy.taxeditor.editor.name.operation.SwapSynonymAndAcceptedOperation;
32 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
33
34 /**
35 * <p>SwapSynonymAndAcceptedHandler class.</p>
36 *
37 * @author n.hoffmann
38 * @created 21.04.2009
39 * @version 1.0
40 */
41 public class SwapSynonymAndAcceptedHandler extends AbstractHandler implements
42 IHandler, IPostOperationEnabled {
43 private static final Logger logger = Logger
44 .getLogger(SwapSynonymAndAcceptedHandler.class);
45 private MultiPageTaxonEditor editor;
46 private Taxon taxon;
47
48 /* (non-Javadoc)
49 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
50 */
51 /** {@inheritDoc} */
52 public Object execute(ExecutionEvent event) throws ExecutionException {
53 editor = EditorUtil.getActiveMultiPageTaxonEditor();
54 Shell shell = HandlerUtil.getActiveShell(event);
55 Synonym synonym = (Synonym) EditorUtil.getSelection(event).getFirstElement();
56
57
58 // Force user to save taxon - not really necessary though, is it?
59 if (!EditorUtil.forceUserSave(editor, shell)) {
60 return null;
61 }
62
63 try {
64 SwapSynonymAndAcceptedOperation operation = new SwapSynonymAndAcceptedOperation(event.getCommand().getName(), editor.getUndoContext(),
65 editor.getTaxon(), synonym, this);
66
67 EditorUtil.executeOperation(operation);
68
69 } catch (NotDefinedException e) {
70 logger.warn("Command name not set");
71 }
72
73 return null;
74 }
75
76 /* (non-Javadoc)
77 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase, boolean)
78 */
79 @Override
80 public boolean postOperation(CdmBase objectAffectedByOperation) {
81 // Redraw existing editor
82 //((IPostOperationEnabled) editor).postOperation(null);
83
84 editor.doSave(EditorUtil.getMonitor());
85 editor.close(true);
86
87 if (objectAffectedByOperation instanceof Taxon) {
88
89 taxon = (Taxon) objectAffectedByOperation;
90
91
92 }
93 return true;
94 }
95
96 /* (non-Javadoc)
97 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
98 */
99 @Override
100 public boolean onComplete() {
101 Display display = Display.getCurrent();
102 display.asyncExec(new Runnable() {
103 public void run() {
104 try {
105 EditorUtil.openTaxonBase(taxon.getUuid());
106
107 } catch (Exception e) {
108 EditorUtil.warningDialog("Could not open editor for taxon", this, e.getMessage());
109 }
110
111 }
112 });
113 return true;
114 }
115 }