Merge branch 'develop' into remoting-4.0
[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.handlers.HandlerUtil;
22
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.taxon.Synonym;
25 import eu.etaxonomy.cdm.model.taxon.Taxon;
26 import eu.etaxonomy.taxeditor.editor.EditorUtil;
27 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
28 import eu.etaxonomy.taxeditor.editor.name.operation.SwapSynonymAndAcceptedOperation;
29 import eu.etaxonomy.taxeditor.model.MessagingUtils;
30 import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
31 import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
32
33 /**
34 * <p>SwapSynonymAndAcceptedHandler class.</p>
35 *
36 * @author n.hoffmann
37 * @created 21.04.2009
38 * @version 1.0
39 */
40 public class SwapSynonymAndAcceptedHandler extends AbstractHandler implements
41 IHandler, IPostOperationEnabled {
42 private static final Logger logger = Logger
43 .getLogger(SwapSynonymAndAcceptedHandler.class);
44 private MultiPageTaxonEditor editor;
45 private Taxon taxon;
46
47 /* (non-Javadoc)
48 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
49 */
50 /** {@inheritDoc} */
51 @Override
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(),
65 editor.getUndoContext(),
66 editor.getTaxon(),
67 synonym,
68 this,
69 (ICdmEntitySessionEnabled)editor.getEditorInput());
70
71 EditorUtil.executeOperation(operation);
72
73 } catch (NotDefinedException e) {
74 logger.warn("Command name not set");
75 }
76
77 return null;
78 }
79
80 /* (non-Javadoc)
81 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase, boolean)
82 */
83 @Override
84 public boolean postOperation(CdmBase objectAffectedByOperation) {
85 // Redraw existing editor
86 //((IPostOperationEnabled) editor).postOperation(null);
87
88 editor.doSave(EditorUtil.getMonitor());
89 editor.close(true);
90
91 if (objectAffectedByOperation instanceof Taxon) {
92
93 taxon = (Taxon) objectAffectedByOperation;
94
95
96 }
97 return true;
98 }
99
100 /* (non-Javadoc)
101 * @see eu.etaxonomy.taxeditor.operation.IPostOperationEnabled#onComplete()
102 */
103 @Override
104 public boolean onComplete() {
105 Display display = Display.getCurrent();
106 display.asyncExec(new Runnable() {
107 @Override
108 public void run() {
109 try {
110 EditorUtil.openTaxonBase(taxon.getUuid());
111
112 } catch (Exception e) {
113 MessagingUtils.warningDialog("Could not open editor for taxon", this, e.getMessage());
114 }
115
116 }
117 });
118 return true;
119 }
120 }