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