Massive refactoring of the methodology in former class UiUtils
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / ui / description / DeleteElementCompositeAction.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.description;
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.swt.widgets.Composite;
25
26 import eu.etaxonomy.cdm.model.description.DescriptionBase;
27 import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
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.controller.GlobalController;
32
33 /**
34 * @author p.ciardelli
35 * @created 04.06.2008
36 * @version 1.0
37 */
38 public class DeleteElementCompositeAction extends Action {
39 private static final Logger logger = Logger
40 .getLogger(DeleteElementCompositeAction.class);
41
42 private static String text = "Delete description element";
43 private ImageDescriptor image = TaxEditorPlugin.getDefault()
44 .getImageDescriptor(ITaxEditorConstants.ACTIVE_DELETE_ICON);
45
46 private IUndoableOperation operation;
47 private DescriptionElementBase descriptionElement;
48 private Taxon taxon;
49
50 private Composite elementComposite;
51 private Composite featureComposite;
52 private Composite descriptionComposite;
53 private DescriptionBase description;
54
55 private DeleteElementCompositeAction() {
56 super(text);
57 setImageDescriptor(image);
58 }
59
60 public DeleteElementCompositeAction(Composite elementComposite,
61 Taxon taxon) {
62 this();
63
64 this.elementComposite = elementComposite;
65
66 if (elementComposite.getData() instanceof DescriptionElementBase) {
67 this.descriptionElement = (DescriptionElementBase) elementComposite.getData();
68 } else {
69 throw new IllegalArgumentException(
70 "This action requires an elementComposite with a DescriptionElementBase in its data field.");
71 }
72 this.taxon = taxon;
73
74 featureComposite = elementComposite.getParent();
75 descriptionComposite = featureComposite.getParent();
76 if (descriptionComposite.getData() instanceof DescriptionBase) {
77 this.description = (DescriptionBase) descriptionComposite.getData();
78 } else {
79 throw new IllegalArgumentException(
80 "This action requires that the element composite's \"grandparent\" has a Description in its data field.");
81 }
82 operation = new DeleteElementOperation();
83 }
84
85 public void run() {
86 IOperationHistory operationHistory = GlobalController.getOperationHistory();
87 IUndoContext undoContext = GlobalController.getWorkbenchUndoContext();
88 operation.addContext(undoContext);
89 try {
90 operationHistory.execute(operation, null, null);
91 operationHistory.add(operation);
92 } catch (ExecutionException e) {
93 e.printStackTrace();
94 }
95 }
96
97 class DeleteElementOperation extends AbstractOperation {
98
99 public DeleteElementOperation() {
100 super("'" + text + "'");
101 }
102
103 @Override
104 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
105 throws ExecutionException {
106
107 // Delete element from description
108 description.removeElement(descriptionElement);
109
110 // Dipose element composite
111 elementComposite.dispose();
112
113 return Status.OK_STATUS;
114 }
115
116 @Override
117 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
118 throws ExecutionException {
119 // TODO Auto-generated method stub
120 return Status.OK_STATUS;
121 }
122
123 @Override
124 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
125 throws ExecutionException {
126 // TODO Auto-generated method stub
127 return Status.OK_STATUS;
128 }
129 }
130 }