OperationHistoryAction.java
Go to the documentation of this file.00001 package edu.rice.cs.hpc.traceviewer.operation;
00002
00003
00004 import org.eclipse.core.commands.operations.IOperationHistoryListener;
00005 import org.eclipse.core.commands.operations.IUndoableOperation;
00006 import org.eclipse.core.commands.operations.OperationHistoryEvent;
00007 import org.eclipse.jface.action.Action;
00008 import org.eclipse.jface.action.ActionContributionItem;
00009 import org.eclipse.jface.action.IAction;
00010 import org.eclipse.jface.action.IMenuCreator;
00011 import org.eclipse.jface.resource.ImageDescriptor;
00012 import org.eclipse.swt.widgets.Menu;
00013
00014
00015
00016
00017
00018
00019 public abstract class OperationHistoryAction extends Action
00020 implements IAction, IOperationHistoryListener, IMenuCreator
00021 {
00022 private Menu menu;
00023 private IUndoableOperation []operations;
00024
00025 public OperationHistoryAction(ImageDescriptor img) {
00026 super(null, Action.AS_DROP_DOWN_MENU);
00027 setImageDescriptor(img);
00028 setMenuCreator(this);
00029 TraceOperation.getOperationHistory().addOperationHistoryListener(this);
00030 }
00031
00032 @Override
00033 public void dispose() {
00034 if (menu != null) {
00035 menu.dispose();
00036 menu = null;
00037 }
00038 }
00039
00040
00041 @Override
00042 public Menu getMenu(Menu parent) {
00043 return null;
00044 }
00045
00046 protected Menu getMenu()
00047 {
00048 return menu;
00049 }
00050
00051
00052
00053
00054
00055 protected IUndoableOperation[] getOperations() {
00056 return operations;
00057 }
00058
00059 @Override
00060 public void run() {
00061 execute();
00062 }
00063
00064 protected void addActionToMenu(Menu parent, Action action) {
00065 ActionContributionItem item = new ActionContributionItem(action);
00066 item.fill(parent, -1);
00067 }
00068
00069 @Override
00070
00071
00072
00073
00074 public void historyNotification(final OperationHistoryEvent event)
00075 {
00076 final IUndoableOperation operation = event.getOperation();
00077
00078 if (operation.hasContext(TraceOperation.undoableContext)) {
00079 switch(event.getEventType()) {
00080 case OperationHistoryEvent.DONE:
00081 case OperationHistoryEvent.UNDONE:
00082 case OperationHistoryEvent.REDONE:
00083 operations = setStatus();
00084 break;
00085 }
00086 }
00087 }
00088
00089 protected void debug(String prefix, IUndoableOperation []ops) {
00090 System.out.print(prefix + " [ ");
00091 boolean comma = false;
00092 for (IUndoableOperation op: ops) {
00093 if (comma)
00094 System.out.print(" , ");
00095 System.out.print(op.getLabel());
00096 if (!comma)
00097 comma = true;
00098 }
00099 System.out.println(" ]");
00100 }
00101
00102 abstract protected IUndoableOperation[] getHistory();
00103 abstract protected void execute();
00104 abstract protected void execute(IUndoableOperation operation) ;
00105 abstract protected IUndoableOperation[] setStatus();
00106 }