47576da82276bf5456a8fab46aa91c109cd0ef6c
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / actions / UndoableAction.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;
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.jface.action.Action;
22 import org.eclipse.jface.resource.ImageDescriptor;
23
24 import eu.etaxonomy.taxeditor.UiUtil;
25
26 /**
27 * @author p.ciardelli
28 * @created 09.06.2008
29 * @version 1.0
30 */
31 public abstract class UndoableAction extends Action {
32 private static final Logger logger = Logger.getLogger(UndoableAction.class);
33
34 protected IUndoableOperation operation;
35 private String text;
36
37 protected UndoableAction(String text, ImageDescriptor image) {
38 super(text);
39 setImageDescriptor(image);
40 this.text = text;
41
42 operation = new UndoableOperation();
43 }
44
45
46 public void run() {
47 IOperationHistory operationHistory = UiUtil.getOperationHistory();
48 IUndoContext undoContext = UiUtil.getWorkbenchUndoContext();
49 operation.addContext(undoContext);
50 try {
51 operationHistory.execute(operation, null, null);
52 operationHistory.add(operation);
53 } catch (ExecutionException e) {
54 e.printStackTrace();
55 }
56 }
57
58 protected abstract IStatus executeOperation();
59 protected abstract IStatus redoOperation();
60 protected abstract IStatus undoOperation();
61
62 class UndoableOperation extends AbstractOperation {
63
64 public UndoableOperation() {
65 super("'" + text + "'");
66 }
67
68 @Override
69 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
70 throws ExecutionException {
71 return executeOperation();
72 }
73
74 @Override
75 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
76 throws ExecutionException {
77 return redoOperation();
78 }
79
80 @Override
81 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
82 throws ExecutionException {
83 return undoOperation();
84 }
85 }
86
87 }