37dca191c0426116abb562311039abf312cc9c5d
[taxeditor.git] / 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.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.StructuredSelection;
22 import org.eclipse.swt.widgets.Shell;
23 import org.eclipse.ui.PartInitException;
24 import org.eclipse.ui.handlers.HandlerUtil;
25
26 import eu.etaxonomy.cdm.model.taxon.Synonym;
27 import eu.etaxonomy.cdm.model.taxon.Taxon;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
30 import eu.etaxonomy.taxeditor.editor.Page;
31 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
32 import eu.etaxonomy.taxeditor.operations.SwapSynonymAndAcceptedOperation;
33 import eu.etaxonomy.taxeditor.propertysheet.name.SynonymPropertySource;
34 import eu.etaxonomy.taxeditor.store.CdmStore;
35
36 /**
37 * @author n.hoffmann
38 * @created 21.04.2009
39 * @version 1.0
40 */
41 public class SwapSynonymAndAcceptedHandler extends AbstractHandler implements
42 IHandler {
43 private static final Logger logger = Logger
44 .getLogger(SwapSynonymAndAcceptedHandler.class);
45
46 /* (non-Javadoc)
47 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
48 */
49 public Object execute(ExecutionEvent event) throws ExecutionException {
50 MultiPageTaxonEditor multiEditor = EditorUtil.getActiveEditor();
51
52 if (multiEditor.isDirty()) {
53 if (!MessageDialog.openConfirm((Shell) multiEditor.getAdapter(Shell.class), "Save before proceeding",
54 "All changes must be saved before proceeding.\n\n" +
55 "Save current changes?")) {
56 return null;
57 }
58 multiEditor.doSave(null);
59 }
60
61 TaxonNameEditor editor = (TaxonNameEditor) multiEditor.getPage(Page.NAME);
62
63 ISelection menuSelection = HandlerUtil.getActiveMenuSelection(event);
64 Synonym synonym = (Synonym) ((SynonymPropertySource) ((StructuredSelection) menuSelection).getFirstElement()).getTaxonBase();
65
66
67 SwapSynonymAndAcceptedOperation operation;
68
69 try {
70 operation = new SwapSynonymAndAcceptedOperation(event.getCommand().getName(), editor.getUndoContext(),
71 editor.getTaxon(), synonym, editor);
72 EditorUtil.executeOperation(operation);
73
74 // TODO this is all very spooky
75 // After performing the logic we do have to mangle around with the windows
76 Taxon newTaxon = operation.getNewTaxon();
77
78 // save and delete the taxon data
79 CdmStore.getTaxonService().save(newTaxon);
80 CdmStore.getTaxonService().delete(editor.getTaxon());
81
82 multiEditor.doSave(null);
83 EditorUtil.close(multiEditor);
84
85 // open an editor with the new
86 EditorUtil.open(newTaxon.getUuid());
87
88 } catch (NotDefinedException e) {
89 logger.warn("Command name not set");
90 } catch (PartInitException e) {
91 logger.error("Could not open editor with new taxon", e);
92 }
93
94 return null;
95 }
96 }