Results 1 to 3 of 3
  1. #1
    pollux is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default [Beginner]Eclipse RCP Undo/redo operation

    Hello,
    First, sorry for my english :o

    Recently, I tried to use this tutorial :

    http:
    //help.eclipse.org/help32/index.jsp?topic=/org.eclipse.platform.doc.isv/guide/wrkAdv_undo.htm]Help - Eclipse SDK


    I created a simple RCP application (Hello RCP)
    I added 2 Menus (File and Edit)
    Java Code:
    public class ApplicationActionBarAdvisor extends ActionBarAdvisor {
    
        public ApplicationActionBarAdvisor(IActionBarConfigurer configurer) {
            super(configurer);
        }
    
        protected void makeActions(IWorkbenchWindow window) {
        	
        }
    
        protected void fillMenuBar(IMenuManager menuBar) {
        	IWorkbenchWindow window = getActionBarConfigurer().getWindowConfigurer().getWindow();
        	menuBar.add(createFileMenu(window));
        	menuBar.add(createEditMenu(window));
        }
    
    	private MenuManager createFileMenu(IWorkbenchWindow window) {
    		MenuManager menuMgr = new MenuManager("Fichier",IWorkbenchActionConstants.M_FILE);
    		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.FILE_START));
    		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
    		IWorkbenchAction action = ActionFactory.QUIT.create(window);
    		menuMgr.add(action);
    		action.setText("Quitter");
    		menuMgr.add(new GroupMarker(IWorkbenchActionConstants.FILE_END));
    		return menuMgr;
    	}
    	
    	private MenuManager createEditMenu(IWorkbenchWindow window) {
    		MenuManager menuEdit = new MenuManager("Edition",IWorkbenchActionConstants.M_EDIT);
    		IWorkbenchAction undo = ActionFactory.UNDO.create(window);
    		IWorkbenchAction redo = ActionFactory.REDO.create(window);
    
    		menuEdit.add(undo);
    		menuEdit.add(redo);
    		return menuEdit;
    	}
    }
    In file menu, I added an ActionSet
    Java Code:
       <extension
             point="org.eclipse.ui.actionSets">
          <actionSet
                id="MonApplication.actionSet2"
                label="label"
                visible="true">
             <action
                   class="fr.MonApplication.Actions.MonAction"
                   id="MonApplication.action2"
                   label="mon action"
                   menubarPath="file/groupe1"
                   style="push">
             </action>
          </actionSet>
       </extension>
    This ActionSet started an action who implemented IWorkbenchWindowActionDelegate
    Java Code:
    public class MonAction implements IWorkbenchWindowActionDelegate {
    
    	private IWorkbenchWindow window;
    	
    	@Override
    	public void dispose() {
    		// TODO Auto-generated method stub
    	}
    
    	@Override
    	public void init(IWorkbenchWindow window) {
    		this.window = window;
    	}
    
    	@Override
    	public void run(IAction action) {
    		//Création d'une opération annulable
    		IUndoableOperation operation = new ActionOperation(window.getShell());
    		//Récupéreation de l'historique des opérations du workbench
    		IWorkbench workbench = window.getWorkbench();
    		IOperationHistory operationHistory = workbench.getOperationSupport().getOperationHistory();
    		operationHistory = OperationHistoryFactory.getOperationHistory();
    		//Récupération du contexte d'annulation du workbench
    		IUndoContext undoContext = workbench.getOperationSupport().getUndoContext();
    		//ajout du contexte d'annulation
    		operation.addContext(undoContext);
    		//On lance l'opération d'ajout d'équipement à la fiche de manoeuvre
    		try {
    			operationHistory.execute(operation,null,null);
    		} catch (ExecutionException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    	}
    
    	@Override
    	public void selectionChanged(IAction action, ISelection selection) {
    		// TODO Auto-generated method stub
    
    	}
    }
    The Action run an undoable opération
    Java Code:
    public class ActionOperation extends AbstractOperation{
    
    	private Shell shell;
    	
    	
    	public ActionOperation(Shell shell) {
    		super("Test operation");
    		this.shell = shell;
    	}
    
    	@Override
    	public IStatus execute(IProgressMonitor monitor, IAdaptable info)
    			throws ExecutionException {
    		MessageDialog.openInformation(shell, "Test", "Action executée");
    		return Status.OK_STATUS;
    	}
    
    	@Override
    	public IStatus redo(IProgressMonitor monitor, IAdaptable info)
    			throws ExecutionException {
    		MessageDialog.openInformation(shell, "Test", "action Undo executée");
    		return Status.OK_STATUS;
    	}
    
    	@Override
    	public IStatus undo(IProgressMonitor monitor, IAdaptable info)
    			throws ExecutionException {
    		MessageDialog.openInformation(shell, "Test", "action Redo executée");
    		return Status.OK_STATUS;
    	}
    }
    My problem :

    I execute my operation (execution ok), the undo menu remains disabled, whereas the operationHistory is not emty
    The used example is very simple but I don't understand why the menu remains disabled


    Thanks in advance for your help :)

    Vince
    Last edited by pollux; 02-05-2009 at 07:28 PM.

  2. #2
    smangan is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default How to enable Undo/Redo Menus

    An UndoActionHandler must be applied for the menu to be enabled.

    Create your handlers:

    private UndoActionHandler undoAction;

    private RedoActionHandler redoAction;


    In your createPartControl method, assign the handles for undo/redo:

    public void createPartControl(Composite parent) {
    ...
    createGlobalActionHandlers();
    }

    private void createGlobalActionHandlers() {
    // set up action handlers that operate on the current context
    undoAction = new UndoActionHandler(this.getSite(), undoContext);
    redoAction = new RedoActionHandler(this.getSite(), undoContext);
    IActionBars actionBars = getViewSite().getActionBars();
    actionBars.setGlobalActionHandler(ActionFactory.UN DO.getId(),
    undoAction);
    actionBars.setGlobalActionHandler(ActionFactory.RE DO.getId(),
    redoAction);
    }

    Now when your view is selected, the undo/redo options should appear.

  3. #3
    pollux is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default

    Hi,

    Thanks a lot . It works :D

    Now when your view is selected, the undo/redo options should appear.
    How can I use Undo/Redo when view is not selected?

Similar Threads

  1. Replies: 6
    Last Post: 04-20-2009, 01:46 AM
  2. SWT Undo Redo
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 04:43 PM
  3. Undo/Redo JTextArea
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-27-2008, 07:52 PM
  4. Frame close operation
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-21-2007, 08:39 AM
  5. undo java
    By new214 in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •