1 /*******************************************************************************
2 * Copyright (c) 2007- VTT Technical Research Centre of Finland.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.ui.workbench.handler;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
15 import java.util.concurrent.atomic.AtomicReference;
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
22 import org.eclipse.jface.operation.IRunnableWithProgress;
23 import org.eclipse.swt.widgets.Shell;
24 import org.eclipse.ui.IActionBars;
25 import org.eclipse.ui.IWorkbenchPart;
26 import org.eclipse.ui.PlatformUI;
27 import org.eclipse.ui.handlers.HandlerUtil;
28 import org.simantics.Simantics;
29 import org.simantics.db.ChangeSetIdentifier;
30 import org.simantics.db.Operation;
31 import org.simantics.db.Session;
32 import org.simantics.db.exception.DatabaseException;
33 import org.simantics.db.exception.NoHistoryException;
34 import org.simantics.db.service.ManagementSupport;
35 import org.simantics.db.service.UndoRedoSupport;
36 import org.simantics.utils.ui.workbench.WorkbenchUtils;
39 * @author Kalle Kondelin
41 public class SessionRedoHandler extends AbstractHandler {
43 private final boolean DEBUG = false;
46 public Object execute(ExecutionEvent e) throws ExecutionException {
48 System.out.println("--\nRedo handler called.");
50 IWorkbenchPart part = HandlerUtil.getActivePart(e);
53 IActionBars actionBars = WorkbenchUtils.getActionBars(part);
54 final Session session = Simantics.peekSession();
59 final AtomicReference<String> msg = new AtomicReference<String>();
60 IRunnableWithProgress redo = new IRunnableWithProgress() {
62 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
64 monitor.beginTask("Redo", IProgressMonitor.UNKNOWN);
65 msg.set( redo( session ) );
66 } catch (NoHistoryException e) {
67 msg.set("Nothing to redo.");
68 } catch (DatabaseException e) {
69 throw new InvocationTargetException(e);
76 // busyCursorWhile does not work because locking the session for undo
77 // will also lock the UI completely.
78 //PlatformUI.getWorkbench().getProgressService().busyCursorWhile(undo);
79 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
80 new ProgressMonitorDialog(shell).run(true, false, redo);
81 actionBars.getStatusLineManager().setMessage( msg.get() );
82 } catch (InvocationTargetException e1) {
83 throw new ExecutionException("Redo failed, database failure.", e1.getTargetException());
84 } catch (InterruptedException e1) {
85 throw new ExecutionException("Redo failed, interrupted.", e1);
90 private String redo(Session session) throws DatabaseException {
91 UndoRedoSupport support = session.getService(UndoRedoSupport.class);
92 List<Operation> ops = support.redo(session, 1);
94 return "Nothing to redo.";
96 Operation mainOperation = ops.get(0);
99 long csId = mainOperation.getCSId();
101 ManagementSupport management = session.getService(ManagementSupport.class);
102 for(ChangeSetIdentifier id : management.getChangeSetIdentifiers(csId, csId))
104 msg = "Redid: " + SessionUndoHandler.getComment(session, id);
107 System.out.println(msg);