3b86e3ee553f63fadad8b3c4d47dd3caa328e25a
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / DeleteSynonymCompositeAction.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.actions.ui;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.AbstractOperation;
15 import org.eclipse.core.commands.operations.IOperationHistory;
16 import org.eclipse.core.commands.operations.IUndoContext;
17 import org.eclipse.core.commands.operations.IUndoableOperation;
18 import org.eclipse.core.runtime.IAdaptable;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.jface.action.Action;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.util.IPropertyChangeListener;
25 import org.eclipse.jface.util.PropertyChangeEvent;
26
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.Taxon;
29 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
30 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
31 import eu.etaxonomy.taxeditor.UiUtil;
32 import eu.etaxonomy.taxeditor.actions.cdm.DeleteSynonymFromTaxonAction;
33 import eu.etaxonomy.taxeditor.editor.name.NameComposite;
34
35 /**
36 * @author p.ciardelli
37 * @created 04.06.2008
38 * @version 1.0
39 */
40 public class DeleteSynonymCompositeAction extends Action {
41 private static final Logger logger = Logger
42 .getLogger(DeleteSynonymCompositeAction.class);
43
44 private static String text = "Delete synonym from taxon";
45 private ImageDescriptor image = TaxEditorPlugin.getDefault()
46 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
47
48 private Synonym synonym;
49
50 private Taxon taxon;
51 private NameComposite composite;
52 private IUndoableOperation operation;
53
54 private DeleteSynonymCompositeAction() {
55 super(text);
56 setImageDescriptor(image);
57 }
58
59 public DeleteSynonymCompositeAction(NameComposite composite, Taxon taxon) {
60 this();
61
62 if (composite.getData() instanceof Synonym) {
63 this.synonym = (Synonym) composite.getData();
64 } else {
65 throw new IllegalArgumentException(
66 "This action requires a composite with a Synonym in its data field.");
67 }
68
69 this.taxon = taxon;
70 this.composite = composite;
71
72 operation = new DeleteSynonymOperation();
73 }
74
75 public void run() {
76 IOperationHistory operationHistory = UiUtil.getOperationHistory();
77 IUndoContext undoContext = UiUtil.getWorkbenchUndoContext();
78 operation.addContext(undoContext);
79 try {
80 operationHistory.execute(operation, null, null);
81 operationHistory.add(operation);
82 } catch (ExecutionException e) {
83 e.printStackTrace();
84 }
85 }
86
87 class DeleteSynonymOperation extends AbstractOperation {
88
89 public DeleteSynonymOperation() {
90 super("'" + text + "'");
91 }
92
93 @Override
94 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
95 throws ExecutionException {
96 // Delete synonym from taxon
97 Action action = new DeleteSynonymFromTaxonAction(synonym, taxon);
98 action.addPropertyChangeListener(new IPropertyChangeListener() {
99 public void propertyChange(PropertyChangeEvent event) {
100 if (event.getProperty().equals(
101 ITaxEditorConstants.SYNONYM)) {
102 // Delete namecomposite
103 composite.dispose();
104 }
105 }
106 });
107 action.run();
108
109 return Status.OK_STATUS;
110 }
111
112 @Override
113 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
114 throws ExecutionException {
115 // TODO Auto-generated method stub
116 return Status.OK_STATUS;
117 }
118
119 @Override
120 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
121 throws ExecutionException {
122 // TODO Auto-generated method stub
123 return Status.OK_STATUS;
124 }
125 }
126 }