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