UndoOperationAction.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.traceviewer.operation;
00002
00003 import org.eclipse.core.commands.ExecutionException;
00004 import org.eclipse.core.commands.operations.IUndoableOperation;
00005 import org.eclipse.core.runtime.IStatus;
00006 import org.eclipse.jface.action.Action;
00007 import org.eclipse.jface.resource.ImageDescriptor;
00008 import org.eclipse.swt.widgets.Control;
00009 import org.eclipse.swt.widgets.Menu;
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 public class UndoOperationAction extends OperationHistoryAction {
00020
00021
00022 public UndoOperationAction(ImageDescriptor img) {
00023 super(img);
00024 }
00025
00026 @Override
00027 protected IUndoableOperation[] getHistory() {
00028 return TraceOperation.getUndoHistory();
00029 }
00030
00031 @Override
00032 protected void execute() {
00033 IUndoableOperation[] operations = getHistory();
00034 if (operations.length<1)
00035 return;
00036
00037 IUndoableOperation[] redos = TraceOperation.getRedoHistory();
00038
00039 if (redos.length == 0) {
00040
00041
00042
00043
00044 doUndo();
00045 }
00046 doUndo();
00047
00048 }
00049
00050
00051
00052
00053 private void doUndo() {
00054 try {
00055 IStatus status = TraceOperation.getOperationHistory().
00056 undo(TraceOperation.undoableContext, null, null);
00057 if (!status.isOK()) {
00058 System.err.println("Cannot undo: " + status.getMessage());
00059 }
00060 } catch (ExecutionException e) {
00061 e.printStackTrace();
00062 }
00063 }
00064
00065 @Override
00066 protected void execute(IUndoableOperation operation) {
00067 try {
00068 TraceOperation.getOperationHistory().undoOperation(operation, null, null);
00069 } catch (ExecutionException e) {
00070 e.printStackTrace();
00071 }
00072 }
00073
00074 @Override
00075 public Menu getMenu(Control parent) {
00076 Menu menu = getMenu();
00077 if (menu != null)
00078 menu.dispose();
00079
00080 menu = new Menu(parent);
00081
00082 IUndoableOperation[] operations = getHistory();
00083
00084
00085 for (int i=operations.length-1; i>=0; i--) {
00086 final IUndoableOperation op = operations[i];
00087 Action action = new Action(op.getLabel()) {
00088 public void run() {
00089 execute(op);
00090 }
00091 };
00092 addActionToMenu(menu, action);
00093 }
00094 return menu;
00095 }
00096
00097 @Override
00098 protected IUndoableOperation[] setStatus() {
00099 final IUndoableOperation []ops = getHistory();
00100
00101
00102 boolean status = (ops != null) && (ops.length>0);
00103 setEnabled(status);
00104 return ops;
00105 }
00106 }