1) Re-arranged packages:
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / view / nameviewersupport / ChangeSynonymToTaxonUiAction.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.taxeditor.view.nameviewersupport;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.core.runtime.Status;
15 import org.eclipse.jface.action.Action;
16 import org.eclipse.jface.resource.ImageDescriptor;
17 import org.eclipse.jface.util.IPropertyChangeListener;
18 import org.eclipse.jface.util.PropertyChangeEvent;
19 import org.eclipse.ui.IEditorPart;
20 import org.eclipse.ui.PartInitException;
21
22 import eu.etaxonomy.cdm.model.taxon.Synonym;
23 import eu.etaxonomy.cdm.model.taxon.Taxon;
24 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
25 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
26 import eu.etaxonomy.taxeditor.UiUtil;
27 import eu.etaxonomy.taxeditor.actions.UndoableAction;
28 import eu.etaxonomy.taxeditor.actions.cdm.SwapSynonymAndTaxonAction;
29 import eu.etaxonomy.taxeditor.actions.ui.OpenTaxonEditorAction;
30
31 /**
32 * @author p.ciardelli
33 * @created 09.06.2008
34 * @version 1.0
35 */
36 public class ChangeSynonymToTaxonUiAction extends UndoableAction {
37 private static final Logger logger = Logger
38 .getLogger(ChangeSynonymToTaxonUiAction.class);
39
40 private static String text = "Make synonym this taxon's accepted taxon";
41 private static ImageDescriptor image = TaxEditorPlugin.getDefault()
42 .getImageDescriptor(ITaxEditorConstants.SWAP_SYNONYM_AND_TAXON_ICON);
43
44 private NameComposite composite;
45 private Taxon taxon;
46 private Synonym synonym;
47
48 public ChangeSynonymToTaxonUiAction(Synonym synonym, Taxon taxon) {
49 super(text, image);
50
51 this.synonym = synonym;
52 this.taxon = taxon;
53 }
54
55 @Override
56 protected IStatus executeOperation() {
57 Action action = new SwapSynonymAndTaxonAction(synonym, taxon);
58 action.addPropertyChangeListener(new IPropertyChangeListener() {
59 @Override
60 public void propertyChange(PropertyChangeEvent event) {
61 if (event.getProperty().equals(ITaxEditorConstants.TAXON)) {
62
63 if (event.getNewValue() instanceof Taxon) {
64 Taxon newTaxon = (Taxon) event.getNewValue();
65
66 // Close editor for existing taxon
67 IEditorPart oldEditor;
68 try {
69 oldEditor = UiUtil.getEditorByTaxon(taxon);
70 UiUtil.closeEditor(oldEditor, true);
71 } catch (PartInitException e) {
72 // TODO Auto-generated catch block
73 e.printStackTrace();
74 }
75
76 // Remove old taxon from tree
77 TaxEditorPlugin.getDefault().removeSessionTaxon(taxon);
78
79 // Add new taxon to tree
80 TaxEditorPlugin.getDefault().addSessionTaxon(newTaxon);
81
82 // Open editor for new taxon
83 new OpenTaxonEditorAction(newTaxon).run();
84
85 // Remove old taxon from recent names list
86 TaxEditorPlugin.getDefault()
87 .getObservableRecentNamesList().remove(taxon);
88 }
89
90 }
91 }
92 });
93 action.run();
94 return Status.OK_STATUS;
95 }
96
97 @Override
98 protected IStatus redoOperation() {
99 return Status.OK_STATUS;
100 }
101
102 @Override
103 protected IStatus undoOperation() {
104 return Status.OK_STATUS;
105 }
106 }