Moving editor sources back into trunk
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / store / operations / DeleteDescriptionElementOperation.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.store.operations;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18
19 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
20 import eu.etaxonomy.cdm.model.description.TaxonDescription;
21 import eu.etaxonomy.cdm.model.taxon.Taxon;
22
23 /**
24 * @author p.ciardelli
25 * @created 05.02.2009
26 * @version 1.0
27 */
28 public class DeleteDescriptionElementOperation extends AbstractPostOperation {
29 private static final Logger logger = Logger
30 .getLogger(DeleteDescriptionElementOperation.class);
31
32 private DescriptionElementBase element;
33 private TaxonDescription description = null;
34
35 public DeleteDescriptionElementOperation(String label, IUndoContext undoContext,
36 Taxon taxon, DescriptionElementBase element, IPostOperationEnabled postOperationEnabled) {
37 super(label, undoContext, taxon, postOperationEnabled);
38
39 this.element = element;
40 }
41
42 /* (non-Javadoc)
43 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
44 */
45 @Override
46 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
47 throws ExecutionException {
48
49 // Find description to which element belongs
50 for (TaxonDescription descr : taxon.getDescriptions()) {
51 for (DescriptionElementBase elem : descr.getElements()) {
52 if (elem.equals(element)) {
53 description = descr;
54 break;
55 }
56 }
57 }
58
59 // Remove element from description
60 if (description == null) {
61 logger.error("Couldn't find element's description!");
62 } else {
63 description.removeElement(element);
64 }
65
66 // Redraw editor if exists
67 return postExecute(null);
68 }
69
70 /* (non-Javadoc)
71 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
72 */
73 @Override
74 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
75 throws ExecutionException {
76 return execute(monitor, info);
77 }
78
79 /* (non-Javadoc)
80 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
81 */
82 @Override
83 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
84 throws ExecutionException {
85
86 description.addElement(element);
87
88 return postExecute(element);
89 }
90 }