5ba19336cf43619d1f2f29b6a134c7318521ae27
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / DeleteMisappliedNameCompositeAction.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.Taxon;
28 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
29 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
30 import eu.etaxonomy.taxeditor.UiUtil;
31 import eu.etaxonomy.taxeditor.actions.cdm.DeleteMisappliedNameFromTaxonAction;
32 import eu.etaxonomy.taxeditor.editor.name.NameComposite;
33
34 /**
35 * @author p.ciardelli
36 * @created 04.06.2008
37 * @version 1.0
38 */
39 public class DeleteMisappliedNameCompositeAction extends Action {
40 private static final Logger logger = Logger
41 .getLogger(DeleteMisappliedNameCompositeAction.class);
42
43 private static String text = "Delete misapplied name from taxon";
44 private ImageDescriptor image = TaxEditorPlugin.getDefault()
45 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
46
47 private Taxon misappliedName;
48
49 private Taxon taxon;
50 private NameComposite composite;
51 private IUndoableOperation operation;
52
53 private DeleteMisappliedNameCompositeAction() {
54 super(text);
55 setImageDescriptor(image);
56 }
57
58 public DeleteMisappliedNameCompositeAction(NameComposite composite, Taxon taxon) {
59 this();
60
61 if (composite.getData() instanceof Taxon) {
62 this.misappliedName = (Taxon) composite.getData();
63 } else {
64 throw new IllegalArgumentException(
65 "This action requires a composite with a Taxon in its data field.");
66 }
67
68 this.taxon = taxon;
69 this.composite = composite;
70
71 operation = new DeleteSynonymOperation();
72 }
73
74 public void run() {
75 IOperationHistory operationHistory = UiUtil.getOperationHistory();
76 IUndoContext undoContext = UiUtil.getWorkbenchUndoContext();
77 operation.addContext(undoContext);
78 try {
79 operationHistory.execute(operation, null, null);
80 operationHistory.add(operation);
81 } catch (ExecutionException e) {
82 e.printStackTrace();
83 }
84 }
85
86 class DeleteSynonymOperation extends AbstractOperation {
87
88 public DeleteSynonymOperation() {
89 super("'" + text + "'");
90 }
91
92 @Override
93 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
94 throws ExecutionException {
95 // Delete synonym from taxon
96 Action action = new DeleteMisappliedNameFromTaxonAction(misappliedName, taxon);
97 action.addPropertyChangeListener(new IPropertyChangeListener() {
98 public void propertyChange(PropertyChangeEvent event) {
99 if (event.getProperty().equals(
100 ITaxEditorConstants.MISAPPLIED_NAME)) {
101 // Delete namecomposite
102 // TODO: put this in taxonFactory?
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 }